-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdutf16.pas
353 lines (330 loc) · 10.1 KB
/
dutf16.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
// RFC 2781 https://tools.ietf.org/html/rfc2781
{$MODE FPC}
{$MODESWITCH DEFAULTPARAMETERS}
{$MODESWITCH OUT}
{$MODESWITCH RESULT}
unit dutf16;
interface
const
//
// UTF16_IS_SYSTEM_ENDIAN_BIG
//
// Returns True whenever system is big endian.
//
// Can be used as a BigEndian parameter if encoding of utf-16 strings
// your program deals with is in sync with system endian.
//
UTF16_IS_SYSTEM_ENDIAN_BIG = {$IFDEF ENDIAN_BIG}True{$ELSE}False{$ENDIF};
//
// DecodeUTF16Char
// DecodeUTF16LEChar
// DecodeUTF16BEChar
//
// Decodes 16-bit value sequence and returns Unicode character (code
// point).
//
// DecodeUTF16Char uses current system endian.
//
// DecodeUTF16CharIgnore
//
// Works like DecodeUTF16Char, but silently ignores all invalid data.
//
// DecodeUTF16CharReplace
//
// Works like DecodeUTF16Char, but instead of failing, returns the
// Replacement character.
//
// Parameters:
//
// Cursor: pointer to utf-16 character
// CursorEnd: end of the string
// BigEndian: True - UTF-16BE, False - UTF-16LE, system endian by default
// Replacement: the replacement character in DecodeUTF16CharReplace variant
//
// Returns:
//
// Result: True if character has been succesfully decoded
// U: decoded Unicode character (code point)
// WNext: pointer to next character
//
function DecodeUTF16Char(var Cursor: PWideChar; CursorEnd: PWideChar; out U: Cardinal; BigEndian: Boolean = UTF16_IS_SYSTEM_ENDIAN_BIG): Boolean; inline;
function DecodeUTF16LEChar(var Cursor: PWideChar; CursorEnd: PWideChar; out U: Cardinal): Boolean;
function DecodeUTF16BEChar(var Cursor: PWideChar; CursorEnd: PWideChar; out U: Cardinal): Boolean;
function DecodeUTF16CharIgnore(var Cursor: PWideChar; CursorEnd: PWideChar; out U: Cardinal; BigEndian: Boolean = UTF16_IS_SYSTEM_ENDIAN_BIG): Boolean; inline;
function DecodeUTF16CharReplace(var Cursor: PWideChar; CursorEnd: PWideChar; out U: Cardinal; Replacement: Cardinal; BigEndian: Boolean = UTF16_IS_SYSTEM_ENDIAN_BIG): Boolean; inline;
//
// SkipUTF16BOM
//
// Skips "Byte order mark" (BOM) and detects endian.
//
// Parameters:
//
// WStr: beginning of utf-16 string
// WEnd: end of utf-16 string
//
// Returns:
//
// Result=UTF16_NOBOM: BOM was not found, WNext=WStr
// RFC 2781 4.3: "If the first two octets of the text is not 0xFE
// followed by 0xFF, and is not 0xFF followed by 0xFE, then the text
// SHOULD be interpreted as being big-endian."
// Result=UTF16_LE: BOM has been found and text is UTF16-LE
// Result=UTF16_BE: BOM has been found and text is UTF16-BE
//
type
TUtf16BOM = (UTF16_NOBOM, UTF16_LE, UTF16_BE);
function SkipUTF16BOM(var WStr: PWideChar; WEnd: PWideChar): TUtf16BOM;
//
// GetUTF16CharSize
//
// Returns number of bytes required for UTF-16 encoding of specified
// character.
//
// Parameters:
//
// U: Unicode character (code point)
//
// Returns:
//
// Result: 2 or 4 - number of bytes
// 0 - the character cannot be encoded
//
function GetUTF16CharSize(U: Cardinal): SizeInt; inline;
//
// ValidateUTF16
//
// Checks if UTF-16 string is Valid.
//
function ValidateUTF16(Cursor, CursorEnd: PWideChar; out Err: PWideChar; BigEndian: Boolean = UTF16_IS_SYSTEM_ENDIAN_BIG): Boolean;
function ValidateUTF16(Cursor, CursorEnd: PWideChar; BigEndian: Boolean = UTF16_IS_SYSTEM_ENDIAN_BIG): Boolean;
//
// SwapEndianUTF16
//
// Swaps endian of specified UTF-16 string in place. Does not do any
// validation.
//
procedure SwapEndianUTF16(Cursor, CursorEnd: PWideChar); inline;
//
// EncodeUTF16Char
// EncodeUTF16LEChar
// EncodeUTF16BEChar
//
// Encodes UTF-16 character.
//
function EncodeUTF16Char(U: Cardinal; var Cursor: PWideChar; CursorEnd: PWideChar; BigEndian: Boolean = UTF16_IS_SYSTEM_ENDIAN_BIG): Boolean; inline;
function EncodeUTF16LEChar(U: Cardinal; var Cursor: PWideChar; CursorEnd: PWideChar): Boolean;
function EncodeUTF16BEChar(U: Cardinal; var Cursor: PWideChar; CursorEnd: PWideChar): Boolean;
implementation
function DecodeUTF16Char(var Cursor: PWideChar; CursorEnd: PWideChar; out U: Cardinal; BigEndian: Boolean = UTF16_IS_SYSTEM_ENDIAN_BIG): Boolean; inline;
begin
if BigEndian then begin
Result := DecodeUTF16BEChar(PWideChar(Cursor), PWideChar(CursorEnd), U);
end else
Result := DecodeUTF16LEChar(PWideChar(Cursor), PWideChar(CursorEnd), U);
end;
function DecodeUTF16LEChar(var Cursor: PWideChar; CursorEnd: PWideChar; out U: Cardinal): Boolean;
var
W: Word;
S: PWideChar;
begin
S := Cursor;
if S >= CursorEnd then begin
U := 0;
Exit(False);
end;
W := {$IFDEF ENDIAN_BIG}SwapEndian{$ENDIF}(Word(Ord(S[0])));
if (W < $D800) or (W > $DFFF) then begin
U := W;
Cursor := S + 1;
Exit(True);
end else begin
if W > $DBFF then begin
U := W;
Exit(False);
end else begin
if S + 1 >= CursorEnd then
Exit(False);
U := (W and %1111111111) shl 10 + $10000;
W := {$IFDEF ENDIAN_BIG}SwapEndian{$ENDIF}(Word(Ord(S[1])));
if (W < $DC00) or (W > $DFFF) then
Exit(False);
U := U or (W and %1111111111);
Cursor := S + 2;
Exit(True);
end;
end;
end;
function DecodeUTF16BEChar(var Cursor: PWideChar; CursorEnd: PWideChar; out U: Cardinal): Boolean;
var
W: Word;
S: PWideChar;
begin
S := Cursor;
if S >= CursorEnd then begin
U := 0;
Exit(False);
end;
W := {$IFDEF ENDIAN_LITTLE}SwapEndian{$ENDIF}(Word(Ord(S[0])));
if (W < $D800) or (W > $DFFF) then begin
U := W;
Cursor := S + 1;
Exit(True);
end else begin
if W > $DBFF then begin
U := W;
Exit(False);
end else begin
if S + 1 >= CursorEnd then
Exit(False);
U := (W and %1111111111) shl 10 + $10000;
W := {$IFDEF ENDIAN_LITTLE}SwapEndian{$ENDIF}(Word(Ord(S[1])));
if (W < $DC00) or (W > $DFFF) then
Exit(False);
U := U or (W and %1111111111);
Cursor := S + 2;
Exit(True);
end;
end;
end;
function DecodeUTF16CharIgnore(var Cursor: PWideChar; CursorEnd: PWideChar; out U: Cardinal; BigEndian: Boolean = UTF16_IS_SYSTEM_ENDIAN_BIG): Boolean; inline;
begin
if BigEndian then begin
while Cursor < CursorEnd do begin
if DecodeUTF16BEChar(PWideChar(Cursor), PWideChar(CursorEnd), U) then
Exit(True);
Inc(Cursor);
end;
end else begin
while Cursor < CursorEnd do begin
if DecodeUTF16LEChar(PWideChar(Cursor), PWideChar(CursorEnd), U) then
Exit(True);
Inc(Cursor);
end;
end;
Exit(False);
end;
function DecodeUTF16CharReplace(var Cursor: PWideChar; CursorEnd: PWideChar; out U: Cardinal; Replacement: Cardinal; BigEndian: Boolean = UTF16_IS_SYSTEM_ENDIAN_BIG): Boolean; inline;
begin
if BigEndian then begin
if Cursor < CursorEnd then begin
if DecodeUTF16BEChar(PWideChar(Cursor), PWideChar(CursorEnd), U) then begin
Exit(True);
end else begin
U := Replacement;
Inc(Cursor);
Exit(True);
end;
end;
end else begin
if Cursor < CursorEnd then begin
if DecodeUTF16LEChar(PWideChar(Cursor), PWideChar(CursorEnd), U) then begin
Exit(True);
end else begin
U := Replacement;
Inc(Cursor);
Exit(True);
end;
end;
end;
Exit(False);
end;
function SkipUTF16BOM(var WStr: PWideChar; WEnd: PWideChar): TUtf16BOM;
begin
if WStr >= WEnd then begin
Exit(UTF16_NOBOM);
end;
case Ord(WStr^) of
($FE shl 8) + $FF: begin
Inc(WStr);
Exit(UTF16_LE);
end;
($FF shl 8) + $FE: begin
Inc(WStr);
Exit(UTF16_BE);
end;
else
Exit(UTF16_NOBOM);
end;
end;
function GetUTF16CharSize(U: Cardinal): SizeInt;
begin
if U < $10000 then begin
Exit(2);
end else if U > $10FFFF then begin
Exit(0);
end else
Exit(4);
end;
function ValidateUTF16(Cursor, CursorEnd: PWideChar; out Err: PWideChar; BigEndian: Boolean = UTF16_IS_SYSTEM_ENDIAN_BIG): Boolean;
var
U: Cardinal;
begin
while DecodeUTF16Char(Cursor, CursorEnd, U, BigEndian) do
;
Err := Cursor;
Result := Cursor >= CursorEnd;
end;
function ValidateUTF16(Cursor, CursorEnd: PWideChar; BigEndian: Boolean = UTF16_IS_SYSTEM_ENDIAN_BIG): Boolean;
var
U: Cardinal;
begin
while DecodeUTF16Char(Cursor, CursorEnd, U, BigEndian) do
;
Result := Cursor >= CursorEnd;
end;
procedure SwapEndianUTF16(Cursor, CursorEnd: PWideChar); inline;
var
T: Word;
begin
while Cursor < CursorEnd do begin
Word(Cursor^) := SwapEndian(Word(Cursor^));
Inc(Cursor);
end;
end;
function EncodeUTF16Char(U: Cardinal; var Cursor: PWideChar; CursorEnd: PWideChar; BigEndian: Boolean = UTF16_IS_SYSTEM_ENDIAN_BIG): Boolean;
begin
if BigEndian then begin
Result := EncodeUTF16BEChar(U, PWideChar(Cursor), PWideChar(CursorEnd));
end else begin
Result := EncodeUTF16LEChar(U, PWideChar(Cursor), PWideChar(CursorEnd));
end;
end;
function EncodeUTF16LEChar(U: Cardinal; var Cursor: PWideChar; CursorEnd: PWideChar): Boolean;
begin
if U < $10000 then begin
if Cursor >= CursorEnd then
Exit(False);
Word(Cursor^) := {$IFDEF ENDIAN_BIG}SwapEndian{$ENDIF}(U);
Inc(Cursor);
Exit(True);
end else if U <= $10FFFF then begin
if Cursor + 1 >= CursorEnd then
Exit(False);
Dec(U, $10000);
Word(Cursor[0]) := {$IFDEF ENDIAN_BIG}SwapEndian{$ENDIF}(Word(%1101100000000000 or (U shr 10)));
Word(Cursor[1]) := {$IFDEF ENDIAN_BIG}SwapEndian{$ENDIF}(Word(%1101110000000000 or (U and %1111111111)));
Inc(Cursor, 2);
Exit(True);
end else
Exit(False);
end;
function EncodeUTF16BEChar(U: Cardinal; var Cursor: PWideChar; CursorEnd: PWideChar): Boolean;
begin
if U < $10000 then begin
if Cursor >= CursorEnd then
Exit(False);
Word(Cursor^) := {$IFDEF ENDIAN_LITTLE}SwapEndian{$ENDIF}(Word(U));
Inc(Cursor);
Exit(True);
end else if U <= $10FFFF then begin
if Cursor + 1 >= CursorEnd then
Exit(False);
Dec(U, $10000);
Word(Cursor[0]) := {$IFDEF ENDIAN_LITTLE}SwapEndian{$ENDIF}(Word(%1101100000000000 or (U shr 10)));
Word(Cursor[1]) := {$IFDEF ENDIAN_LITTLE}SwapEndian{$ENDIF}(Word(%1101110000000000 or (U and %1111111111)));
Inc(Cursor, 2);
Exit(True);
end else
Exit(False);
end;
end.