-
Notifications
You must be signed in to change notification settings - Fork 32
/
Quick.ORM.UTF8Helper.pas
376 lines (323 loc) · 9.63 KB
/
Quick.ORM.UTF8Helper.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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
{ ***************************************************************************
Copyright (c) 2016-2019 Kike Pérez
Unit : Quick.ORM.UTF8Helper
Description : Helper functions for SynCommons RawUT8 type
Author : Kike Pérez
Version : 1.2
Created : 18/07/2017
Modified : 08/05/2019
This file is part of QuickORM: https://github.com/exilon/QuickORM
Uses Synopse mORMot framework. Copyright (C) 2017 Arnaud Bouchez
Synopse Informatique - https://synopse.info
***************************************************************************
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*************************************************************************** }
unit Quick.UTF8Helper;
{$i QuickORM.inc}
{$I Synopse.inc}
interface
uses
Classes,
System.SysUtils,
SynCommons;
type
TRawUTF8Helper = record helper for RawUTF8
private
function GetChars(Index: Integer): AnsiChar;
function GetLength: Integer; inline;
function GetCharCount: Integer; inline;
class function EndsText(const ASubText, AText: RawUTF8) : Boolean; static;
function IndexOfAny(const AnyOf: array of AnsiChar): Integer; overload;
function IndexOfAny(const AnyOf: array of AnsiChar; StartIndex: Integer): Integer; overload;
function IndexOfAny(const AnyOf: array of AnsiChar; StartIndex: Integer; Count: Integer): Integer; overload;
public
property Length : Integer read GetLength;
property CharCount : Integer read GetCharCount;
function ToBoolean : Boolean; inline;
function ToInteger : Integer; inline;
function ToInt64 : Int64; inline;
function ToSingle : Single; inline;
function ToDouble : Double; inline;
function ToExtended : Extended; inline;
function ToString : string;
function ToLower : RawUTF8;
function ToUpper : RawUTF8;
function Capitalize : RawUTF8;
function CapitalizeAll : RawUTF8;
function Contains(const Value: RawUTF8; IgnoreCase : Boolean = False): Boolean;
function IsEmpty: Boolean;
function StartsWith(const Value: RawUTF8; IgnoreCase: Boolean = False) : Boolean;
function EndsWith(const Value: RawUTF8; IgnoreCase: Boolean = False) : Boolean;
function Replace(OldChar: Char; NewChar: Char): RawUTF8;
procedure LoadFromFile(const aFilename : TFileName);
function Substring(StartIndex: Integer): RawUTF8; overload; inline;
function Substring(StartIndex: Integer; Length: Integer): RawUTF8; overload; inline;
function Split(const Separator: array of AnsiChar) : TArray<RawUTF8>;
property Chars[Index: Integer]: AnsiChar read GetChars;
function Trim: RawUTF8; overload;
function TrimLeft: RawUTF8; overload;
function TrimRight: RawUTF8; overload;
end;
implementation
{TRawUTF8Helper}
{$ZEROBASEDSTRINGS ON}
function TRawUTF8Helper.GetChars(Index: Integer): AnsiChar;
begin
Result := Self[Index];
end;
function TRawUTF8Helper.GetLength : Integer;
begin
Result := System.Length(Self);
end;
function TRawUTF8Helper.GetCharCount : Integer;
begin
Result := System.Length(Self.ToString);
end;
function TRawUTF8Helper.ToBoolean : Boolean;
begin
Result := StrToBool(Self);
end;
function TRawUTF8Helper.ToInteger : Integer;
begin
Result := UTF8ToInteger(Self);
end;
function TRawUTF8Helper.ToInt64 : Int64;
begin
Result := UTF8ToInt64(Self);
end;
function TRawUTF8Helper.ToSingle : Single;
begin
Result := Single.Parse(Self);
end;
function TRawUTF8Helper.ToDouble : Double;
begin
Result := Double.Parse(Self);
end;
function TRawUTF8Helper.ToExtended : Extended;
begin
Result := Extended.Parse(Self);
end;
function TRawUTF8Helper.ToString : string;
begin
Result := UTF8ToString(Self);
end;
function TRawUTF8Helper.ToLower : RawUTF8;
begin
Result := AnsiLowerCase(Self);
end;
function TRawUTF8Helper.ToUpper : RawUTF8;
begin
Result := AnsiUpperCase(Self);
end;
function TRawUTF8Helper.Capitalize : RawUTF8;
var
s : string;
begin
Result := '';
if Self.Length = 0 then Exit;
s := Self.ToLower;
Result := AnsiUpperCase(Copy(s, 1, 1)) + (Copy(s, 2, s.Length)).Trim;
end;
function TRawUTF8Helper.CapitalizeAll : RawUTF8;
var
cword : RawUTF8;
begin
Result := '';
if Self.Length = 0 then Exit;
for cword in Self.Split([' ']) do
begin
if Result = '' then Result := cword.Capitalize
else Result := Result + ' ' + cword.Capitalize;
end;
end;
function TRawUTF8Helper.Contains(const Value: RawUTF8; IgnoreCase : Boolean = False): Boolean;
begin
if IgnoreCase then Result := PosEx(Value.ToLower, Self.ToLower, 1) > 0
else Result := PosEx(Value, Self, 1) > 0;
end;
function TRawUTF8Helper.IsEmpty: Boolean;
begin
Result := Self = '';
end;
function TRawUTF8Helper.StartsWith(const Value: RawUTF8; IgnoreCase: Boolean = False): Boolean;
var
s,v : string;
begin
if Value = '' then
Result := True
else
begin
s := Self.ToString;
v := Value.ToString;
if not IgnoreCase then
begin
Result := System.SysUtils.StrLComp(PChar(s), PChar(v), v.Length) = 0;
end
else
begin
Result := AnsiStrIComp(PChar(v),PChar(Copy(s,1,v.Length))) = 0;
//Result := System.SysUtils.StrLIComp(PChar(Self.ToString), PChar(Value), Value.Length) = 0;
end;
end;
end;
class function TRawUTF8Helper.EndsText(const ASubText, AText: RawUTF8): Boolean;
var
SubTextLocation: Integer;
t, s : string;
begin
if ASubText = '' then
Result := True
else
begin
t := AText.ToString;
s := ASubText.ToString;
SubTextLocation := t.Length - s.Length;
if (SubTextLocation >= 0) and (ByteType(t, SubTextLocation) <> mbTrailByte) then
Result := AnsiStrIComp(PChar(s), PChar(@t[SubTextLocation])) = 0
else
Result := False;
end;
end;
function TRawUTF8Helper.EndsWith(const Value: RawUTF8; IgnoreCase: Boolean = False): Boolean;
var
SubTextLocation: Integer;
v : string;
begin
if IgnoreCase then
Result := EndsText(Value, Self)
else
if Value = '' then
Result := True
else
begin
v := Value.ToString;
SubTextLocation := Self.CharCount - v.Length;
if (SubTextLocation >= 0) and (ByteType(Self, SubTextLocation) <> mbTrailByte) then
Result := string.Compare(v, 0, Self, SubTextLocation, v.Length, []) = 0
else
Result := False;
end;
end;
function TRawUTF8Helper.Replace(OldChar: Char; NewChar: Char): RawUTF8;
begin
Result := StringReplaceAll(Self,StringToUTF8(OldChar),StringToUTF8(NewChar));
end;
procedure TRawUTF8Helper.LoadFromFile(const aFilename : TFileName);
begin
Self := AnyTextFileToRawUTF8(Self,True);
end;
function TRawUTF8Helper.Substring(StartIndex: Integer): RawUTF8;
begin
Result := System.Copy(Self, StartIndex + 1, Self.Length);
end;
function TRawUTF8Helper.Substring(StartIndex, Length: Integer): RawUTF8;
begin
Result := System.Copy(Self, StartIndex + 1, Length);
end;
function TRawUTF8Helper.IndexOfAny(const AnyOf: array of AnsiChar; StartIndex, Count: Integer): Integer;
var
i: Integer;
c: AnsiChar;
max: Integer;
begin
if (StartIndex + Count) >= Self.Length then
max := Self.Length
else max := StartIndex + Count;
i := StartIndex;
while i < max do
begin
for c in AnyOf do
if Self[i] = c then Exit(i);
Inc(i);
end;
Result := -1;
end;
function TRawUTF8Helper.IndexOfAny(const AnyOf: array of AnsiChar; StartIndex: Integer): Integer;
begin
Result := IndexOfAny(AnyOf, StartIndex, Self.Length);
end;
function TRawUTF8Helper.IndexOfAny(const AnyOf: array of AnsiChar): Integer;
begin
Result := IndexOfAny(AnyOf, 0, Self.Length);
end;
function TRawUTF8Helper.Split(const Separator: array of AnsiChar) : TArray<RawUTF8>;
const
DeltaGrow = 32;
var
NextSeparator, LastIndex: Integer;
Total: Integer;
CurrentLength: Integer;
S: RawUTF8;
begin
Total := 0;
LastIndex := 0;
CurrentLength := 0;
NextSeparator := IndexOfAny(Separator, LastIndex);
while (NextSeparator >= 0) do
begin
S := Substring(LastIndex, NextSeparator - LastIndex);
if (S <> '') or ((S = '')) then
begin
Inc(Total);
if CurrentLength < Total then
begin
CurrentLength := Total + DeltaGrow;
SetLength(Result, CurrentLength);
end;
Result[Total - 1] := S;
end;
LastIndex := NextSeparator + 1;
NextSeparator := IndexOfAny(Separator, LastIndex);
end;
if (LastIndex < Self.Length) then
begin
Inc(Total);
SetLength(Result, Total);
Result[Total - 1] := Substring(LastIndex, Self.Length - LastIndex);
end
else SetLength(Result, Total);
end;
function TRawUTF8Helper.Trim: RawUTF8;
var
I, L: Integer;
begin
L := Self.Length - 1;
I := 0;
if (L > -1) and (Self[I] > ' ') and (Self[L] > ' ') then Exit(Self);
while (I <= L) and (Self[I] <= ' ') do Inc(I);
if I > L then Exit('');
while Self[L] <= ' ' do Dec(L);
Result := Self.SubString(I, L - I + 1);
end;
function TRawUTF8Helper.TrimLeft: RawUTF8;
var
I, L: Integer;
begin
L := Self.Length - 1;
I := 0;
while (I <= L) and (Self[I] <= ' ') do Inc(I);
if I > 0 then
Result := Self.SubString(I)
else
Result := Self;
end;
function TRawUTF8Helper.TrimRight: RawUTF8;
var
I: Integer;
begin
I := Self.Length - 1;
if (I >= 0) and (Self[I] > ' ') then Result := Self
else begin
while (I >= 0) and (Self.Chars[I] <= ' ') do Dec(I);
Result := Self.SubString(0, I + 1);
end;
end;
end.