Преглед изворни кода

rtl: add BytesOf(String): TBytes for delphi compatibility

git-svn-id: trunk@19390 -
paul пре 14 година
родитељ
комит
fa43a448b9

+ 1 - 0
.gitattributes

@@ -10801,6 +10801,7 @@ tests/test/units/system/tvalc.pp svneol=native#text/plain
 tests/test/units/sysutils/strtotimetest.pp svneol=native#text/plain
 tests/test/units/sysutils/strtotimetest.pp svneol=native#text/plain
 tests/test/units/sysutils/tastrcmp.pp svneol=native#text/plain
 tests/test/units/sysutils/tastrcmp.pp svneol=native#text/plain
 tests/test/units/sysutils/tastrcmp1.pp svneol=native#text/plain
 tests/test/units/sysutils/tastrcmp1.pp svneol=native#text/plain
+tests/test/units/sysutils/tbytesof.pp svneol=native#text/pascal
 tests/test/units/sysutils/tdirex.pp svneol=native#text/plain
 tests/test/units/sysutils/tdirex.pp svneol=native#text/plain
 tests/test/units/sysutils/tencodingerrors.pp svneol=native#text/pascal
 tests/test/units/sysutils/tencodingerrors.pp svneol=native#text/pascal
 tests/test/units/sysutils/tencodingtest.pp svneol=native#text/pascal
 tests/test/units/sysutils/tencodingtest.pp svneol=native#text/pascal

+ 16 - 0
rtl/objpas/sysutils/sysansi.inc

@@ -89,4 +89,20 @@ begin
     HashName:=$ffffffff
     HashName:=$ffffffff
   else
   else
     HashName:=TheHash;
     HashName:=TheHash;
+end;
+
+function BytesOf(const Val: RawByteString): TBytes;
+var
+  Len:Integer;
+begin
+  Len:=Length(Val);
+  SetLength(Result,Len);
+  if Len>0 then
+    Move(Val[1],Result[0],Len);
+end;
+
+function BytesOf(const Val: AnsiChar): TBytes;
+begin
+  SetLength(Result,1);
+  Result[0]:=Byte(Val);
 end;
 end;

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

@@ -26,3 +26,6 @@ function AnsiStrPos(str,substr : PChar) : PChar;
 function AnsiStrRScan(Str : PChar;Chr : Char) : PChar;
 function AnsiStrRScan(Str : PChar;Chr : Char) : PChar;
 function AnsiStrScan(Str : PChar;Chr: Char) : PChar;
 function AnsiStrScan(Str : PChar;Chr: Char) : PChar;
 function HashName(Name: PAnsiChar): LongWord;
 function HashName(Name: PAnsiChar): LongWord;
+
+function BytesOf(const Val: RawByteString): TBytes;
+function BytesOf(const Val: AnsiChar): TBytes;

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

@@ -167,3 +167,15 @@ function StrPCopy(Dest: PWideChar; const Source: UnicodeString): PWideChar; over
 begin
 begin
   StrPCopy := StrPLCopy(Dest, Source, length(Source));
   StrPCopy := StrPLCopy(Dest, Source, length(Source));
 end;
 end;
+
+{$IFNDEF VER2_4}
+function BytesOf(const Val: UnicodeString): TBytes;
+begin
+  Result:=TEncoding.Default.GetBytes(Val);
+end;
+
+function BytesOf(const Val: WideChar): TBytes; overload;
+begin
+  Result:=TEncoding.Default.GetBytes(Val);
+end;
+{$ENDIF VER2_4}

+ 4 - 1
rtl/objpas/sysutils/sysunih.inc

@@ -41,4 +41,7 @@ Procedure UnicodeFmtStr(Var Res: UnicodeString; Const Fmt : UnicodeString; Const
 
 
 function StrPLCopy(Dest: PWideChar; const Source: UnicodeString; MaxLen: SizeInt): PWideChar; overload;
 function StrPLCopy(Dest: PWideChar; const Source: UnicodeString; MaxLen: SizeInt): PWideChar; overload;
 function StrPCopy(Dest: PWideChar; const Source: UnicodeString): PWideChar; overload;
 function StrPCopy(Dest: PWideChar; const Source: UnicodeString): PWideChar; overload;
-
+{$IFNDEF VER2_4}
+function BytesOf(const Val: UnicodeString): TBytes; overload;
+function BytesOf(const Val: WideChar): TBytes; overload;
+{$ENDIF VER2_4}

+ 39 - 0
tests/test/units/sysutils/tbytesof.pp

@@ -0,0 +1,39 @@
+program tbytesof;
+
+{$mode objfpc}{$H+}
+
+uses
+  SysUtils, Classes;
+
+function CheckBytes(const B: TBytes): Boolean;
+const
+  Etalon: array[0..3] of Byte = (84, 101, 115, 116);
+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;
+  B: TBytes;
+begin
+  S := 'Test';
+  B := BytesOf(S);
+  if not CheckBytes(B) then
+    halt(1);
+  B := BytesOf(S[1]);
+  if not CheckBytes(B) then
+    halt(2);
+  U := S;
+  B := BytesOf(U);
+  if not CheckBytes(B) then
+    halt(3);
+  B := BytesOf(U[1]);
+  if not CheckBytes(B) then
+    halt(4);
+end.