Commits

camlspotter committed 5f5ca25

added String.split

  • Participants
  • Parent commits 72da43c

Comments (0)

Files changed (3)

 2.1.2 (Not yet)
 ------------
 
+- Added String.split
+
 2.1.1
 ------------
 

File lib/xstring.ml

   in
   aux [] 0 0
 
+(* split a string according to char_sep predicate *)
+let split char_sep str =
+  let len = String.length str in
+  if len = 0 then [] else
+    let rec skip_sep cur =
+      if cur >= len then cur
+      else if char_sep str.[cur] then skip_sep (succ cur)
+      else cur  in
+    let rec split beg cur =
+      if cur >= len then 
+	if beg = cur then []
+	else [String.sub str beg (len - beg)]
+      else if char_sep str.[cur] 
+	   then 
+	     let nextw = skip_sep cur in
+	      (String.sub str beg (cur - beg))
+		::(split nextw nextw)
+	   else split beg (succ cur) in
+    let wstart = skip_sep 0 in
+    split wstart wstart
+
 let make1 = String.make 1
 
 module Set = Xset.Make(struct type t = string let compare (x:string) y = compare x y end)
 let split_at len str = String.sub str 0 len, String.sub str len (String.length str - len)
 let take len str = String.sub str 0 len
 let drop len str = String.sub str len (String.length str - len)
+

File lib/xstring.mli

 (** [make1 = String.make 1] *) 
 
 val split_by_newline : string -> string list
+val split : (char -> bool) -> string -> string list
 
 module Set : Xset.S with type elt = String.t