File: utils.ml

package info (click to toggle)
opam-file-format 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 280 kB
  • sloc: ml: 2,356; makefile: 4
file content (33 lines) | stat: -rw-r--r-- 1,360 bytes parent folder | download | duplicates (4)
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
(**************************************************************************)
(*                                                                        *)
(*    Copyright 2020 OCamlPro                                             *)
(*                                                                        *)
(*  All rights reserved. This file is distributed under the terms of the  *)
(*  GNU Lesser General Public License version 2.1, with the special       *)
(*  exception on linking described in the file LICENSE.                   *)
(*                                                                        *)
(**************************************************************************)


(* Common test functions *)

let add_after_first ?(cond=fun _ -> true) c sep str =
  try
    let length = String.length str in
    if not (cond str) then str else
    let split = String.index str c in
    let fst = String.sub str 0 (split+1) in
    let snd = String.sub str (split+1) (length - split -1) in
    fst ^ sep ^ snd
  with _ -> failwith (Printf.sprintf "error with '%c' on %s" c str)

let split_on_char sep s =
  let r = ref [] in
  let j = ref (String.length s) in
  for i = String.length s - 1 downto 0 do
    if String.get s i = sep then begin
      r := String.sub s (i + 1) (!j - i - 1) :: !r;
      j := i
    end
  done;
  String.sub s 0 !j :: !r