-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfppunycode.pas
More file actions
387 lines (325 loc) · 8.16 KB
/
fppunycode.pas
File metadata and controls
387 lines (325 loc) · 8.16 KB
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
unit fppunycode;
{$mode objfpc}{$H+}
interface
uses
SysUtils, Classes, Math
;
function UTF8ToPunycode(const UTF8Str: string): string;
function PunycodeToUTF8(const aPunycodeStr: string): string;
implementation
uses
StrUtils
;
const
_BASE = 36;
_TMIN = 1;
_TMAX = 26;
_SKEW = 38;
_DAMP = 700;
_INITIAL_BIAS = 72;
_INITIAL_N = $80;
_DELIMITER = '-';
type
TUnicodeArray = array of Cardinal;
// Bias adaptation for the next iteration
function Adapt(aDelta, aNumPoints: Cardinal; aFirstTime: Boolean): Cardinal; inline;
var
k: Cardinal;
begin
if aDelta=0 then Exit(0);
if aFirstTime then
aDelta := aDelta div _DAMP
else
aDelta := aDelta shr 1;
aDelta += (aDelta div aNumPoints);
k := 0;
while aDelta > (((_BASE - _TMIN) * _TMAX) div 2) do
begin
aDelta := aDelta div (_BASE - _TMIN);
Inc(k, _BASE);
end;
Result := k + (((_BASE - _TMIN + 1) * aDelta) div (aDelta + _SKEW));
end;
// Encode a digit into a character
function EncodeDigit(aDigit: Cardinal): Char; inline;
begin
if aDigit < 26 then
Result := Chr(Ord('a') + aDigit)
else
Result := Chr(Ord('0') + aDigit - 26);
end;
// Decode a character into a digit
function DecodeDigit(C: Char): Cardinal; inline;
begin
case C of
'a'..'z': Result := Ord(C) - Ord('a');
'A'..'Z': Result := Ord(C) - Ord('A');
'0'..'9': Result := Ord(C) - Ord('0') + 26;
else Result := _BASE; // Error
end;
end;
// Convert a UTF-8 string into an array of Unicode code points
function UTF8ToUnicodeArray(const aUTF8Str: string): TUnicodeArray;
var
i, aPos: Integer;
aCodePoint: Cardinal;
aByteCount: Integer;
begin
Initialize(Result);
SetLength(Result, Length(aUTF8Str));
aPos := 0;
i := 1;
while i <= Length(aUTF8Str) do
begin
// Determine the number of bytes in the UTF-8 character
case Ord(aUTF8Str[i]) of
$00..$7F: // 1-byte sequence (ASCII)
begin
aCodePoint := Ord(aUTF8Str[i]);
aByteCount := 1;
end;
$C0..$DF: // 2-byte sequence
begin
aCodePoint := Ord(aUTF8Str[i]) and $1F;
aByteCount := 2;
end;
$E0..$EF: // 3-byte sequence
begin
aCodePoint := Ord(aUTF8Str[i]) and $0F;
aByteCount := 3;
end;
$F0..$F7: // 4-byte sequence
begin
aCodePoint := Ord(aUTF8Str[i]) and $07;
aByteCount := 4;
end;
else // Skip unknown byte
begin
Inc(i);
Continue;
end;
end;
if (i + aByteCount - 1) > Length(aUTF8Str) then
Break;
Inc(i);
while (aByteCount > 1) and (i <= Length(aUTF8Str)) do
begin
if (Ord(aUTF8Str[i]) and $C0) <> $80 then
Break;
aCodePoint := (aCodePoint shl 6) or (Ord(aUTF8Str[i]) and $3F);
Inc(i);
Dec(aByteCount);
end;
// Append code point to array
if aPos < Length(Result) then
begin
Result[aPos] := aCodePoint;
Inc(aPos);
end;
end;
SetLength(Result, aPos);
end;
function UnicodeToUTF8Char(U: Cardinal; Dest: PByte): Integer; inline;
begin
if U <= $7F then
begin
Dest^ := U;
Result := 1;
end
else if U <= $7FF then
begin
Dest^ := $C0 or (U shr 6);
Inc(Dest);
Dest^ := $80 or (U and $3F);
Result := 2;
end
else if U <= $FFFF then
begin
Dest^ := $E0 or (U shr 12);
Inc(Dest);
Dest^ := $80 or ((U shr 6) and $3F);
Inc(Dest);
Dest^ := $80 or (U and $3F);
Result := 3;
end
else if U <= $10FFFF then
begin
Dest^ := $F0 or (U shr 18);
Inc(Dest);
Dest^ := $80 or ((U shr 12) and $3F);
Inc(Dest);
Dest^ := $80 or ((U shr 6) and $3F);
Inc(Dest);
Dest^ := $80 or (U and $3F);
Result := 4;
end
else
Result := 0; // Invalid character
end;
// Convert an array of Unicode code points into a UTF-8 string
function UnicodeArrayToUTF8(const UnicodeArray: TUnicodeArray): String;
var
MaxLen, ActualLen, i: Integer;
pDest: PByte;
begin
// Maximum possible length: each character is up to 4 bytes (UTF-8 can encode surrogates)
MaxLen := Length(UnicodeArray) * 4;
//Initialize(Result);
SetLength(Result, MaxLen);
if MaxLen = 0 then Exit;
pDest := @Result[1];
ActualLen := 0;
for i := 0 to High(UnicodeArray) do
Inc(ActualLen, UnicodeToUTF8Char(UnicodeArray[i], pDest + ActualLen));
SetLength(Result, ActualLen);
end;
// Main function for encoding into Punycode
function UTF8ToPunycode(const UTF8Str: string): string;
var
aInput: TUnicodeArray;
aOutput: string;
aInputLen, aBasicLen, aHandledLen: Integer;
aBias, aDelta, N, M, Q, K, T: Cardinal;
i: Integer;
begin
aInput := UTF8ToUnicodeArray(UTF8Str);
aInputLen := Length(aInput);
if aInputLen = 0 then
Exit(EmptyStr);
aOutput := EmptyStr;
aBasicLen := 0;
// Copy all basic ASCII characters
for i := 0 to aInputLen - 1 do
begin
if aInput[i] < $80 then
begin
aOutput += Chr(aInput[i]);
Inc(aBasicLen);
end;
end;
aHandledLen := aBasicLen;
// Append delimiter if there are basic characters
if (aBasicLen > 0) and (aBasicLen < aInputLen) then
aOutput += _DELIMITER
else if (aBasicLen = aInputLen) then
Exit(aOutput);
N := _INITIAL_N;
aDelta := 0;
aBias := _INITIAL_BIAS;
while aHandledLen < aInputLen do
begin
// Find the smallest code point >= N
M := High(Cardinal);
for i := 0 to aInputLen - 1 do
begin
if (aInput[i] >= N) and (aInput[i] < M) then
M := aInput[i];
end;
if M = High(Cardinal) then
Break;
// Increment delta
aDelta += (M - N) * (aHandledLen + 1);
N := M;
// Process all characters equal to N
for i := 0 to aInputLen - 1 do
begin
if aInput[i] < N then
Inc(aDelta)
else if aInput[i] = N then
begin
Q := aDelta;
K := _BASE;
while True do
begin
if K <= aBias then
T := _TMIN
else if K >= aBias + _TMAX then
T := _TMAX
else
T := K - aBias;
if Q < T then
Break;
aOutput += EncodeDigit(T + ((Q - T) mod (_BASE - T)));
Q := (Q - T) div (_BASE - T);
Inc(K, _BASE);
end;
aOutput += EncodeDigit(Q);
aBias := Adapt(aDelta, aHandledLen + 1, aHandledLen = aBasicLen);
aDelta := 0;
Inc(aHandledLen);
end;
end;
Inc(aDelta);
Inc(N);
end;
Result := aOutput;
end;
// Main function for decoding from Punycode
function PunycodeToUTF8(const aPunycodeStr: string): string;
var
aOutput: TUnicodeArray = ();
aInputLen, aOutputLen, aBasicLen: Integer;
aBias, N, I, K, aDigit, T, W: Cardinal;
aPos, aDelimPos: Integer;
aOldI: Cardinal;
D: Cardinal = 0;
begin
aInputLen := Length(aPunycodeStr);
// Find the last delimiter
aDelimPos:=RPos(_DELIMITER, aPunycodeStr);
if aDelimPos > 0 then
aBasicLen := aDelimPos - 1
else
aBasicLen := 0;
SetLength(aOutput, aBasicLen);
aOutputLen := aBasicLen;
// Copy basic characters
for aPos := 1 to aBasicLen do
aOutput[aPos - 1] := Ord(aPunycodeStr[aPos]);
N := _INITIAL_N;
I := 0;
aBias := _INITIAL_BIAS;
if aDelimPos > 0 then
aPos := aDelimPos + 1
else
aPos := 1;
while aPos <= aInputLen do
begin
aOldI := I;
W := 1;
K := _BASE;
while aPos <= aInputLen do
begin
aDigit := DecodeDigit(aPunycodeStr[aPos]);
Inc(aPos);
if aDigit >= _BASE then
Exit(EmptyStr); // Invalid character
I += aDigit * W;
if K <= aBias then
T := _TMIN
else if K >= aBias + _TMAX then
T := _TMAX
else
T := K - aBias;
if aDigit < T then
Break;
W *= (_BASE - T);
Inc(K, _BASE);
end;
aBias := Adapt(I - aOldI, aOutputLen + 1, aOldI = 0);
DivMod(I, aOutputLen + 1, D, I);
N += D;
if aOutputLen >= High(aOutput) then SetLength(aOutput, Round(aOutputLen*1.33)+1);
// Prevent out-of-bounds access
if I > aOutputLen then
I := aOutputLen;
if I < aOutputLen then
Move(aOutput[I], aOutput[I + 1], (aOutputLen - I) * SizeOf(Cardinal));
aOutput[I] := N;
Inc(aOutputLen);
Inc(I);
end;
SetLength(aOutput, aOutputLen);
Result := UnicodeArrayToUTF8(aOutput);
end;
end.