Skip to content

Commit cfb08a0

Browse files
committed
xapi-stdext-std: replace String.split_f with Astring functions
This also allows to drop String.isspace Signed-off-by: Pau Ruiz Safont <pau.ruizsafont@cloud.com> Signed-off-by: Pau Ruiz Safont <pau.safont@vates.tech>
1 parent 9678b4d commit cfb08a0

10 files changed

Lines changed: 34 additions & 114 deletions

File tree

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

Lines changed: 0 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -12,37 +12,6 @@
1212
* GNU Lesser General Public License for more details.
1313
*)
1414
module String = struct
15-
(** Returns true for whitespace characters, false otherwise *)
16-
let isspace = function ' ' | '\n' | '\r' | '\t' -> true | _ -> false
17-
18-
let split_f p str =
19-
let split_one seq =
20-
let not_p c = not (p c) in
21-
let a = Seq.take_while not_p seq in
22-
let b = Seq.drop_while not_p seq in
23-
(a, b)
24-
in
25-
let drop seq = Seq.drop_while p seq in
26-
let rec split acc chars =
27-
if Seq.is_empty chars then
28-
acc
29-
else
30-
let a, b = split_one chars in
31-
let b = drop b in
32-
let acc =
33-
if Seq.is_empty a then
34-
acc
35-
else
36-
Seq.cons a acc
37-
in
38-
split acc b
39-
in
40-
String.to_seq str
41-
|> split Seq.empty
42-
|> Seq.map String.of_seq
43-
|> List.of_seq
44-
|> List.rev
45-
4615
let sub_to_end s start =
4716
let length = String.length s in
4817
String.sub s start (length - start)

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

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,19 +12,11 @@
1212
* GNU Lesser General Public License for more details.
1313
*)
1414
module String : sig
15-
val isspace : char -> bool
16-
(** True if the character is whitespace *)
17-
1815
val replaced : replace:(char -> string option) -> string -> string
1916
(** [replaced ~replacement str] applies [replace] to all characters in [str]
2017
and when it returns [Some rep] the character is replaced with [rep] in
2118
the resulting string *)
2219

23-
val split_f : (char -> bool) -> string -> string list
24-
(** Take a predicate and a string, return a list of strings separated by
25-
runs of characters where the predicate was true. Avoid if possible, it's
26-
very costly to execute. *)
27-
2820
val split : limit:int -> char -> string -> string list
2921
(** split a string on a single char *)
3022

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

Lines changed: 1 addition & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -52,24 +52,6 @@ let test_split =
5252
in
5353
("split", tests)
5454

55-
let test_split_f =
56-
let specs =
57-
[
58-
(XString.isspace, "foo bar", ["foo"; "bar"])
59-
; (XString.isspace, "foo bar", ["foo"; "bar"])
60-
; (XString.isspace, "foo \n\t\r bar", ["foo"; "bar"])
61-
; (XString.isspace, " foo bar ", ["foo"; "bar"])
62-
; (XString.isspace, "", [])
63-
; (XString.isspace, " ", [])
64-
]
65-
in
66-
let test (splitter, splitted, expected) =
67-
let name = Printf.sprintf {|"%s"|} (String.escaped splitted) in
68-
test_list (XString.split_f splitter) (name, splitted, expected)
69-
in
70-
let tests = List.map test specs in
71-
("split_f", tests)
72-
7355
let test_rtrim =
7456
let spec =
7557
[
@@ -90,5 +72,4 @@ let test_rtrim =
9072
in
9173
("rtrim", List.map test spec)
9274

93-
let () =
94-
Alcotest.run "Xstringext" [test_split; test_split_f; test_rtrim]
75+
let () = Alcotest.run "Xstringext" [test_split; test_rtrim]

ocaml/tests/test_guest_agent.ml

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ module Networks = Generic.MakeStateless (struct
4242
)
4343

4444
let construct_tree tree path =
45-
let nodes =
46-
Xapi_stdext_std.Xstringext.String.split_f (fun s -> s = '/') path
47-
in
45+
let nodes = Astring.String.cuts ~empty:false ~sep:"/" path in
4846
add_path_to_tree tree nodes
4947

5048
let rec list_helper children = function
@@ -60,9 +58,7 @@ module Networks = Generic.MakeStateless (struct
6058
)
6159

6260
let list (T (_root, children)) path =
63-
let nodes =
64-
Xapi_stdext_std.Xstringext.String.split_f (fun s -> s = '/') path
65-
in
61+
let nodes = Astring.String.cuts ~empty:false ~sep:"/" path in
6662
list_helper children nodes
6763

6864
let transform input =
@@ -231,9 +227,7 @@ module Initial_guest_metrics = Generic.MakeStateless (struct
231227
)
232228

233229
let construct_mtree mtree (path, leaf_value) =
234-
let nodes =
235-
Xapi_stdext_std.Xstringext.String.split_f (fun s -> s = '/') path
236-
in
230+
let nodes = Astring.String.cuts ~empty:false ~sep:"/" path in
237231
add_leaf_to_mtree nodes leaf_value mtree
238232

239233
let rec list_helper children = function
@@ -254,9 +248,7 @@ module Initial_guest_metrics = Generic.MakeStateless (struct
254248
| Lf (_, _) ->
255249
[]
256250
| Mt (_, children) ->
257-
let nodes =
258-
Xapi_stdext_std.Xstringext.String.split_f (fun s -> s = '/') path
259-
in
251+
let nodes = Astring.String.cuts ~empty:false ~sep:"/" path in
260252
list_helper children nodes
261253

262254
let rec lookup_helper mtree = function
@@ -274,9 +266,7 @@ module Initial_guest_metrics = Generic.MakeStateless (struct
274266
)
275267

276268
let lookup mtree path =
277-
let nodes =
278-
Xapi_stdext_std.Xstringext.String.split_f (fun s -> s = '/') path
279-
in
269+
let nodes = Astring.String.cuts ~empty:false ~sep:"/" path in
280270
lookup_helper mtree nodes
281271

282272
let transform input =

ocaml/xapi/extauth_plugin_ADpbis.ml

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ module Lwsmd = struct
9292
)
9393
end
9494

95+
let is_word_sep = function '(' | ')' | ' ' | '\t' | '.' -> true | _ -> false
96+
9597
let match_error_tag (lines : string list) =
9698
let err_catch_list =
9799
[
@@ -105,8 +107,7 @@ let match_error_tag (lines : string list) =
105107
]
106108
in
107109
let split_to_words str =
108-
let seps = ['('; ')'; ' '; '\t'; '.'] in
109-
Stringext.split_f (fun s -> List.exists (fun sep -> sep = s) seps) str
110+
Astring.String.fields ~empty:false ~is_sep:is_word_sep str
110111
in
111112
let rec has_err lines err_pattern =
112113
match lines with
@@ -133,7 +134,7 @@ let extract_sid_from_group_list group_list =
133134
(fun (_, v) ->
134135
let v = Stringext.replace ")" "" v in
135136
let v = Stringext.replace "sid =" "|" v in
136-
let vs = Stringext.split_f (fun c -> c = '|') v in
137+
let vs = Astring.String.cuts ~empty:false ~sep:"|" v in
137138
let sid = String.trim (List.nth vs 1) in
138139
debug "extract_sid_from_group_list get sid=[%s]" sid ;
139140
sid
@@ -166,7 +167,7 @@ module AuthADlw : Auth_signature.AUTH_MODULE = struct
166167
Locking_helpers.Named_mutex.create "IS_SERVER_AVAILABLE"
167168

168169
let splitlines s =
169-
Stringext.split_f (fun c -> c = '\n') (Stringext.replace "#012" "\n" s)
170+
Astring.String.cuts ~empty:false ~sep:"\n" (Stringext.replace "#012" "\n" s)
170171

171172
let pbis_common_with_password (password : string) (pbis_cmd : string)
172173
(pbis_args : string list) =
@@ -350,9 +351,7 @@ module AuthADlw : Auth_signature.AUTH_MODULE = struct
350351
!exited_code
351352
(Stringext.replace "\n" ";" !output) ;
352353
let split_to_words s =
353-
Stringext.split_f
354-
(fun c -> c = '(' || c = ')' || c = '.' || c = ' ')
355-
s
354+
Astring.String.fields ~empty:false ~is_sep:is_word_sep s
356355
in
357356
let revlines =
358357
List.rev
@@ -624,7 +623,7 @@ module AuthADlw : Auth_signature.AUTH_MODULE = struct
624623
(* first, we try to authenticated user against our external user database *)
625624
(* pbis_common will raise an Auth_failure if external authentication fails *)
626625
let domain, user =
627-
match Stringext.split_f (fun c -> c = '\\') username with
626+
match Astring.String.cuts ~empty:false ~sep:"\\" username with
628627
| [domain; user] ->
629628
(domain, user)
630629
| [user] ->
@@ -989,7 +988,8 @@ module AuthADlw : Auth_signature.AUTH_MODULE = struct
989988
| "" ->
990989
[]
991990
| disabled_modules_string ->
992-
Stringext.split_f (fun c -> c = ',') disabled_modules_string
991+
Astring.String.cuts ~empty:false ~sep:","
992+
disabled_modules_string
993993
with Not_found -> []
994994
in
995995
let disabled_module_params =

ocaml/xapi/workload_balancing.ml

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -74,25 +74,12 @@ let raise_internal_error args =
7474
raise (Api_errors.Server_error (Api_errors.wlb_internal_error, args))
7575

7676
let split_host_port url =
77-
try
78-
if url.[0] = '[' then (
79-
(* IPv6 *)
80-
let host_end = String.rindex url ']' in
81-
if url.[host_end + 1] <> ':' then raise_url_invalid url ;
82-
let host = String.sub url 1 (host_end - 1) in
83-
let port =
84-
String.sub url (host_end + 2) (String.length url - host_end - 2)
85-
in
86-
(host, int_of_string port)
87-
) else
88-
match
89-
Xapi_stdext_std.Xstringext.String.split_f (fun a -> a = ':') url
90-
with
91-
| [host; port] ->
92-
(host, int_of_string port)
93-
| _ ->
94-
raise_url_invalid url
95-
with _ -> raise_url_invalid url
77+
let uri = Uri.of_string ("//" ^ url) in
78+
match (Uri.host uri, Uri.port uri) with
79+
| None, _ | _, None ->
80+
raise_url_invalid url
81+
| Some host, Some port ->
82+
(host, port)
9683

9784
let wlb_host_port ~__context =
9885
let pool = Helpers.get_pool ~__context in

ocaml/xapi/xapi_globs.ml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1390,12 +1390,12 @@ let citrix_patch_key =
13901390

13911391
let trusted_patch_key = ref citrix_patch_key
13921392

1393+
let fields_of = Astring.(String.fields ~empty:false ~is_sep:Char.Ascii.is_white)
1394+
13931395
let gen_list_option name desc of_string string_of opt =
13941396
let parse s =
13951397
opt := [] ;
1396-
try
1397-
Xapi_stdext_std.Xstringext.String.(split_f isspace s)
1398-
|> List.iter (fun x -> opt := of_string x :: !opt)
1398+
try fields_of s |> List.iter (fun x -> opt := of_string x :: !opt)
13991399
with e ->
14001400
D.error "Unable to parse %s=%s (expected space-separated list) error: %s"
14011401
name s (Printexc.to_string e)

ocaml/xapi/xapi_host_crashdump.ml

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616
*)
1717

1818
module Listext = Xapi_stdext_std.Listext
19-
module Xstringext = Xapi_stdext_std.Xstringext
2019
module Date = Clock.Date
2120
open Xapi_stdext_pervasives.Pervasiveext
2221
open Xapi_support
@@ -50,6 +49,8 @@ let delete_crashdump_dir filename =
5049
(ExnHelper.string_of_exn e) ;
5150
raise e
5251

52+
let fields_of = Astring.(String.fields ~empty:false ~is_sep:Char.Ascii.is_white)
53+
5354
(* Called once on host boot to resync the crash directory with the database *)
5455
let resynchronise ~__context ~host =
5556
debug "Xapi_host_crashdump.resynchronise" ;
@@ -103,9 +104,7 @@ let resynchronise ~__context ~host =
103104
debug "Adding record corresponding to new crashdump %s" filename ;
104105
let cmd = Printf.sprintf "%s --bytes -s %s/%s" du crash_dir filename in
105106
let size =
106-
match
107-
Xstringext.String.(split_f isspace (Helpers.get_process_output cmd))
108-
with
107+
match fields_of (Helpers.get_process_output cmd) with
109108
| size :: _ ->
110109
Int64.of_string size
111110
| _ ->

ocaml/xapi/xapi_pif.ml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ module L = Debug.Make (struct let name = "license" end)
1919

2020
open Xapi_database.Db_filter_types
2121
module Listext = Xapi_stdext_std.Listext.List
22-
module Stringext = Xapi_stdext_std.Xstringext.String
2322
module Date = Clock.Date
2423
open Network
2524

@@ -717,6 +716,8 @@ let forget ~__context ~self =
717716

718717
let scan_m = Mutex.create ()
719718

719+
let fields_of = Astring.(String.fields ~empty:false ~is_sep:Char.Ascii.is_white)
720+
720721
let scan ~__context ~host =
721722
let dbg = Context.string_of_task __context in
722723
refresh_all ~__context ~host ;
@@ -732,9 +733,9 @@ let scan ~__context ~host =
732733
debug "No boot from SAN interface found" ;
733734
([], [])
734735
| m :: u :: _ ->
735-
Stringext.(split_f isspace m, split_f isspace u)
736+
(fields_of m, fields_of u)
736737
| m :: _ ->
737-
Stringext.(split_f isspace m, [])
738+
(fields_of m, [])
738739
with e ->
739740
warn "Error when executing script %s: %s; ignoring"
740741
!Xapi_globs.non_managed_pifs

ocaml/xapi/xha_interface.ml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -349,9 +349,10 @@ module LiveSetInformation = struct
349349
| Some u ->
350350
u
351351
in
352-
let set f x =
353-
List.map f Xapi_stdext_std.Xstringext.String.(split_f isspace x)
352+
let fields_of =
353+
Astring.(String.fields ~empty:false ~is_sep:Char.Ascii.is_white)
354354
in
355+
let set f x = List.map f (fields_of x) in
355356
Some
356357
{
357358
id= uuid (find "HostID")

0 commit comments

Comments
 (0)