Blame src/FileStringExt.ml

Packit 9ff65e
(******************************************************************************)
Packit 9ff65e
(*  ocaml-fileutils: files and filenames common operations                    *)
Packit 9ff65e
(*                                                                            *)
Packit 9ff65e
(*  Copyright (C) 2003-2014, Sylvain Le Gall                                  *)
Packit 9ff65e
(*                                                                            *)
Packit 9ff65e
(*  This library is free software; you can redistribute it and/or modify it   *)
Packit 9ff65e
(*  under the terms of the GNU Lesser General Public License as published by  *)
Packit 9ff65e
(*  the Free Software Foundation; either version 2.1 of the License, or (at   *)
Packit 9ff65e
(*  your option) any later version, with the OCaml static compilation         *)
Packit 9ff65e
(*  exception.                                                                *)
Packit 9ff65e
(*                                                                            *)
Packit 9ff65e
(*  This library is distributed in the hope that it will be useful, but       *)
Packit 9ff65e
(*  WITHOUT ANY WARRANTY; without even the implied warranty of                *)
Packit 9ff65e
(*  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the file         *)
Packit 9ff65e
(*  COPYING for more details.                                                 *)
Packit 9ff65e
(*                                                                            *)
Packit 9ff65e
(*  You should have received a copy of the GNU Lesser General Public License  *)
Packit 9ff65e
(*  along with this library; if not, write to the Free Software Foundation,   *)
Packit 9ff65e
(*  Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA             *)
Packit 9ff65e
(******************************************************************************)
Packit 9ff65e
Packit 9ff65e
(** Extended String module
Packit 9ff65e
  *)
Packit 9ff65e
Packit 9ff65e
(** Split a string, separator not included
Packit 9ff65e
  *)
Packit 9ff65e
let split ?(start_acc=[]) ?(start_pos=0) ~map sep str =
Packit 9ff65e
  let str_len = String.length str in
Packit 9ff65e
  let rec split_aux acc pos =
Packit 9ff65e
    if pos < str_len then begin
Packit 9ff65e
      let pos_sep =
Packit 9ff65e
        try
Packit 9ff65e
          String.index_from str pos sep
Packit 9ff65e
        with Not_found ->
Packit 9ff65e
          str_len
Packit 9ff65e
      in
Packit 9ff65e
      let part = String.sub str pos (pos_sep - pos) in
Packit 9ff65e
      let acc = (map part) :: acc in
Packit 9ff65e
        if pos_sep >= str_len then
Packit 9ff65e
          (* Nothing more in the string *)
Packit 9ff65e
          List.rev acc
Packit 9ff65e
        else if pos_sep = (str_len - 1) then
Packit 9ff65e
          (* String end with a separator *)
Packit 9ff65e
          List.rev ((map "") :: acc)
Packit 9ff65e
        else
Packit 9ff65e
          split_aux acc (pos_sep + 1)
Packit 9ff65e
    end else
Packit 9ff65e
      List.rev acc
Packit 9ff65e
  in
Packit 9ff65e
    split_aux start_acc start_pos
Packit 9ff65e
Packit 9ff65e
Packit 9ff65e
(** Cut in two a string, separator not included
Packit 9ff65e
  *)
Packit 9ff65e
let break_at_first sep str =
Packit 9ff65e
  let pos_sep =
Packit 9ff65e
    String.index str sep
Packit 9ff65e
  in
Packit 9ff65e
    (String.sub str 0 pos_sep),
Packit 9ff65e
    (String.sub str (pos_sep + 1) ((String.length str) - pos_sep - 1))
Packit 9ff65e
Packit 9ff65e