-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmv.StringUtils.pas
240 lines (208 loc) · 6.15 KB
/
mv.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
unit mv.StringUtils;
interface
uses
System.Types, System.SysUtils, System.ZLib, System.NetEncoding,
System.RegularExpressions, System.Classes, System.StrUtils;
function SplitString(const AInput: string): TStringDynArray;
function JoinString(const Arr: TStringDynArray): string;
function EscapeString(const s: string): string;
function ExtractField(const Source, Delimiter: string; Index: Integer): string;
function AddSlashes(AString: string): string;
function StandardizeLineEndings(const S: string): string;
function CompressString(const AString: string): string;
function DecompressString(AString: string): string;
implementation
function SplitString(const AInput: string): TStringDynArray;
var
i, StartPos: Integer;
CurrentChar: Char;
InDoubleQuote, InSingleQuote, Escaping: Boolean;
begin
SetLength(Result, 0);
StartPos := 1;
InDoubleQuote := False;
InSingleQuote := False;
Escaping := False;
for i := 1 to Length(AInput) do
begin
if Escaping then
begin
Escaping := False;
Continue;
end;
CurrentChar := AInput[i];
case CurrentChar of
' ', #9, #10, #13: // Space, Tab, LF, CR
begin
if not InDoubleQuote and not InSingleQuote then
begin
if StartPos < i then
begin
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := Copy(AInput, StartPos, i - StartPos);
end;
StartPos := i + 1;
end;
end;
'"':
begin
if not InSingleQuote then
begin
InDoubleQuote := not InDoubleQuote;
if not InDoubleQuote and (StartPos < i) then
begin
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := Copy(AInput, StartPos, i - StartPos + 1);
StartPos := i + 1;
end;
end;
end;
'''':
begin
if not InDoubleQuote then
begin
InSingleQuote := not InSingleQuote;
if not InSingleQuote and (StartPos < i) then
begin
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := Copy(AInput, StartPos, i - StartPos + 1);
StartPos := i + 1;
end;
end;
end;
'\':
begin
if InDoubleQuote or InSingleQuote then
Escaping := True; // Mark the next character to be skipped.
end;
end;
end;
if StartPos <= Length(AInput) then
begin
SetLength(Result, Length(Result) + 1);
Result[High(Result)] := Copy(AInput, StartPos, Length(AInput) - StartPos + 1);
end;
end;
function JoinString(const Arr: TStringDynArray): string;
var
s, Encoded: string;
begin
Result := '';
for s in Arr do
begin
Encoded := EscapeString(s);
if (Pos(' ', Encoded) > 0) or (Pos('"', s) > 0) or (Pos('''', s) > 0) or (Pos('\', s) > 0) then
Result := Result + '"' + Encoded + '" ' // Wrap with double quotes after escaping
else
Result := Result + Encoded + ' ';
end;
// Remove the trailing space, if present
if (Result <> '') and (Result[Length(Result)] = ' ') then
SetLength(Result, Length(Result) - 1);
end;
function EscapeString(const s: string): string;
var
i: Integer;
begin
Result := '';
for i := 1 to Length(s) do
begin
if s[i] in ['\', '"'] then
Result := Result + '\'; // Add a backslash before the character
Result := Result + s[i];
end;
end;
function ExtractField(const Source, Delimiter: string; Index: Integer): string;
var
PosStart, PosEnd: Integer;
CurrentIndex: Integer;
begin
Result := '';
if Index < 1 then Exit;
PosStart := 1;
CurrentIndex := 1;
while CurrentIndex <= Index do
begin
PosEnd := PosEx(Delimiter, Source, PosStart);
if PosEnd = 0 then PosEnd := Length(Source) + 1;
if CurrentIndex = Index then
begin
Result := Copy(Source, PosStart, PosEnd - PosStart);
Break;
end;
Inc(CurrentIndex);
PosStart := PosEnd + Length(Delimiter);
end;
end;
function AddSlashes(AString: string): string;
begin
Result := AString;
Result := ReplaceStr(Result, '"', '\"');
Result := ReplaceStr(Result, '''', '\''');
end;
function StandardizeLineEndings(const S: string): string;
begin
Result := S;
// Replace CR+LF with LF to make sure we don't double-convert in the next step.
Result := StringReplace(Result, #13#10, #10, [rfReplaceAll]);
Result := StringReplace(Result, #13, #10, [rfReplaceAll]);
// Replace LF with CR+LF.
Result := StringReplace(Result, #10, #13#10, [rfReplaceAll]);
end;
function CompressString(const AString: string): string;
var
Input, Output: TBytesStream;
Compressor: TZCompressionStream;
begin
Result := '';
if '' = AString then Exit;
Input := TBytesStream.Create(TEncoding.UTF8.GetBytes(AString));
try
Output := TBytesStream.Create;
try
Compressor := TZCompressionStream.Create(Output);
try
Compressor.CopyFrom(Input, Input.Size);
finally
Compressor.Free;
end;
Result := '!' + TNetEncoding.Base64URL.EncodeBytesToString(Output.Bytes);
finally
Output.Free;
end;
finally
Input.Free;
end;
end;
function DecompressString(AString: string): string;
var
Input, Output: TBytesStream;
Decompressor: TZDecompressionStream;
DecodedBytes: TBytes;
begin
Result := '';
if '' = AString then Exit;
Result := AString;
if '!' <> AString[1] then Exit;
AString := AString.Substring(2);
DecodedBytes := TNetEncoding.Base64URL.DecodeStringToBytes(AString);
Input := TBytesStream.Create(DecodedBytes);
try
Output := TBytesStream.Create;
try
Decompressor := TZDecompressionStream.Create(Input);
try
Output.CopyFrom(Decompressor, 0);
finally
Decompressor.Free;
end;
Result := TEncoding.UTF8.GetString(Output.Bytes, 0, Output.Size);
finally
Output.Free;
end;
except
Result := AString;
end;
Input.Free;
end;
end.