Skip to content

Commit

Permalink
build cleanly with newer extlib (labelled String.starts_with et al)
Browse files Browse the repository at this point in the history
  • Loading branch information
rr0gi committed Feb 14, 2024
1 parent 2068e04 commit 712c9b1
Show file tree
Hide file tree
Showing 8 changed files with 19 additions and 16 deletions.
2 changes: 1 addition & 1 deletion httpev.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions log.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
2 changes: 1 addition & 1 deletion lwt_mark.ml
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ let orphan_mark = special "<orphan>"
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
Expand Down
2 changes: 1 addition & 1 deletion memory.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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; }

Expand Down
10 changes: 5 additions & 5 deletions nix.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion static_config.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 7 additions & 4 deletions stre.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion test.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 712c9b1

Please sign in to comment.