Skip to content

Commit 8648ebb

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 dd040a9 commit 8648ebb

10 files changed

Lines changed: 34 additions & 110 deletions

File tree

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

Lines changed: 0 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,32 +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 = if Seq.is_empty a then acc else Seq.cons a acc in
33-
split acc b
34-
in
35-
String.to_seq str
36-
|> split Seq.empty
37-
|> Seq.map String.of_seq
38-
|> List.of_seq
39-
|> List.rev
40-
4115
let sub_to_end s start =
4216
let length = String.length s in
4317
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
@@ -621,7 +620,7 @@ module AuthADlw : Auth_signature.AUTH_MODULE = struct
621620
(* first, we try to authenticated user against our external user database *)
622621
(* pbis_common will raise an Auth_failure if external authentication fails *)
623622
let domain, user =
624-
match Stringext.split_f (fun c -> c = '\\') username with
623+
match Astring.String.cuts ~empty:false ~sep:"\\" username with
625624
| [domain; user] ->
626625
(domain, user)
627626
| [user] ->
@@ -978,7 +977,8 @@ module AuthADlw : Auth_signature.AUTH_MODULE = struct
978977
| "" ->
979978
[]
980979
| disabled_modules_string ->
981-
Stringext.split_f (fun c -> c = ',') disabled_modules_string
980+
Astring.String.cuts ~empty:false ~sep:","
981+
disabled_modules_string
982982
with Not_found -> []
983983
in
984984
let disabled_module_params =

ocaml/xapi/workload_balancing.ml

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -74,26 +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
82-
raise_url_invalid url ;
83-
let host = String.sub url 1 (host_end - 1) in
84-
let port =
85-
String.sub url (host_end + 2) (String.length url - host_end - 2)
86-
in
87-
(host, int_of_string port)
88-
) else
89-
match
90-
Xapi_stdext_std.Xstringext.String.split_f (fun a -> a = ':') url
91-
with
92-
| [host; port] ->
93-
(host, int_of_string port)
94-
| _ ->
95-
raise_url_invalid url
96-
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)
9783

9884
let wlb_host_port ~__context =
9985
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
@@ -1364,12 +1364,12 @@ let citrix_patch_key =
13641364

13651365
let trusted_patch_key = ref citrix_patch_key
13661366

1367+
let fields_of = Astring.(String.fields ~empty:false ~is_sep:Char.Ascii.is_white)
1368+
13671369
let gen_list_option name desc of_string string_of opt =
13681370
let parse s =
13691371
opt := [] ;
1370-
try
1371-
Xapi_stdext_std.Xstringext.String.(split_f isspace s)
1372-
|> List.iter (fun x -> opt := of_string x :: !opt)
1372+
try fields_of s |> List.iter (fun x -> opt := of_string x :: !opt)
13731373
with e ->
13741374
D.error "Unable to parse %s=%s (expected space-separated list) error: %s"
13751375
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

@@ -709,6 +708,8 @@ let forget ~__context ~self =
709708

710709
let scan_m = Mutex.create ()
711710

711+
let fields_of = Astring.(String.fields ~empty:false ~is_sep:Char.Ascii.is_white)
712+
712713
let scan ~__context ~host =
713714
let dbg = Context.string_of_task __context in
714715
refresh_all ~__context ~host ;
@@ -724,9 +725,9 @@ let scan ~__context ~host =
724725
debug "No boot from SAN interface found" ;
725726
([], [])
726727
| m :: u :: _ ->
727-
Stringext.(split_f isspace m, split_f isspace u)
728+
(fields_of m, fields_of u)
728729
| m :: _ ->
729-
Stringext.(split_f isspace m, [])
730+
(fields_of m, [])
730731
with e ->
731732
warn "Error when executing script %s: %s; ignoring"
732733
!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)