-
Notifications
You must be signed in to change notification settings - Fork 362
Expand file tree
/
Copy pathChar.elm
More file actions
268 lines (196 loc) · 5.09 KB
/
Copy pathChar.elm
File metadata and controls
268 lines (196 loc) · 5.09 KB
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
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
module Char exposing
( Char
, isUpper, isLower, isAlpha, isAlphaNum
, isDigit, isOctDigit, isHexDigit
, toUpper, toLower, toLocaleUpper, toLocaleLower
, toCode, fromCode
)
{-| Functions for working with characters. Character literals are enclosed in
`'a'` pair of single quotes.
# Characters
@docs Char
# ASCII Letters
@docs isUpper, isLower, isAlpha, isAlphaNum
# Digits
@docs isDigit, isOctDigit, isHexDigit
# Conversion
@docs toUpper, toLower, toLocaleUpper, toLocaleLower
# Unicode Code Points
@docs toCode, fromCode
-}
import Basics exposing (Bool, Int, (&&), (||), (>=), (<=))
import Elm.Kernel.Char
-- CHAR
{-| A `Char` is a single [unicode][u] character:
'a'
'0'
'Z'
'?'
'"'
'Σ'
'🙈'
'\t'
'\"'
'\''
'\u{1F648}' -- '🙈'
**Note 1:** You _cannot_ use single quotes around multiple characters like in
JavaScript. This is how we distinguish [`String`](String#String) and `Char`
values in syntax.
**Note 2:** You can use the unicode escapes from `\u{0000}` to `\u{10FFFF}` to
represent characters by their code point. You can also include the unicode
characters directly. Using the escapes can be better if you need one of the
many whitespace characters with different widths.
[u]: https://en.wikipedia.org/wiki/Unicode
-}
type Char = Char -- NOTE: The compiler provides the real implementation.
-- CLASSIFICATION
{-| Detect upper case ASCII characters.
isUpper 'A' == True
isUpper 'B' == True
...
isUpper 'Z' == True
isUpper '0' == False
isUpper 'a' == False
isUpper '-' == False
isUpper 'Σ' == False
-}
isUpper : Char -> Bool
isUpper char =
isUpperCode (toCode char)
isUpperCode : Int -> Bool
isUpperCode code =
code <= 0x5A && 0x41 <= code
{-| Detect lower case ASCII characters.
isLower 'a' == True
isLower 'b' == True
...
isLower 'z' == True
isLower '0' == False
isLower 'A' == False
isLower '-' == False
isLower 'π' == False
-}
isLower : Char -> Bool
isLower char =
isLowerCode (toCode char)
isLowerCode : Int -> Bool
isLowerCode code =
0x61 <= code && code <= 0x7A
{-| Detect upper case and lower case ASCII characters.
isAlpha 'a' == True
isAlpha 'b' == True
isAlpha 'E' == True
isAlpha 'Y' == True
isAlpha '0' == False
isAlpha '-' == False
isAlpha 'π' == False
-}
isAlpha : Char -> Bool
isAlpha char =
let
code =
toCode char
in
isLowerCode code || isUpperCode code
{-| Detect upper case and lower case ASCII characters.
isAlphaNum 'a' == True
isAlphaNum 'b' == True
isAlphaNum 'E' == True
isAlphaNum 'Y' == True
isAlphaNum '0' == True
isAlphaNum '7' == True
isAlphaNum '-' == False
isAlphaNum 'π' == False
-}
isAlphaNum : Char -> Bool
isAlphaNum char =
let
code =
toCode char
in
isLowerCode code || isUpperCode code || isDigitCode code
{-| Detect digits `0123456789`
isDigit '0' == True
isDigit '1' == True
...
isDigit '9' == True
isDigit 'a' == False
isDigit 'b' == False
isDigit 'A' == False
-}
isDigit : Char -> Bool
isDigit char =
isDigitCode (toCode char)
isDigitCode : Int -> Bool
isDigitCode code =
code <= 0x39 && 0x30 <= code
{-| Detect octal digits `01234567`
isOctDigit '0' == True
isOctDigit '1' == True
...
isOctDigit '7' == True
isOctDigit '8' == False
isOctDigit 'a' == False
isOctDigit 'A' == False
-}
isOctDigit : Char -> Bool
isOctDigit char =
let
code =
toCode char
in
code <= 0x37 && 0x30 <= code
{-| Detect hexadecimal digits `0123456789abcdefABCDEF`
-}
isHexDigit : Char -> Bool
isHexDigit char =
let
code =
toCode char
in
(0x30 <= code && code <= 0x39)
|| (0x41 <= code && code <= 0x46)
|| (0x61 <= code && code <= 0x66)
-- CONVERSIONS
{-| Convert to upper case. -}
toUpper : Char -> Char
toUpper =
Elm.Kernel.Char.toUpper
{-| Convert to lower case. -}
toLower : Char -> Char
toLower =
Elm.Kernel.Char.toLower
{-| Convert to upper case, according to any locale-specific case mappings. -}
toLocaleUpper : Char -> Char
toLocaleUpper =
Elm.Kernel.Char.toLocaleUpper
{-| Convert to lower case, according to any locale-specific case mappings. -}
toLocaleLower : Char -> Char
toLocaleLower =
Elm.Kernel.Char.toLocaleLower
{-| Convert to the corresponding Unicode [code point][cp].
[cp]: https://en.wikipedia.org/wiki/Code_point
toCode 'A' == 65
toCode 'B' == 66
toCode '木' == 0x6728
toCode '𝌆' == 0x1D306
toCode '😃' == 0x1F603
-}
toCode : Char -> Int
toCode =
Elm.Kernel.Char.toCode
{-| Convert a Unicode [code point][cp] to a character.
fromCode 65 == 'A'
fromCode 66 == 'B'
fromCode 0x6728 == '木'
fromCode 0x1D306 == '𝌆'
fromCode 0x1F603 == '😃'
fromCode -1 == '�'
The full range of unicode is from `0` to `0x10FFFF`. With numbers outside that
range, you get [the replacement character][fffd].
[cp]: https://en.wikipedia.org/wiki/Code_point
[fffd]: https://en.wikipedia.org/wiki/Specials_(Unicode_block)#Replacement_character
-}
fromCode : Int -> Char
fromCode =
Elm.Kernel.Char.fromCode