-
Notifications
You must be signed in to change notification settings - Fork 7
/
unicodehelper.pas
630 lines (559 loc) · 16.8 KB
/
unicodehelper.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
{
BSD 2-Clause License
Copyright (c) 2017, Daniel Mecklenburg Jr. All rights reserved.
Redistribution and use in source and binary forms, with or without modification,
are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
}
unit UnicodeHelper;
{$mode objfpc}{$H+}
{$modeswitch typehelpers}
interface
uses
SysUtils;
type
TUnicodeStringArray = array of UnicodeString;
TWords = array of Word;
TWideChars = array of WideChar;
TWideCharHelper = type helper for WideChar
public
function getUTF8Length : integer; // from 1 to 3 (4=not yet).
function getUTF16Length : integer; // always 2
function getCPLength : integer; // always 1
function toCharCode : integer;
function fromCharCode(chr : integer) : WideChar;
end;
TUnicodeStringHelper = type helper for UnicodeString
public
function length : integer; overload;
function substring(index : Integer): unicodestring; overload;
function substring(index : Integer; len : Integer): unicodestring; overload;
function charCodeAt(index : integer) : integer;
function charAt(index : integer) : WideChar;
function split(const Separators: array of WideChar): TUnicodeStringArray; overload;
// other String Helper type functions can be added as required.
function toWideCharArray : TWideChars;
function toWordArray : TWords;
function toUTF8Bytes : TBytes;
function toUTF16Bytes : TBytes;
function toCPBytes : TBytes;
function toEncodedCPBytes(table : PWord) : TBytes;
function getUTF8BytesLength : integer; // varies
function getUTF16BytesLength : integer; // length * 2
function getCPBytesLength : integer; // length
function fromUTF8Bytes(bytes : TBytes) : UnicodeString;
function fromUTF16Bytes(bytes : TBytes) : UnicodeString;
function fromCPBytes(bytes : TBytes) : UnicodeString;
function hasUTF8BrokenBytes(bytes : TBytes) : boolean;
function hasUTF16BrokenBytes(bytes : TBytes) : boolean;
function hasCPBrokenBytes(bytes : TBytes) : boolean; // always false
function getUTF8BrokenBytes(bytes : TBytes) : TBytes;
function getUTF16BrokenBytes(bytes : TBytes) : TBytes;
function getCPBrokenBytes(bytes : TBytes) : TBytes; // always returns []
procedure mapCP(map : TWideChars);
end;
implementation
{ TWideCHarHelper }
function TWideCharHelper.getUTF8Length : integer;
begin
if integer(self) < $80 then result := 1
else if integer(self) < $800 then result := 2
else if integer(self) < $10000 then result := 3
else result := 4;
end;
function TWideCharHelper.getUTF16Length : integer; inline;
begin
result := 2;
end;
function TWideCharHelper.getCPLength : integer; inline;
begin
result := 1;
end;
function TWideCharHelper.toCharCode : integer; inline;
begin
result := integer(self);
end;
function TWideCharHelper.fromCharCode(chr : integer) : WideChar; inline;
begin
result := WideChar(chr);
end;
{ TUnicodeStringHelper }
{
length : length of the UnicodeString in WideChars.
}
function TUnicodeStringHelper.length : integer; inline;
begin
result := system.length(self);
end;
function TUnicodeStringHelper.substring(index : Integer): unicodestring;
var
strlen, len : integer;
begin
strlen := self.length;
if (index < 0) or (index >= strlen) then
result := ''
else
begin
len := strlen - index;
setlength(result, len);
move(self[1 + index], result[1], len * sizeof(WideChar));
end;
end;
function TUnicodeStringHelper.substring(index : Integer; len : Integer): unicodestring;
var
strlen : integer;
begin
strlen := self.length;
if (index < 0) or (index >= strlen) or (len <= 0) then
result := ''
else
begin
if index + len > strlen then
len := strlen - index;
setlength(result, len);
move(self[1 + index], result[1], len * sizeof(WideChar));
end;
end;
function TUnicodeStringHelper.charCodeAt(index : integer) : integer; inline;
begin
if (index < 0) or (index >= self.length) then
result := 0
else
result := self[index + 1].toCharCode;
end;
function TUnicodeStringHelper.charAt(index : integer) : WideChar; inline;
begin
if (index < 0) or (index >= self.length) then
result := WideChar(0)
else
result := self[index + 1];
end;
Function TUnicodeStringHelper.split(const Separators: array of WideChar): TUnicodeStringArray;
var
i, j, lastpos : integer;
ch : widechar;
x : UnicodeString;
begin
x := self;
setlength(result, 0);
lastpos := 0;
for i := 0 to self.length - 1 do
begin
ch := self.charAt(i);
for j := 0 to system.length(Separators) - 1 do
begin
if ch = Separators[j] then
begin
setlength(result, system.length(result) + 1);
result[system.length(result) - 1] := self.substring(lastpos, i - lastpos);
lastpos := i + 1;
break;
end;
end;
end;
setlength(result, system.length(result) + 1);
result[system.length(result) - 1] := self.substring(lastpos);
end;
{
Function Split(const Separators: array of Char; ACount: Integer): TStringArray; overload;
Function Split(const Separators: array of Char; Options: TStringSplitOptions): TStringArray; overload;
Function Split(const Separators: array of Char; ACount: Integer; Options: TStringSplitOptions): TStringArray; overload;
Function Split(const Separators: array of string): TStringArray; overload;
Function Split(const Separators: array of string; ACount: Integer): TStringArray; overload;
Function Split(const Separators: array of string; Options: TStringSplitOptions): TStringArray; overload;
Function Split(const Separators: array of string; ACount: Integer; Options: TStringSplitOptions): TStringArray; overload;
Function Split(const Separators: array of Char; AQuote: Char): TStringArray; overload;
Function Split(const Separators: array of Char; AQuoteStart, AQuoteEnd: Char): TStringArray; overload;
Function Split(const Separators: array of Char; AQuoteStart, AQuoteEnd: Char; Options: TStringSplitOptions): TStringArray; overload;
Function Split(const Separators: array of Char; AQuoteStart, AQuoteEnd: Char; ACount: Integer): TStringArray; overload;
Function Split(const Separators: array of Char; AQuoteStart, AQuoteEnd: Char; ACount: Integer; Options: TStringSplitOptions): TStringArray; overload;
Function Split(const Separators: array of string; AQuote: Char): TStringArray; overload;
Function Split(const Separators: array of string; AQuoteStart, AQuoteEnd: Char): TStringArray; overload;
Function Split(const Separators: array of string; AQuoteStart, AQuoteEnd: Char; Options: TStringSplitOptions): TStringArray; overload;
Function Split(const Separators: array of string; AQuoteStart, AQuoteEnd: Char; ACount: Integer): TStringArray; overload;
Function Split(const Separators: array of string; AQuoteStart, AQuoteEnd: Char; ACount: Integer; Options: TStringSplitOptions): TStringArray; overload;
}
{
toUTF8Bytes : returns array of bytes encoded in UTF8.
}
function TUnicodeStringHelper.toWideCharArray : TWideChars;
begin
setlength(result, self.length);
move(self[1], result[0], sizeof(WideChar));
end;
function TUnicodeStringHelper.toWordArray : TWords;
var
len : longint;
begin
len := self.length;
setlength(result, len);
move(self[1], result[0], sizeof(WideChar) * len);
end;
function TUnicodeStringHelper.toUTF8Bytes : TBytes;
var
cv, i, len, cl : integer;
p : pbyte;
cw : WideChar;
begin
len := self.getUTF8BytesLength;
setlength(Result, len);
p := @Result[0];
for i := 1 to self.length do
begin
cw := self[i];
cv := cw.toCharCode;
cl := cw.getUTF8Length;
case cl of
1:
begin
p^ := cv;
end;
2:
begin
p^ := %11000000 or ((cv >> 6) and %00011111);
p += 1;
p^ := %10000000 or (cv and %00111111);
end;
3:
begin
p^ := %11100000 or ((cv >> 12) and %00001111);
p += 1;
p^ := %10000000 or ((cv >> 6) and %00111111);
p += 1;
p^ := %10000000 or (cv and %00111111);
end;
4: raise exception.create('Characters $10000+ unsupported');
end;
p += 1;
end;
end;
{
toUTF16Bytes : returns array of bytes encoded in UTF16.
}
function TUnicodeStringHelper.toUTF16Bytes : TBytes;
var
len : integer;
begin
len := self.getUTF16BytesLength;
setlength(Result, len);
move(self[1], Result[0], len);
end;
{
toCPBytes : returns array of bytes. if character is beyond the 255 range, it
is converted to NULL.
}
function TUnicodeStringHelper.toCPBytes : TBytes;
var
len, i, cv : integer;
begin
len := self.length;
setlength(Result, len);
for i := 1 to len do
begin
cv := self[i].toCharCode;
if cv > 255 then
cv := 0;
Result[i - 1] := cv;
end;
end;
{
toEncodedCPBytes : convert unicodestring to 8 bit ascii using mapping table
}
function TUnicodeStringHelper.toEncodedCPBytes(table : PWord) : TBytes;
var
len, i, cv : integer;
ascii, j : integer;
begin
len := self.length;
setlength(Result, len);
for i := 1 to len do
begin
cv := self[i].toCharCode;
ascii := 0;
// skip control codes
for j := 32 to 255 do
begin
if cv = table[j] then
begin
ascii := j;
break;
end;
end;
Result[i - 1] := ascii;
end;
end;
{
getUTF8BytesLength : returns number of bytes required to encode as UTF8.
}
function TUnicodeStringHelper.getUTF8BytesLength : integer;
var
i : integer;
begin
result := 0;
for i := 1 to system.length(self) do
result += self[i].getUTF8Length;
end;
{
getUTF16BytesLength : returns number of bytes required to encode as UTF16.
}
function TUnicodeStringHelper.getUTF16BytesLength : integer; inline;
begin
result := self.length << 1;
end;
{
getCPBytesLength : returns number of bytes required to encode as codepage.
Does not consider any characters beyond the 255 charcode value.
}
function TUnicodeStringHelper.getCPBytesLength : integer; inline;
begin
result := self.length;
end;
{
fromUTF8Bytes : returns unicodestring of UTF8 in bytes. ignores broken bytes
of partial codepoints on end. use hasUTF8BrokenBytes / getUTF8BrokenBytes to
detect / retrieve the broken bytes to pump into next chunk from stream.
}
function TUnicodeStringHelper.fromUTF8Bytes(bytes : TBytes) : UnicodeString;
var
len, pos : integer;
val : UInt32;
b : byte;
begin
len := system.length(bytes);
result := '';
pos := 0;
while pos < len do
begin
b := bytes[pos];
if (b and %11111000) = %11110000 then
begin
// 4 bytes
raise exception.create('Characters $10000+ unsupported');
end
else if (b and %11110000) = %11100000 then
begin
// 3 bytes
if pos + 3 <= len then
begin
val := (bytes[pos + 2] and $3F)
or ((bytes[pos + 1] and $3F) << 6)
or ((b and $0F) << 12);
result += WideChar(val);
end;
//else broken
pos += 3;
end
else if (b and %11100000) = %11000000 then
begin
// 2 bytes
if pos + 2 <= len then
begin
val := (bytes[pos + 1] and $3F)
or ((b and $1F) << 6);
result += WideChar(val);
end;
//else broken
pos += 2;
end
else if (b and %10000000) = %00000000 then
begin
// 1 byte
result += WideChar(b);
pos += 1;
end;
end;
end;
{
fromUTF16Bytes : returns unicodestring of UTF16 in bytes. ignores broken bytes
of partial codepoints on end. use hasUTF16BrokenBytes / getUTF16BrokenBytes
to detect / retrieve the broken bytes to pump into next chunk from stream.
}
function TUnicodeStringHelper.fromUTF16Bytes(bytes : TBytes) : UnicodeString;
var
len, pos : integer;
begin
len := system.length(bytes);
result := '';
pos := 0;
while pos < len do
begin
if pos + 1 < len then
result += widechar(bytes[pos] + (bytes[pos + 1] << 8)); // little endian
pos += 2;
end;
end;
{
fromCPBytes : returns unicodestring of ascii in bytes.
}
function TUnicodeStringHelper.fromCPBytes(bytes : TBytes) : UnicodeString;
var
len, i : integer;
begin
len := system.length(bytes);
result := '';
for i := 0 to len - 1 do
result += WideChar(bytes[i]);
end;
{
getUTF8BrokenBytes : returns left overs of broken codepoints in byte array.
}
function TUnicodeStringHelper.getUTF8BrokenBytes(bytes : TBytes) : TBytes;
var
len, pos : integer;
b : byte;
begin
len := system.length(bytes);
pos := 0;
while pos < len do
begin
b := bytes[pos];
if (b and %11111000) = %11110000 then
begin
// 4 bytes
raise exception.create('Characters $10000+ unsupported');
end
else if (b and %11110000) = %11100000 then
begin
// 3 bytes
if pos + 3 > len then
begin
setlength(result, len - pos);
move(bytes[pos], result[0], len-pos);
exit;
end;
pos += 3;
end
else if (b and %11100000) = %11000000 then
begin
// 2 bytes
if pos + 2 > len then
begin
setlength(result, len - pos);
move(bytes[pos], result[0], len-pos);
exit;
end;
pos += 2;
end
else if (b and %10000000) = %00000000 then
begin
pos += 1;
end;
end;
setlength(result, 0);
end;
{
getUTF16BrokenBytes : returns left overs of broken codepoints in byte array.
}
function TUnicodeStringHelper.getUTF16BrokenBytes(bytes : TBytes) : TBytes;
begin
if self.HasUTF16BrokenBytes(bytes) then
begin
setlength(Result, 1);
Result[0] := bytes[system.length(bytes) - 1];
end
else
setlength(Result, 0);
end;
{
getCPBrokenBytes : always returns empty byte array.
}
function TUnicodeStringHelper.getCPBrokenBytes(bytes : TBytes) : TBytes; inline;
begin
setlength(Result, 0);
end;
{
hasUTF8BrokenBytes : returns true if there is a broken codepoint at the end
of the byte array.
}
function TUnicodeStringHelper.hasUTF8BrokenBytes(bytes : TBytes) : boolean;
var
len, pos : integer;
b : byte;
begin
len := system.length(bytes);
pos := 0;
while pos < len do
begin
b := bytes[pos];
if (b and %11111000) = %11110000 then
begin
// 4 bytes
raise exception.create('Characters $10000+ unsupported');
end
else if (b and %11110000) = %11100000 then
begin
// 3 bytes
if pos + 3 >= len then
exit(true);
pos += 3;
end
else if (b and %11100000) = %11000000 then
begin
// 2 bytes
if pos + 2 >= len then
exit(true);
pos += 2;
end
else if (b and %10000000) = %00000000 then
begin
pos += 1;
end;
end;
result := false;
end;
{
hasUTF16BrokenBytes : returns true if there is a broken codepoint at the end
of the byte array.
}
function TUnicodeStringHelper.hasUTF16BrokenBytes(bytes : TBytes) : boolean; inline;
begin
result := ((system.length(bytes) and $1) <> 0);
end;
{
hasCPBrokenBytes : always returns false.
}
function TUnicodeStringHelper.hasCPBrokenBytes(bytes : TBytes) : boolean; inline;
begin
result := false;
end;
{
mapCP : will convert a codepaged unicodestring to true unicode using an
array [0..255] of WideChars. if a character is outside the 0-255 range, it
will be mapped to null.
}
procedure TUnicodeStringHelper.mapCP(map : TWideChars);
var
len, i : integer;
pwc : PWideChar;
cpchr : integer;
begin
if system.length(map) <> 256 then
raise exception.create('Invalid mapping table length. Needs 256 characters.');
len := self.length;
pwc := getmemory(len * sizeof(WideChar));
move(self[1], pwc, len * sizeof(WideChar));
self := '';
for i := 0 to len - 1 do
begin
cpchr := pwc[i].toCharCode;
if cpchr > 255 then
cpchr := 0; // set to null if out of range.
self += map[cpchr];
end;
freememory(pwc);
end;
end.