Debian Codegraph @

kernel/pstring.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 type t = string
12
13 type char63 = Uint63.t
14
15 (* Maximum length on a 32-bits system. *)
16 let max_length_int : int = 16777211
17
18 let max_length : Uint63.t = Uint63.of_int max_length_int
19
20 let to_string : t -> string = fun s -> s
21
22 let of_string : string -> t option = fun s ->
23 if String.length s <= max_length_int then Some s else None
24
25 (* Return a string of size [max_length] if the parameter is too large.
26 Use [c land 255] if [c] is not a valid character. *)
27 let make : Uint63.t -> char63 -> t = fun i c ->
28 let i = Uint63.to_int_min i max_length_int in
29 let c = Uint63.l_and c (Uint63.of_int 255) in
30 let c = Char.chr (Uint63.to_int_min c 255) in
31 String.make i c
32
33 let length : t -> Uint63.t = fun s ->
34 Uint63.of_int (String.length s)
35
36 (* Out of bound access gives '\x00'. *)
37 let get : t -> Uint63.t -> char63 = fun s i ->
38 let i = Uint63.to_int_min i max_length_int in
39 if i < String.length s then
40 Uint63.of_int (Char.code (String.get s i))
41 else
42 Uint63.zero
43
44 (* Returns an empty string if the [off] is out of bounds. If the [off] is
45 in bounds, but there are less than [len] characters from this offset,
46 the full suffix from [off] is returned. *)
47 let sub : t -> Uint63.t -> Uint63.t -> t = fun s off len ->
48 let off = Uint63.to_int_min off max_int in
49 let len = Uint63.to_int_min len max_int in
50 let len_s = String.length s in
51 if off < len_s then
52 String.sub s off (min len (len_s - off))
53 else
54 ""
55
56 (* Only the longest possible prefix of [s2] is used, so that the resulting
57 string satisfies the maximum length constraint. *)
58 let cat : t -> t -> t = fun s1 s2 ->
59 let len1 = String.length s1 in
60 let len2 = String.length s2 in
61 if len1 + len2 <= max_length_int then
62 s1 ^ s2
63 else
64 s1 ^ String.sub s2 0 (max_length_int - len1)
65
66 let compare : t -> t -> int =
67 String.compare
68
69 let equal : t -> t -> bool =
70 String.equal
71
72 let hash : t -> int =
73 Hashtbl.hash
74
75 let unsafe_of_string : string -> t = fun s -> s
76
77 let compile : t -> string =
78 Printf.sprintf "Pstring.unsafe_of_string %S"