-
Notifications
You must be signed in to change notification settings - Fork 463
/
Copy pathChar.res
59 lines (47 loc) · 1.33 KB
/
Char.res
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
// FIXME:
// This exists for compatibility reason.
// Move this into Pervasives or Core
// Below is all deprecated and should be removed in v13
type t = char
external code: t => int = "%identity"
external unsafe_chr: int => t = "%identity"
external chr: int => t = "%identity"
external bytes_create: int => array<char> = "Array"
external bytes_unsafe_set: (array<'a>, int, 'a) => unit = "%array_unsafe_set"
@scope("String") @variadic
external unsafe_to_string: array<char> => string = "fromCodePoint"
let escaped = param =>
switch param {
| '\'' => "\\'"
| '\\' => "\\\\"
| '\n' => "\\n"
| '\t' => "\\t"
| '\r' => "\\r"
| '\b' => "\\b"
| ' ' .. '~' as c =>
let s = bytes_create(1)
bytes_unsafe_set(s, 0, c)
unsafe_to_string(s)
| c =>
let n = code(c)
let s = bytes_create(4)
bytes_unsafe_set(s, 0, '\\')
bytes_unsafe_set(s, 1, unsafe_chr(48 + n / 100))
bytes_unsafe_set(s, 2, unsafe_chr(48 + mod(n / 10, 10)))
bytes_unsafe_set(s, 3, unsafe_chr(48 + mod(n, 10)))
unsafe_to_string(s)
}
let lowercase_ascii = c =>
if c >= 'A' && c <= 'Z' {
unsafe_chr(code(c) + 32)
} else {
c
}
let uppercase_ascii = c =>
if c >= 'a' && c <= 'z' {
unsafe_chr(code(c) - 32)
} else {
c
}
let compare = (c1, c2) => code(c1) - code(c2)
let equal = (c1: t, c2: t) => compare(c1, c2) == 0