-
Notifications
You must be signed in to change notification settings - Fork 159
/
ctype.go
213 lines (178 loc) · 3.7 KB
/
ctype.go
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
package linux
import (
"unicode"
)
var characterTable []uint16
func generateCharacterTable() {
for i := 0; i < 255; i++ {
var c uint16
// Each of the bitwise expressions below were copied from the enum
// values, like _ISupper, etc.
if unicode.IsUpper(rune(i)) {
c |= ((1 << (0)) << 8)
}
if unicode.IsLower(rune(i)) {
c |= ((1 << (1)) << 8)
}
if unicode.IsLetter(rune(i)) {
c |= ((1 << (2)) << 8)
}
if unicode.IsDigit(rune(i)) {
c |= ((1 << (3)) << 8)
}
if unicode.IsDigit(rune(i)) ||
(i >= 'a' && i <= 'f') ||
(i >= 'A' && i <= 'F') {
// IsXDigit. This is the same implementation as the Mac version.
// There may be a better way to do this.
c |= ((1 << (4)) << 8)
}
if unicode.IsSpace(rune(i)) {
c |= ((1 << (5)) << 8)
}
if unicode.IsPrint(rune(i)) {
c |= ((1 << (6)) << 8)
}
// The IsSpace check is required becuase Go treats spaces as graphic
// characters, which C does not.
if unicode.IsGraphic(rune(i)) && !unicode.IsSpace(rune(i)) {
c |= ((1 << (7)) << 8)
}
// FIXME: Blank is not implemented.
// if unicode.IsBlank(rune(i)) {
// c |= ((1 << (8)) >> 8)
// }
if unicode.IsControl(rune(i)) {
c |= ((1 << (9)) >> 8)
}
if unicode.IsPunct(rune(i)) {
c |= ((1 << (10)) >> 8)
}
if unicode.IsLetter(rune(i)) || unicode.IsDigit(rune(i)) {
c |= ((1 << (11)) >> 8)
}
// Yes, I know this is a hideously slow way to do it but I just want to
// test if this works right now.
characterTable = append(characterTable, c)
}
}
// CtypeLoc handles __ctype_b_loc(). It returns a character table.
func CtypeLoc() [][]uint16 {
if len(characterTable) == 0 {
generateCharacterTable()
}
return [][]uint16{characterTable}
}
const (
cFalse int = 0
cTrue int = 1
)
// IsAlpha handles isalpha().
func IsAlpha(_c int) int {
if _c < 'A' || _c > 'z' {
return cFalse
} else if _c > 'Z' && _c < 'a' {
return cFalse
}
return cTrue
}
// IsAlnum handles isalnum().
func IsAlnum(_c int) int {
if IsDigit(_c) == cTrue {
return cTrue
}
if IsAlpha(_c) == cTrue {
return cTrue
}
return cFalse
}
// IsCntrl handles iscnrl().
func IsCntrl(_c int) int {
if unicode.IsControl(rune(_c)) {
return cTrue
}
return cFalse
}
// IsDigit handles isdigit().
func IsDigit(_c int) int {
if _c >= '0' && _c <= '9' {
return cTrue
}
return cFalse
}
// IsGraph handles isgraph().
func IsGraph(_c int) int {
if _c == ' ' {
return cFalse // Different implementation between C and Go
}
if unicode.IsGraphic(rune(_c)) {
return cTrue
}
return cFalse
}
// IsLower handles islower().
func IsLower(_c int) int {
if unicode.IsLower(rune(_c)) {
return cTrue
}
return cFalse
}
// IsPrint handles isprint().
func IsPrint(_c int) int {
if unicode.IsPrint(rune(_c)) {
return cTrue
}
return cFalse
}
// IsPunct handles isprunct().
func IsPunct(_c int) int {
if unicode.IsPunct(rune(_c)) {
return cTrue
}
return cFalse
}
// IsSpace handles isspace().
func IsSpace(_c int) int {
if unicode.IsSpace(rune(_c)) {
return cTrue
}
return cFalse
}
// IsUpper handles isupper().
func IsUpper(_c int) int {
if unicode.IsUpper(rune(_c)) {
return cTrue
}
return cFalse
}
// IsXDigit handles isxdigit().
func IsXDigit(_c int) int {
if _c >= '0' && _c <= '9' {
return cTrue
}
if _c >= 'A' && _c <= 'F' {
return cTrue
}
if _c >= 'a' && _c <= 'f' {
return cTrue
}
return cFalse
}
// ToUpper handles toupper().
func ToUpper(_c int) int {
return int(unicode.ToUpper(rune(_c)))
}
// ToLower handles tolower().
func ToLower(_c int) int {
return int(unicode.ToLower(rune(_c)))
}
// IsAscii handles isascii().
func IsAscii(_c int) int {
// TODO
return cFalse
}
// ToAscii handles toascii().
func ToAscii(_c int) int {
// TODO
return cFalse
}