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
| ///////////////////////////////////////////////////////////////////////////
// //
// Copyright (c) 2015 by Charta Software B.V. //
// All rights reserved //
// //
// Version: 1.7.0.83525 //
// Web site: https://pascal.chartasoftware.com/ //
// //
// This code and information are provided "as is" without warranty of //
// any kind. Dissemination of this information or reproduction of //
// this material is strictly forbidden unless prior written permission //
// is obtained from Charta Software B.V.. //
// //
///////////////////////////////////////////////////////////////////////////
unit Character;
interface
uses
Boolean,
Byte.Nibble,
Integer._32.Positive;
type
{$IF Defined(NEXTGEN)}
TAnsiCharacter = type Char;
TWideCharacter = Char;
TCharacter = Char;
{$ELSE}
TAnsiCharacter = type AnsiChar;
TWideCharacter = type WideChar;
TCharacter = TAnsiCharacter;
{$ENDIF}
type
{$IF Defined(NEXTGEN)}
PCharacter = PChar;
PAnsiCharacter = PChar;
PWideCharacter = PChar;
{$ELSE}
PCharacter = PAnsiChar;
PAnsiCharacter = PAnsiChar;
PWideCharacter = PWideChar;
{$ENDIF}
type
TCharacterSet = set of TCharacter;
type
TCharacterCodePoint = TPositiveInteger32;
PCharacterCodePoint = ^TCharacterCodePoint;
function CharacterToUpperCase(CodePoint: TCharacterCodePoint): TCharacterCodePoint;
function CharacterToLowerCase(CodePoint: TCharacterCodePoint): TCharacterCodePoint;
function EqualCharactersIgnoreCase(Left: TCharacterCodePoint; Right: TCharacterCodePoint): TBoolean;
function CharacterToNibble(Value: TCharacterCodePoint): TNibble;
function NibbleToCharacter(Value: TNibble): TCharacterCodePoint;
implementation
uses
Character.Set_.Unicode,
Exception,
Integer._32.Positive.TextFormat;
function CharacterToNibble(Value: TCharacterCodePoint): TNibble;
begin
case Chr(Value) of
'0'..'9': Result := Ord(Value) - Ord('0');
'A'..'F': Result := 10 + Ord(Value) - Ord('A');
'a'..'f': Result := 10 + Ord(Value) - Ord('a');
else
raise EException.Create('Unknown character: ' + PositiveInteger32ToText(Value));
end;
end;
function NibbleToCharacter(Value: TNibble): TCharacterCodePoint;
begin
if Value < 10 then
Result := Ord('0') + Value
else
Result := Ord('A') + Value - 10;
end;
function CharacterToUpperCase(CodePoint: TCharacterCodePoint): TCharacterCodePoint;
begin
// TODO: We should use UnicodeData.txt and SpecialCasing.txt instead
if (CodePoint >= TUnicode.LATIN_SMALL_LETTER_A) and (CodePoint <= TUnicode.LATIN_SMALL_LETTER_Z) then
Result := CodePoint - TUnicode.LATIN_SMALL_LETTER_A + TUnicode.LATIN_CAPITAL_LETTER_A
else
Result := CodePoint;
end;
function CharacterToLowerCase(CodePoint: TCharacterCodePoint): TCharacterCodePoint;
begin
// TODO: We should use UnicodeData.txt and SpecialCasing.txt instead
if (CodePoint >= TUnicode.LATIN_CAPITAL_LETTER_A) and (CodePoint <= TUnicode.LATIN_CAPITAL_LETTER_Z) then
Result := CodePoint - TUnicode.LATIN_CAPITAL_LETTER_A + TUnicode.LATIN_SMALL_LETTER_A
else
Result := CodePoint;
end;
function EqualCharactersIgnoreCase(Left: TCharacterCodePoint; Right: TCharacterCodePoint): TBoolean;
begin
Result := CharacterToUpperCase(Left) = CharacterToUpperCase(Right);
end;
end.
|