Selaa lähdekoodia

* Fixed conversion from PWideChar to ansistring.
+ test.

git-svn-id: trunk@34219 -

yury 9 vuotta sitten
vanhempi
commit
980c4741ac
3 muutettua tiedostoa jossa 70 lisäystä ja 1 poistoa
  1. 1 0
      .gitattributes
  2. 10 1
      rtl/inc/ustrings.inc
  3. 59 0
      tests/test/tcpstr27.pp

+ 1 - 0
.gitattributes

@@ -12126,6 +12126,7 @@ tests/test/tcpstr26a.pp svneol=native#text/plain
 tests/test/tcpstr26b.pp svneol=native#text/plain
 tests/test/tcpstr26c.pp svneol=native#text/plain
 tests/test/tcpstr26d.pp svneol=native#text/plain
+tests/test/tcpstr27.pp svneol=native#text/plain
 tests/test/tcpstr2a.pp svneol=native#text/plain
 tests/test/tcpstr3.pp svneol=native#text/plain
 tests/test/tcpstr4.pp svneol=native#text/plain

+ 10 - 1
rtl/inc/ustrings.inc

@@ -392,7 +392,10 @@ begin
     exit;
   Size := IndexWord(p^, -1, 0);
   if Size>0 then
+  begin
+    cp:=TranslatePlaceholderCP(cp);
     widestringmanager.Wide2AnsiMoveProc(P,result,cp,Size);
+  end;
 end;
 {$endif FPC_HAS_PWIDECHAR_TO_ANSISTR}
 
@@ -759,7 +762,13 @@ begin
     end
   else
     i := high(arr)+1;
-  widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),RawByteString(fpc_WideCharArray_To_AnsiStr),cp,i);
+  if i > 0 then
+  begin
+    cp:=TranslatePlaceholderCP(cp);
+    widestringmanager.Wide2AnsiMoveProc (pwidechar(@arr),RawByteString(fpc_WideCharArray_To_AnsiStr),cp,i);
+  end
+  else
+    fpc_WideCharArray_To_AnsiStr:='';
 end;
 {$endif FPC_HAS_WIDECHARARRAY_TO_ANSISTR}
 

+ 59 - 0
tests/test/tcpstr27.pp

@@ -0,0 +1,59 @@
+
+var
+  data: array[0..3] of widechar;
+
+procedure error(code: longint);
+begin
+  writeln('Error code: ', code);
+  Halt(code);
+end;
+
+procedure check(const s: ansistring; code: longint);
+var
+  us: unicodestring;
+begin
+  if StringCodePage(s) <> DefaultSystemCodePage then begin
+    writeln('Incorrect string code page: ', StringCodePage(s), '. Expected: ', DefaultSystemCodePage, '.');
+    error(code);
+  end;
+  us:=s;
+  if (Length(us) = 3) and (us[1] = data[0]) and (us[2] = data[1]) and (us[3] = data[2]) then
+    exit;
+  writeln('Incorrect string: ', us);
+  error(code);
+end;
+
+procedure test(cp: TSystemCodePage; code: longint);
+var
+  s: ansistring;
+begin
+  writeln('Testing default code page ', cp, '...');
+  DefaultSystemCodePage:=cp;
+  // Test fpc_unicodestr_to_ansistr
+  s:=unicodestring(data);
+  check(s, code + 1);
+  // Test fpc_widestr_to_ansistr
+  s:=widestring(data);
+  check(s, code + 2);
+  // Test fpc_widechararray_to_ansistr
+  s:=data;
+  check(s, code + 3);
+  // Test fpc_pwidechar_to_ansistr
+  s:=PWideChar(data);
+  check(s, code + 4);
+  // Test fpc_uchar_to_ansistr
+  s:=data[0] + data[1] + data[2];
+  check(s, code + 5);
+end;
+
+begin
+  // Cyrillic АБВ, null-terminated
+  data[0]:=widechar($410);
+  data[1]:=widechar($411);
+  data[2]:=widechar($412);
+  data[3]:=#0;
+  writeln('Original string: ', unicodestring(data));
+  test(CP_UTF8, 0);
+  test(1251, 10);
+  writeln('Test OK.');
+end.