Skip to content

Commit b225a18

Browse files
author
Colin James
committed
Replace startswith and endswith with stdlib calls
Drops `startswith` and `endswith` from `String` (within `xapi-stdext/xapi-stdext-std`). These functions were introduced in OCaml 4.13: ``` String.starts_with : prefix:string -> string -> bool ``` Usage sites are adapted to supply the relevant labelled argument in each case, e.g. ~prefix:"foo". Signed-off-by: Colin James <[email protected]>
1 parent 8355506 commit b225a18

21 files changed

+34
-92
lines changed

ocaml/database/parse_db_conf.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ let parse_db_conf s =
168168
let connections : db_connection list ref = ref [] in
169169
while !lines <> [] do
170170
let line = List.hd !lines in
171-
if String.startswith "[" line then
171+
if String.starts_with ~prefix:"[" line then
172172
connections := read_block () :: !connections
173173
else
174174
consume_line ()

ocaml/idl/datamodel_utils.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ let find_self_parameter (msg : message) =
183183
)
184184

185185
let plural name =
186-
if Xstringext.String.endswith "metrics" name then
186+
if String.ends_with ~suffix:"metrics" name then
187187
name ^ " instances"
188188
else
189189
name ^ "s"

ocaml/libs/xapi-stdext/lib/xapi-stdext-std/xstringext.ml

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,6 @@ module String = struct
3939
done ;
4040
!accu
4141

42-
(** True if string 'x' ends with suffix 'suffix' *)
43-
let endswith suffix x =
44-
let x_l = String.length x and suffix_l = String.length suffix in
45-
suffix_l <= x_l && String.sub x (x_l - suffix_l) suffix_l = suffix
46-
47-
(** True if string 'x' starts with prefix 'prefix' *)
48-
let startswith prefix x =
49-
let x_l = String.length x and prefix_l = String.length prefix in
50-
prefix_l <= x_l && String.sub x 0 prefix_l = prefix
51-
5242
(** Returns true for whitespace characters, false otherwise *)
5343
let isspace = function ' ' | '\n' | '\r' | '\t' -> true | _ -> false
5444

ocaml/libs/xapi-stdext/lib/xapi-stdext-std/xstringext.mli

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ module String : sig
2929
val fold_right : (char -> 'a -> 'a) -> string -> 'a -> 'a
3030
(** Iterate over the characters in a string in reverse order. *)
3131

32-
val endswith : string -> string -> bool
33-
(** True if string 'x' ends with suffix 'suffix' *)
34-
35-
val startswith : string -> string -> bool
36-
(** True if string 'x' starts with prefix 'prefix' *)
37-
3832
val isspace : char -> bool
3933
(** True if the character is whitespace *)
4034

ocaml/libs/xapi-stdext/lib/xapi-stdext-std/xstringext_test.ml

Lines changed: 1 addition & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -127,44 +127,6 @@ let test_has_substr =
127127
in
128128
("has_substr", List.map test spec)
129129

130-
let test_startswith =
131-
let spec =
132-
[
133-
("", "", true)
134-
; ("", "foo bar", true)
135-
; ("foofo", "foof", false)
136-
; ("foof", "foof", true)
137-
; ("f", "foof", true)
138-
; ("fo", "foof", true)
139-
; ("of", "foof", false)
140-
; ("ff", "foof", false)
141-
]
142-
in
143-
let test (contained, container, expected) =
144-
let name = Printf.sprintf {|"%s" starts with "%s"|} container contained in
145-
test_boolean (XString.startswith contained) (name, container, expected)
146-
in
147-
("startswith", List.map test spec)
148-
149-
let test_endswith =
150-
let spec =
151-
[
152-
("", "", true)
153-
; ("", "foo bar", true)
154-
; ("ofoof", "foof", false)
155-
; ("foof", "foof", true)
156-
; ("f", "foof", true)
157-
; ("fo", "foof", false)
158-
; ("of", "foof", true)
159-
; ("ff", "foof", false)
160-
]
161-
in
162-
let test (contained, container, expected) =
163-
let name = Printf.sprintf {|"%s" ends with "%s"|} container contained in
164-
test_boolean (XString.endswith contained) (name, container, expected)
165-
in
166-
("endswith", List.map test spec)
167-
168130
let test_rtrim =
169131
let spec =
170132
[
@@ -187,12 +149,4 @@ let test_rtrim =
187149

188150
let () =
189151
Alcotest.run "Xstringext"
190-
[
191-
test_rev_map
192-
; test_split
193-
; test_split_f
194-
; test_has_substr
195-
; test_startswith
196-
; test_endswith
197-
; test_rtrim
198-
]
152+
[test_rev_map; test_split; test_split_f; test_has_substr; test_rtrim]

ocaml/mpathalert/mpathalert.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ let rec retry_with_session f rpc x =
9595
retry_with_session f rpc x
9696

9797
let keep_mpath =
98-
List.filter (fun (key, _) -> Xstringext.String.startswith "mpath-" key)
98+
List.filter (fun (key, _) -> String.starts_with ~prefix:"mpath-" key)
9999

100100
let create_alert ~host_uuid_string ~host_name ~pbd_uuid_string key value
101101
timestamp scsi_id =

ocaml/quicktest/qt.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ module VM = struct
122122
match
123123
List.filter
124124
(fun self ->
125-
Xapi_stdext_std.Xstringext.String.startswith startswith
125+
String.starts_with ~prefix:startswith
126126
(Client.Client.VM.get_name_label ~rpc ~session_id ~self)
127127
&& Client.Client.VM.get_is_a_template ~rpc ~session_id ~self
128128
)

ocaml/rrd2csv/src/rrd2csv.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,7 +282,7 @@ module Ds_selector = struct
282282
match the non-null fields of f *)
283283
let filter11 f d =
284284
true
285-
&& (Xstringext.String.startswith f.metric d.metric || f.metric = "")
285+
&& (String.starts_with ~prefix:f.metric d.metric || f.metric = "")
286286
&& (f.cf = d.cf || f.cf = None)
287287
&& ( match (f.owner, d.owner) with
288288
| None, _ ->
@@ -601,7 +601,7 @@ let print_last session_id data_sources =
601601
let filter_ds_that_starts_with_name dss name =
602602
List.fold_left
603603
(fun acc ds ->
604-
if Xstringext.String.startswith name ds.Ds_selector.metric then
604+
if String.starts_with ~prefix:name ds.Ds_selector.metric then
605605
ds :: acc
606606
else
607607
acc

ocaml/xapi/extauth_plugin_ADpbis.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -363,7 +363,7 @@ module AuthADlw : Auth_signature.AUTH_MODULE = struct
363363
let errcode =
364364
List.hd
365365
(List.filter
366-
(fun w -> String.startswith "LW_ERROR_" w)
366+
(fun w -> String.starts_with ~prefix:"LW_ERROR_" w)
367367
(split_to_words errcodeline)
368368
)
369369
in

ocaml/xapi/fileserver.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ let send_file (uri_base : string) (dir : string) (req : Request.t)
7878
let file_path =
7979
Xapi_stdext_unix.Unixext.resolve_dot_and_dotdot file_path
8080
in
81-
if not (String.startswith dir file_path) then (
81+
if not (String.starts_with ~prefix:dir file_path) then (
8282
debug "Rejecting request for file: %s (outside of directory %s)"
8383
file_path dir ;
8484
Http_svr.response_forbidden ~req s

ocaml/xapi/gpg.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ exception InvalidSignature
2828
let parse_gpg_status status_data =
2929
let lines = String.split '\n' status_data in
3030
let status_contains substr =
31-
List.exists (fun s -> String.startswith substr s) lines
31+
List.exists (fun s -> String.starts_with ~prefix:substr s) lines
3232
in
3333
if
3434
not
@@ -40,7 +40,7 @@ let parse_gpg_status status_data =
4040
let validsig = "[GNUPG:] VALIDSIG" in
4141
if status_contains validsig then
4242
let validsigline =
43-
List.find (fun s -> String.startswith validsig s) lines
43+
List.find (fun s -> String.starts_with ~prefix:validsig s) lines
4444
in
4545
match String.split ' ' validsigline with
4646
| _ :: _ :: fingerprint :: _ ->

ocaml/xapi/import.ml

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ let assert_can_restore_backup ~__context rpc session_id (x : header) =
189189
let get_vm_uuid_of_snap s =
190190
let snapshot_of = Ref.string_of s.API.vM_snapshot_of in
191191
try
192-
if Xstringext.String.startswith "Ref:" snapshot_of then
192+
if String.starts_with ~prefix:"Ref:" snapshot_of then
193193
(* This should be a snapshot in the archive *)
194194
let v =
195195
List.find
@@ -198,7 +198,7 @@ let assert_can_restore_backup ~__context rpc session_id (x : header) =
198198
in
199199
let v = get_vm_record v.snapshot in
200200
Some v.API.vM_uuid
201-
else if Xstringext.String.startswith Ref.ref_prefix snapshot_of then
201+
else if String.starts_with ~prefix:Ref.ref_prefix snapshot_of then
202202
(* This should be a snapshot in a live system *)
203203
if Db.is_valid_ref __context s.API.vM_snapshot_of then
204204
Some (Db.VM.get_uuid ~__context ~self:s.API.vM_snapshot_of)
@@ -2313,9 +2313,7 @@ let read_map_params name params =
23132313
(* include ':' *)
23142314
let filter_params =
23152315
List.filter
2316-
(fun (p, _) ->
2317-
Xstringext.String.startswith name p && String.length p > len
2318-
)
2316+
(fun (p, _) -> String.starts_with ~prefix:name p && String.length p > len)
23192317
params
23202318
in
23212319
List.map

ocaml/xapi/nm.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -215,7 +215,7 @@ let create_bond ~__context bond mtu persistent =
215215
let overrides =
216216
List.filter_map
217217
(fun (k, v) ->
218-
if String.startswith "bond-" k then
218+
if String.starts_with ~prefix:"bond-" k then
219219
Some (String.sub_to_end k 5, v)
220220
else
221221
None

ocaml/xapi/workload_balancing.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -233,7 +233,7 @@ let wlb_request host meth body encoded_auth =
233233
let filtered_headers headers =
234234
List.map
235235
(fun s ->
236-
if String.startswith "Authorization:" s then
236+
if String.starts_with ~prefix:"Authorization:" s then
237237
"Authorization: Basic <password>"
238238
else
239239
s

ocaml/xapi/xapi_network.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ let create ~__context ~name_label ~name_description ~mTU ~other_config ~bridge
252252
(not is_internal_session)
253253
&& (String.length bridge > 15
254254
|| List.exists
255-
(fun s -> String.startswith s bridge)
255+
(fun s -> String.starts_with ~prefix:s bridge)
256256
bridge_blacklist
257257
)
258258
then

ocaml/xapi/xapi_pif.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,7 @@ let refresh_all ~__context ~host =
132132
List.iter (fun self -> refresh_internal ~__context ~self) pifs
133133

134134
let bridge_naming_convention (device : string) =
135-
if String.startswith "eth" device then
135+
if String.starts_with ~prefix:"eth" device then
136136
"xenbr" ^ String.sub device 3 (String.length device - 3)
137137
else
138138
"br" ^ device

ocaml/xapi/xapi_pool.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2839,8 +2839,8 @@ let enable_external_auth ~__context ~pool:_ ~config ~service_name ~auth_type =
28392839
(Api_errors.auth_unknown_type, [msg_of_e])
28402840
)
28412841
| err_of_e
2842-
when Xstringext.String.startswith
2843-
Api_errors.auth_enable_failed err_of_e ->
2842+
when String.starts_with ~prefix:Api_errors.auth_enable_failed
2843+
err_of_e ->
28442844
raise
28452845
(Api_errors.Server_error
28462846
( Api_errors.pool_auth_prefix ^ err_of_e
@@ -2923,7 +2923,7 @@ let disable_external_auth ~__context ~pool:_ ~config =
29232923
debug
29242924
"Failed to disable the external authentication of at least one \
29252925
host in the pool" ;
2926-
if Xstringext.String.startswith Api_errors.auth_disable_failed err
2926+
if String.starts_with ~prefix:Api_errors.auth_disable_failed err
29272927
then (* tagged exception *)
29282928
raise
29292929
(Api_errors.Server_error

ocaml/xapi/xapi_pool_update.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -817,7 +817,7 @@ let pool_update_download_handler (req : Request.t) s _ =
817817
if host_uuid <> localhost_uuid then
818818
proxy_request req s host_uuid
819819
else if
820-
(not (String.startswith !Xapi_globs.host_update_dir filepath))
820+
(not (String.starts_with ~prefix:!Xapi_globs.host_update_dir filepath))
821821
|| not (Sys.file_exists filepath)
822822
then (
823823
debug

ocaml/xapi/xapi_secret.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ let clean_out_passwds ~__context strmap =
4141
Db.Secret.destroy ~__context ~self:s
4242
with _ -> ()
4343
in
44-
let check_key (k, _) = String.endswith "password_secret" k in
44+
let check_key (k, _) = String.ends_with ~suffix:"password_secret" k in
4545
let secrets = List.map snd (List.filter check_key strmap) in
4646
List.iter delete_secret secrets
4747

@@ -55,7 +55,7 @@ let copy ~__context ~secret =
5555
(* Modify a ((string * string) list) by duplicating all the passwords found in
5656
* it *)
5757
let duplicate_passwds ~__context strmap =
58-
let check_key k = String.endswith "password_secret" k in
58+
let check_key k = String.ends_with ~suffix:"password_secret" k in
5959
let possibly_duplicate (k, v) =
6060
if check_key k then
6161
let sr = Db.Secret.get_by_uuid ~__context ~uuid:v in
@@ -70,7 +70,7 @@ let duplicate_passwds ~__context strmap =
7070

7171
let move_passwds_to_secrets ~__context strmap =
7272
let maybe_move (k, value) =
73-
if String.endswith "password" k then (
73+
if String.ends_with ~suffix:"password" k then (
7474
let new_k = k ^ "_secret" in
7575
warn
7676
"Replacing deprecated %s with %s, please avoid using %s in \

ocaml/xapi/xapi_xenops.ml

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1643,7 +1643,11 @@ module Xenopsd_metadata = struct
16431643
(List.assoc Xapi_globs.persist_xenopsd_md oc)
16441644
|> Xapi_stdext_unix.Unixext.resolve_dot_and_dotdot
16451645
in
1646-
if not (String.startswith Xapi_globs.persist_xenopsd_md_root file_path)
1646+
if
1647+
not
1648+
(String.starts_with ~prefix:Xapi_globs.persist_xenopsd_md_root
1649+
file_path
1650+
)
16471651
then
16481652
warn "Not persisting xenopsd metadata to bad location: '%s'" file_path
16491653
else (
@@ -1893,7 +1897,7 @@ let update_vm ~__context id =
18931897
let results =
18941898
List.filter_map
18951899
(fun (path, _) ->
1896-
if String.startswith dir path then
1900+
if String.starts_with ~prefix:dir path then
18971901
let rest =
18981902
String.sub path (String.length dir)
18991903
(String.length path - String.length dir)

ocaml/xe-cli/newcli.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,9 @@ let rec read_rest_of_headers ic =
118118
debug "read '%s'\n" r ;
119119
let hdr =
120120
List.find
121-
(fun s -> String.startswith (s ^ ": ") (String.lowercase_ascii r))
121+
(fun s ->
122+
String.starts_with ~prefix:(s ^ ": ") (String.lowercase_ascii r)
123+
)
122124
hdrs
123125
in
124126
let value = end_of_string r (String.length hdr + 2) in

0 commit comments

Comments
 (0)