diff options
author | alainfrisch <alain@frisch.fr> | 2016-06-20 11:36:42 +0200 |
---|---|---|
committer | alainfrisch <alain@frisch.fr> | 2016-07-09 23:05:25 +0200 |
commit | 4c979b80a8c2e3f77bb083b79a4eecccf3bda067 (patch) | |
tree | 635a52abae305dc316b78304b8dec9bd75cc0325 /stdlib | |
parent | bd9d5555b6f5dd5c19449975094ea3c28b45766b (diff) | |
download | ocaml-4c979b80a8c2e3f77bb083b79a4eecccf3bda067.tar.gz |
String.split
Diffstat (limited to 'stdlib')
-rw-r--r-- | stdlib/string.ml | 11 | ||||
-rw-r--r-- | stdlib/string.mli | 15 |
2 files changed, 26 insertions, 0 deletions
diff --git a/stdlib/string.ml b/stdlib/string.ml index 7189ce99f3..3290283371 100644 --- a/stdlib/string.ml +++ b/stdlib/string.ml @@ -127,6 +127,17 @@ type t = string let compare (x: t) (y: t) = Pervasives.compare x y external equal : string -> string -> bool = "caml_string_equal" +let split sep s = + let r = ref [] in + let j = ref (length s) in + for i = length s - 1 downto 0 do + if unsafe_get s i = sep then begin + r := sub s (i + 1) (!j - i - 1) :: !r; + j := i + end + done; + sub s 0 !j :: !r + (* Deprecated functions implemented via other deprecated functions *) [@@@ocaml.warning "-3"] let uppercase s = diff --git a/stdlib/string.mli b/stdlib/string.mli index b7d895658f..11dd70205b 100644 --- a/stdlib/string.mli +++ b/stdlib/string.mli @@ -282,6 +282,21 @@ val equal: t -> t -> bool (** The equal function for strings. @since 4.03.0 *) +val split: char -> string -> string list +(** [String.split sep s] returns the list of all (possibly empty) + substrings of [s] that are delimited by the [sep] character. + + The function's output is specified by the following invariants: + + - The list is not empty. + - Concatenating its elements using [sep] as a separator returns a + string equal to the input ([String.concat (String.make 1 sep) + (String.split sep s) = s]). + - No string in the result contains the [sep] character. + + @since 4.04.0 +*) + (**/**) (* The following is for system use only. Do not call directly. *) |