package grace

  1. Overview
  2. Docs
A fancy diagnostics library that allows your compilers to exit with grace

Install

dune-project
 Dependency

Authors

Maintainers

Sources

grace-0.3.0.tbz
sha256=6948979d6ffb5e596773baead81e9ceef36726d6956261bdd62abb2666a45bfc
sha512=db8b39cc9a77d919ab3123bb4047bb6c672c61db9fc6810951e267b2b113c4ac07266ef57188c6db0c02cb4d43d054204cd66ebc91648dbd1da1228022b0e67b

doc/src/grace.std/string.ml.html

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
include Stdlib.StringLabels

let sexp_of_t = Sexplib.Std.sexp_of_string
let t_of_sexp = Sexplib.Std.string_of_sexp
let is_empty t = length t = 0
let append = ( ^ )

let split_lines =
  let back_up_at_newline ~t ~pos ~eol =
    pos := !pos - if !pos > 0 && unsafe_get t (!pos - 1) = '\r' then 2 else 1;
    eol := !pos + 1
  in
  fun t ->
    let n = length t in
    if n = 0
    then []
    else (
      (* Invariant: [-1 <= pos < eol]. *)
      let pos = ref (n - 1) in
      let eol = ref n in
      let ac = ref [] in
      (* We treat the end of the string specially, because if the string ends with a
         newline, we don't want an extra empty string at the end of the output. *)
      if unsafe_get t !pos = '\n' then back_up_at_newline ~t ~pos ~eol;
      while !pos >= 0 do
        if not (unsafe_get t !pos = '\n')
        then decr pos
        else (
          (* Because [pos < eol], we know that [start <= eol]. *)
          let start = !pos + 1 in
          ac := sub t ~pos:start ~len:(!eol - start) :: !ac;
          back_up_at_newline ~t ~pos ~eol)
      done;
      sub t ~pos:0 ~len:!eol :: !ac)
;;

let lfind ?(pos = 0) t ~f =
  let n = length t in
  let rec loop i =
    if i = n then None else if f (unsafe_get t i) then Some i else loop (i + 1)
  in
  loop pos
;;

let rfind ?pos t ~f =
  let pos =
    match pos with
    | Some pos -> pos
    | None -> length t - 1
  in
  let rec loop i =
    if i < 0 then None else if f (unsafe_get t i) then Some i else loop (i - 1)
  in
  loop pos
;;

let drop_prefix t n = if n > length t then "" else sub t ~pos:n ~len:(length t - n)
let prefix t n = if n > length t then t else sub t ~pos:0 ~len:n

let lstrip ?(drop = Char.Ascii.is_whitespace) t =
  match lfind t ~f:(fun c -> not (drop c)) with
  | None -> ""
  | Some 0 -> t
  | Some n -> drop_prefix t n
;;

let rstrip ?(drop = Char.Ascii.is_whitespace) t =
  match rfind t ~f:(fun c -> not (drop c)) with
  | None -> ""
  | Some i when i = length t - 1 -> t
  | Some i -> prefix t (i + 1)
;;