Browse Source

* don't convert terminating #0 of UCS4Strings to widestring
* fixed length check in twide5.pp

git-svn-id: trunk@9518 -

Jonas Maebe 17 years ago
parent
commit
01592d7b5d
2 changed files with 9 additions and 6 deletions
  1. 6 4
      rtl/inc/wustrings.inc
  2. 3 2
      tests/test/twide5.pp

+ 6 - 4
rtl/inc/wustrings.inc

@@ -1864,8 +1864,9 @@ function WideStringToUCS4String(const s : WideString) : UCS4String;
         inc(destindex);
         inc(i,len);
       end;
-    result[destindex]:=UCS4Char(0);
-    { destindex <= slen }
+    { destindex <= slen (surrogate pairs may have been merged) }
+    { destindex+1 for terminating #0 (dynamic arrays are       }
+    { implicitely filled with zero)                            }
     setlength(result,destindex+1);
   end;
 
@@ -1908,9 +1909,10 @@ function UCS4StringToWideString(const s : UCS4String) : WideString;
     i        : SizeInt;
     resindex : SizeInt;
   begin
-    SetLength(result,length(s));
+    { skip terminating #0 }
+    SetLength(result,length(s)-1);
     resindex:=1;
-    for i:=0 to high(s) do
+    for i:=0 to high(s)-1 do
       ConcatUTF32ToWideStr(s[i],result,resindex);
     { adjust result length (may be too big due to growing }
     { for surrogate pairs)                                }

+ 3 - 2
tests/test/twide5.pp

@@ -21,14 +21,15 @@ begin
      (ws[8]<>#$dc04) then
     halt(1);
   us:=WideStringToUCS4String(ws);
-  if (length(us)<>7) or
+  if (length(us)<>8) or
      (us[0]<>UCS4Char(widechar('é'))) or
      (us[1]<>UCS4Char(widechar('ł'))) or
      (us[2]<>UCS4Char(widechar('Ł'))) or
      (us[3]<>UCS4Char(widechar('ć'))) or
      (us[4]<>UCS4Char(widechar('ç'))) or
      (us[5]<>UCS4Char(widechar('Ź'))) or
-     (us[6]<>UCS4Char($2F804)) then
+     (us[6]<>UCS4Char($2F804)) or
+     (us[7]<>UCS4Char(0)) then
     halt(2);
   ws:=UCS4StringToWideString(us);
   if (length(ws)<>8) or