Source file char.ml
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
external code: char -> int = "%identity"
external unsafe_chr: int -> char = "%identity"
let chr n =
if n < 0 || n > 255 then invalid_arg "Char.chr" else unsafe_chr n
external bytes_create: int -> bytes = "caml_create_bytes"
external bytes_unsafe_set : bytes -> int -> char -> unit
= "%bytes_unsafe_set"
external unsafe_to_string : bytes -> string = "%bytes_to_string"
let escaped = function
| '\'' -> "\\'"
| '\\' -> "\\\\"
| '\n' -> "\\n"
| '\t' -> "\\t"
| '\r' -> "\\r"
| '\b' -> "\\b"
| ' ' .. '~' as c ->
let s = bytes_create 1 in
bytes_unsafe_set s 0 c;
unsafe_to_string s
| c ->
let n = code c in
let s = bytes_create 4 in
bytes_unsafe_set s 0 '\\';
bytes_unsafe_set s 1 (unsafe_chr (48 + n / 100));
bytes_unsafe_set s 2 (unsafe_chr (48 + (n / 10) mod 10));
bytes_unsafe_set s 3 (unsafe_chr (48 + n mod 10));
unsafe_to_string s
let lowercase_ascii = function
| 'A' .. 'Z' as c -> unsafe_chr(code c + 32)
| c -> c
let uppercase_ascii = function
| 'a' .. 'z' as c -> unsafe_chr(code c - 32)
| c -> c
type t = char
let compare c1 c2 = code c1 - code c2
let equal (c1: t) (c2: t) = compare c1 c2 = 0
external seeded_hash_param :
int -> int -> int -> 'a -> int = "caml_hash" [@@noalloc]
let seeded_hash seed x = seeded_hash_param 10 100 seed x
let hash x = seeded_hash_param 10 100 0 x
module Ascii = struct
let min = '\x00'
let max = '\x7F'
let is_valid = function '\x00' .. '\x7F' -> true | _ -> false
let is_upper = function 'A' .. 'Z' -> true | _ -> false
let is_lower = function 'a' .. 'z' -> true | _ -> false
let is_letter = function 'A' .. 'Z' | 'a' .. 'z' -> true | _ -> false
let is_alphanum = function
| '0' .. '9' | 'A' .. 'Z' | 'a' .. 'z' -> true | _ -> false
let is_white = function ' ' | '\t' .. '\r' -> true | _ -> false
let is_blank = function ' ' | '\t' -> true | _ -> false
let is_graphic = function '!' .. '~' -> true | _ -> false
let is_print = function ' ' .. '~' -> true | _ -> false
let is_control = function '\x00' .. '\x1F' | '\x7F' -> true | _ -> false
let is_digit = function '0' .. '9' -> true | _ -> false
let digit_to_int = function
| '0' .. '9' as c -> code c - 0x30
| c -> invalid_arg (escaped c ^ ": not a decimal digit")
let digit_of_int n = unsafe_chr (0x30 + abs (n mod 10))
let is_hex_digit = function
| '0' .. '9' | 'A' .. 'F' | 'a' .. 'f' -> true
| _ -> false
let hex_digit_to_int = function
| '0' .. '9' as c -> code c - 0x30
| 'A' .. 'F' as c -> 10 + code c - 0x41
| 'a' .. 'f' as c -> 10 + code c - 0x61
| c -> invalid_arg (escaped c ^ ": not a hexadecimal digit")
let lower_hex_digit_of_int n =
let d = abs (n mod 16) in
unsafe_chr (if d < 10 then 0x30 + d else 0x57 + d)
let upper_hex_digit_of_int n =
let d = abs (n mod 16) in
unsafe_chr (if d < 10 then 0x30 + d else 0x37 + d)
let lowercase = lowercase_ascii
let uppercase = uppercase_ascii
end