Skip to content

Commit 75e79fb

Browse files
committed
xapi-stdext-std: change String.replace to replace characters
The few users that needed to replace strings, have been replaced with Astring's cuts, as most of them were already segmenting strings, or they are run in very specific, infrequent codepaths for efficiency to not matter. Others have been replaced by Astring's filter as they were removing characters, and the rest have been converted to the new String.replace. map_unlikely can be removed from the interface and only have String.replaced and String.replace Signed-off-by: Pau Ruiz Safont <pau.ruizsafont@cloud.com> Signed-off-by: Pau Ruiz Safont <pau.safont@vates.tech>
1 parent 70604a0 commit 75e79fb

11 files changed

Lines changed: 48 additions & 83 deletions

File tree

ocaml/idl/ocaml_backend/gen_rbac.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ let permission_description = "A basic permission"
8585
let permission_name wire_name =
8686
let s1 = replace_char (Printf.sprintf "permission_%s" wire_name) '.' '_' in
8787
let s2 = replace_char s1 '/' '_' in
88-
let s3 = Xapi_stdext_std.Xstringext.String.replace "*" "WILDCHAR" s2 in
88+
let s3 = Xapi_stdext_std.Xstringext.String.replace '*' ~by:"WILDCHAR" s2 in
8989
replace_char s3 ':' '_'
9090

9191
let permission_index = ref 0

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

Lines changed: 10 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -32,46 +32,6 @@ module String = struct
3232
else
3333
s
3434

35-
(** find all occurences of needle in haystack and return all their respective index *)
36-
let find_all needle haystack =
37-
let m = String.length needle and n = String.length haystack in
38-
if m > n then
39-
[]
40-
else
41-
let i = ref 0 and found = ref [] in
42-
while !i < n - m + 1 do
43-
if String.sub haystack !i m = needle then (
44-
found := !i :: !found ;
45-
i := !i + m
46-
) else
47-
incr i
48-
done ;
49-
List.rev !found
50-
51-
(* replace all @f substring in @s by @t *)
52-
let replace f t s =
53-
let indexes = find_all f s in
54-
let n = List.length indexes in
55-
if n > 0 then (
56-
let len_f = String.length f and len_t = String.length t in
57-
let new_len = String.length s + (n * len_t) - (n * len_f) in
58-
let new_b = Bytes.make new_len '\000' in
59-
let orig_offset = ref 0 and dest_offset = ref 0 in
60-
List.iter
61-
(fun h ->
62-
let len = h - !orig_offset in
63-
Bytes.blit_string s !orig_offset new_b !dest_offset len ;
64-
Bytes.blit_string t 0 new_b (!dest_offset + len) len_t ;
65-
orig_offset := !orig_offset + len + len_f ;
66-
dest_offset := !dest_offset + len + len_t
67-
)
68-
indexes ;
69-
Bytes.blit_string s !orig_offset new_b !dest_offset
70-
(String.length s - !orig_offset) ;
71-
Bytes.unsafe_to_string new_b
72-
) else
73-
s
74-
7535
let map_unlikely s f =
7636
let changed = ref false in
7737
let m = ref 0 in
@@ -92,5 +52,15 @@ module String = struct
9252
) else
9353
s
9454

55+
let replace char ~by s =
56+
let replaceable = Stdlib.Char.equal char in
57+
let get_replacement c =
58+
if replaceable c then
59+
Some by
60+
else
61+
None
62+
in
63+
map_unlikely s get_replacement
64+
9565
let replaced ~replace s = map_unlikely s replace
9666
end

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

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,16 @@ module String : sig
1717
and when it returns [Some rep] the character is replaced with [rep] in
1818
the resulting string *)
1919

20+
val replace : char -> by:string -> string -> string
21+
(** [replace ch ~by s] replaces all the occurrences of [ch] in [s] by [~by]
22+
*)
23+
2024
val split : limit:int -> char -> string -> string list
2125
(** split a string on a single char *)
2226

2327
val rtrim : string -> string
2428
(** FIXME document me|remove me if similar to strip *)
2529

26-
val replace : string -> string -> string -> string
27-
(** replace all [f] substring in [s] by [t] *)
28-
29-
val map_unlikely : string -> (char -> string option) -> string
30-
(** map a string trying to fill the buffer by chunk *)
31-
3230
val sub_to_end : string -> int -> string
3331
(** a substring from the specified position to the end of the string *)
3432
end

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

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -75,23 +75,23 @@ let test_rtrim =
7575
let test_replace =
7676
let spec =
7777
[
78-
(("*", "WILDCHAR", ""), "")
79-
; (("*", "", "*"), "")
80-
; (("*", "WILDCHAR", "*"), "WILDCHAR")
81-
; (("*", "WILDCHAR", "**"), "WILDCHARWILDCHAR")
82-
; (("*", "WILDCHAR", "***"), "WILDCHARWILDCHARWILDCHAR")
83-
; (({|"|}, "", ""), "")
84-
; (({|"|}, "", "a"), "a")
85-
; (({|"|}, "", {|"a"|}), "a")
86-
; (({|"|}, "", {|a"a|}), "aa")
78+
(('*', "WILDCHAR", ""), "")
79+
; (('*', "", "*"), "")
80+
; (('*', "WILDCHAR", "*"), "WILDCHAR")
81+
; (('*', "WILDCHAR", "**"), "WILDCHARWILDCHAR")
82+
; (('*', "WILDCHAR", "***"), "WILDCHARWILDCHARWILDCHAR")
83+
; (('"', "", ""), "")
84+
; (('"', "", "a"), "a")
85+
; (('"', "", {|"a"|}), "a")
86+
; (('"', "", {|a"a|}), "aa")
8787
]
8888
in
8989
let test ((char, by, case), expected) =
9090
let name =
91-
Printf.sprintf "replace %S by %S in %S is %S" char (String.escaped by)
91+
Printf.sprintf "replace '%c' by %S in %S is %S" char (String.escaped by)
9292
(String.escaped case) (String.escaped expected)
9393
in
94-
test_string (XString.replace char by) (name, case, expected)
94+
test_string (XString.replace char ~by) (name, case, expected)
9595
in
9696
("replace", List.map test spec)
9797

ocaml/rrd2csv/src/rrd2csv.ml

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -185,13 +185,7 @@ module Ds_selector = struct
185185
let escape_metric s =
186186
let quote s = Printf.sprintf "\"%s\"" s in
187187
if String.contains s '"' then
188-
quote
189-
(Xstringext.String.map_unlikely s (function
190-
| '\"' ->
191-
Some "\"\""
192-
| _ ->
193-
None
194-
))
188+
quote (Xstringext.String.replace '"' ~by:{|""|} s)
195189
else if String.contains s ',' || String.contains s '\n' then
196190
quote s
197191
else

ocaml/xapi/audit_log.ml

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -113,12 +113,8 @@ let transfer_all_audit_files fd_out ?filter since =
113113
atransfer_try_gz ""
114114

115115
(* map the ISO8601 timestamp format into the one in our logs *)
116-
let log_timestamp_of_iso8601 iso8601_timestamp =
117-
let module Xstringext = Xapi_stdext_std.Xstringext in
118-
let step1 = iso8601_timestamp in
119-
let step2 = Xstringext.String.replace "-" "" step1 in
120-
let step3 = Xstringext.String.replace "Z" "" step2 in
121-
step3
116+
let log_timestamp_of_iso8601 iso8601 =
117+
Astring.String.filter (function '-' | 'Z' -> false | _ -> true) iso8601
122118

123119
(*
124120
Assume that RBAC access for the session_id already verified by xapi_http.ml

ocaml/xapi/extauth_plugin_ADpbis.ml

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,9 +132,11 @@ let match_error_tag (lines : string list) =
132132
let extract_sid_from_group_list group_list =
133133
List.map
134134
(fun (_, v) ->
135-
let v = Stringext.replace ")" "" v in
136-
let v = Stringext.replace "sid =" "|" v in
137-
let vs = Astring.String.cuts ~empty:false ~sep:"|" v in
135+
let vs =
136+
Astring.String.filter (function ')' -> false | _ -> true) v
137+
|> Astring.String.cuts ~empty:false ~sep:"sid ="
138+
|> List.concat_map (Astring.String.cuts ~empty:false ~sep:"|")
139+
in
138140
let sid = String.trim (List.nth vs 1) in
139141
debug "extract_sid_from_group_list get sid=[%s]" sid ;
140142
sid
@@ -167,7 +169,8 @@ module AuthADlw : Auth_signature.AUTH_MODULE = struct
167169
Locking_helpers.Named_mutex.create "IS_SERVER_AVAILABLE"
168170

169171
let splitlines s =
170-
Astring.String.cuts ~empty:false ~sep:"\n" (Stringext.replace "#012" "\n" s)
172+
Astring.String.cuts ~empty:false ~sep:"#012" s
173+
|> List.concat_map (Astring.String.cuts ~empty:false ~sep:"\n")
171174

172175
let pbis_common_with_password (password : string) (pbis_cmd : string)
173176
(pbis_args : string list) =
@@ -349,7 +352,7 @@ module AuthADlw : Auth_signature.AUTH_MODULE = struct
349352
if !exited_code <> 0 then (
350353
error "execute '%s': exit_code=[%d] output=[%s]" debug_cmd
351354
!exited_code
352-
(Stringext.replace "\n" ";" !output) ;
355+
(Stringext.replace '\n' ~by:";" !output) ;
353356
let split_to_words s =
354357
Astring.String.fields ~empty:false ~is_sep:is_word_sep s
355358
in
@@ -1126,8 +1129,8 @@ module AuthADlw : Auth_signature.AUTH_MODULE = struct
11261129
in
11271130
debug "execute %s: stdout=[%s],stderr=[%s]"
11281131
pbis_force_domain_leave_script
1129-
(Stringext.replace "\n" ";" output)
1130-
(Stringext.replace "\n" ";" stderr)
1132+
(Stringext.replace '\n' ~by:";" output)
1133+
(Stringext.replace '\n' ~by:";" stderr)
11311134
with e ->
11321135
debug "exception executing %s: %s" pbis_force_domain_leave_script
11331136
(ExnHelper.string_of_exn e)

ocaml/xapi/pciops.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,8 @@ let _unhide_pci ~__context pci =
126126
Printf.sprintf "(%s)" (Db.PCI.get_pci_id ~__context ~self:pci)
127127
in
128128
let new_value =
129-
Xapi_stdext_std.Xstringext.String.replace bdf_paren "" raw_value
129+
Astring.String.cuts ~empty:false ~sep:bdf_paren raw_value
130+
|> String.concat ""
130131
in
131132
let cmd =
132133
match new_value with

ocaml/xapi/pvs_proxy_control.ml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ open D
2121
let proxy_port_name vif =
2222
(* Interface names in Linux are at most 15 characters. We derive a
2323
name from the MAC address to ensure uniqueness, and make it fit. *)
24-
let mac = Xapi_stdext_std.Xstringext.String.replace ":" "" vif.API.vIF_MAC in
24+
let mac =
25+
Astring.String.filter (function ':' -> false | _ -> true) vif.API.vIF_MAC
26+
in
2527
Printf.sprintf "pvs%s" mac
2628

2729
(** [proxies] returns all currently attached proxies *)

ocaml/xapi/rbac_audit.ml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -462,9 +462,9 @@ let audit_line_of __context session_id allowed_denied ok_error result_error
462462
?sexpr_of_args action permission
463463
)
464464
in
465-
let line = Xapi_stdext_std.Xstringext.String.replace "\n" " " _line in
465+
let line = Xapi_stdext_std.Xstringext.String.replace '\n' ~by:" " _line in
466466
(* no \n in line *)
467-
let line = Xapi_stdext_std.Xstringext.String.replace "\r" " " line in
467+
let line = Xapi_stdext_std.Xstringext.String.replace '\r' ~by:" " line in
468468
(* no \r in line *)
469469
let audit_line = append_line "%s" line in
470470
(*D.debug "line=%s, audit_line=%s" line audit_line;*)

0 commit comments

Comments
 (0)