-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata-char.red
179 lines (154 loc) · 4.51 KB
/
data-char.red
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
Red [
Title: "Data.Char"
Author: "unchartedworks"
File: %data-char.red
Tabs: 4
Rights: "unchartedworks. All rights reserved."
License: "MIT"
]
#include %prelude.red
isControl: function [
"These characters are specifically the Unicode values U+0000 to U+001F and U+007F to U+009F."
c [char!]
][
n: (to-hex to-integer c)
((#00000000 <= n) && (n <= #0000001f)) || ((#0000007f <= n) && (n <= #0000009f))
]
isSpace: function [
"a character set containing only the in-line whitespace characters space (U+0020) and tab (U+0009)."
c [char!]
][
(c == #" ") || (c == #"^-") || (c == #"^M") || (c == #"^/") || (c == #"^K") || (c == #"^L")
]
isLower: function [
"a character set containing only the in-line lowercase characters."
c [char!]
][
(c <= #"z") && (c >= #"a")
]
isUpper: function [
"a character set containing only the in-line uppercase characters."
c [char!]
][
(c <= #"Z") && (c >= #"A")
]
isAlpha: function [
"a character set containing only the letter characters."
c [char!]
][
(isLower c) || (isUpper c)
]
isAlphaNum: function [
"a character set containing only the letter and digit characters."
c [char!]
][
(isAlpha c) || (isDigit c)
]
isPrint: function [
"a character set containing only the printable characters."
c [char!]
][
(isLower c) || (isUpper c)
]
isDigit: function [
"a character set containing only the digit characters."
c [char!]
][
(c <= #"9") && (c >= #"0")
]
isOctDigit: function [
"a character set containing only the oct digit characters."
c [char!]
][
(c <= #"7") && (c >= #"0")
]
isHexDigit: function [
"a character set containing only the hex digit characters."
c [char!]
][
elem c "0123456789ABCDEFabcdef"
]
isLetter: function [
"a character set containing only the letter characters."
c [char!]
][
isAlpha c
]
isPunctuation: function [
"Selects Unicode punctuation characters, including various kinds of connectors, brackets and quotes. It's not finished yet."
"This function returns True if its argument has one of the Category, or False otherwise"
c [char!]
][
punctuations: "!^"#$%&'()*+,-./:;<=>?@[\]^^_`{|}~"
elem c punctuations
]
isSeparator: function [
"This function returns True if its argument has one of Space, LineSeparator, ParagraphSeparator"
c [char!]
][
n: (to-hex to-integer c)
(isSpace c) || (n == #00002028) || (n == #00002029)
]
isAscii: function [
"Selects the first 128 characters of the Unicode character set, corresponding to the ASCII character set."
c [char!]
][
n: (to-hex to-integer c)
c < #00000080
]
isLatin1: function [
"Selects the first 256 characters of the Unicode character set, corresponding to the ASCII character set."
c [char!]
][
n: (to-hex to-integer c)
c < #00000100
]
isAsciiLower: function [
"a character set containing only the in-line lowercase characters."
c [char!]
][
(c <= #"z") && (c >= #"a")
]
isAsciiUpper: function [
"a character set containing only the in-line uppercase characters."
c [char!]
][
(c <= #"Z") && (c >= #"A")
]
toUpper: function [
"Convert a letter to the corresponding upper-case letter, if any. Any other character is returned unchanged."
c' [char! string!]
][
c: either (char? c') [c'][copy c']
uppercase c
]
toLower: function [
"Convert a letter to the corresponding lower-case letter, if any. Any other character is returned unchanged."
c' [char! string!]
][
c: either (char? c') [c'][copy c']
lowercase c
]
digitToInt: function [
"Convert a single digit Char to the corresponding Int. This function fails unless its argument satisfies isHexDigit, but recognises both upper- and lower-case hexadecimal digits (that is, '0'..'9', 'a'..'f', 'A'..'F')."
c [char!]
][
case [
(isDigit c) ((to-integer c) - (to-integer #"0"))
((#"a" <= c) && (c <= #"f")) ((to-integer c) - (to-integer #"a") + 10)
((#"A" <= c) && (c <= #"F")) ((to-integer c) - (to-integer #"A") + 10)
true (cause-error 'script 'invalid-arg [c])
]
]
intToDigit: function [
"Convert an Int in the range 0..15 to the corresponding single digit Char. This function fails on other inputs, and generates lower-case hexadecimal digits."
x [integer!]
][
case [
((0 <= x) && (x <= 9)) (to-char (x + (to-integer #"0")))
((10 <= x) && (x < 16)) (to-char (x + (to-integer #"a")))
true (cause-error 'script 'invalid-arg [x])
]
]
ord: :to-integer
chr: :to-char