From 5560f6b3f2a21721a68e7d9ca824289f2ca44b78 Mon Sep 17 00:00:00 2001
From: paul <paul@idefix.freepascal.org>
Date: Mon, 26 Sep 2011 04:49:09 +0000
Subject: [PATCH] compiler + rtl: pass codepage to fpc_CharArray_To_AnsiStr to
 get the preserve the codepage of result string

git-svn-id: trunk@19240 -
---
 .gitattributes         |  1 +
 compiler/ncnv.pas      |  4 +---
 rtl/inc/astrings.inc   | 16 ++++++++++++++--
 rtl/inc/compproc.inc   |  2 +-
 tests/test/tcpstr11.pp | 11 +++++++++++
 5 files changed, 28 insertions(+), 6 deletions(-)
 create mode 100644 tests/test/tcpstr11.pp

diff --git a/.gitattributes b/.gitattributes
index 6fe4287105..8a5fcb429d 100644
--- a/.gitattributes
+++ b/.gitattributes
@@ -9947,6 +9947,7 @@ tests/test/tconstref3.pp svneol=native#text/pascal
 tests/test/tconstref4.pp svneol=native#text/pascal
 tests/test/tcpstr1.pp svneol=native#text/plain
 tests/test/tcpstr10.pp svneol=native#text/pascal
+tests/test/tcpstr11.pp svneol=native#text/pascal
 tests/test/tcpstr2.pp svneol=native#text/plain
 tests/test/tcpstr2a.pp svneol=native#text/plain
 tests/test/tcpstr3.pp svneol=native#text/plain
diff --git a/compiler/ncnv.pas b/compiler/ncnv.pas
index d9b646e4a8..5a875907cc 100644
--- a/compiler/ncnv.pas
+++ b/compiler/ncnv.pas
@@ -921,8 +921,7 @@ implementation
             addstatement(newstat,ctemprefnode.create(restemp));
             result:=newblock;
           end
-        else if is_widechar(tarraydef(left.resultdef).elementdef) and
-                (tstringdef(resultdef).stringtype=st_ansistring) then
+        else if (tstringdef(resultdef).stringtype=st_ansistring) then
           begin
             result:=ccallnode.createinternres(
                       'fpc_'+chartype+'array_to_'+tstringdef(resultdef).stringtypname,
@@ -1134,7 +1133,6 @@ implementation
       begin
         result:=nil;
         if (left.nodetype=stringconstn) and
-           //(tstringdef(resultdef).stringtype in [st_ansistring,st_shortstring]) and
            ((tstringdef(resultdef).stringtype=st_shortstring) or
             ((tstringdef(resultdef).stringtype=st_ansistring) and
              (tstringdef(resultdef).encoding<>CP_NONE)
diff --git a/rtl/inc/astrings.inc b/rtl/inc/astrings.inc
index c70b812f25..96fd16f1df 100644
--- a/rtl/inc/astrings.inc
+++ b/rtl/inc/astrings.inc
@@ -550,9 +550,12 @@ begin
 end;
 
 
-Function fpc_CharArray_To_AnsiStr(const arr: array of char; zerobased: boolean = true): ansistring; compilerproc;
+Function fpc_CharArray_To_AnsiStr(const arr: array of ansichar; {$ifdef FPC_HAS_CPSTRING}cp : TSystemCodePage;{$endif FPC_HAS_CPSTRING}zerobased: boolean = true): RawByteString; compilerproc;
 var
   i  : SizeInt;
+{$ifndef FPC_HAS_CPSTRING}
+  cp : TSystemCodePage;
+{$endif FPC_HAS_CPSTRING}
 begin
   if (zerobased) then
     begin
@@ -569,7 +572,16 @@ begin
     i := high(arr)+1;
   SetLength(fpc_CharArray_To_AnsiStr,i);
   if i > 0 then
-    Move (arr[0],Pointer(fpc_CharArray_To_AnsiStr)^,i);
+    begin
+{$ifdef FPC_HAS_CPSTRING}
+      if (cp=0) then
+        cp:=DefaultSystemCodePage;
+{$else FPC_HAS_CPSTRING}
+      cp:=DefaultSystemCodePage;
+{$endif FPC_HAS_CPSTRING}
+      Move (arr[0],Pointer(fpc_CharArray_To_AnsiStr)^,i);
+      SetCodePage(fpc_CharArray_To_AnsiStr,cp,False);
+    end;
 end;
 
 {$ifndef FPC_STRTOCHARARRAYPROC}
diff --git a/rtl/inc/compproc.inc b/rtl/inc/compproc.inc
index c82cf74808..653f770a5a 100644
--- a/rtl/inc/compproc.inc
+++ b/rtl/inc/compproc.inc
@@ -273,7 +273,7 @@ Function fpc_ShortStr_To_AnsiStr (Const S2 : ShortString{$ifdef FPC_HAS_CPSTRING
 Function fpc_Char_To_AnsiStr(const c : Char{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
 
 Function fpc_PChar_To_AnsiStr(const p : pchar{$ifdef FPC_HAS_CPSTRING};cp : TSystemCodePage{$endif FPC_HAS_CPSTRING}): RawByteString; compilerproc;
-Function fpc_CharArray_To_AnsiStr(const arr: array of char; zerobased: boolean = true): ansistring; compilerproc;
+Function fpc_CharArray_To_AnsiStr(const arr: array of char; {$ifdef FPC_HAS_CPSTRING}cp : TSystemCodePage;{$endif FPC_HAS_CPSTRING}zerobased: boolean = true): RawByteString; compilerproc;
 {$ifndef FPC_STRTOCHARARRAYPROC}
 function fpc_ansistr_to_chararray(arraysize: SizeInt; const src: ansistring): fpc_big_chararray; compilerproc;
 {$else ndef FPC_STRTOCHARARRAYPROC}
diff --git a/tests/test/tcpstr11.pp b/tests/test/tcpstr11.pp
new file mode 100644
index 0000000000..9f4542a88c
--- /dev/null
+++ b/tests/test/tcpstr11.pp
@@ -0,0 +1,11 @@
+program tcpstr11;
+type
+  cp866 = type AnsiString(866);
+var
+  A: cp866;
+  c: array[0..5] of ansichar = '�ਢ��';
+begin
+  A := c;
+  if StringCodePage(A) <> 866 then
+    halt(1);
+end.