Coccinelle release 1.0.0-rc14
[bpt/coccinelle.git] / bundles / extlib / extlib-1.5.2 / uChar.ml
1 (*
2 * UChar - Unicode (ISO-UCS) characters
3 * Copyright (C) 2002, 2003 Yamagata Yoriyuki
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version,
9 * with the special exception on linking described in file LICENSE.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *)
20
21 type t = int
22
23 exception Out_of_range
24
25 external unsafe_chr_of_uint : int -> t = "%identity"
26 external uint_code : t -> int = "%identity"
27
28 let char_of c =
29 if c >= 0 && c < 0x100 then Char.chr c else raise Out_of_range
30
31 let of_char = Char.code
32
33 let code c = if c >= 0 then c else raise Out_of_range
34
35 let chr n =
36 if n >= 0 && n lsr 31 = 0 then n else invalid_arg "UChar.chr"
37
38 let chr_of_uint n = if n lsr 31 = 0 then n else invalid_arg "UChar.uint_chr"
39
40 let eq (u1 : t) (u2 : t) = u1 = u2
41 let compare u1 u2 =
42 let sgn = (u1 lsr 16) - (u2 lsr 16) in
43 if sgn = 0 then (u1 land 0xFFFF) - (u2 land 0xFFFF) else sgn
44
45 type uchar = t
46
47 let int_of_uchar u = uint_code u
48 let uchar_of_int n = chr_of_uint n