Skip to content

Commit d1639ef

Browse files
committed
Adds a few more libraries
1 parent 25ac5a4 commit d1639ef

File tree

6 files changed

+955
-0
lines changed

6 files changed

+955
-0
lines changed

src/Char/Extra.elm

Lines changed: 193 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,193 @@
1+
module Char.Extra exposing (isSpace, isControl)
2+
3+
{-| Convenience functionality on [`Char`](https://package.elm-lang.org/packages/elm/core/latest/Char)
4+
5+
@docs isSpace, isControl
6+
7+
-}
8+
9+
import Array exposing (Array)
10+
11+
12+
type AsciiCharacterClass
13+
= C -- control
14+
| Cw -- control whitespace
15+
| W -- whitespace
16+
| D -- digit
17+
| L -- lowercase
18+
| Lx -- lowercase hex digit
19+
| U -- uppercase
20+
| Ux -- uppercase hex digit
21+
| P -- punctuation
22+
23+
24+
asciiCharacterClass : Array AsciiCharacterClass
25+
asciiCharacterClass =
26+
Array.fromList
27+
[ C
28+
, C
29+
, C
30+
, C
31+
, C
32+
, C
33+
, C
34+
, C
35+
, C
36+
, Cw
37+
, Cw
38+
, C
39+
, Cw
40+
, Cw
41+
, C
42+
, C
43+
, C
44+
, C
45+
, C
46+
, C
47+
, C
48+
, C
49+
, C
50+
, C
51+
, C
52+
, C
53+
, C
54+
, C
55+
, C
56+
, C
57+
, C
58+
, C
59+
, W
60+
, P
61+
, P
62+
, P
63+
, P
64+
, P
65+
, P
66+
, P
67+
, P
68+
, P
69+
, P
70+
, P
71+
, P
72+
, P
73+
, P
74+
, P
75+
, D
76+
, D
77+
, D
78+
, D
79+
, D
80+
, D
81+
, D
82+
, D
83+
, D
84+
, D
85+
, P
86+
, P
87+
, P
88+
, P
89+
, P
90+
, P
91+
, P
92+
, Ux
93+
, Ux
94+
, Ux
95+
, Ux
96+
, Ux
97+
, Ux
98+
, U
99+
, U
100+
, U
101+
, U
102+
, U
103+
, U
104+
, U
105+
, U
106+
, U
107+
, U
108+
, U
109+
, U
110+
, U
111+
, U
112+
, U
113+
, U
114+
, U
115+
, U
116+
, U
117+
, U
118+
, P
119+
, P
120+
, P
121+
, P
122+
, P
123+
, P
124+
, Lx
125+
, Lx
126+
, Lx
127+
, Lx
128+
, Lx
129+
, Lx
130+
, L
131+
, L
132+
, L
133+
, L
134+
, L
135+
, L
136+
, L
137+
, L
138+
, L
139+
, L
140+
, L
141+
, L
142+
, L
143+
, L
144+
, L
145+
, L
146+
, L
147+
, L
148+
, L
149+
, L
150+
, P
151+
, P
152+
, P
153+
, P
154+
, C
155+
]
156+
157+
158+
{-| Returns true if the given character is an ASCII control character.
159+
-}
160+
isControl : Char -> Bool
161+
isControl c =
162+
let
163+
code =
164+
Char.toCode c
165+
in
166+
if code >= 80 then
167+
False
168+
169+
else
170+
case Array.get code asciiCharacterClass of
171+
Just C ->
172+
True
173+
174+
Just Cw ->
175+
True
176+
177+
_ ->
178+
False
179+
180+
181+
{-| Returns true if the given character is whitespace character.
182+
-}
183+
isSpace : Char -> Bool
184+
isSpace char =
185+
case Array.get (Char.toCode char) asciiCharacterClass of
186+
Just W ->
187+
True
188+
189+
Just Cw ->
190+
True
191+
192+
_ ->
193+
False

0 commit comments

Comments
 (0)