1995-08-09 08:06:35 -07:00
|
|
|
(***********************************************************************)
|
|
|
|
(* *)
|
1996-04-30 07:53:58 -07:00
|
|
|
(* Objective Caml *)
|
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 *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* Automatique. Distributed only by permission. *)
|
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
|
|
|
(* $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 =
|
|
|
|
if n < 0 or n > 255 then invalid_arg "Char.chr" else unsafe_chr n
|
|
|
|
|
|
|
|
external is_printable: char -> bool = "is_printable"
|
|
|
|
|
1996-10-31 08:03:04 -08:00
|
|
|
external string_create: int -> string = "create_string"
|
|
|
|
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
|
|
|
|
'\'' -> "\\'"
|
|
|
|
| '\\' -> "\\\\"
|
|
|
|
| '\n' -> "\\n"
|
|
|
|
| '\t' -> "\\t"
|
1996-10-31 08:03:04 -08:00
|
|
|
| c -> if is_printable c then begin
|
|
|
|
let s = string_create 1 in
|
|
|
|
string_unsafe_set s 1 c;
|
|
|
|
s
|
|
|
|
end else begin
|
1995-05-04 03:15:53 -07:00
|
|
|
let n = code c in
|
1996-10-31 08:03:04 -08:00
|
|
|
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));
|
1995-05-04 03:15:53 -07:00
|
|
|
s
|
|
|
|
end
|
1996-10-31 08:03:04 -08:00
|
|
|
|
|
|
|
let lowercase c =
|
|
|
|
if (c >= 'A' && c <= 'Z')
|
|
|
|
|| (c >= '\192' && c <= '\214')
|
|
|
|
|| (c >= '\217' && c <= '\222')
|
|
|
|
then unsafe_chr(code c + 32)
|
|
|
|
else c
|
|
|
|
|
|
|
|
let uppercase c =
|
|
|
|
if (c >= 'a' && c <= 'z')
|
|
|
|
|| (c >= '\224' && c <= '\246')
|
|
|
|
|| (c >= '\249' && c <= '\254')
|
|
|
|
then unsafe_chr(code c - 32)
|
|
|
|
else c
|