mirror of
https://gitlab.com/freepascal.org/fpc/source.git
synced 2025-04-16 03:59:28 +02:00
rtl: add BytesOf(String): TBytes for delphi compatibility
git-svn-id: trunk@19390 -
This commit is contained in:
parent
7817f5017d
commit
fa43a448b9
1
.gitattributes
vendored
1
.gitattributes
vendored
@ -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/tastrcmp.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/tencodingerrors.pp svneol=native#text/pascal
|
||||
tests/test/units/sysutils/tencodingtest.pp svneol=native#text/pascal
|
||||
|
@ -89,4 +89,20 @@ begin
|
||||
HashName:=$ffffffff
|
||||
else
|
||||
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;
|
@ -26,3 +26,6 @@ function AnsiStrPos(str,substr : PChar) : PChar;
|
||||
function AnsiStrRScan(Str : PChar;Chr : Char) : PChar;
|
||||
function AnsiStrScan(Str : PChar;Chr: Char) : PChar;
|
||||
function HashName(Name: PAnsiChar): LongWord;
|
||||
|
||||
function BytesOf(const Val: RawByteString): TBytes;
|
||||
function BytesOf(const Val: AnsiChar): TBytes;
|
@ -167,3 +167,15 @@ function StrPCopy(Dest: PWideChar; const Source: UnicodeString): PWideChar; over
|
||||
begin
|
||||
StrPCopy := StrPLCopy(Dest, Source, length(Source));
|
||||
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}
|
||||
|
@ -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 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
tests/test/units/sysutils/tbytesof.pp
Normal file
39
tests/test/units/sysutils/tbytesof.pp
Normal file
@ -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.
|
Loading…
Reference in New Issue
Block a user