-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunicode.pas
90 lines (75 loc) · 2.25 KB
/
unicode.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
{$MODE OBJFPC} { -*- delphi -*- }
{$INCLUDE settings.inc}
unit unicode;
interface
const
kEOF = -1;
{$IFOPT C+} kNone = -2; {$ENDIF}
type
TUnicodeCodepointRangeInternal = {$IFOPT C+} -2 {$ELSE} -1 {$ENDIF} ..$10FFFF;
TUnicodeCodepointRange = type TUnicodeCodepointRangeInternal;
TUnicodeCodepoint = record
private
FValue: TUnicodeCodepointRange;
public
property Value: TUnicodeCodepointRange read FValue;
{$IFOPT C+} function GetDebugDescription(): UTF8String; {$ENDIF}
end;
operator := (const Value: TUnicodeCodepointRange): TUnicodeCodepoint; inline;
operator = (const Op1: TUnicodeCodepoint; const Op2: TUnicodeCodepointRange): Boolean; inline;
operator = (const Op1, Op2: TUnicodeCodepoint): Boolean; inline;
type
TUnicodeCodepointArray = array of TUnicodeCodepoint;
type
TU = record end;
const
U: TU = ();
operator + (const U: TU; const Value: TUnicodeCodepointRange): TUnicodeCodepoint; inline;
implementation
uses exceptions, sysutils {$IFOPT C+}, utf8 {$ENDIF};
operator := (const Value: TUnicodeCodepointRange): TUnicodeCodepoint;
begin
Assert((Value < $D800) or (Value > $DFFF)); // exclude surrogates
Result.FValue := Value;
end;
operator = (const Op1: TUnicodeCodepoint; const Op2: TUnicodeCodepointRange): Boolean;
begin
Assert((Op2 < $D800) or (Op2 > $DFFF)); // exclude surrogates
Result := Op1.Value = Op2;
end;
operator = (const Op1, Op2: TUnicodeCodepoint): Boolean;
begin
Result := Op1.Value = Op2.Value;
end;
operator + (const U: TU; const Value: TUnicodeCodepointRange): TUnicodeCodepoint;
begin
Result.FValue := Value;
end;
{$IFOPT C+}
function TUnicodeCodepoint.GetDebugDescription(): UTF8String;
begin
if (FValue = kEOF) then
Result := 'EOF'
else
if (FValue = kNone) then
Result := 'NO CHARACTER'
else
if (FValue = $0000) then
Result := 'U+0000 NULL'
else
if (FValue = $000A) then
Result := 'U+000A LF'
else
if (FValue = $000D) then
Result := 'U+000D CR'
else
if (FValue = $0020) then
Result := 'U+0020 SPACE'
else
if (FValue < $0020) then
Result := 'U+' + IntToHex(FValue, 4) + ' (<control>)'
else
Result := 'U+' + IntToHex(FValue, 4) + ' (' + CodepointToUTF8(FValue) + ')';
end;
{$ENDIF}
end.