-
Notifications
You must be signed in to change notification settings - Fork 8
/
DX.Sparkle.Utils.pas
83 lines (68 loc) · 1.62 KB
/
DX.Sparkle.Utils.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
unit DX.Sparkle.Utils;
interface
uses
System.Classes, System.SysUtils;
type
TBytesHelper = record helper for TBytes
function ToRawBytesString: RawByteString;
end;
TRawByteStringHelper = record helper for RawByteString
function ToBytes: TBytes;
end;
function Encode(const AInput: string): RawByteString;
function Decode(
const AInput: RawByteString;
const AContentType: string): String; overload;
function Decode(
const AInput: TBytes;
const AContentType: string): String; overload;
implementation
function Encode(const AInput: string): RawByteString;
begin
result := TEncoding.UTF8.GetBytes(AInput).ToRawBytesString;
end;
function Decode(
const AInput: RawByteString;
const AContentType: string): String;
begin
result := Decode(AInput.ToBytes, AContentType);
end;
function Decode(
const AInput: TBytes;
const AContentType: string): String;
var
LEncoding: TEncoding;
begin
if AContentType.ToUpper.contains('UTF-8') then
begin
result := TEncoding.UTF8.GetString(AInput);
end
else
begin
LEncoding := nil;
TEncoding.GetBufferEncoding(AInput, LEncoding);
result := LEncoding.GetString(AInput);
end;
end;
{ TEncodingHelper }
function TBytesHelper.ToRawBytesString: RawByteString;
begin
SetLength(result, Length(self));
if Length(self) > 0 then
begin
Move(self[0], result[1], Length(self));
end;
end;
{ TRawByteStringHelper }
function TRawByteStringHelper.ToBytes: TBytes;
var
LResult: TBytes;
begin
SetLength(LResult, Length(self));
if Length(self) > 0 then
begin
Move(self[1], LResult[0], Length(self));
end;
result := LResult;
end;
end.