Skip to content

Commit 1b21740

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 <[email protected]> Signed-off-by: Pau Ruiz Safont <[email protected]>
1 parent 8438123 commit 1b21740

File tree

10 files changed

+32
-73
lines changed

10 files changed

+32
-73
lines changed

ocaml/idl/ocaml_backend/gen_rbac.ml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ let permission_description = "A basic permission"
7272
let permission_name wire_name =
7373
let s1 = replace_char (Printf.sprintf "permission_%s" wire_name) '.' '_' in
7474
let s2 = replace_char s1 '/' '_' in
75-
let s3 = Xapi_stdext_std.Xstringext.String.replace "*" "WILDCHAR" s2 in
75+
let s3 = Xapi_stdext_std.Xstringext.String.replace '*' ~by:"WILDCHAR" s2 in
7676
replace_char s3 ':' '_'
7777

7878
let permission_index = ref 0

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

Lines changed: 5 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,10 @@ 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 = if replaceable c then Some by else None in
58+
map_unlikely s get_replacement
59+
9560
let replaced ~replace s = map_unlikely s replace
9661
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/rrd2csv/src/rrd2csv.ml

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -185,14 +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-
)
195-
)
188+
quote (Xstringext.String.replace '"' ~by:{|""|} s)
196189
else if String.contains s ',' || String.contains s '\n' then
197190
quote s
198191
else

ocaml/xapi/audit_log.ml

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

104104
(* map the ISO8601 timestamp format into the one in our logs *)
105-
let log_timestamp_of_iso8601 iso8601_timestamp =
106-
let module Xstringext = Xapi_stdext_std.Xstringext in
107-
let step1 = iso8601_timestamp in
108-
let step2 = Xstringext.String.replace "-" "" step1 in
109-
let step3 = Xstringext.String.replace "Z" "" step2 in
110-
step3
105+
let log_timestamp_of_iso8601 iso8601 =
106+
Astring.String.filter (function '-' | 'Z' -> false | _ -> true) iso8601
111107

112108
(*
113109
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
@@ -1115,8 +1118,8 @@ module AuthADlw : Auth_signature.AUTH_MODULE = struct
11151118
in
11161119
debug "execute %s: stdout=[%s],stderr=[%s]"
11171120
pbis_force_domain_leave_script
1118-
(Stringext.replace "\n" ";" output)
1119-
(Stringext.replace "\n" ";" stderr)
1121+
(Stringext.replace '\n' ~by:";" output)
1122+
(Stringext.replace '\n' ~by:";" stderr)
11201123
with e ->
11211124
debug "exception executing %s: %s" pbis_force_domain_leave_script
11221125
(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
@@ -453,9 +453,9 @@ let audit_line_of __context session_id allowed_denied ok_error result_error
453453
?sexpr_of_args action permission
454454
)
455455
in
456-
let line = Xapi_stdext_std.Xstringext.String.replace "\n" " " _line in
456+
let line = Xapi_stdext_std.Xstringext.String.replace '\n' ~by:" " _line in
457457
(* no \n in line *)
458-
let line = Xapi_stdext_std.Xstringext.String.replace "\r" " " line in
458+
let line = Xapi_stdext_std.Xstringext.String.replace '\r' ~by:" " line in
459459
(* no \r in line *)
460460
let audit_line = append_line "%s" line in
461461
(*D.debug "line=%s, audit_line=%s" line audit_line;*)

ocaml/xapi/storage_mux.ml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,8 @@ module Mux = struct
9797

9898
let m = Mutex.create ()
9999

100-
let filename_of dp = Xapi_stdext_std.Xstringext.String.replace "/" "-" dp
100+
let filename_of dp =
101+
Xapi_stdext_std.Xstringext.String.replace '/' ~by:"-" dp
101102

102103
let write dp info =
103104
let filename = filename_of dp in

0 commit comments

Comments
 (0)