Browse Source

rtl: added BytesOf and StringOf, issue 34580, from silvioprog

mattias 6 years ago
parent
commit
5c7c57af41
1 changed files with 21 additions and 0 deletions
  1. 21 0
      packages/rtl/sysutils.pas

+ 21 - 0
packages/rtl/sysutils.pas

@@ -204,6 +204,9 @@ procedure AppendStr(var Dest: String; const S: string);
 
 function Format(const Fmt: String; const Args: array of JSValue): String;
 
+function BytesOf(const AVal: string): TBytes;
+function StringOf(const ABytes: TBytes): string;
+
 // JavaScript built-in functions
 function LocaleCompare(const s1, s2, locales: String): Boolean; assembler; overload;
 function NormalizeStr(const S: String; const Norm: String = 'NFC'): String; assembler; overload; // not in IE
@@ -1985,6 +1988,24 @@ begin
     end;
 end;
 
+function BytesOf(const AVal: string): TBytes;
+var
+  I: SizeUInt;
+begin
+  SetLength(Result, Length(AVal));
+  for I := 0 to Length(AVal)-1 do
+    Result[I] := Ord(AVal[I+1]);
+end;
+
+function StringOf(const ABytes: TBytes): string;
+var
+  I: Integer;
+begin
+  SetLength(Result, Length(ABytes));
+  for I := 0 to Length(ABytes)-1 do
+    Result[I+1] := Char(ABytes[I]);
+end;
+
 function LocaleCompare(const s1, s2, locales: String): Boolean; assembler;
 asm
   return s1.localeCompare(s2,locales) == 0;