/
batChar.ml
120 lines (100 loc) · 3.31 KB
/
batChar.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
(*
* BatChar - Additional character operations
* Copyright (C) 1996 Xavier Leroy
* 2008 David Teller
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version,
* with the special exception on linking described in file LICENSE.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*)
(*BISECT-IGNORE-BEGIN*)
include Char
let is_whitespace = function
| ' ' | '\010' | '\013' | '\009' | '\026' | '\012' -> true
| _ -> false
let is_newline = function
| '\010' | '\013' -> true
| _ -> false
let is_digit = function
| '0'..'9' -> true
| _ -> false
let is_uppercase c = 'A' <= c && c <= 'Z'
let is_lowercase c = 'a' <= c && c <= 'z'
let is_uppercase_latin1 c =
is_uppercase c ||
( '\192' (*À*)<= c && c <= '\214' (*Ö*) ) ||
( '\216' (*Ø*) <= c && c <= '\221'(*Ý*) )
let is_lowercase_latin1 c =
is_lowercase c ||
( '\222' (*Þ*) <= c && c <= '\246'(*ö*) ) ||
( '\248'(*ø*) <= c && c <= '\255' (*'ÿ'*) )
let is_latin1 c =
is_uppercase_latin1 c || is_lowercase_latin1 c
let is_symbol = function
| '!' | '%' | '&' | '$' | '#' | '+' | '-' | '/' | ':' | '<' | '='
| '>' | '?' | '@' | '\\' | '~' | '^' | '|' | '*' -> true
| _ -> false
let is_letter c =
is_uppercase c || is_lowercase c
external unsafe_int : char-> int = "%identity"
external unsafe_chr : int -> char = "%identity"
(*BISECT-IGNORE-END*)
let of_digit i =
if i >= 0 && i < 10 then
Char.unsafe_chr (i + Char.code '0')
else
invalid_arg "Char.of_digit"
(*$T of_digit
of_digit 6 = '6'
try ignore (of_digit (-2)); false with Invalid_argument _ -> true
try ignore (of_digit (46)); false with Invalid_argument _ -> true
*)
let enum () =
BatEnum.map unsafe_chr (BatEnum.( -- ) 0 255)
(*$T enum
let e = enum () in for i = 0 to 255 do assert (Char.chr i = BatEnum.get_exn e) done; BatEnum.is_empty e
*)
let ( -- ) from last =
BatEnum.map unsafe_chr (BatEnum.( -- ) (unsafe_int from) (unsafe_int last))
(*$T (--)
let e = Char.chr 12 -- Char.chr 52 in for i = 12 to 52 do assert (Char.chr i = BatEnum.get_exn e) done; BatEnum.is_empty e
*)
(*BISECT-IGNORE-BEGIN*)
let range ?until:(last = unsafe_chr 255) from =
from -- last
(*BISECT-IGNORE-END*)
module Infix = struct
let (--) = (--)
end
let print out t = BatInnerIO.write out t
(*$T print
BatIO.to_string print 'a' = "a"
BatIO.to_string print '\n' = "\n"
*)
(*BISECT-IGNORE-BEGIN*)
let ord (x:char) y =
if x > y then BatOrd.Gt
else if y > x then BatOrd.Lt
else BatOrd.Eq
let equal (x:char) y = x == y (* safe because int-like value *)
let hash = code
(*BISECT-IGNORE-END*)
module Incubator = struct
module Comp = struct
type t = char
let compare = compare
end
module Ord = BatOrd.Ord(Comp)
module Eq = BatOrd.EqComp(Comp)
end