From 712c9b1942b4dd6eab02609c715a8e8eb96d92be Mon Sep 17 00:00:00 2001 From: ip Date: Wed, 14 Feb 2024 15:17:39 -0500 Subject: [PATCH] build cleanly with newer extlib (labelled String.starts_with et al) --- httpev.ml | 2 +- log.ml | 4 ++-- lwt_mark.ml | 2 +- memory.ml | 2 +- nix.ml | 10 +++++----- static_config.ml | 2 +- stre.ml | 11 +++++++---- test.ml | 2 +- 8 files changed, 19 insertions(+), 16 deletions(-) diff --git a/httpev.ml b/httpev.ml index 948cd92..1e6b847 100644 --- a/httpev.ml +++ b/httpev.ml @@ -261,7 +261,7 @@ let is_body_ready { line1; content_length; parsed_headers=_; buf; } final = match body_len - length with | 0 -> true (* workaround MSIE 6 *) - | 2 when String.starts_with line1 "POST" && body.[body_len - 2] = '\r' && body.[body_len - 1] = '\n' -> + | 2 when Stre.starts_with line1 "POST" && body.[body_len - 2] = '\r' && body.[body_len - 1] = '\n' -> Buffer.clear buf; Buffer.add_string buf (Stre.slice ~last:(-2) body); true diff --git a/log.ml b/log.ml index 9d619ca..51574cd 100644 --- a/log.ml +++ b/log.ml @@ -58,9 +58,9 @@ module State = struct let set_filter ?name level = match name with | None -> default_level := level; Hashtbl.iter (fun _ x -> Logger.set_filter x level) all - | Some name when String.ends_with name "*" -> + | Some name when Stre.ends_with name "*" -> let prefix = String.slice ~last:(-1) name in - Hashtbl.iter (fun k x -> if String.starts_with k prefix then Logger.set_filter x level) all + Hashtbl.iter (fun k x -> if Stre.starts_with k prefix then Logger.set_filter x level) all | Some name -> Logger.set_filter (facility name) level let set_loglevels s = diff --git a/lwt_mark.ml b/lwt_mark.ml index e32b389..72e347f 100644 --- a/lwt_mark.ml +++ b/lwt_mark.ml @@ -102,7 +102,7 @@ let orphan_mark = special "" let log_add_line mark msg = let msg = lazy begin let msg = !!msg in - if String.ends_with msg "\n" then msg else msg ^ "\n" + if Stre.ends_with msg "\n" then msg else msg ^ "\n" end in LastN.add msg mark.logs diff --git a/memory.ml b/memory.ml index 08b8c88..c92886b 100644 --- a/memory.ml +++ b/memory.ml @@ -49,7 +49,7 @@ let get_vm_info ?(swap=false) () = | false -> 0 | true -> Action.file_lines ("/proc/self/smaps") |> - List.fold_left (fun acc s -> if String.starts_with s "Swap:" then acc + get_num s else acc) 0 + List.fold_left (fun acc s -> if Stre.starts_with s "Swap:" then acc + get_num s else acc) 0 in { rss; vsize; nr_maps; swap_used = swap_used * 1024; } diff --git a/nix.ml b/nix.ml index a730b3e..4f9b3b1 100644 --- a/nix.ml +++ b/nix.ml @@ -144,7 +144,7 @@ let inet_addr_of_string s = let unix_addr_of_string s = let open Unix in - if String.starts_with s "unix:" then + if Stre.starts_with s "unix:" then ADDR_UNIX (String.slice ~first:5 s) else Exn.fail "invalid UNIX addr %S" s @@ -236,14 +236,14 @@ let mounts () = (** @param path must be normalized *) let find_mount path = assert (not @@ Filename.is_relative path); - assert (not @@ String.exists path "//"); - assert (not @@ String.exists path "/./"); - assert (not @@ String.exists path "/../"); + assert (not @@ Stre.exists path "//"); + assert (not @@ Stre.exists path "/./"); + assert (not @@ Stre.exists path "/../"); let mount = ref ("","",[]) in let bound x = let (_,b,_) = x in b in mounts () |> List.iter (fun (_,bind,_ as part) -> - if String.starts_with path bind && String.length bind > String.length (bound !mount) then + if Stre.starts_with path bind && String.length bind > String.length (bound !mount) then mount := part); assert (bound !mount <> ""); !mount diff --git a/static_config.ml b/static_config.ml index 4d8f0e1..33d6085 100644 --- a/static_config.ml +++ b/static_config.ml @@ -151,7 +151,7 @@ let show ?(all=false) root = iter begin fun name v -> match String.fold_left (fun n c -> if c = '\n' then n + 1 else n) 0 v with | 0 -> - if String.starts_with v " " || String.ends_with v " " then + if Stre.starts_with v " " || Stre.ends_with v " " then begin match choose_quote v with | None -> bprintf b "%s :%d\n%s\n" name 0 v | Some c -> bprintf b "%s := %c%s%c\n" name c v c diff --git a/stre.ml b/stre.ml index 99ff7d8..ddf3e3e 100644 --- a/stre.ml +++ b/stre.ml @@ -99,10 +99,6 @@ let after s sep = try String.(let i = find s sep + length sep in sub s i (length let divide s sep = try String.split s sep with Invalid_string -> s, "" let dividec s sep = try splitc s sep with Not_found -> s, "" -(** remove prefix from string if present *) -let drop_prefix s pre = if String.starts_with s pre then slice s ~first:(String.length pre) else s -let drop_suffix s suf = if String.ends_with s suf then slice ~last:(- String.length suf) s else s - let qreplace str sub by = Pcre.qreplace ~rex:(Pcre.regexp @@ Pcre.quote sub) ~templ:by str @@ -127,6 +123,13 @@ let starts_with s ?(pos=0) prefix = with Not_found -> false +let ends_with s suffix = String.ends_with s suffix[@warning "-6"] +let exists s sub = String.exists s sub[@warning "-6"] + +(** remove prefix from string if present *) +let drop_prefix s pre = if starts_with s pre then slice s ~first:(String.length pre) else s +let drop_suffix s suf = if ends_with s suf then slice ~last:(- String.length suf) s else s + let istarts_with s ?(pos=0) prefix = if pos < 0 || pos > String.length s then invalid_arg "Stre.istarts_with"; if String.length s < pos + String.length prefix then false else diff --git a/test.ml b/test.ml index 6518423..d95dd19 100644 --- a/test.ml +++ b/test.ml @@ -115,7 +115,7 @@ let () = test "Stre.before" begin fun () -> let invariant s sub = let a = Stre.before s sub in let b = Stre.after s sub in - t ~msg:(sprintf "invariant1 %S %S" s sub) s (a ^ (if String.exists s sub then sub else "") ^ b); + t ~msg:(sprintf "invariant1 %S %S" s sub) s (a ^ (if Stre.exists s sub then sub else "") ^ b); t ~msg:(sprintf "invariant2a %S %S" s sub) a (Stre.before (a ^ sub ^ b) sub); t ~msg:(sprintf "invariant2b %S %S" s sub) b (Stre.after (a ^ sub ^ b) sub); assert_equal ~msg:(sprintf "divide %S %S" s sub) (a,b) (Stre.divide s sub);