-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathintutils.pas
555 lines (484 loc) · 18 KB
/
intutils.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
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit intutils;
interface
{$DEFINE TESTS}
function ParseUInt64(const Value: UTF8String; Default: UInt64 = 0): UInt64;
function ParseUInt32(const Value: UTF8String; Default: UInt32 = 0): UInt32;
function ParseInt64(const Value: UTF8String; Default: Int64 = 0): Int64;
function ParseInt32(const Value: UTF8String; Default: Int32 = 0): Int32;
// Reads an integer, returns how many bytes were parsed. Parsed integer is Output.
// Output is undefined if return value is zero.
// Will not read more than MaxLength bytes from Value.
function ParseUInt64(Value: PByte; MaxLength: Cardinal; out Output: UInt64): Cardinal;
function ParseUInt32(Value: PByte; MaxLength: Cardinal; out Output: UInt32): Cardinal;
function ParseInt64(Value: PByte; MaxLength: Cardinal; out Output: Int64): Cardinal;
function ParseInt32(Value: PByte; MaxLength: Cardinal; out Output: Int32): Cardinal;
implementation
function ParseUInt64(Value: PByte; MaxLength: Cardinal; out Output: UInt64): Cardinal;
var
BufferEnd: PByte;
Current: PByte;
Temp: UInt64;
begin
Current := Value;
BufferEnd := Value + MaxLength;
if ((Current >= BufferEnd) or (Current^ < $30) or (Current^ > $39)) then
begin
Result := 0;
exit;
end;
Output := 0;
repeat
{$PUSH}
{$OVERFLOWCHECKS-}
{$RANGECHECKS-}
Temp := Output * 10 + (Current^ - $30); // $R-
if (Temp < Output) then
begin
Result := 0;
exit;
end;
Output := Temp;
Inc(Current);
{$POP}
until ((Current >= BufferEnd) or (Current^ < $30) or (Current^ > $39));
Assert(Current > Value);
Result := Current - Value; // $R-
end;
function ParseUInt32(Value: PByte; MaxLength: Cardinal; out Output: UInt32): Cardinal;
var
Temp: UInt64;
begin
Result := ParseUInt64(Value, MaxLength, Temp);
if (Result > 0) then
begin
if (Temp > High(UInt32)) then
begin
Result := 0;
end
else
begin
Output := Temp; // $R-
end;
end;
end;
function ParseInt64(Value: PByte; MaxLength: Cardinal; out Output: Int64): Cardinal;
var
Unsigned: UInt64;
begin
Result := 0;
if (MaxLength = 0) then
exit;
if (Value^ = $2D) then
begin
Result := ParseUInt64(Value + 1, MaxLength - 1, Unsigned); // $R-
if (Result = 0) then
exit;
if (Unsigned > UInt64(-Low(Int64))) then
begin
Result := 0;
exit;
end;
Inc(Result);
if (Unsigned = UInt64(-Low(Int64))) then
begin
Output := Low(Int64);
exit;
end;
Output := -Unsigned; // $R-
exit;
end
else
begin
Result := ParseUInt64(Value, MaxLength, Unsigned);
if (Result = 0) then
exit;
if (Unsigned > UInt64(High(Int64))) then
begin
Result := 0;
exit;
end;
Output := Unsigned; // $R-
end;
end;
function ParseInt32(Value: PByte; MaxLength: Cardinal; out Output: Int32): Cardinal;
var
Unsigned: UInt64;
begin
Result := 0;
if (MaxLength = 0) then
exit;
if (Value^ = $2D) then
begin
Result := ParseUInt64(Value + 1, MaxLength - 1, Unsigned); // $R-
if (Result = 0) then
exit;
if (Unsigned > UInt64(-Low(Int32))) then
begin
Result := 0;
exit;
end;
Inc(Result);
if (Unsigned = UInt64(-Low(Int32))) then
begin
Output := Low(Int32);
exit;
end;
Output := -Unsigned; // $R-
exit;
end
else
begin
Result := ParseUInt64(Value, MaxLength, Unsigned);
if (Result = 0) then
exit;
if (Unsigned > UInt64(High(Int32))) then
begin
Result := 0;
exit;
end;
Output := Unsigned; // $R-
end;
end;
function ParseUInt64(const Value: UTF8String; Default: UInt64 = 0): UInt64;
begin
if (Value = '') then
begin
Result := Default;
end
else
if (ParseUInt64(PByte(@Value[1]), Length(Value), Result) <> Length(Value)) then // $R-
begin
Result := Default;
end;
end;
function ParseUInt32(const Value: UTF8String; Default: UInt32 = 0): UInt32;
var
Temp: UInt64;
begin
if (Value = '') then
begin
Result := Default;
end
else
if ((ParseUInt64(PByte(@Value[1]), Length(Value), Temp) <> Length(Value)) or // $R-
(Temp > High(UInt32))) then
begin
Result := Default;
end
else
begin
Result := Temp; // $R-
end;
end;
function ParseInt64(const Value: UTF8String; Default: Int64 = 0): Int64;
begin
if (Value = '') then
begin
Result := Default;
end
else
if (ParseInt64(PByte(@Value[1]), Length(Value), Result) <> Length(Value)) then // $R-
begin
Result := Default;
end;
end;
function ParseInt32(const Value: UTF8String; Default: Int32 = 0): Int32;
begin
if (Value = '') then
begin
Result := Default;
end
else
if (ParseInt32(PByte(@Value[1]), Length(Value), Result) <> Length(Value)) then // $R-
begin
Result := Default;
end;
end;
{$IFDEF TESTS}
procedure TestParseUInt64();
var
ParsedUInt64: UInt64;
ParsedLength: Cardinal;
Buffer: UTF8String;
begin
Buffer := '123';
ParsedLength := ParseUInt64(PByte(@Buffer[1]), Length(Buffer), ParsedUInt64); // $R-
Assert(ParsedLength = 3);
Assert(ParsedUInt64 = 123);
ParsedLength := ParseUInt64(PByte(0), 0, ParsedUInt64); // $R-
Assert(ParsedLength = 0);
Buffer := '-123'; // negative (leading garbage)
ParsedLength := ParseUInt64(PByte(@Buffer[1]), Length(Buffer), ParsedUInt64); // $R-
Assert(ParsedLength = 0);
Buffer := '12a3'; // trailing garbage
ParsedLength := ParseUInt64(PByte(@Buffer[1]), Length(Buffer), ParsedUInt64); // $R-
Assert(ParsedLength = 2);
Assert(ParsedUInt64 = 12);
Buffer := '12345678901234567890123456789012345678901234567890'; // much too long
ParsedLength := ParseUInt64(PByte(@Buffer[1]), Length(Buffer), ParsedUInt64); // $R-
Assert(ParsedLength = 0);
Buffer := '18446744073709551620'; // overflow on multiplication
ParsedLength := ParseUInt64(PByte(@Buffer[1]), Length(Buffer), ParsedUInt64); // $R-
Assert(ParsedLength = 0);
Buffer := '18446744073709551616'; // overflow on addition
ParsedLength := ParseUInt64(PByte(@Buffer[1]), Length(Buffer), ParsedUInt64); // $R-
Assert(ParsedLength = 0);
Buffer := '18446744073709551615'; // max
ParsedLength := ParseUInt64(PByte(@Buffer[1]), Length(Buffer), ParsedUInt64); // $R-
Assert(ParsedLength = Length(Buffer));
Assert(ParsedUInt64 = 18446744073709551615);
Buffer := '00000000000000000000000000000000000000000000000000';
ParsedLength := ParseUInt64(PByte(@Buffer[1]), Length(Buffer), ParsedUInt64); // $R-
Assert(ParsedLength = Length(Buffer));
Assert(ParsedUInt64 = 0);
end;
procedure TestParseInt64();
var
ParsedInt64: Int64;
ParsedLength: Cardinal;
Buffer: UTF8String;
begin
Buffer := '123';
ParsedLength := ParseInt64(PByte(@Buffer[1]), Length(Buffer), ParsedInt64); // $R-
Assert(ParsedLength = 3);
Assert(ParsedInt64 = 123);
ParsedLength := ParseInt64(PByte(0), 0, ParsedInt64); // $R-
Assert(ParsedLength = 0);
Buffer := '-123'; // negative
ParsedLength := ParseInt64(PByte(@Buffer[1]), Length(Buffer), ParsedInt64); // $R-
Assert(ParsedLength = 4);
Assert(ParsedInt64 = -123);
Buffer := '--123'; // leading garbage
ParsedLength := ParseInt64(PByte(@Buffer[1]), Length(Buffer), ParsedInt64); // $R-
Assert(ParsedLength = 0);
Buffer := '-0';
ParsedLength := ParseInt64(PByte(@Buffer[1]), Length(Buffer), ParsedInt64); // $R-
Assert(ParsedLength = 2);
Assert(ParsedInt64 = 0);
Buffer := '12a3'; // trailing garbage
ParsedLength := ParseInt64(PByte(@Buffer[1]), Length(Buffer), ParsedInt64); // $R-
Assert(ParsedLength = 2);
Assert(ParsedInt64 = 12);
Buffer := '12345678901234567890123456789012345678901234567890'; // much too long
ParsedLength := ParseInt64(PByte(@Buffer[1]), Length(Buffer), ParsedInt64); // $R-
Assert(ParsedLength = 0);
Buffer := '18446744073709551620'; // overflow on multiplication
ParsedLength := ParseInt64(PByte(@Buffer[1]), Length(Buffer), ParsedInt64); // $R-
Assert(ParsedLength = 0);
Buffer := '18446744073709551616'; // overflow on addition
ParsedLength := ParseInt64(PByte(@Buffer[1]), Length(Buffer), ParsedInt64); // $R-
Assert(ParsedLength = 0);
Buffer := '18446744073709551615'; // overflow on conversion to signed
ParsedLength := ParseInt64(PByte(@Buffer[1]), Length(Buffer), ParsedInt64); // $R-
Assert(ParsedLength = 0);
Buffer := '9223372036854775808'; // overflow on conversion to signed
ParsedLength := ParseInt64(PByte(@Buffer[1]), Length(Buffer), ParsedInt64); // $R-
Assert(ParsedLength = 0);
Buffer := '9223372036854775807'; // high(int64)
ParsedLength := ParseInt64(PByte(@Buffer[1]), Length(Buffer), ParsedInt64); // $R-
Assert(ParsedLength = Length(Buffer));
Assert(ParsedInt64 = 9223372036854775807);
Buffer := '-9223372036854775807'; // -high(int64)
ParsedLength := ParseInt64(PByte(@Buffer[1]), Length(Buffer), ParsedInt64); // $R-
Assert(ParsedLength = Length(Buffer));
Assert(ParsedInt64 = -9223372036854775807);
Buffer := '-9223372036854775808'; // low(int64)
ParsedLength := ParseInt64(PByte(@Buffer[1]), Length(Buffer), ParsedInt64); // $R-
Assert(ParsedLength = Length(Buffer));
Assert(ParsedInt64 = -9223372036854775808);
Buffer := '00000000000000000000000000000000000000000000000000';
ParsedLength := ParseInt64(PByte(@Buffer[1]), Length(Buffer), ParsedInt64); // $R-
Assert(ParsedLength = Length(Buffer));
Assert(ParsedInt64 = 0);
end;
procedure TestQuickParseUInt64();
begin
Assert(ParseUInt64('123') = 123);
Assert(ParseUInt64('') = 0);
Assert(ParseUInt64('-123') = 0);
Assert(ParseUInt64('12a3') = 0);
Assert(ParseUInt64('12345678901234567890123456789012345678901234567890') = 0);
Assert(ParseUInt64('18446744073709551620') = 0);
Assert(ParseUInt64('18446744073709551616') = 0);
Assert(ParseUInt64('18446744073709551615') = 18446744073709551615);
Assert(ParseUInt64('00000000000000000000000000000000000000000000000000') = 0);
end;
procedure TestQuickParseInt64();
begin
Assert(ParseInt64('123') = 123);
Assert(ParseInt64('') = 0);
Assert(ParseInt64('-123') = -123);
Assert(ParseInt64('--123') = 0);
Assert(ParseInt64('-0') = 0);
Assert(ParseInt64('12a3') = 0);
Assert(ParseInt64('12345678901234567890123456789012345678901234567890') = 0);
Assert(ParseInt64('18446744073709551620') = 0);
Assert(ParseInt64('18446744073709551616') = 0);
Assert(ParseInt64('18446744073709551615') = 0);
Assert(ParseInt64('9223372036854775808') = 0);
Assert(ParseInt64('9223372036854775807') = 9223372036854775807);
Assert(ParseInt64('-9223372036854775807') = -9223372036854775807);
Assert(ParseInt64('-9223372036854775808') = -9223372036854775808);
Assert(ParseInt64('00000000000000000000000000000000000000000000000000') = 0);
end;
procedure TestParseUInt32();
var
ParsedUInt32: UInt32;
ParsedLength: Cardinal;
Buffer: UTF8String;
begin
Buffer := '123';
ParsedLength := ParseUInt32(PByte(@Buffer[1]), Length(Buffer), ParsedUInt32); // $R-
Assert(ParsedLength = 3);
Assert(ParsedUInt32 = 123);
ParsedLength := ParseUInt32(PByte(0), 0, ParsedUInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '-123'; // negative (leading garbage)
ParsedLength := ParseUInt32(PByte(@Buffer[1]), Length(Buffer), ParsedUInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '12a3'; // trailing garbage
ParsedLength := ParseUInt32(PByte(@Buffer[1]), Length(Buffer), ParsedUInt32); // $R-
Assert(ParsedLength = 2);
Assert(ParsedUInt32 = 12);
Buffer := '12345678901234567890123456789012345678901234567890'; // much too long
ParsedLength := ParseUInt32(PByte(@Buffer[1]), Length(Buffer), ParsedUInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '18446744073709551620'; // overflow on multiplication
ParsedLength := ParseUInt32(PByte(@Buffer[1]), Length(Buffer), ParsedUInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '18446744073709551616'; // overflow on addition
ParsedLength := ParseUInt32(PByte(@Buffer[1]), Length(Buffer), ParsedUInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '4294967296'; // overflow on range check
ParsedLength := ParseUInt32(PByte(@Buffer[1]), Length(Buffer), ParsedUInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '4294967295'; // max
ParsedLength := ParseUInt32(PByte(@Buffer[1]), Length(Buffer), ParsedUInt32); // $R-
Assert(ParsedLength = Length(Buffer));
Assert(ParsedUInt32 = 4294967295);
Buffer := '00000000000000000000000000000000000000000000000000';
ParsedLength := ParseUInt32(PByte(@Buffer[1]), Length(Buffer), ParsedUInt32); // $R-
Assert(ParsedLength = Length(Buffer));
Assert(ParsedUInt32 = 0);
end;
procedure TestParseInt32();
var
ParsedInt32: Int32;
ParsedLength: Cardinal;
Buffer: UTF8String;
begin
Buffer := '123';
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = 3);
Assert(ParsedInt32 = 123);
ParsedLength := ParseInt32(PByte(0), 0, ParsedInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '-123'; // negative
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = 4);
Assert(ParsedInt32 = -123);
Buffer := '--123'; // leading garbage
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '-0';
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = 2);
Assert(ParsedInt32 = 0);
Buffer := '12a3'; // trailing garbage
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = 2);
Assert(ParsedInt32 = 12);
Buffer := '12345678901234567890123456789012345678901234567890'; // much too long
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '18446744073709551620'; // overflow on multiplication
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '18446744073709551616'; // overflow on addition
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '18446744073709551615'; // overflow on conversion to signed
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '9223372036854775808'; // overflow on conversion to signed
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '9223372036854775807'; // overflow on conversion to signed
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '-9223372036854775807'; // overflow on conversion to signed
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '-9223372036854775808'; // overflow on conversion to signed
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '4294967295'; // overflow on conversion to signed
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '-2147483649'; // overflow on conversion to signed
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = 0);
Buffer := '2147483647'; // highest value
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = Length(Buffer));
Assert(ParsedInt32 = 2147483647);
Buffer := '-2147483648'; // lowest value
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = Length(Buffer));
Assert(ParsedInt32 = -2147483648);
Buffer := '00000000000000000000000000000000000000000000000000';
ParsedLength := ParseInt32(PByte(@Buffer[1]), Length(Buffer), ParsedInt32); // $R-
Assert(ParsedLength = Length(Buffer));
Assert(ParsedInt32 = 0);
end;
procedure TestQuickParseUInt32();
begin
Assert(ParseUInt32('123') = 123);
Assert(ParseUInt32('') = 0);
Assert(ParseUInt32('-123') = 0);
Assert(ParseUInt32('12a3') = 0);
Assert(ParseUInt32('12345678901234567890123456789012345678901234567890') = 0);
Assert(ParseUInt32('18446744073709551620') = 0);
Assert(ParseUInt32('18446744073709551616') = 0);
Assert(ParseUInt32('18446744073709551615') = 0);
Assert(ParseUInt32('4294967296') = 0);
Assert(ParseUInt32('4294967295') = 4294967295);
Assert(ParseUInt32('00000000000000000000000000000000000000000000000000') = 0);
end;
procedure TestQuickParseInt32();
begin
Assert(ParseInt32('123') = 123);
Assert(ParseInt32('') = 0);
Assert(ParseInt32('-123') = -123);
Assert(ParseInt32('--123') = 0);
Assert(ParseInt32('-0') = 0);
Assert(ParseInt32('12a3') = 0);
Assert(ParseInt32('12345678901234567890123456789012345678901234567890') = 0);
Assert(ParseInt32('18446744073709551620') = 0);
Assert(ParseInt32('18446744073709551616') = 0);
Assert(ParseInt32('18446744073709551615') = 0);
Assert(ParseInt32('9223372036854775808') = 0);
Assert(ParseInt32('9223372036854775807') = 0);
Assert(ParseInt32('-9223372036854775807') = 0);
Assert(ParseInt32('-9223372036854775808') = 0);
Assert(ParseInt32('4294967295') = 0);
Assert(ParseInt32('-2147483649') = 0);
Assert(ParseInt32('2147483647') = 2147483647);
Assert(ParseInt32('-2147483648') = -2147483648);
Assert(ParseInt32('00000000000000000000000000000000000000000000000000') = 0);
end;
{$ENDIF}
initialization
{$IFDEF TESTS}
TestParseUInt64();
TestParseInt64();
TestQuickParseUInt64();
TestQuickParseInt64();
TestParseUInt32();
TestParseInt32();
TestQuickParseUInt32();
TestQuickParseInt32();
{$ENDIF}
end.