* correctly convert a single WideChar to a PChar constant with the correct code page

+ added test
This commit is contained in:
Sven/Sarah Barth 2022-08-28 21:39:10 +02:00
parent 889c164f78
commit a93942cd27
2 changed files with 34 additions and 3 deletions

View File

@ -149,7 +149,7 @@ uses
symtable,
defutil,defcmp,
{ pass 1 }
htypechk,procinfo,
htypechk,procinfo,pass_1,
nmem,ncnv,ninl,ncon,nld,nadd,
{ parser specific stuff }
pbase,pexpr,
@ -844,10 +844,20 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
{ maybe pchar ? }
else
if is_char(def.pointeddef) and
((node.nodetype=stringconstn) or is_constcharnode(node)) then
((node.nodetype=stringconstn) or is_constcharnode(node) or is_constwidecharnode(node)) then
begin
{ ensure that a widestring is converted to the current codepage }
if is_wide_or_unicode_string(node.resultdef) then
if is_constwidecharnode(node) then
begin
initwidestring(pw);
concatwidestringchar(pw,tcompilerwidechar(word(tordconstnode(node).value.svalue)));
hp:=cstringconstnode.createunistr(pw);
donewidestring(pw);
node.free;
do_typecheckpass(hp);
node:=hp;
end;
if (node.nodetype=stringconstn) and is_wide_or_unicode_string(node.resultdef) then
tstringconstnode(node).changestringtype(getansistringdef);
{ create a tcb for the string data (it's placed in a separate
asmlist) }

21
tests/tbs/tb0696.pp Normal file
View File

@ -0,0 +1,21 @@
{$codepage utf8}
program tb0696;
var
ar: PChar = 'Ё';
ar2: PChar;
l, i: SizeInt;
begin
Writeln(strlen(ar)); //Error
Writeln(Length(Ar)); //Error
l := Length(ar);
ar2 := 'Ё';
Writeln(strlen(ar2)); //Ok
Writeln(Length(Ar2)); //Ok
if l <> Length(ar2) then
Halt(1);
for i := 0 to l - 1 do
if ar[i] <> ar2[i] then
Halt(2 + i);
//Readln;
end.