/
ctype.c
180 lines (167 loc) · 3.56 KB
/
ctype.c
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
/* ctype-style APIs.
*
* Copyright (C) 2012-2016 Reece H. Dunn
*
* This file is part of ucd-tools.
*
* ucd-tools is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* ucd-tools is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with ucd-tools. If not, see <http://www.gnu.org/licenses/>.
*/
#include "ucd/ucd.h"
int ucd_isalnum(codepoint_t c)
{
switch (ucd_lookup_category(c))
{
case UCD_CATEGORY_Ll:
case UCD_CATEGORY_Lm:
case UCD_CATEGORY_Lo:
case UCD_CATEGORY_Lt:
case UCD_CATEGORY_Lu:
case UCD_CATEGORY_Nd:
case UCD_CATEGORY_Nl:
case UCD_CATEGORY_No:
return 1;
default:
return 0;
}
}
int ucd_isalpha(codepoint_t c)
{
switch (ucd_lookup_category(c))
{
case UCD_CATEGORY_Ll:
case UCD_CATEGORY_Lm:
case UCD_CATEGORY_Lo:
case UCD_CATEGORY_Lt:
case UCD_CATEGORY_Lu:
return 1;
default:
return 0;
}
}
int ucd_isblank(codepoint_t c)
{
switch (ucd_lookup_category(c))
{
case UCD_CATEGORY_Zs:
return 1;
case UCD_CATEGORY_Cc:
return c == 0x09; // U+0009 : CHARACTER TABULATION
default:
return 0;
}
}
int ucd_iscntrl(codepoint_t c)
{
return ucd_lookup_category(c) == UCD_CATEGORY_Cc;
}
int ucd_isdigit(codepoint_t c)
{
return (c >= 0x30 && c <= 0x39); // [0-9]
}
int ucd_isgraph(codepoint_t c)
{
switch (ucd_lookup_category(c))
{
case UCD_CATEGORY_Cc:
case UCD_CATEGORY_Cf:
case UCD_CATEGORY_Cn:
case UCD_CATEGORY_Co:
case UCD_CATEGORY_Cs:
case UCD_CATEGORY_Zl:
case UCD_CATEGORY_Zp:
case UCD_CATEGORY_Zs:
case UCD_CATEGORY_Ii:
return 0;
default:
return 1;
}
}
int ucd_islower(codepoint_t c)
{
return ucd_lookup_category(c) == UCD_CATEGORY_Ll
|| ucd_toupper(c) != c;
}
int ucd_isprint(codepoint_t c)
{
switch (ucd_lookup_category(c))
{
case UCD_CATEGORY_Cc:
case UCD_CATEGORY_Cf:
case UCD_CATEGORY_Cn:
case UCD_CATEGORY_Co:
case UCD_CATEGORY_Cs:
case UCD_CATEGORY_Ii:
return 0;
default:
return 1;
}
}
int ucd_ispunct(codepoint_t c)
{
switch (ucd_lookup_category(c))
{
case UCD_CATEGORY_Pc:
case UCD_CATEGORY_Pd:
case UCD_CATEGORY_Pe:
case UCD_CATEGORY_Pf:
case UCD_CATEGORY_Pi:
case UCD_CATEGORY_Po:
case UCD_CATEGORY_Ps:
return 1;
default:
return 0;
}
}
int ucd_isspace(codepoint_t c)
{
switch (ucd_lookup_category(c))
{
case UCD_CATEGORY_Zl:
case UCD_CATEGORY_Zp:
return 1;
case UCD_CATEGORY_Zs:
switch (c) // Exclude characters with the <noBreak> DispositionType
{
case 0x00A0: // U+00A0 : NO-BREAK SPACE
case 0x2007: // U+2007 : FIGURE SPACE
case 0x202F: // U+202F : NARROW NO-BREAK SPACE
return 0;
}
return 1;
case UCD_CATEGORY_Cc:
switch (c) // Include control characters marked as White_Space
{
case 0x09: // U+0009 : CHARACTER TABULATION
case 0x0A: // U+000A : LINE FEED
case 0x0B: // U+000B : LINE TABULATION
case 0x0C: // U+000C : FORM FEED
case 0x0D: // U+000D : CARRIAGE RETURN
case 0x85: // U+0085 : NEXT LINE
return 1;
}
default:
return 0;
}
}
int ucd_isupper(codepoint_t c)
{
return ucd_lookup_category(c) == UCD_CATEGORY_Lu
|| ucd_tolower(c) != c;
}
int ucd_isxdigit(codepoint_t c)
{
return (c >= 0x30 && c <= 0x39) // [0-9]
|| (c >= 0x41 && c <= 0x46) // [A-Z]
|| (c >= 0x61 && c <= 0x66); // [a-z]
}