Browse Source

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

Sven/Sarah Barth 3 years ago
parent
commit
a93942cd27
2 changed files with 34 additions and 3 deletions
  1. 13 3
      compiler/ngtcon.pas
  2. 21 0
      tests/tbs/tb0696.pp

+ 13 - 3
compiler/ngtcon.pas

@@ -149,7 +149,7 @@ uses
    symtable,
    symtable,
    defutil,defcmp,
    defutil,defcmp,
    { pass 1 }
    { pass 1 }
-   htypechk,procinfo,
+   htypechk,procinfo,pass_1,
    nmem,ncnv,ninl,ncon,nld,nadd,
    nmem,ncnv,ninl,ncon,nld,nadd,
    { parser specific stuff }
    { parser specific stuff }
    pbase,pexpr,
    pbase,pexpr,
@@ -844,10 +844,20 @@ function get_next_varsym(def: tabstractrecorddef; const SymList:TFPHashObjectLis
         { maybe pchar ? }
         { maybe pchar ? }
         else
         else
           if is_char(def.pointeddef) and
           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
             begin
               { ensure that a widestring is converted to the current codepage }
               { 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);
                 tstringconstnode(node).changestringtype(getansistringdef);
               { create a tcb for the string data (it's placed in a separate
               { create a tcb for the string data (it's placed in a separate
                 asmlist) }
                 asmlist) }

+ 21 - 0
tests/tbs/tb0696.pp

@@ -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.
+