From 14c2f248dfc8afa82de4f02dbaa41c1a49487702 Mon Sep 17 00:00:00 2001 From: paul Date: Thu, 6 Oct 2011 06:58:07 +0000 Subject: [PATCH] rtl: add WideBytesOf(UnicodeString): TBytes for delphi compatibility git-svn-id: trunk@19391 - --- rtl/objpas/sysutils/sysuni.inc | 9 +++++++++ rtl/objpas/sysutils/sysunih.inc | 1 + tests/test/units/sysutils/tbytesof.pp | 15 +++++++++++++++ 3 files changed, 25 insertions(+) diff --git a/rtl/objpas/sysutils/sysuni.inc b/rtl/objpas/sysutils/sysuni.inc index c8132d1e52..0928401a84 100644 --- a/rtl/objpas/sysutils/sysuni.inc +++ b/rtl/objpas/sysutils/sysuni.inc @@ -179,3 +179,12 @@ begin Result:=TEncoding.Default.GetBytes(Val); end; {$ENDIF VER2_4} +function WideBytesOf(const Value: UnicodeString): TBytes; +var + Len:Integer; +begin + Len:=Length(Value)*SizeOf(UnicodeChar); + SetLength(Result,Len); + if Len>0 then + Move(Value[1],Result[0],Len); +end; diff --git a/rtl/objpas/sysutils/sysunih.inc b/rtl/objpas/sysutils/sysunih.inc index d25dfcd40f..ec480387d7 100644 --- a/rtl/objpas/sysutils/sysunih.inc +++ b/rtl/objpas/sysutils/sysunih.inc @@ -45,3 +45,4 @@ function StrPCopy(Dest: PWideChar; const Source: UnicodeString): PWideChar; over function BytesOf(const Val: UnicodeString): TBytes; overload; function BytesOf(const Val: WideChar): TBytes; overload; {$ENDIF VER2_4} +function WideBytesOf(const Value: UnicodeString): TBytes; diff --git a/tests/test/units/sysutils/tbytesof.pp b/tests/test/units/sysutils/tbytesof.pp index 640c7cea09..158c3ddeaf 100644 --- a/tests/test/units/sysutils/tbytesof.pp +++ b/tests/test/units/sysutils/tbytesof.pp @@ -17,6 +17,18 @@ begin Result := Result and (B[I] = Etalon[I]); end; +function CheckWideBytes(const B: TBytes): Boolean; +const + Etalon: array[0..7] of Byte = (84, 00, 101, 00, 115, 00, 116, 00); +var + I: Integer; +begin + Result := Length(B) <= Length(Etalon); + if Result then + for I := Low(B) to High(B) do + Result := Result and (B[I] = Etalon[I]); +end; + var S: AnsiString; U: UnicodeString; @@ -36,4 +48,7 @@ begin B := BytesOf(U[1]); if not CheckBytes(B) then halt(4); + B := WideBytesOf(U); + if not CheckWideBytes(B) then + halt(5); end.