forked from grijjy/GrijjyFoundation
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGrijjy.SysUtils.pas
1163 lines (995 loc) · 31.9 KB
/
Grijjy.SysUtils.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
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
unit Grijjy.SysUtils;
{< System level utilities }
{$INCLUDE 'Grijjy.inc'}
interface
uses
System.SysUtils;
type
{ Class representing a buffer of bytes, with methods to efficiently add data
to the buffer.
Use this class to build up a buffer of bytes using (many) smaller appends.
This is more efficient then concatenating multiple TBytes together. }
TgoByteBuffer = class
{$REGION 'Internal Declarations'}
private
FBuffer: TBytes;
FSize: Integer;
FDeltaMask: Integer;
FDeltaShift: Integer;
FCapacity: Integer;
{$ENDREGION 'Internal Declarations'}
public
{ Create a new byte buffer.
Parameters:
ACapacity: (optional) initial capacity. Defaults to 256.
ADelta: (optional) number of bytes to increase capacity when buffer
becomes to small. When ADelta is not a power of 2, it is adjusted to
the next power of 2 }
constructor Create(const ACapacity: Integer = 256;
const ADelta: Integer = 256);
{ Appends a single byte.
Parameters:
AByte: the byte to append. }
procedure Append(const AByte: Byte); overload;
{ Appends an array of bytes.
Parameters:
ABytes: the array of bytes to append. }
procedure Append(const ABytes: TBytes); overload;
{ Appends an array of bytes.
Parameters:
ABytes: the array of bytes to append. }
procedure Append(const ABytes: array of Byte); overload;
{ Appends a segment of an array of bytes.
Parameters:
ABytes: the array of bytes containing the segment to append.
AIndex: index into ABytes of the start of the segment. The segment runs
until the end of the ABytes array. If the index is invalid, nothing
happens. }
procedure Append(const ABytes: TBytes; const AIndex: Integer); overload;
{ Appends a segment of an array of bytes.
Parameters:
ABytes: the array of bytes containing the segment to append.
AIndex: index into ABytes of the start of the segment. The segment runs
until the end of the ABytes array. If the index is invalid, nothing
happens. }
procedure Append(const ABytes: array of Byte; const AIndex: Integer); overload;
{ Appends a segment of an array of bytes.
Parameters:
ABytes: the array of bytes containing the segment to append.
AIndex: index into ABytes of the start of the segment. If the index is
invalid, nothing happens.
ASize: the number of bytes in the segment to append. If the size exceeds
beyond the end of the ABytes array, then it will be adjust to fit. }
procedure Append(const ABytes: TBytes; const AIndex, ASize: Integer); overload;
{ Appends a segment of an array of bytes.
Parameters:
ABytes: the array of bytes containing the segment to append.
AIndex: index into ABytes of the start of the segment. If the index is
invalid, nothing happens.
ASize: the number of bytes in the segment to append. If the size exceeds
beyond the end of the ABytes array, then it will be adjust to fit. }
procedure Append(const ABytes: array of Byte; const AIndex, ASize: Integer); overload;
{ Appends an untyped memory buffer.
Parameters:
ABuffer: untyped memory buffer with the data to append.
ASize: the number of bytes in buffer to append. }
procedure AppendBuffer(const ABuffer; const ASize: Integer);
{ Clears the buffer. This does not free the memory for the buffer, so
subsequent appends will use the already allocated memory.
To free the memory for the buffer, free the object or call TrimExcess
after clearing the buffer. }
procedure Clear;
{ Sets the capacity to the used number of bytes, reducing memory to the
minimum required. Call this after calling Clear to completely release
all memory. }
procedure TrimExcess;
{ Returns the buffer as a byte array.
For performance reasons, this method does @bold(not) make a copy. Instead
it calls TrimExcess and returns the internal buffer. This means that any
changes you make to bytes in the returned buffer, will also affect this
buffer object. }
function ToBytes: TBytes;
{ Current capacity (number of reserved bytes) }
property Capacity: Integer read FCapacity;
{ Current size of the buffer in bytes }
property Size: Integer read FSize;
{ Provides direct access to the buffer. Note that this value can change as
you append to the buffer. So you should generally use ToBytes instead. }
property Buffer: TBytes read FBuffer;
end;
{ Returns the name of the machine.
Tech notes:
* On Windows, returns result from GetComputerName.
* On other platforms, returns result from Posix gethostname api }
function goGetMachineName: String;
{ Returns the ID of the current process. }
function goGetCurrentProcessId: UInt32;
{ Retrieves the number of bytes currently allocated by the process. }
function goGetAllocatedMemory: Int64;
{ Converts a byte array to a hex string.
Parameters:
ABytes: the array of bytes to convert.
Returns:
A string containing 2 hex digits for each converted byte.
@bold(Note): the returned string is in lowercase format. }
function goToHexString(const ABytes: TBytes): String;
{ Converts a hex string to a byte array.
Parameters:
AString: the string to convert.
Returns:
The converted byte array.
Raises:
EArgumentException if the string contains illegal characters.
@bold(Note): AString may contain both lowercase and uppercase hex digits,
and it may contain an even or odd number of characters. If the string
contains an odd number of characters, then it is prefixed with an
additional 0. }
function goParseHexString(const AString: String): TBytes;
{ Tries to convert a hex string to a byte array.
Parameters:
AString: the string to convert.
ABytes: is set to the converted byte array.
Returns:
True on success, False on failure.
@bold(Note): AString may contain both lowercase and uppercase hex digits,
and it may contain an even or odd number of characters. If the string
contains an odd number of characters, then it is prefixed with an
additional 0. }
function goTryParseHexString(const AString: String;
out ABytes: TBytes): Boolean;
{ Converts a digit to a byte.
Parameters:
AChar: the character to convert.
Returns:
The value of the hex digit.
Raises:
EArgumentException if the characeter is an illegal hex character.
@bold(Note): AChar may be both lowercase and uppercase. }
function goCharToHex(const AChar: Char): Byte;
{ Fast Unicode to UTF-8 conversion.
Parameters:
ASource: Unicode string.
Returns:
A byte array with the UTF-8 data.
@bold(Note): This function is optimized for speed and doesn't perform any
error checking. If ASource contains invalid characters, then the result will
be invalid as well. As a result, this version is 2-10 times faster than
TEncoding.UTF8.GetBytes. }
function goUtf16ToUtf8(const ASource: String): TBytes; overload;
{ Fast Unicode to UTF-8 conversion using a provided buffer.
Parameters:
ASource: Unicode string.
ASourceLength: the number of characters in ASource to use.
ABuffer: pointer to the buffer to store the UTF-8 data into. The buffer must
be at least large enough to store ((ASourceLength + 1) * 3) bytes.
Returns:
The number of UTF-8 bytes stored in the buffer.
@bold(Note): This function is optimized for speed and doesn't perform any
error checking. If ASource contains invalid characters, then the result will
be invalid as well. As a result, this version is 2-10 times faster than
TEncoding.UTF8.GetBytes. }
function goUtf16ToUtf8(const ASource: String; const ASourceLength: Integer;
const ABuffer: Pointer): Integer; overload;
{ Fast UTF-8 to Unicode conversion.
Parameters:
ASource: the UTF-8 data.
Returns:
The Unicode string.
@bold(Note): This function is optimized for speed and doesn't perform any
error checking. If ASource contains invalid bytes, then the result will be
invalid as well. As a result, this version is 2-35 times faster than
TEncoding.UTF8.GetString. }
function goUtf8ToUtf16(const ASource: TBytes): String; overload; inline;
{ Fast UTF-8 to Unicode conversion.
Parameters:
ASource: pointer to the UTF-8 data.
ASourceLength: the number of bytes in ASource to use.
Returns:
The Unicode string.
@bold(Note): This function is optimized for speed and doesn't perform any
error checking. If ASource contains invalid bytes, then the result will be
invalid as well. As a result, this version is 2-35 times faster than
TEncoding.UTF8.GetString. }
function goUtf8ToUtf16(const ASource: Pointer; const ASourceLength: Integer): String; overload;
{ Fast UTF-8 to Unicode conversion using a provided buffer.
Parameters:
ASource: pointer to the UTF-8 data.
ASourceLength: the number of bytes in ASource to use.
ABuffer: pointer to the buffer to store the UTF-16 characters into. The
buffer must be at least large enough to store (ASourceLength + 1)
WideChar's.
Returns:
The number of WideChar's (NOT bytes) stored in the buffer.
@bold(Note): This function is optimized for speed and doesn't perform any
error checking. If ASource contains invalid bytes, then the result will be
invalid as well. As a result, this version is 2-35 times faster than
TEncoding.UTF8.GetString. }
function goUtf8ToUtf16(const ASource: Pointer; const ASourceLength: Integer;
const ABuffer: Pointer): Integer; overload;
{ Calculates a Murmur hash (v2) of a block of data.
See https://sites.google.com/site/murmurhash/
Parameters:
AData: the data to calulate the hash for
ALen: the size of the data (in bytes).
Returns:
The hash value for the data.
This hash function is *much* faster than Delphi's built-in BobJenkinsHash and
is also better in avoiding hash collisions. }
function goMurmurHash2(const AData; ALen: Integer): Integer;
{ Reverses the bytes in (part of) a byte array.
Parameters:
ABytes: the byte array.
AIndex: the index to start reversing.
ALength: the number of bytes to reverse.
Raises:
EArgumentOutOfRangeException (AIndex + ALength) is out of range. }
procedure goReverseBytes(const ABytes: TBytes; const AIndex, ALength: Integer);
{ Converts a number of bytes to a string, using KB, MB, GB, TB suffixes as
appropriate }
function goByteCountToString(const AByteCount: Int64): String;
var
{ Format settings that always use a period ('.') as a decimal separator }
goUSFormatSettings: TFormatSettings;
implementation
uses
{$IF Defined(MSWINDOWS)}
Winapi.Windows,
Winapi.PSApi,
{$ELSEIF Defined(MACOS)}
Macapi.Mach,
{$ENDIF}
{$IF Defined(POSIX)}
Posix.Base,
Posix.UniStd,
{$ENDIF}
{$IF Defined(LINUX)}
Posix.SysTime,
{$ENDIF}
System.Math,
System.RTLConsts;
{$IFDEF MSWINDOWS}
function goGetMachineName: String;
var
ComputerName: array [0..MAX_COMPUTERNAME_LENGTH] of Char;
Size: DWORD;
begin
Size := MAX_COMPUTERNAME_LENGTH + 1;
if (GetComputerName(ComputerName, Size)) then
Result := ComputerName
else
Result := '';
end;
function goGetCurrentProcessId: UInt32;
begin
Result := GetCurrentProcessId;
end;
{$ELSE}
function goGetMachineName: String;
var
HostName: TBytes;
HostNameStr: MarshaledAString;
begin
SetLength(HostName, 513);
HostNameStr := @HostName[0];
if (gethostname(HostNameStr, 512) = 0) then
Result := String(HostNameStr)
else
Result := '';
end;
function goGetCurrentProcessId: UInt32;
begin
Result := getpid;
end;
{$ENDIF}
{$IF Defined(MSWINDOWS)}
function goGetAllocatedMemory: Int64;
var
Counters: TProcessMemoryCounters;
begin
{$HINTS OFF}
if (GetProcessMemoryInfo(GetCurrentProcess, @Counters, SizeOf(Counters))) then
Result := Counters.WorkingSetSize
else
Result := 0;
{$HINTS ON}
end;
{$ELSEIF Defined(MACOS)}
type
time_value_t = record
Seconds: Integer;
MicroSeconds: Integer;
end;
type
policy_t = Integer;
task_flavor_t = natural_t;
type
TTaskBasicInfo32 = record
SuspendCount: Integer;
VirtualSize: natural_t;
ResidentSize: natural_t;
UserTime: time_value_t;
SystemTime: time_value_t;
Policy: policy_t;
end;
const
TASK_BASIC_INFO_32 = 4;
TASK_BASIC_INFO_32_COUNT = SizeOf(TTaskBasicInfo32) div SizeOf(natural_t);
function task_info(task: mach_port_t; flavor: task_flavor_t; info: Pointer;
var count: mach_msg_type_number_t): kern_return_t; cdecl;
external libc name _PU + 'task_info';
type
TVMStatistics = record
FreeCount: natural_t;
ActiveCount: natural_t;
InactiveCount: natural_t;
WireCount: natural_t;
ZeroFillCount: natural_t;
Reactivations: natural_t;
Pageins: natural_t;
Pageouts: natural_t;
Faults: natural_t;
CowFaults: natural_t;
Lookups: natural_t;
Hits: natural_t;
PurgeableCount: natural_t;
Purges: natural_t;
SpeculativeCount: natural_t;
end;
const
HOST_VM_INFO = 2;
function host_statistics(host_priv: host_t; flavor: host_flavor_t;
host_info_out: host_info_t;
var host_info_outCnt: mach_msg_type_number_t): kern_return_t; cdecl;
external libc name _PU + 'host_statistics';
function goGetAllocatedMemory: Int64;
var
Err: kern_return_t;
Count: mach_msg_type_number_t;
Info: TTaskBasicInfo32;
begin
Count := TASK_BASIC_INFO_32_COUNT;
Err := task_info(mach_task_self, TASK_BASIC_INFO_32, @Info, Count);
if (Err = KERN_SUCCESS) then
Result := Info.ResidentSize
else
Result := 0;
end;
{$ELSEIF Defined(ANDROID)}
type
TMallinfo = record
arena: Integer;
ordblks: Integer;
smblks: Integer;
hblks: Integer;
hblkhd: Integer;
usmblks: Integer;
fsmblks: Integer;
uordblks: Integer;
fordblks: Integer;
keepcost: Integer;
end;
function mallinfo: TMallinfo; cdecl;
external libc name _PU + 'mallinfo';
function goGetAllocatedMemory: Int64;
var
Info: TMallinfo;
begin
Info := mallinfo;
Result := Info.uordblks;
end;
{$ELSEIF Defined(LINUX)}
const
RUSAGE_SELF = 1;
type
Trusage = record
ru_utime: timeval;
ru_stime: timeval;
ru_maxrss: Int64; // maximum resident set size
ru_ixrss: Int64; // integral shared memory size
ru_idrss: Int64; // integral unshared data size
ru_isrss: Int64; // integral unshared stack size
ru_minflt: Int64; // page reclaims (soft page faults)
ru_majflt: Int64; // page faults (hard page faults)
ru_nswap: Int64; // swaps
ru_inblock: Int64; // block input operations
ru_oublock: Int64; // block output operations
ru_msgsnd: Int64; // IPC messages sent
ru_msgrcv: Int64; // IPC messages received
ru_nsignals: Int64; // signals received
ru_nvcsw: Int64; // voluntary context switches
ru_nivcsw: Int64; // involuntary context switches
end;
function getrusage(who: Integer; out rusage: Trusage): Integer; cdecl;
external libc name _PU + 'getrusage';
function goGetAllocatedMemory: Int64;
var
Rusage: Trusage;
begin
if getrusage(RUSAGE_SELF, Rusage) = 0 then
Result := Rusage.ru_maxrss * 1024
else
Result := 0;
end;
{$ELSE}
function goGetAllocatedMemory: Int64;
begin
{ TODO : Implement for this OS }
Result := 0;
end;
{$ENDIF}
function goToHexString(const ABytes: TBytes): String;
const
HEX_CHARS: array [0..15] of Char = '0123456789abcdef';
var
I, J: Integer;
begin
if (ABytes = nil) then
Exit('');
SetLength(Result, Length(ABytes) * 2);
J := Low(Result);
for I := 0 to Length(ABytes) - 1 do
begin
Result[J] := HEX_CHARS[ABytes[I] shr 4];
Result[J + 1] := HEX_CHARS[ABytes[I] and $0F];
Inc(J, 2);
end;
end;
function goParseHexString(const AString: String): TBytes;
var
I: Integer;
S: String;
begin
if Odd(AString.Length) then
S := '0' + AString
else
S := AString;
SetLength(Result, S.Length shr 1);
for I := 0 to Length(Result) - 1 do
Result[I] := (goCharToHex(S.Chars[(I shl 1)]) shl 4)
or goCharToHex(S.Chars[(I shl 1) + 1]);
end;
function goTryParseHexString(const AString: String;
out ABytes: TBytes): Boolean;
begin
try
ABytes := goParseHexString(AString);
Result := True;
except
ABytes := nil;
Result := False;
end;
end;
function goCharToHex(const AChar: Char): Byte;
begin
case AChar of
'0'..'9': Result := Ord(AChar) - Ord('0');
'A'..'F': Result := Ord(AChar) - Ord('A') + 10;
'a'..'f': Result := Ord(AChar) - Ord('a') + 10;
else
raise EArgumentException.CreateRes(@sArgumentInvalid);
end;
end;
{$POINTERMATH ON}
{ Quick Unicode and UTF-8/UTF-16 recap.
Unicode codepoint range: U+0..U+10FFFF, except U+D800..U+DFFF (these are used
for surrogates).
Codepoints U+0..U+FFFF are stored in a single WideChar.
Codepoints U+1000..U+10FFFF are stores as two WideChar's (surrogate pair)
UTF-8 Encodes each Unicode codepoint in 1-4 bytes. The high bits of the first
byte tell how many bytes are used:
Range Scalar codepoint value 1st byte 2nd byte 3rd byte 4th byte
-------------------------------------------------------------------------------------
U+0..U+7F 00000000 0xxxxxxx 0xxxxxxx
U+80..U+7FF 00000yyy yyxxxxxx 110yyyyy 10xxxxxx
U+800..U+FFFF zzzzyyyy yyxxxxxx 1110zzzz 10yyyyyy 10xxxxxx
U+10000..U+10FFFF 000uuuuu zzzzyyyy yyxxxxxx 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx }
function goUtf16ToUtf8(const ASource: String): TBytes;
var
SrcLength, DstLength: Integer;
Dst: array [0..255] of Byte;
begin
SrcLength := Length(ASource);
if (SrcLength = 0) then
Exit(nil);
DstLength := (SrcLength + 1) * 3;
if (DstLength <= Length(Dst)) then
begin
DstLength := goUtf16ToUtf8(ASource, SrcLength, @Dst);
SetLength(Result, DstLength);
Move(Dst, Result[0], DstLength);
end
else
begin
SetLength(Result, DstLength);
SetLength(Result, goUtf16ToUtf8(ASource, SrcLength, Result));
end;
end;
function goUtf16ToUtf8(const ASource: String; const ASourceLength: Integer;
const ABuffer: Pointer): Integer;
var
SrcLength: Integer;
S: PWord;
D, DStart: PByte;
Codepoint: UInt32;
begin
SrcLength := ASourceLength;
S := Pointer(ASource);
D := ABuffer;
DStart := D;
{ Try to convert 2 wide characters at a time if possible. This speeds up the
process if those 2 characters are both ASCII characters (U+0..U+7F). }
while (SrcLength >= 2) do
begin
if ((PCardinal(S)^ and $FF80FF80) = 0) then
begin
{ Common case: 2 ASCII characters in a row.
00000000 0yyyyyyy 00000000 0xxxxxxx => 0yyyyyyy 0xxxxxxx }
D[0] := S[0]; // 00000000 0yyyyyyy => 0yyyyyyy
D[1] := S[1]; // 00000000 0xxxxxxx => 0xxxxxxx
Inc(S, 2);
Inc(D, 2);
Dec(SrcLength, 2);
end
else
begin
Codepoint := S^;
Inc(S);
Dec(SrcLength);
if (Codepoint < $80) then
begin
{ ASCI character (U+0..U+7F).
00000000 0xxxxxxx => 0xxxxxxx }
D^ := Codepoint;
Inc(D);
end
else if (Codepoint < $800) then
begin
{ 2-byte sequence (U+80..U+7FF)
00000yyy yyxxxxxx => 110yyyyy 10xxxxxx }
D^ := (Codepoint shr 6) or $C0; // 00000yyy yyxxxxxx => 110yyyyy
Inc(D);
D^ := (Codepoint and $3F) or $80; // 00000yyy yyxxxxxx => 10xxxxxx
Inc(D);
end
else if (Codepoint >= $D800) and (Codepoint <= $DBFF) then
begin
{ The codepoint is part of a UTF-16 surrogate pair:
S[0]: 110110yy yyyyyyyy ($D800-$DBFF, high-surrogate)
S[1]: 110111xx xxxxxxxx ($DC00-$DFFF, low-surrogate)
Where the UCS4 codepoint value is:
0000yyyy yyyyyyxx xxxxxxxx + $00010000 (U+10000..U+10FFFF)
This can be calculated using:
(((S[0] and $03FF) shl 10) or (S[1] and $03FF)) + $00010000
However it can be calculated faster using:
(S[0] shl 10) + S[1] - $035FDC00
because:
* S[0] shl 10: also shifts the leading 110110 to the left, making
the result $D800 shl 10 = $03600000 too large
* S[1] is $0000DC00 too large
* So we need to subract $0360DC00 (sum of the above)
* But we need to add $00010000
* So in total, we subtract $035FDC00 (difference of the above) }
Codepoint := (Codepoint shl 10) + S^ - $035FDC00;
Inc(S);
Dec(SrcLength);
{ The resulting codepoint is encoded as a 4-byte UTF-8 sequence:
000uuuuu zzzzyyyy yyxxxxxx => 11110uuu 10uuzzzz 10yyyyyy 10xxxxxx }
Assert(Codepoint > $FFFF);
D^ := (Codepoint shr 18) or $F0; // 000uuuuu zzzzyyyy yyxxxxxx => 11110uuu
Inc(D);
D^ := ((Codepoint shr 12) and $3F) or $80; // 000uuuuu zzzzyyyy yyxxxxxx => 10uuzzzz
Inc(D);
D^ := ((Codepoint shr 6) and $3F) or $80; // 000uuuuu zzzzyyyy yyxxxxxx => 10yyyyyy
Inc(D);
D^ := (Codepoint and $3F) or $80; // 000uuuuu zzzzyyyy yyxxxxxx => 10xxxxxx
Inc(D);
end
else
begin
{ 3-byte sequence (U+800..U+FFFF, excluding U+D800..U+DFFF).
zzzzyyyy yyxxxxxx => 1110zzzz 10yyyyyy 10xxxxxx }
D^ := (Codepoint shr 12) or $E0; // zzzzyyyy yyxxxxxx => 1110zzzz
Inc(D);
D^ := ((Codepoint shr 6) and $3F) or $80; // zzzzyyyy yyxxxxxx => 10yyyyyy
Inc(D);
D^ := (Codepoint and $3F) or $80; // zzzzyyyy yyxxxxxx => 10xxxxxx
Inc(D);
end;
end;
end;
{ We may have 1 wide character left to encode.
Use the same process as above. }
if (SrcLength <> 0) then
begin
Codepoint := S^;
Inc(S);
if (Codepoint < $80) then
begin
D^ := Codepoint;
Inc(D);
end
else if (Codepoint < $800) then
begin
D^ := (Codepoint shr 6) or $C0;
Inc(D);
D^ := (Codepoint and $3F) or $80;
Inc(D);
end
else if (Codepoint >= $D800) and (Codepoint <= $DBFF) then
begin
Codepoint := (Codepoint shl 10) + S^ - $35FDC00;
Assert(Codepoint > $FFFF);
D^ := (Codepoint shr 18) or $F0;
Inc(D);
D^ := ((Codepoint shr 12) and $3F) or $80;
Inc(D);
D^ := ((Codepoint shr 6) and $3F) or $80;
Inc(D);
D^ := (Codepoint and $3F) or $80;
Inc(D);
end
else
begin
D^ := (Codepoint shr 12) or $E0;
Inc(D);
D^ := ((Codepoint shr 6) and $3F) or $80;
Inc(D);
D^ := (Codepoint and $3F) or $80;
Inc(D);
end;
end;
Result := D - DStart;
end;
function goUtf8ToUtf16(const ASource: TBytes): String;
begin
Result := goUtf8ToUtf16(ASource, Length(ASource));
end;
function goUtf8ToUtf16(const ASource: Pointer; const ASourceLength: Integer): String;
var
Dst: array [0..127] of Char;
DstLength: Integer;
begin
if (ASourceLength = 0) then
Exit('');
if (ASourceLength < Length(Dst)) then
begin
DstLength := goUtf8ToUtf16(ASource, ASourceLength, @Dst);
SetString(Result, PChar(@Dst), DstLength);
end
else
begin
SetLength(Result, ASourceLength + 1);
SetLength(Result, goUtf8ToUtf16(ASource, ASourceLength, Pointer(Result)));
end;
end;
function goUtf8ToUtf16(const ASource: Pointer; const ASourceLength: Integer;
const ABuffer: Pointer): Integer;
var
SrcLength: Integer;
S: PByte;
D, DStart: PWord;
Codepoint: UInt32;
begin
SrcLength := ASourceLength;
S := ASource;
D := ABuffer;
DStart := D;
{ Try to convert 4 bytes at a time. This speeds up the process if those 4
bytes are all ASCII characters (U+0..U+7F) }
while (SrcLength >= 4) do
begin
if ((PCardinal(S)^ and $80808080) = 0) then
begin
{ Common case: 4 ASCII characters in a row.
0zzzzzzz 0yyyyyyy 0xxxxxxx 0wwwwwww => 00000000 0zzzzzzz 00000000 0yyyyyyy 00000000 0xxxxxxx 00000000 0wwwwwww }
D[0] := S[0]; // 0zzzzzzz => 00000000 0zzzzzzz
D[1] := S[1]; // 0yyyyyyy => 00000000 0yyyyyyy
D[2] := S[2]; // 0xxxxxxx => 00000000 0xxxxxxx
D[3] := S[3]; // 0wwwwwww => 00000000 0wwwwwww
Inc(S, 4);
Inc(D, 4);
Dec(SrcLength, 4);
end
else
begin
Codepoint := S^;
Inc(S);
if (Codepoint < $80) then
begin
{ ASCI character (U+0..U+7F).
0xxxxxxx => 00000000 0xxxxxxx }
D^ := Codepoint;
Dec(SrcLength);
end
else
if ((Codepoint shr 5) = $06) then
begin
{ 2-byte sequence (U+80..U+7FF)
110yyyyy 10xxxxxx => 00000yyy yyxxxxxx }
D^ := ((Codepoint shl 6) and $7FF) // 110yyyyy => 00000yyy yy000000
+ (S^ and $3F); // 10xxxxxx => 00000000 00xxxxxx
Inc(S);
Dec(SrcLength, 2);
end
else
begin
if ((Codepoint shr 4) = $0E) then
begin
{ 3-byte sequence (U+800..U+FFFF, excluding U+D800..U+DFFF).
1110zzzz 10yyyyyy 10xxxxxx => zzzzyyyy yyxxxxxx }
Codepoint :=
((Codepoint shl 12) and $FFFF) // 1110zzzz => zzzz0000 00000000
+ ((S^ shl 6) and $FFF); // 10yyyyyy => 0000yyyy yy000000
Inc(S);
Inc(Codepoint, S^ and $3F); // 10xxxxxx => 00000000 00xxxxxx
Inc(S);
Dec(SrcLength, 3);
Assert(CodePoint <= $FFFF);
D^ := Codepoint;
end
else
begin
Assert((Codepoint shr 3) = $1E);
{ 4-byte sequence (U+10000..U+10FFFF).
11110uuu 10uuzzzz 10yyyyyy 10xxxxxx => 000uuuuu zzzzyyyy yyxxxxxx }
Codepoint :=
((Codepoint shl 18) and $1FFFFF) // 11110uuu => 000uuu00 00000000 00000000
+ ((S^ shl 12) and $3FFFF); // 10uuzzzz => 000000uu zzzz0000 00000000
Inc(S);
Inc(Codepoint, (S^ shl 6) and $FFF);// 10yyyyyy => 00000000 0000yyyy yy000000
Inc(S);
Inc(Codepoint, S^ and $3F); // 10xxxxxx => 00000000 00000000 00xxxxxx
Inc(S);
Dec(SrcLength, 4);
{ The value $00010000 must be subtracted from this codepoint, and the
result must be converted to a UTF-16 surrogate pair:
D[0]: 110110yy yyyyyyyy ($D800-$DBFF, high-surrogate)
D[1]: 110111xx xxxxxxxx ($DC00-$DFFF, low-surrogate) }
Assert(Codepoint > $FFFF);
Dec(Codepoint, $00010000);
D^ := $D800 + (Codepoint shr 10);
Inc(D);
D^ := $DC00 + (Codepoint and $3FF);
end;
end;
Inc(D);
end;
end;
{ We may 1-3 bytes character left to encode.
Use the same process as above. }
while (SrcLength > 0) do
begin
Codepoint := S^;
Inc(S);
if (Codepoint < $80) then
begin
D^ := Codepoint;
Dec(SrcLength);
end
else
if ((Codepoint shr 5) = $06) then
begin
D^ := ((Codepoint shl 6) and $7FF) + (S^ and $3F);
Inc(S);
Dec(SrcLength, 2);
end
else
begin
if ((Codepoint shr 4) = $0E) then
begin
Codepoint := ((Codepoint shl 12) and $FFFF) + ((S^ shl 6) and $FFF);
Inc(S);
Inc(Codepoint, S^ and $3F);
Inc(S);
Dec(SrcLength, 3);
Assert(CodePoint <= $FFFF);
D^ := Codepoint;
end
else
begin
Assert((Codepoint shr 3) = $1E);
Codepoint := ((Codepoint shl 18) and $1FFFFF) + ((S^ shl 12) and $3FFFF);
Inc(S);
Inc(Codepoint, (S^ shl 6) and $FFF);
Inc(S);
Inc(Codepoint, S^ and $3F);
Inc(S);
Dec(SrcLength, 4);
Assert(CodePoint > $FFFF);
D^ := $D7C0 + (Codepoint shr 10);
Inc(D);
D^ := $DC00 + (Codepoint and $3FF);
end;
end;
Inc(D);
end;
Result := D - DStart;
end;
{$POINTERMATH OFF}
{$IFOPT Q+}
{$DEFINE OVERFLOW_CHECKS_WAS_ON}
{$ENDIF}
{$Q-}
function goMurmurHash2(const AData; ALen: Integer): Integer;
{ https://sites.google.com/site/murmurhash/MurmurHash2.cpp?attredirects=0 }
const
M = $5bd1e995;
R = 24;
var
H, K: Cardinal;
Data: PByte;
label
label1, label2, label3, finish;
begin
H := ALen;
Data := @AData;
while (ALen >= 4) do
begin
K := PCardinal(Data)^;
K := K * M;
K := K xor (K shr R);
K := K * M;
H := H * M;
H := H xor K;
Inc(Data, 4);
Dec(ALen, 4);
end;
case ALen of
3: goto label3;
2: goto label2;
1: goto label1;
else
goto finish;
end;
label3:
H := H xor (Data[2] shl 16);
label2:
H := H xor (Data[1] shl 8);
label1:
H := H xor Data[0];
H := H * M;
finish: