Permalink
Browse files

[cleanup] Base.String: changed String.split to a much simpler String.…

…slice

(which corresponds to every single call case)
  • Loading branch information...
1 parent a2ea488 commit 461365b0c09eef481a76d349683c21535c0ecd13 Louis Gesbert committed Jun 23, 2011
@@ -121,7 +121,7 @@ type conf_line =
| Package of package_name
| Infos of (filename list * string list) (* filenames, and imported packages *)
-let conf_split s = String.split (fun c -> List.mem c [ ' ' ; '\t' ]) s
+let conf_split s = String.slice_chars " \t" s
let process_line ~position line =
let line =
match String.findi '#' line with
@@ -208,7 +208,7 @@ let load_conf conffile =
match process_line ~position line with
| None -> package
| Some (Package package_name) ->
- if package_name = "" || not ( List.for_all String.is_word (String.split ((=)'.') package_name ) )
+ if package_name = "" || not ( List.for_all String.is_word (String.slice '.' package_name ) )
then
OManager.error (
"%a@\n" ^^
@@ -101,7 +101,7 @@ let options_parser_with_default ?name (_default_m, default_o) =
| p -> (match name with None -> p | Some n -> Filename.concat p n)
in
- let lflags = BaseString.split (fun x -> x=',') flags in
+ let lflags = BaseString.slice ',' flags in
let restore,lflags =
let found, lflags = consume_option "restore" lflags in
let r =
@@ -148,7 +148,7 @@ let options_parser_with_default ?name (_default_m, default_o) =
(fun (_,o) i ->
let opt = get_local_options o in
(module Badop_local : Badop.S), Badop.Options_Local { opt with Badop.revision = Some i }),
- "<int>", "Open the db at given revision. Be careful, it will overwrite current db";
+ "<int>", "Revert the database to the given revision. Be careful, all data after that revision will be cleared";
["--db-template"],
A.func A.unit
(fun (m,o) () ->
View
@@ -256,4 +256,4 @@ let rec add_bash_completion ?name ?names ?default args =
" Dumps a bash completion in ./bash_completion") :: args in
new_args
-let split s = String.split (fun c -> List.mem c [' '; ','; ';']) s
+let split s = String.slice_chars " .;" s
View
@@ -642,53 +642,47 @@ let rev_char_list_of_string s =
(* =========================================================================== *)
(*
- Splitting: if you need to patch this code, cf comment in mli
+ Splitting, dicing & slicing
*)
-let split crit s =
- let l = length s in
- let rec aux r count pos =
- if pos < 0 then
- if count = 0 then r
- else unsafe_sub s 0 count :: r
- else
- let c = unsafe_get s pos in
- if crit c then
- let r = if count = 0 then r else
- unsafe_sub s (succ pos) count :: r in
- aux r 0 (pred pos)
- else
- aux r (succ count) (pred pos)
+let slice cut str =
+ let rec aux pos =
+ try
+ let i = String.index_from str pos cut in
+ if i==pos then aux (succ pos)
+ else unsafe_sub str pos (i - pos) :: aux (succ i)
+ with Not_found | Invalid_argument _ ->
+ let l = String.length str in
+ if l==pos then []
+ else [ unsafe_sub str pos (l - pos) ]
in
- aux [] 0 (pred l)
+ aux 0
+
+let slice_chars cut str =
+ let rec index_from pos = if String.contains cut str.[pos] then pos else index_from (succ pos) in
+ let rec aux pos =
+ try
+ let i = index_from pos in
+ if i==pos then aux (succ pos)
+ else unsafe_sub str pos (i - pos) :: aux (succ i)
+ with Invalid_argument _ ->
+ let l = String.length str in
+ if l==pos then []
+ else [ unsafe_sub str pos (l - pos) ]
+ in
+ aux 0
let split_char c s =
- let l = length s in
- let rec search_char i =
- if i=l then s, ""
- else
- if unsafe_get s i = c then sub s 0 i, sub s (i+1) (l-i-1)
- else search_char (succ i) in
- search_char 0
+ try
+ let i = String.index s c in
+ unsafe_sub s 0 i, unsafe_sub s (i + 1) (String.length s - i - 1)
+ with Not_found -> s, ""
let split_char_last c s =
- let l = length s in
- let rec search_char i =
- if i=0 then s, ""
- else
- let pi = pred i in
- if unsafe_get s pi = c then sub s 0 pi, sub s (pi+1) (l-pi-1)
- else search_char pi in
- search_char l
-
-let split_chars cl s =
- let l = length s in
- let rec search_char i =
- if i=l then s, ""
- else
- if List.mem (unsafe_get s i) cl then sub s 0 i, sub s (i+1) (l-i-1)
- else search_char (succ i) in
- search_char 0
+ try
+ let i = String.rindex s c in
+ unsafe_sub s 0 i, unsafe_sub s (i + 1) (String.length s - i - 1)
+ with Not_found -> s, ""
(* =========================================================================== *)
@@ -414,32 +414,30 @@ val rev_char_list_of_string : string -> char list
(** {6 Splitting} *)
(**
- TODO:implement the following function if needed
- val split_first : (char -> bool) -> string -> string * string
- val split_last : (char -> bool) -> string -> string * string
- And then remove [split_char], [split_char_last], [split_chars]
+ cuts a string at the occurences of the given character. The separation
+ character is removed. Empty strings are'nt returned (that is,
+ [slice '/' "/path//x/"] returns [["path";"x"]]). Non tail-recursive.
*)
+val slice : char -> string -> string list
(**
- splits a string using character criterion
+ same as slice, but cuts at every character present in the string given as
+ first parameter.
*)
-val split : (char -> bool) -> string -> string list
+val slice_chars : string -> string -> string list
(**
- split a string in two substrings, split is first occurrence of char in string
+ split a string in two substrings, at the first occurence of [char], which is removed.
+ If [char] doesn't appear in [string], returns [(string,"")]
*)
val split_char : char -> string -> string * string
(**
- split a string in two substrings, split is last occurrence of char in string
+ split a string in two substrings, at the last occurence of [char], which is removed.
+ If [char] doesn't appear in [string], returns [(string,"")]
*)
val split_char_last : char -> string -> string * string
-(**
- split a string in two substrings, split is first occurrence of one of chars in string
-*)
-val split_chars : char list -> string -> string * string
-
(** {6 UNDOCUMENTED : use it at your own risk} *)
(** TODO: remove from String *)
@@ -178,7 +178,7 @@ let is_contained str = function
let flag str = function
| None -> false
- | Some var -> List.mem str (Base.String.split (function ',' -> true | _ -> false) var)
+ | Some var -> List.mem str (Base.String.slice ',' var)
let _ =
match check_vars with
View
@@ -604,7 +604,7 @@ let simplify_path =
path
else
let b = path.[len-1] = path_sep.[0] in
- let l = String.split ((=) path_sep.[0]) path in (* warning : "" are eliminated by split *)
+ let l = String.slice path_sep.[0] path in
let b2 = path.[0] = path_sep.[0] in
let l = aux (not b2) l in
let path = String.concat path_sep l in
@@ -71,7 +71,7 @@ let of_list =
in stop_fold []
let parse s =
- let li = String.split (fun t -> List.mem t ['/'; ' '; ',' ;'{' ; '}']) s in
+ let li = String.slice_chars "/ ,{}" s in
of_list li
let gen_print f list = String.concat_map ~left:"{" ~right:"}" ", " f list
View
@@ -121,7 +121,7 @@ end
let map_type_from_type_map_and_path type_path_map current_path ?(definition=false) (typ : BslTypes.t) =
let module_path = String.concat "." current_path in
let map_type_key name =
- let parts = List.map String.lowercase (String.split ((=) '.') name) in
+ let parts = List.map String.lowercase (String.slice '.' name) in
let nkey = BslKey.normalize_string (String.concat "_" parts) in
let rec find_first_success rest_rev_path =
let nkey = String.concat "_" (List.rev_map String.lowercase (nkey::rest_rev_path)) in
@@ -644,7 +644,7 @@ struct
let of_string s =
let elts =
- String.split (fun c -> List.mem c ['~'; '/'; ','; '.'; '#']) s in
+ String.slice_chars "~/,.#" s in
build elts
let to_string = function
@@ -72,8 +72,7 @@ let debug fmt =
let spliter g =
List.map String.trim
- (String.split
- (fun c -> List.mem c ['{'; '}'; ' '; ','; ';']) g)
+ (String.slice_chars "{} ,;" g)
(* options state *)
@@ -194,8 +194,7 @@ let finalize_options () =
let spliter g =
List.map String.trim
- (String.split
- (fun c -> List.mem c ['{'; '}'; ' '; ','; ';']) g)
+ (String.slice_chars "{} ,;" g)
let itersplit f sss = List.iter f (spliter sss)
View
@@ -230,7 +230,7 @@ let get_resource_tracker ic =
else raise Unknown_cookie
let split_cookie str =
- List.map (fun x -> let a, b = String.split_char '=' x in ((String.trim a), b)) (String.split ((=) ';') str)
+ List.map (fun x -> let a, b = String.split_char '=' x in ((String.trim a), b)) (String.slice ';' str)
let collect_cookies (ics,ecs,ic_updts) =
List.iter (fun ec ->
@@ -198,7 +198,7 @@ let create hr =
{ hr with HST.hr_ec = ec; hr_ic = ic; hr_dt2 = dt2; }
let split_cookie str =
- List.map (fun x -> let a, b = String.split_char '=' x in ((String.trim a), b)) (String.split ((=) ';') str)
+ List.map (fun x -> let a, b = String.split_char '=' x in ((String.trim a), b)) (String.slice ';' str)
let collect_cookies sched (_,expired) =
List.iteri
@@ -87,7 +87,7 @@ let create() =
let id_of_cookie str =
(* let () = prerr_endline str in *)
- let cookies = String.split ((=)';') str |>
+ let cookies = String.slice ';' str |>
List.map
(fun x ->
let a, b = String.split_char '=' x in
@@ -49,7 +49,7 @@ let icfs plplst s ls n =
in
aux plplst
-let verlstch ch str = List.map int_of_string (String.split (function c -> c = ch) str)
+let verlstch ch str = List.map int_of_string (String.slice ch str)
let verlst = verlstch '.'
let verlstm = verlstch '-'
@@ -182,7 +182,7 @@ let get_compression_limit mime_type =
(* We find that the mime_type value actually includes the charset. It's
not supposed to but we can't do anything about that. We'll just have to
strip it out here. *)
- match String.split (function ';' -> true | _ -> false) mime_type with
+ match String.slice ';' mime_type with
| [] -> CL_Never
| mt::_ ->
(match StringMap.find_opt (String.trim mt) comp_lim_map with
@@ -168,7 +168,7 @@ let http_server_callback runtime (buf,pos_ref) =
| Some cb -> cb runtime.rt_proto.rt_payload !pos_ref buf
| None -> true
let split_cookie str =
- List.map (fun x -> let a, b = String.split_char '=' x in ((String.trim a), b)) (String.split ((=) ';') str)
+ List.map (fun x -> let a, b = String.split_char '=' x in ((String.trim a), b)) (String.slice ';' str)
}}
% FIXME: parse this...
View
@@ -41,7 +41,7 @@ let resolve_UNIX name =
try
(Unix.gethostbyname name).Unix.h_addr_list.(0)
|> Unix.string_of_inet_addr
- |> String.split ((=) '.')
+ |> String.slice '.'
|> List.map int_of_string
|> function [a;b;c;d] -> Some (a,b,c,d) | _ -> None
with Unix.Unix_error _ -> None
@@ -156,7 +156,7 @@ let resolve_UNIX name =
try
(Unix.gethostbyname name).Unix.h_addr_list.(0)
|> Unix.string_of_inet_addr
- |> String.split ((=) '.')
+ |> String.slice '.'
|> List.map int_of_string
|> function [a;b;c;d] -> Some (a,b,c,d) | _ -> None
with Not_found | Failure _ -> None
@@ -83,7 +83,7 @@ let sign_serialized_license data sym_key rsa_sig_privkey=
let check_license_data license_data sym_key rsa_sig_pubkey = try
let license_content = CL.symetric_decrypt (CL.from_hex sym_key) (CL.from_base64 license_data) in
let p1, p2 =
- let l = Base.String.split ((=) '\n') license_content in
+ let l = Base.String.slice '\n' license_content in
if List.length l = 2 then
let p1 = List.nth l 0
and p2 = List.nth l 1 in
@@ -94,36 +94,36 @@ let check_license_data license_data sym_key rsa_sig_pubkey = try
with C.Error _ | InvalidLicense -> None
let recover_license serialized_license signature =
- let l = Base.String.split ((=) ';') serialized_license in
+ let l = Base.String.slice ';' serialized_license in
if List.length l = 5 then
let lv = List.nth l 0
and lid = List.nth l 1
and ci = List.nth l 2
and pbi = List.nth l 3
and pii = List.nth l 4 in
- let cl = Base.String.split ((=) ',') ci
- and pbl = Base.String.split ((=) ',') pbi
- and pil = Base.String.split ((=) ',') pii in
+ let cl = Base.String.slice ',' ci
+ and pbl = Base.String.slice ',' pbi
+ and pil = Base.String.slice ',' pii in
let customer_info_data =
List.fold_left (
fun acc elt ->
- let kv = Base.String.split ((=) '=') elt in
+ let kv = Base.String.slice '=' elt in
let k = List.nth kv 0
and v = (CryptoLib.from_base64 (List.nth kv 1)) in
StringMap.add k v acc
) StringMap.empty cl
and product_bool_info_data =
List.fold_left (
fun acc elt ->
- let kv = Base.String.split ((=) '=') elt in
+ let kv = Base.String.slice '=' elt in
let k = List.nth kv 0
and v = List.nth kv 1 in
StringMap.add k (bool_of_string v) acc
) StringMap.empty pbl
and product_int_info_data =
List.fold_left (
fun acc elt ->
- let kv = Base.String.split ((=) '=') elt in
+ let kv = Base.String.slice '=' elt in
let k = List.nth kv 0
and v = List.nth kv 1 in
StringMap.add k (int_of_string v) acc
@@ -136,7 +136,7 @@ let system_uuid () = try
if Sys.file_exists sysprof then
let ic = Unix.open_process_in (Printf.sprintf "%s -detailLevel full SPHardwareDataType | grep UUID" sysprof) in
let huuid = input_line ic in
- let l = Base.String.split ((=) ':') huuid in
+ let l = Base.String.slice ':' huuid in
let uuid = Base.String.trim (List.nth l 1) in
uuid
else ""
@@ -103,7 +103,7 @@ end
(*
(* Transcript the type to a qml-well-parsed type in a generation of qml-code *)
- let module_spliter = String.split (fun c -> List.mem c ['~'; '/'; ','; '.'; '#'])
+ let module_spliter = String.slice_chars "~/,.#"
let ocaml_module_path_manager_from_string full pwd =
let pfull = module_spliter full and ppwd = module_spliter pwd in
@@ -261,7 +261,7 @@ module SharedChannel = struct
match s with
| "own" -> {o with kind = `server}
| _ ->
- let l = String.split (function ':' -> true | _ -> false) s in
+ let l = String.slice ':' s in
{ o with
kind = `client;
dir_endpoint =
Oops, something went wrong.

0 comments on commit 461365b

Please sign in to comment.