package lsp

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file string.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
(* Because other the syntax s.[x] causes trouble *)
module String = Stdlib.String
include StringLabels

let compare a b = Ordering.of_int (String.compare a b)

module T = struct
  type t = StringLabels.t

  let compare = compare

  let equal (x : t) (y : t) = x = y

  let hash (s : t) = Hashtbl.hash s

  let to_dyn s = Dyn.String s
end

let to_dyn = T.to_dyn

let equal : string -> string -> bool = ( = )

let hash = Hashtbl.hash

let capitalize = capitalize_ascii

let uncapitalize = uncapitalize_ascii

let uppercase = uppercase_ascii

let lowercase = lowercase_ascii

let index = index_opt

let index_from = index_from_opt

let rindex = rindex_opt

let rindex_from = rindex_from_opt

let break s ~pos = (sub s ~pos:0 ~len:pos, sub s ~pos ~len:(length s - pos))

let is_empty s = length s = 0

let rec check_prefix s ~prefix len i =
  i = len || (s.[i] = prefix.[i] && check_prefix s ~prefix len (i + 1))

let rec check_suffix s ~suffix suffix_len offset i =
  i = suffix_len
  || s.[offset + i] = suffix.[i]
     && check_suffix s ~suffix suffix_len offset (i + 1)

let is_prefix s ~prefix =
  let len = length s in
  let prefix_len = length prefix in
  len >= prefix_len && check_prefix s ~prefix prefix_len 0

let is_suffix s ~suffix =
  let len = length s in
  let suffix_len = length suffix in
  len >= suffix_len && check_suffix s ~suffix suffix_len (len - suffix_len) 0

let drop_prefix s ~prefix =
  if is_prefix s ~prefix then
    if length s = length prefix then
      Some ""
    else
      Some (sub s ~pos:(length prefix) ~len:(length s - length prefix))
  else
    None

let drop_suffix s ~suffix =
  if is_suffix s ~suffix then
    if length s = length suffix then
      Some s
    else
      Some (sub s ~pos:0 ~len:(length s - length suffix))
  else
    None

let extract_words s ~is_word_char =
  let rec skip_blanks i =
    if i = length s then
      []
    else if is_word_char s.[i] then
      parse_word i (i + 1)
    else
      skip_blanks (i + 1)
  and parse_word i j =
    if j = length s then
      [ sub s ~pos:i ~len:(j - i) ]
    else if is_word_char s.[j] then
      parse_word i (j + 1)
    else
      sub s ~pos:i ~len:(j - i) :: skip_blanks (j + 1)
  in
  skip_blanks 0

let extract_comma_space_separated_words s =
  extract_words s ~is_word_char:(function
    | ','
    | ' '
    | '\t'
    | '\n' ->
      false
    | _ -> true)

let extract_blank_separated_words s =
  extract_words s ~is_word_char:(function
    | ' '
    | '\t' ->
      false
    | _ -> true)

let lsplit2 s ~on =
  match index s on with
  | None -> None
  | Some i ->
    Some (sub s ~pos:0 ~len:i, sub s ~pos:(i + 1) ~len:(length s - i - 1))

let lsplit2_exn s ~on =
  match lsplit2 s ~on with
  | Some s -> s
  | None -> Code_error.raise "lsplit2_exn" [ ("s", String s); ("on", Char on) ]

let rsplit2 s ~on =
  match rindex s on with
  | None -> None
  | Some i ->
    Some (sub s ~pos:0 ~len:i, sub s ~pos:(i + 1) ~len:(length s - i - 1))

include String_split

let escape_only c s =
  let n = ref 0 in
  let len = length s in
  for i = 0 to len - 1 do
    if unsafe_get s i = c then incr n
  done;
  if !n = 0 then
    s
  else
    let b = Bytes.create (len + !n) in
    n := 0;
    for i = 0 to len - 1 do
      if unsafe_get s i = c then (
        Bytes.unsafe_set b !n '\\';
        incr n
      );
      Bytes.unsafe_set b !n (unsafe_get s i);
      incr n
    done;
    Bytes.unsafe_to_string b

let longest_map l ~f =
  List.fold_left l ~init:0 ~f:(fun acc x -> max acc (length (f x)))

let longest l = longest_map l ~f:Fun.id

let longest_prefix = function
  | [] -> ""
  | [ x ] -> x
  | x :: xs ->
    let rec loop len i =
      if i < len && List.for_all xs ~f:(fun s -> s.[i] = x.[i]) then
        loop len (i + 1)
      else
        i
    in
    let len =
      List.fold_left ~init:(length x) ~f:(fun acc x -> min acc (length x)) xs
    in
    sub ~pos:0 x ~len:(loop len 0)

let exists =
  let rec loop s i len f =
    if i = len then
      false
    else
      f (unsafe_get s i) || loop s (i + 1) len f
  in
  fun s ~f -> loop s 0 (length s) f

let for_all =
  let rec loop s i len f =
    i = len || (f (unsafe_get s i) && loop s (i + 1) len f)
  in
  fun s ~f -> loop s 0 (length s) f

let maybe_quoted s =
  let escaped = escaped s in
  if (s == escaped || s = escaped) && not (String.contains s ' ') then
    s
  else
    Printf.sprintf {|"%s"|} escaped

module O = Comparable.Make (T)

module Set = struct
  include O.Set

  let pp fmt t =
    Format.fprintf fmt "Set (@[%a@])"
      (Format.pp_print_list Format.pp_print_string ~pp_sep:(fun fmt () ->
           Format.fprintf fmt "@ "))
      (to_list t)
end

module Map = struct
  include O.Map

  let pp f fmt t =
    Format.pp_print_list
      (fun fmt (k, v) -> Format.fprintf fmt "@[<hov 2>(%s@ =@ %a)@]" k f v)
      fmt (to_list t)
end

module Table = Hashtbl.Make (T)

let enumerate_gen s =
  let s = " " ^ s ^ " " in
  let rec loop = function
    | [] -> []
    | [ x ] -> [ x ]
    | [ x; y ] -> [ x; s; y ]
    | x :: l -> x :: ", " :: loop l
  in
  fun l -> concat (loop l) ~sep:""

let enumerate_and = enumerate_gen "and"

let enumerate_or = enumerate_gen "or"

let enumerate_one_of = function
  | [ x ] -> x
  | s -> "One of " ^ enumerate_or s

let concat ~sep = function
  | [] -> ""
  | [ x ] -> x
  | xs -> concat ~sep xs

let take s len = sub s ~pos:0 ~len:(min (length s) len)

let drop s n =
  let len = length s in
  sub s ~pos:(min n len) ~len:(max (len - n) 0)

let split_n s n =
  let len = length s in
  let n = min n len in
  (sub s ~pos:0 ~len:n, sub s ~pos:n ~len:(len - n))

let findi =
  let rec loop s len ~f i =
    if i >= len then
      None
    else if f (String.unsafe_get s i) then
      Some i
    else
      loop s len ~f (i + 1)
  in
  fun ?from s ~f ->
    let len = String.length s in
    let from =
      match from with
      | None -> 0
      | Some i ->
        if i > len - 1 then
          Code_error.raise "findi: invalid from" []
        else
          i
    in
    loop s len ~f from

let rfindi =
  let rec loop s ~f i =
    if i < 0 then
      None
    else if f (String.unsafe_get s i) then
      Some i
    else
      loop s ~f (i - 1)
  in
  fun ?from s ~f ->
    let from =
      let len = String.length s in
      match from with
      | None -> len - 1
      | Some i ->
        if i > len - 1 then
          Code_error.raise "rfindi: invalid from" []
        else
          i
    in
    loop s ~f from

let need_quoting s =
  let len = String.length s in
  len = 0
  ||
  let rec loop i =
    if i = len then
      false
    else
      match s.[i] with
      | ' '
      | '\"'
      | '('
      | ')'
      | '{'
      | '}'
      | ';'
      | '#' ->
        true
      | _ -> loop (i + 1)
  in
  loop 0

let quote_for_shell s =
  if need_quoting s then
    Stdlib.Filename.quote s
  else
    s

let of_list chars =
  let s = Bytes.make (List.length chars) '0' in
  List.iteri chars ~f:(fun i c -> Bytes.set s i c);
  Bytes.to_string s

let sub s ~pos ~len =
  match sub s ~pos ~len with
  | s -> s
  | exception _ ->
    Code_error.raise "sub"
      [ ("s", String s); ("pos", Int pos); ("len", Int len) ]
OCaml

Innovation. Community. Security.