Skip to content

Commit 6a3853e

Browse files
committed
xapi-stdext-std: Replace String.has_substr with Astring's is_infix
Not only it's more efficient, but it's also more ergonomic Signed-off-by: Pau Ruiz Safont <pau.ruizsafont@cloud.com> Signed-off-by: Pau Ruiz Safont <pau.safont@vates.tech>
1 parent 59a5458 commit 6a3853e

8 files changed

Lines changed: 8 additions & 50 deletions

File tree

ocaml/libs/sexpr/sExpr.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ let is_escape_char = function '\\' | '\'' -> true | _ -> false
2727
(* XXX: This escapes "'c'" and "\'c\'" to "\\'c\\'".
2828
* They are both unescaped as "'c'". They have been ported
2929
* to make sure that this corner case is left unchanged.
30-
* It is worth investigating the use of
30+
* It is worth investigating the use of
3131
* - Astring.String.Ascii.escape_string
3232
* - Astring.String.Ascii.unescape
3333
* that have guaranteed invariants and optimised performances *)

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,17 +63,6 @@ module String = struct
6363
else
6464
s
6565

66-
(** has_substr str sub returns true if sub is a substring of str. Simple, naive, slow. *)
67-
let has_substr str sub =
68-
if String.length sub > String.length str then
69-
false
70-
else
71-
let result = ref false in
72-
for start = 0 to String.length str - String.length sub do
73-
if String.sub str start (String.length sub) = sub then result := true
74-
done ;
75-
!result
76-
7766
(** find all occurences of needle in haystack and return all their respective index *)
7867
let find_all needle haystack =
7968
let m = String.length needle and n = String.length haystack in

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

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,6 @@ module String : sig
3131
val rtrim : string -> string
3232
(** FIXME document me|remove me if similar to strip *)
3333

34-
val has_substr : string -> string -> bool
35-
(** True if sub is a substr of str *)
36-
3734
val replace : string -> string -> string -> string
3835
(** replace all [f] substring in [s] by [t] *)
3936

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

Lines changed: 1 addition & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@
1313

1414
module XString = Xapi_stdext_std.Xstringext.String
1515

16-
let test_boolean tested_f (name, case, expected) =
17-
let check () = Alcotest.(check bool) name expected (tested_f case) in
18-
(name, `Quick, check)
19-
2016
let test_string tested_f (name, case, expected) =
2117
let check () = Alcotest.(check string) name expected (tested_f case) in
2218
(name, `Quick, check)
@@ -74,26 +70,6 @@ let test_split_f =
7470
let tests = List.map test specs in
7571
("split_f", tests)
7672

77-
let test_has_substr =
78-
let spec =
79-
[
80-
("", "", true)
81-
; ("", "foo bar", true)
82-
; ("f", "foof", true)
83-
; ("foofo", "foof", false)
84-
; ("foof", "foof", true)
85-
; ("f", "foof", true)
86-
; ("fo", "foof", true)
87-
; ("of", "foof", true)
88-
; ("ff", "foof", false)
89-
]
90-
in
91-
let test (contained, container, expected) =
92-
let name = Printf.sprintf {|"%s" in "%s"|} contained container in
93-
test_boolean (XString.has_substr container) (name, contained, expected)
94-
in
95-
("has_substr", List.map test spec)
96-
9773
let test_rtrim =
9874
let spec =
9975
[
@@ -115,5 +91,4 @@ let test_rtrim =
11591
("rtrim", List.map test spec)
11692

11793
let () =
118-
Alcotest.run "Xstringext"
119-
[test_split; test_split_f; test_has_substr; test_rtrim]
94+
Alcotest.run "Xstringext" [test_split; test_split_f; test_rtrim]

ocaml/quicktest/quicktest_http.ml

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ module Cookies = struct
102102
match body with
103103
| first_line :: _ ->
104104
D.warn "expected = [%s]; received = [%s]" expected first_line ;
105-
Xapi_stdext_std.Xstringext.String.has_substr first_line expected
105+
Astring.String.is_infix ~affix:first_line expected
106106
| _ ->
107107
false
108108
in
@@ -213,9 +213,7 @@ module HTML_Escaping = struct
213213
let bad_command_exp = "&lt;&gt;&apos;\\&quot;&amp;"
214214

215215
let html_escaping expected cmd =
216-
let check_result b =
217-
Xapi_stdext_std.Xstringext.String.has_substr b expected
218-
in
216+
let check_result = Astring.String.is_infix ~affix:expected in
219217
let _, _, _, body = Uds.http_command Xapi_globs.unix_domain_socket cmd in
220218
match body with
221219
| first_line :: _ ->

ocaml/xapi/extauth_plugin_ADpbis.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,7 @@ module AuthADlw : Auth_signature.AUTH_MODULE = struct
238238
pbis_cmd ^ " " ^ List.fold_left (fun p pp -> p ^ " " ^ pp) " " pbis_args
239239
in
240240
let debug_cmd =
241-
if Stringext.has_substr debug_cmd "--password" then
241+
if Astring.String.is_infix ~affix:"--password" debug_cmd then
242242
"(omitted for security)"
243243
else
244244
debug_cmd

ocaml/xapi/map_check.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ let assert_value ~field ~key ~attr ~value =
118118
| Some v ->
119119
if acc = "" then
120120
v
121-
else if Xapi_stdext_std.Xstringext.String.has_substr acc v then
121+
else if Astring.String.is_infix ~affix:v acc then
122122
err value
123123
else
124124
v ^ "," ^ acc

ocaml/xapi/rbac_audit.ml

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -368,8 +368,7 @@ and
368368

369369
let has_to_audit action =
370370
let has_side_effect action =
371-
not (Xapi_stdext_std.Xstringext.String.has_substr action ".get")
372-
(* TODO: a bit slow? *)
371+
not (Astring.String.is_infix ~affix:".get" action)
373372
in
374373
(!Xapi_globs.log_getter || has_side_effect action)
375374
&& not
@@ -480,7 +479,7 @@ let allowed_pre_fn ~__context ~action ?args () =
480479
if
481480
has_to_audit action
482481
(* for now, we only cache arg results for destroy actions *)
483-
&& Xapi_stdext_std.Xstringext.String.has_substr action ".destroy"
482+
&& Astring.String.is_infix ~affix:".destroy" action
484483
then
485484
let args' = add_dummy_args __context action args in
486485
Some (sexpr_of_parameters __context action args')

0 commit comments

Comments
 (0)