(******************************************************************************)
(*  ocaml-fileutils: files and filenames common operations                    *)
(*                                                                            *)
(*  Copyright (C) 2003-2014, Sylvain Le Gall                                  *)
(*                                                                            *)
(*  This library is free software; you can redistribute it and/or modify it   *)
(*  under the terms of the GNU Lesser General Public License as published by  *)
(*  the Free Software Foundation; either version 2.1 of the License, or (at   *)
(*  your option) any later version, with the OCaml static compilation         *)
(*  exception.                                                                *)
(*                                                                            *)
(*  This library is distributed in the hope that it will be useful, but       *)
(*  WITHOUT ANY WARRANTY; without even the implied warranty of                *)
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file         *)
(*  COPYING for more details.                                                 *)
(*                                                                            *)
(*  You should have received a copy of the GNU Lesser General Public License  *)
(*  along with this library; if not, write to the Free Software Foundation,   *)
(*  Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA             *)
(******************************************************************************)

(** Extended String module
  *)

(** Split a string, separator not included
  *)
let split ?(start_acc=[]) ?(start_pos=0) ~map sep str =
  let str_len = String.length str in
  let rec split_aux acc pos =
    if pos < str_len then begin
      let pos_sep =
        try
          String.index_from str pos sep
        with Not_found ->
          str_len
      in
      let part = String.sub str pos (pos_sep - pos) in
      let acc = (map part) :: acc in
        if pos_sep >= str_len then
          (* Nothing more in the string *)
          List.rev acc
        else if pos_sep = (str_len - 1) then
          (* String end with a separator *)
          List.rev ((map "") :: acc)
        else
          split_aux acc (pos_sep + 1)
    end else
      List.rev acc
  in
    split_aux start_acc start_pos


(** Cut in two a string, separator not included
  *)
let break_at_first sep str =
  let pos_sep =
    String.index str sep
  in
    (String.sub str 0 pos_sep),
    (String.sub str (pos_sep + 1) ((String.length str) - pos_sep - 1))


