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
|
|
|
(* *)
|
2014-04-29 04:56:17 -07:00
|
|
|
(* Damien Doligez, projet Gallium, INRIA Rocquencourt *)
|
1995-08-09 08:06:35 -07:00
|
|
|
(* *)
|
2014-04-29 04:56:17 -07:00
|
|
|
(* Copyright 2014 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
|
|
|
(* *)
|
|
|
|
(***********************************************************************)
|
|
|
|
|
2014-04-29 04:56:17 -07:00
|
|
|
(* String operations, based on byte sequence operations *)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
1995-07-10 02:48:27 -07:00
|
|
|
external length : string -> int = "%string_length"
|
|
|
|
external get : string -> int -> char = "%string_safe_get"
|
2014-04-29 04:56:17 -07:00
|
|
|
external set : bytes -> int -> char -> unit = "%string_safe_set"
|
|
|
|
external create : int -> bytes = "caml_create_string"
|
1995-06-15 01:10:01 -07:00
|
|
|
external unsafe_get : string -> int -> char = "%string_unsafe_get"
|
2014-04-29 04:56:17 -07:00
|
|
|
external unsafe_set : bytes -> int -> char -> unit = "%string_unsafe_set"
|
|
|
|
external unsafe_blit : string -> int -> bytes -> int -> int -> unit
|
2003-12-16 10:09:44 -08:00
|
|
|
= "caml_blit_string" "noalloc"
|
2014-04-29 04:56:17 -07:00
|
|
|
external unsafe_fill : bytes -> int -> int -> char -> unit
|
2003-12-16 10:09:44 -08:00
|
|
|
= "caml_fill_string" "noalloc"
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2014-04-29 04:56:17 -07:00
|
|
|
module B = Bytes
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2014-04-29 04:56:17 -07:00
|
|
|
let make = (Obj.magic B.make : int -> char -> string)
|
|
|
|
let copy = (Obj.magic B.copy : string -> string)
|
|
|
|
let sub = (Obj.magic B.sub : string -> int -> int -> string)
|
|
|
|
let fill = B.fill
|
|
|
|
let blit =
|
|
|
|
(Obj.magic B.blit : string -> int -> bytes -> int -> int -> unit)
|
|
|
|
let concat = (Obj.magic B.concat : string -> string list -> string)
|
|
|
|
let iter = (Obj.magic B.iter : (char -> unit) -> string -> unit)
|
|
|
|
let iteri = (Obj.magic B.iteri : (int -> char -> unit) -> string -> unit)
|
|
|
|
let map = (Obj.magic B.map : (char -> char) -> string -> string)
|
1995-05-04 03:15:53 -07:00
|
|
|
|
2014-04-29 04:56:17 -07:00
|
|
|
(* Beware: we cannot use B.trim or B.escape because they always make a
|
|
|
|
copy, but String.mli spells out some cases where we are not allowed
|
|
|
|
to make a copy. *)
|
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
|
|
|
|
2012-03-08 11:52:03 -08:00
|
|
|
let is_space = function
|
|
|
|
| ' ' | '\012' | '\n' | '\r' | '\t' -> true
|
|
|
|
| _ -> false
|
|
|
|
|
|
|
|
let trim s =
|
2014-04-29 04:56:17 -07:00
|
|
|
if s = "" then s
|
|
|
|
else if is_space (unsafe_get s 0) || is_space (unsafe_get s (length s - 1))
|
|
|
|
then B.unsafe_to_string (B.trim (B.unsafe_of_string s))
|
|
|
|
else s
|
2012-03-08 11:52:03 -08:00
|
|
|
|
1995-05-04 03:15:53 -07:00
|
|
|
let escaped s =
|
2014-04-29 04:56:17 -07:00
|
|
|
let rec needs_escape i =
|
|
|
|
if i >= length s then false else
|
|
|
|
match unsafe_get s i with
|
|
|
|
| '"' | '\\' | '\n' | '\t' | '\r' | '\b' -> true
|
|
|
|
| c when is_printable c -> needs_escape (i+1)
|
|
|
|
| _ -> true
|
|
|
|
in
|
|
|
|
if needs_escape 0 then
|
|
|
|
B.unsafe_to_string (B.escaped (B.unsafe_of_string s))
|
|
|
|
else
|
|
|
|
s
|
1998-11-12 06:53:46 -08:00
|
|
|
|
2014-04-29 04:56:17 -07:00
|
|
|
let index = (Obj.magic B.index : string -> char -> int)
|
|
|
|
let rindex = (Obj.magic B.rindex : string -> char -> int)
|
|
|
|
let index_from = (Obj.magic B.index_from : string -> int -> char -> int)
|
|
|
|
let rindex_from = (Obj.magic B.rindex_from : string -> int -> char -> int)
|
|
|
|
let contains = (Obj.magic B.contains : string -> char -> bool)
|
|
|
|
let contains_from = (Obj.magic B.contains_from : string -> int -> char -> bool)
|
|
|
|
let rcontains_from =
|
|
|
|
(Obj.magic B.rcontains_from : string -> int -> char -> bool)
|
|
|
|
let uppercase = (Obj.magic B.uppercase : string -> string)
|
|
|
|
let lowercase = (Obj.magic B.lowercase : string -> string)
|
|
|
|
let capitalize = (Obj.magic B.capitalize : string -> string)
|
|
|
|
let uncapitalize = (Obj.magic B.uncapitalize : string -> string)
|
2002-06-26 02:13:59 -07:00
|
|
|
|
|
|
|
type t = string
|
|
|
|
|
2011-07-20 02:17:07 -07:00
|
|
|
let compare (x: t) (y: t) = Pervasives.compare x y
|