uChar.ml 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748
  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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
  19. *)
  20. type t = int
  21. exception Out_of_range
  22. external unsafe_chr_of_uint : int -> t = "%identity"
  23. external uint_code : t -> int = "%identity"
  24. let char_of c =
  25. if c >= 0 && c < 0x100 then Char.chr c else raise Out_of_range
  26. let of_char = Char.code
  27. let code c = if c >= 0 then c else raise Out_of_range
  28. let chr n =
  29. if n >= 0 && n lsr 31 = 0 then n else invalid_arg "UChar.chr"
  30. let chr_of_uint n = if n lsr 31 = 0 then n else invalid_arg "UChar.uint_chr"
  31. let eq (u1 : t) (u2 : t) = u1 = u2
  32. let compare u1 u2 =
  33. let sgn = (u1 lsr 16) - (u2 lsr 16) in
  34. if sgn = 0 then (u1 land 0xFFFF) - (u2 land 0xFFFF) else sgn
  35. type uchar = t
  36. let int_of_uchar u = uint_code u
  37. let uchar_of_int n = chr_of_uint n