Skip to content

Commit 3efc36a

Browse files
authored
Merge pull request #5881 from psafont/xen_types
2 parents 6159aa3 + 4b691d1 commit 3efc36a

34 files changed

+447
-483
lines changed

ocaml/idl/markdown_backend.ml

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,6 @@ let compare_case_ins x y =
4343
compare (String.lowercase_ascii x) (String.lowercase_ascii y)
4444

4545
let escape s =
46-
let open Xapi_stdext_std.Xstringext in
47-
let sl = String.explode s in
4846
let esc_char = function
4947
| '\\' ->
5048
"\"
@@ -79,8 +77,7 @@ let escape s =
7977
| c ->
8078
String.make 1 c
8179
in
82-
let escaped_list = List.map esc_char sl in
83-
String.concat "" escaped_list
80+
String.to_seq s |> Seq.map esc_char |> List.of_seq |> String.concat ""
8481

8582
let rec of_ty_verbatim = function
8683
| SecretString | String ->

ocaml/libs/http-lib/http.ml

Lines changed: 2 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@ exception Forbidden
2424

2525
exception Method_not_implemented
2626

27-
exception Malformed_url of string
28-
2927
exception Timeout
3028

3129
exception Too_large
@@ -145,61 +143,8 @@ let output_http fd headers =
145143
|> String.concat ""
146144
|> Unixext.really_write_string fd
147145

148-
let explode str = Astring.String.fold_right (fun c acc -> c :: acc) str []
149-
150-
let implode chr_list =
151-
String.concat "" (List.map Astring.String.of_char chr_list)
152-
153-
let urldecode url =
154-
let chars = explode url in
155-
let rec fn ac = function
156-
| '+' :: tl ->
157-
fn (' ' :: ac) tl
158-
| '%' :: a :: b :: tl ->
159-
let cs =
160-
try int_of_string (implode ['0'; 'x'; a; b])
161-
with _ -> raise (Malformed_url url)
162-
in
163-
fn (Char.chr cs :: ac) tl
164-
| x :: tl ->
165-
fn (x :: ac) tl
166-
| [] ->
167-
implode (List.rev ac)
168-
in
169-
fn [] chars
170-
171146
(* Encode @param suitably for appearing in a query parameter in a URL. *)
172-
let urlencode param =
173-
let chars = explode param in
174-
let rec fn = function
175-
| x :: tl ->
176-
let s =
177-
if x = ' ' then
178-
"+"
179-
else
180-
match x with
181-
| 'A' .. 'Z'
182-
| 'a' .. 'z'
183-
| '0' .. '9'
184-
| '$'
185-
| '-'
186-
| '_'
187-
| '.'
188-
| '!'
189-
| '*'
190-
| '\''
191-
| '('
192-
| ')'
193-
| ',' ->
194-
Astring.String.of_char x
195-
| _ ->
196-
Printf.sprintf "%%%2x" (Char.code x)
197-
in
198-
s ^ fn tl
199-
| [] ->
200-
""
201-
in
202-
fn chars
147+
let urlencode param = Uri.pct_encode ~component:`Query param
203148

204149
(** Parses strings of the form a=b;c=d (new, RFC-compliant cookie format)
205150
and a=b&c=d (old, incorrect style) into [("a", "b"); ("c", "d")] *)
@@ -219,7 +164,7 @@ let parse_cookies xs =
219164
List.map
220165
(function
221166
| k :: vs ->
222-
(urldecode k, urldecode (String.concat "=" vs))
167+
(Uri.pct_decode k, Uri.pct_decode (String.concat "=" vs))
223168
| [] ->
224169
raise Http_parse_failure
225170
)

ocaml/libs/http-lib/http.mli

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,7 @@ val output_http : Unix.file_descr -> string list -> unit
235235
val parse_cookies : string -> (string * string) list
236236

237237
val urlencode : string -> string
238+
(** Encode parameter suitably for appearing in a query parameter in a URL. *)
238239

239240
type 'a ll = End | Item of 'a * (unit -> 'a ll)
240241

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,4 +208,15 @@ module List = struct
208208
let find_minimum compare =
209209
let min a b = if compare a b <= 0 then a else b in
210210
function [] -> None | x :: xs -> Some (List.fold_left min x xs)
211+
212+
let find_index f l =
213+
let rec loop i = function
214+
| [] ->
215+
None
216+
| x :: _ when f x ->
217+
Some i
218+
| _ :: xs ->
219+
loop (i + 1) xs
220+
in
221+
loop 0 l
211222
end

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,13 @@ module List : sig
6060
the sort order of [cmp], or [None] if the list is empty. When two ore
6161
more elements match the lowest value, the left-most is returned. *)
6262

63+
val find_index : ('a -> bool) -> 'a list -> int option
64+
(** [find_index f l] returns the position of the first element in [l] that
65+
satisfies [f x]. If there is no such element, returns [None].
66+
67+
When using OCaml compilers 5.1 or later, please use the standard library
68+
instead. *)
69+
6370
(** {1 Using indices to manipulate lists} *)
6471

6572
val chop : int -> 'a list -> 'a list * 'a list

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

Lines changed: 17 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,6 @@ module String = struct
3939
done ;
4040
!accu
4141

42-
let explode string = fold_right (fun h t -> h :: t) string []
43-
44-
let implode list = concat "" (List.map of_char list)
45-
4642
(** True if string 'x' ends with suffix 'suffix' *)
4743
let endswith suffix x =
4844
let x_l = String.length x and suffix_l = String.length suffix in
@@ -56,16 +52,6 @@ module String = struct
5652
(** Returns true for whitespace characters, false otherwise *)
5753
let isspace = function ' ' | '\n' | '\r' | '\t' -> true | _ -> false
5854

59-
(** Removes all the characters from the ends of a string for which the predicate is true *)
60-
let strip predicate string =
61-
let rec remove = function
62-
| [] ->
63-
[]
64-
| c :: cs ->
65-
if predicate c then remove cs else c :: cs
66-
in
67-
implode (List.rev (remove (List.rev (remove (explode string)))))
68-
6955
let escaped ?rules string =
7056
match rules with
7157
| None ->
@@ -81,24 +67,28 @@ module String = struct
8167
in
8268
concat "" (fold_right aux string [])
8369

84-
(** Take a predicate and a string, return a list of strings separated by
85-
runs of characters where the predicate was true (excluding those characters from the result) *)
8670
let split_f p str =
87-
let not_p x = not (p x) in
88-
let rec split_one p acc = function
89-
| [] ->
90-
(List.rev acc, [])
91-
| c :: cs ->
92-
if p c then split_one p (c :: acc) cs else (List.rev acc, c :: cs)
71+
let split_one seq =
72+
let not_p c = not (p c) in
73+
let a = Seq.take_while not_p seq in
74+
let b = Seq.drop_while not_p seq in
75+
(a, b)
9376
in
94-
let rec alternate acc drop chars =
95-
if chars = [] then
77+
let drop seq = Seq.drop_while p seq in
78+
let rec split acc chars =
79+
if Seq.is_empty chars then
9680
acc
9781
else
98-
let a, b = split_one (if drop then p else not_p) [] chars in
99-
alternate (if drop then acc else a :: acc) (not drop) b
82+
let a, b = split_one chars in
83+
let b = drop b in
84+
let acc = if Seq.is_empty a then acc else Seq.cons a acc in
85+
split acc b
10086
in
101-
List.rev (List.map implode (alternate [] true (explode str)))
87+
String.to_seq str
88+
|> split Seq.empty
89+
|> Seq.map String.of_seq
90+
|> List.of_seq
91+
|> List.rev
10292

10393
let index_opt s c =
10494
let rec loop i =

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

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ module String : sig
2929
val fold_right : (char -> 'a -> 'a) -> string -> 'a -> 'a
3030
(** Iterate over the characters in a string in reverse order. *)
3131

32-
val explode : string -> char list
33-
(** Split a string into a list of characters. *)
34-
35-
val implode : char list -> string
36-
(** Concatenate a list of characters into a string. *)
37-
3832
val endswith : string -> string -> bool
3933
(** True if string 'x' ends with suffix 'suffix' *)
4034

@@ -44,17 +38,15 @@ module String : sig
4438
val isspace : char -> bool
4539
(** True if the character is whitespace *)
4640

47-
val strip : (char -> bool) -> string -> string
48-
(** Removes all the characters from the ends of a string for which the predicate is true *)
49-
5041
val escaped : ?rules:(char * string) list -> string -> string
5142
(** Backward-compatible string escaping, defaulting to the built-in
5243
OCaml string escaping but allowing an arbitrary mapping from characters
5344
to strings. *)
5445

5546
val split_f : (char -> bool) -> string -> string list
5647
(** Take a predicate and a string, return a list of strings separated by
57-
runs of characters where the predicate was true *)
48+
runs of characters where the predicate was true. Avoid if possible, it's
49+
very costly to execute. *)
5850

5951
val split : ?limit:int -> char -> string -> string list
6052
(** split a string on a single char *)

ocaml/perftest/createpool.ml

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -350,24 +350,20 @@ let create_sdk_pool session_id sdkname pool_name key ipbase =
350350
let pingable = Array.make (Array.length hosts) false in
351351
let firstboot = Array.make (Array.length hosts) false in
352352
let string_of_status () =
353-
Xstringext.String.implode
354-
(Array.to_list
355-
(Array.mapi
356-
(fun i ping ->
357-
let boot = firstboot.(i) in
358-
match (ping, boot) with
359-
| false, false ->
360-
'.'
361-
| true, false ->
362-
'P'
363-
| true, true ->
364-
'B'
365-
| _, _ ->
366-
'?'
367-
)
368-
pingable
369-
)
370-
)
353+
Array.to_seq pingable
354+
|> Seq.mapi (fun i ping ->
355+
let boot = firstboot.(i) in
356+
match (ping, boot) with
357+
| false, false ->
358+
'.'
359+
| true, false ->
360+
'P'
361+
| true, true ->
362+
'B'
363+
| _, _ ->
364+
'?'
365+
)
366+
|> String.of_seq
371367
in
372368
let has_guest_booted i _vm =
373369
let ip = Printf.sprintf "192.168.%d.%d" pool.ipbase (i + 1) in
@@ -469,24 +465,20 @@ let create_sdk_pool session_id sdkname pool_name key ipbase =
469465
let live = Array.make (Array.length hosts) false in
470466
let enabled = Array.make (Array.length hosts) false in
471467
let string_of_status () =
472-
Xstringext.String.implode
473-
(Array.to_list
474-
(Array.mapi
475-
(fun i live ->
476-
let enabled = enabled.(i) in
477-
match (live, enabled) with
478-
| false, false ->
479-
'.'
480-
| true, false ->
481-
'L'
482-
| true, true ->
483-
'E'
484-
| _, _ ->
485-
'?'
486-
)
487-
live
488-
)
489-
)
468+
Array.to_seq live
469+
|> Seq.mapi (fun i live ->
470+
let enabled = enabled.(i) in
471+
match (live, enabled) with
472+
| false, false ->
473+
'.'
474+
| true, false ->
475+
'L'
476+
| true, true ->
477+
'E'
478+
| _, _ ->
479+
'?'
480+
)
481+
|> String.of_seq
490482
in
491483
let has_host_booted rpc session_id i host =
492484
try

0 commit comments

Comments
 (0)