My working unpac repository
at opam/upstream/stringext 83 lines 2.8 kB view raw view rendered
1## Stringext -- Extra string functions fo OCaml 2 3Extra string functions for OCaml. Mainly splitting. All functions are in the 4`Stringext` module. Here's a snippet of most useful functions out of the mli: 5 6## Api Documentation 7 8```ocaml 9(** string_after [s] [n] returns the substring of [s] that is after 10 character [n] *) 11val string_after : string -> int -> string 12 13(** equivalent to [Str.quote] *) 14val quote : string -> string 15 16(** split [?max] [s] [~on] splits [s] on every [on] occurence upto 17 [max] number of items if [max] is specified. [max] is assumed to 18 be a small number if specified. To not cause stack overflows *) 19val split : ?max:int -> string -> on:char -> string list 20 21(** full_split [s] [~on] will split [s] on every occurence 22 of [on] but will add the separators between the tokens. Maintains 23 the invariant: 24 25 String.concat (full_split s ~on) =s *) 26val full_split : string -> on:char -> string list 27 28(** Trims spaces on the left of the string. In case no trimming is needed 29 the same string is returned without copying *) 30val trim_left : string -> string 31 32(** split_strim_left [s] [~on] [~trim] splits [s] on every character 33 in [on]. Characters in [trim] are trimmed from the left of every 34 result element *) 35val split_trim_left : string -> on:string -> trim:string -> string list 36 37val of_char : char -> string 38 39val of_list : char list -> string 40 41val to_list : string -> char list 42 43val to_array : string -> char array 44 45val of_array : char array -> string 46 47val find_from : ?start:int -> string -> pattern:string -> int option 48 49val replace_all : string -> pattern:string -> with_:string -> string 50 51val replace_all_assoc : string -> (string * string) list -> string 52 53val cut : string -> on:string -> (string * string) option 54(** [String.cut on s] is either the pair [Some (l,r)] of the two 55 (possibly empty) substrings of [s] that are delimited by the first 56 match of the non empty separator string [on] or [None] if [on] 57 can't be matched in [s]. Matching starts from the beginning of [s]. 58 59 The invariant [l ^ on ^ r = s] holds. 60 61 @raise Invalid_argument if [on] is the empty string. *) 62 63val rcut : string -> on:string -> (string * string) option 64(** [String.rcut on s] is like {!cut} but the matching is done backwards 65 starting from the end of [s]. 66 67 @raise Invalid_argument if [on] is the empty string. *) 68 69val chop_prefix : string -> prefix:string -> string option 70 71val drop : string -> int -> string 72 73val take : string -> int -> string 74 75(** [trim_left_sub s ~pos ~len ~chars] Trim all characters inside [chars] 76 from [s] starting from [pos] and up to [len] *) 77val trim_left_sub : string -> pos:int -> len:int -> chars:string -> string 78``` 79 80## Examples 81 82See the directory `examples`. This is a separate dune workspace, so install 83Stringext first.