-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWeb.mORMot.StringUtils.pas
86 lines (65 loc) · 2.1 KB
/
Web.mORMot.StringUtils.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
84
85
86
unit Web.mORMot.StringUtils;
{-------------------------------------------------------------------------------
Adapted from mORMot v1 CrossPlatform units.
See original file for copyright and licence information at:
https://github.com/synopse/mORMot
-------------------------------------------------------------------------------}
interface
uses
JS,
Types,
Web.mORMot.Types;
type
TEncodingType = (etNone, etHttp);
TStringArrayHelper = record helper for TStringDynArray
function Join(const separator: string): string;
procedure Add(const s: string);
procedure Clear;
end;
/// convert a text into UTF-8 binary buffer
function TextToHttpBody(const Text: string): THttpBody;
/// convert a UTF-8 binary buffer into texts
procedure HttpBodyToText(const Body: THttpBody; var Text: string);
implementation
//------------------------------------------------------------------------------
function TextToHttpBody(const Text: string): THttpBody;
begin
// http://ecmanaut.blogspot.fr/2006/07/encoding-decoding-utf8-in-javascript.html
//asm
// @result=unescape(encodeURIComponent(@Text));
//end;
result := encodeURIComponent(Text);
end;
//------------------------------------------------------------------------------
procedure HttpBodyToText(const Body: THttpBody; var Text: string);
begin
Text := decodeURIComponent(Body);
end;
{ TStringArrayHelper }
//------------------------------------------------------------------------------
procedure TStringArrayHelper.Add(const s: string);
var
l: integer;
begin
{ TODO : TStringArrayHelper.Add -> make more efficient? }
l := length(self);
SetLength(self, l + 1);
self[l] := s;
end;
//------------------------------------------------------------------------------
procedure TStringArrayHelper.Clear;
begin
SetLength(self, 0);
end;
//------------------------------------------------------------------------------
function TStringArrayHelper.Join(const separator: string): string;
var
i: integer;
l: integer;
begin
l := length(self);
for i := 0 to l - 2 do
result := result + self[i] + separator;
result := result + self[l - 1];
end;
end.