Skip to content

Commit e41ce96

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 1174f3a commit e41ce96

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
@@ -28,7 +28,7 @@ let is_escape_char = function '\\' | '\'' -> true | _ -> false
2828
(* XXX: This escapes "'c'" and "\'c\'" to "\\'c\\'".
2929
* They are both unescaped as "'c'". They have been ported
3030
* to make sure that this corner case is left unchanged.
31-
* It is worth investigating the use of
31+
* It is worth investigating the use of
3232
* - Astring.String.Ascii.escape_string
3333
* - Astring.String.Ascii.unescape
3434
* 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
@@ -58,17 +58,6 @@ module String = struct
5858
else
5959
s
6060

61-
(** has_substr str sub returns true if sub is a substring of str. Simple, naive, slow. *)
62-
let has_substr str sub =
63-
if String.length sub > String.length str then
64-
false
65-
else
66-
let result = ref false in
67-
for start = 0 to String.length str - String.length sub do
68-
if String.sub str start (String.length sub) = sub then result := true
69-
done ;
70-
!result
71-
7261
(** find all occurences of needle in haystack and return all their respective index *)
7362
let find_all needle haystack =
7463
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
@@ -99,7 +99,7 @@ module Cookies = struct
9999
match body with
100100
| first_line :: _ ->
101101
D.warn "expected = [%s]; received = [%s]" expected first_line ;
102-
Xapi_stdext_std.Xstringext.String.has_substr first_line expected
102+
Astring.String.is_infix ~affix:first_line expected
103103
| _ ->
104104
false
105105
in
@@ -210,9 +210,7 @@ module HTML_Escaping = struct
210210
let bad_command_exp = "&lt;&gt;&apos;\\&quot;&amp;"
211211

212212
let html_escaping expected cmd =
213-
let check_result b =
214-
Xapi_stdext_std.Xstringext.String.has_substr b expected
215-
in
213+
let check_result = Astring.String.is_infix ~affix:expected in
216214
let _, _, _, body = Uds.http_command Xapi_globs.unix_domain_socket cmd in
217215
match body with
218216
| 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
@@ -113,7 +113,7 @@ let assert_value ~field ~key ~attr ~value =
113113
| Some v ->
114114
if acc = "" then
115115
v
116-
else if Xapi_stdext_std.Xstringext.String.has_substr acc v then
116+
else if Astring.String.is_infix ~affix:v acc then
117117
err value
118118
else
119119
v ^ "," ^ acc

ocaml/xapi/rbac_audit.ml

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

363363
let has_to_audit action =
364364
let has_side_effect action =
365-
not (Xapi_stdext_std.Xstringext.String.has_substr action ".get")
366-
(* TODO: a bit slow? *)
365+
not (Astring.String.is_infix ~affix:".get" action)
367366
in
368367
(!Xapi_globs.log_getter || has_side_effect action)
369368
&& not
@@ -471,7 +470,7 @@ let allowed_pre_fn ~__context ~action ?args () =
471470
if
472471
has_to_audit action
473472
(* for now, we only cache arg results for destroy actions *)
474-
&& Xapi_stdext_std.Xstringext.String.has_substr action ".destroy"
473+
&& Astring.String.is_infix ~affix:".destroy" action
475474
then
476475
let args' = add_dummy_args __context action args in
477476
Some (sexpr_of_parameters __context action args')

0 commit comments

Comments
 (0)