1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
2011-07-27 07:17:02 -07:00
|
|
|
(* OCaml *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(* Xavier Leroy, projet Cristal, INRIA Rocquencourt *)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Copyright 1996 Institut National de Recherche en Informatique et *)
|
1999-11-17 10:59:06 -08:00
|
|
|
(* en Automatique. All rights reserved. This file is distributed *)
|
2001-12-07 05:41:02 -08:00
|
|
|
(* under the terms of the GNU Library General Public License, with *)
|
|
|
|
(* the special exception on linking described in file ../LICENSE. *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $Id$ *)
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
(* Character operations *)
|
|
|
|
|
|
|
|
external code: char -> int = "%identity"
|
|
|
|
external unsafe_chr: int -> char = "%identity"
|
|
|
|
|
|
|
|
let chr n =
|
2005-05-19 08:30:35 -07:00
|
|
|
if n < 0 || n > 255 then invalid_arg "Char.chr" else unsafe_chr n
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
external is_printable: char -> bool = "caml_is_printable"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2003-12-16 10:09:44 -08:00
|
|
|
external string_create: int -> string = "caml_create_string"
|
1996-10-31 08:03:04 -08:00
|
|
|
external string_unsafe_get : string -> int -> char = "%string_unsafe_get"
|
|
|
|
external string_unsafe_set : string -> int -> char -> unit
|
|
|
|
= "%string_unsafe_set"
|
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let escaped = function
|
2007-04-16 04:06:51 -07:00
|
|
|
| '\'' -> "\\'"
|
1995-05-04 03:15:53 -07:00
|
|
|
| '\\' -> "\\\\"
|
|
|
|
| '\n' -> "\\n"
|
|
|
|
| '\t' -> "\\t"
|
2007-04-16 04:06:51 -07:00
|
|
|
| '\r' -> "\\r"
|
|
|
|
| '\b' -> "\\b"
|
|
|
|
| c ->
|
|
|
|
if is_printable c then begin
|
|
|
|
let s = string_create 1 in
|
|
|
|
string_unsafe_set s 0 c;
|
|
|
|
s
|
|
|
|
end else begin
|
|
|
|
let n = code c in
|
|
|
|
let s = string_create 4 in
|
|
|
|
string_unsafe_set s 0 '\\';
|
|
|
|
string_unsafe_set s 1 (unsafe_chr (48 + n / 100));
|
|
|
|
string_unsafe_set s 2 (unsafe_chr (48 + (n / 10) mod 10));
|
|
|
|
string_unsafe_set s 3 (unsafe_chr (48 + n mod 10));
|
|
|
|
s
|
|
|
|
end
|
1996-10-31 08:03:04 -08:00
|
|
|
|
|
|
|
let lowercase c =
|
|
|
|
if (c >= 'A' && c <= 'Z')
|
|
|
|
|| (c >= '\192' && c <= '\214')
|
1997-06-17 09:29:20 -07:00
|
|
|
|| (c >= '\216' && c <= '\222')
|
1996-10-31 08:03:04 -08:00
|
|
|
then unsafe_chr(code c + 32)
|
|
|
|
else c
|
|
|
|
|
|
|
|
let uppercase c =
|
|
|
|
if (c >= 'a' && c <= 'z')
|
|
|
|
|| (c >= '\224' && c <= '\246')
|
1997-06-17 09:29:20 -07:00
|
|
|
|| (c >= '\248' && c <= '\254')
|
1996-10-31 08:03:04 -08:00
|
|
|
then unsafe_chr(code c - 32)
|
|
|
|
else c
|
2002-06-26 02:13:59 -07:00
|
|
|
|
|
|
|
type t = char
|
|
|
|
|
|
|
|
let compare c1 c2 = code c1 - code c2
|