Forráskód Böngészése

rtl: add StringOf(TBytes), WideStringOf(TBytes), ByteLength(UnicodeString) for delphi compatibility

git-svn-id: trunk@19392 -
paul 14 éve
szülő
commit
fe782eac07

+ 20 - 0
rtl/objpas/sysutils/sysuni.inc

@@ -178,6 +178,11 @@ function BytesOf(const Val: WideChar): TBytes; overload;
 begin
   Result:=TEncoding.Default.GetBytes(Val);
 end;
+
+function StringOf(const Bytes: TBytes): UnicodeString;
+begin
+  Result:=TEncoding.Default.GetString(Bytes);
+end;
 {$ENDIF VER2_4}
 function WideBytesOf(const Value: UnicodeString): TBytes;
 var
@@ -188,3 +193,18 @@ begin
   if Len>0 then
     Move(Value[1],Result[0],Len);
 end;
+
+function WideStringOf(const Value: TBytes): UnicodeString;
+var
+  Len:Integer;
+begin
+  Len:=Length(Value) div SizeOf(UnicodeChar);
+  SetLength(Result,Len);
+  if Len>0 then
+    Move(Value[0],Result[1],Len*SizeOf(UnicodeChar));
+end;
+
+function ByteLength(const S: UnicodeString): Integer;
+begin
+  Result:=Length(S)*SizeOf(UnicodeChar);
+end;

+ 3 - 0
rtl/objpas/sysutils/sysunih.inc

@@ -44,5 +44,8 @@ function StrPCopy(Dest: PWideChar; const Source: UnicodeString): PWideChar; over
 {$IFNDEF VER2_4}
 function BytesOf(const Val: UnicodeString): TBytes; overload;
 function BytesOf(const Val: WideChar): TBytes; overload;
+function StringOf(const Bytes: TBytes): UnicodeString;
 {$ENDIF VER2_4}
 function WideBytesOf(const Value: UnicodeString): TBytes;
+function WideStringOf(const Value: TBytes): UnicodeString;
+function ByteLength(const S: UnicodeString): Integer;

+ 11 - 6
tests/test/units/sysutils/tbytesof.pp

@@ -1,6 +1,7 @@
 program tbytesof;
 
 {$mode objfpc}{$H+}
+{$apptype console}
 
 uses
   SysUtils, Classes;
@@ -35,20 +36,24 @@ var
   B: TBytes;
 begin
   S := 'Test';
+  U := S;
   B := BytesOf(S);
   if not CheckBytes(B) then
     halt(1);
+  if StringOf(B) <> U then
+    halt(2);
   B := BytesOf(S[1]);
   if not CheckBytes(B) then
-    halt(2);
-  U := S;
+    halt(3);
   B := BytesOf(U);
   if not CheckBytes(B) then
-    halt(3);
+    halt(4);
   B := BytesOf(U[1]);
   if not CheckBytes(B) then
-    halt(4);
+    halt(5);
   B := WideBytesOf(U);
   if not CheckWideBytes(B) then
-    halt(5);
-end.
+    halt(6);
+  if WideStringOf(B) <> U then
+    halt(7);
+end.