[Pkg-libvirt-commits] [libguestfs] 139/146: mllib: Add a better List.assoc function.
Hilko Bengen
bengen at moszumanska.debian.org
Sun Mar 29 17:01:25 UTC 2015
This is an automated email from the git hooks/post-receive script.
bengen pushed a commit to branch master
in repository libguestfs.
commit 8a9bba8d83f5c4d3a3df5e67f92d747225377e2c
Author: Richard W.M. Jones <rjones at redhat.com>
Date: Wed Jan 28 14:17:51 2015 +0000
mllib: Add a better List.assoc function.
You can specify what comparison function is used; and instead of
having it raise 'Not_found', it returns a ~default value.
(cherry picked from commit ab950f8290b681e5ca53c4fa9377b18f50b9b86e)
---
mllib/common_utils.ml | 5 +++++
mllib/common_utils.mli | 4 ++++
sysprep/main.ml | 3 +--
3 files changed, 10 insertions(+), 2 deletions(-)
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index fa71945..6faab93 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -210,6 +210,11 @@ let rec combine3 xs ys zs =
| x::xs, y::ys, z::zs -> (x, y, z) :: combine3 xs ys zs
| _ -> invalid_arg "combine3"
+let rec assoc ?(cmp = compare) ~default x = function
+ | [] -> default
+ | (y, y') :: _ when cmp x y = 0 -> y'
+ | _ :: ys -> assoc ~cmp ~default x ys
+
(* ANSI terminal colours. *)
let ansi_green ?(chan = stdout) () =
if TTY.isatty_stdout () then output_string chan "\x1b[0;32m"
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index b6fe273..e41b157 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -57,6 +57,10 @@ val mapi : (int -> 'a -> 'b) -> 'a list -> 'b list
val combine3 : 'a list -> 'b list -> 'c list -> ('a * 'b * 'c) list
(** Like {!List.combine} but for triples. All lists must be the same length. *)
+val assoc : ?cmp:('a -> 'a -> int) -> default:'b -> 'a -> ('a * 'b) list -> 'b
+(** Like {!List.assoc} but with a user-defined comparison function, and
+ instead of raising [Not_found], it returns the [~default] value. *)
+
val make_message_function : quiet:bool -> ('a, unit, string, unit) format4 -> 'a
(** Timestamped progress messages. Used for ordinary messages when
not [--quiet]. *)
diff --git a/sysprep/main.ml b/sysprep/main.ml
index 057f4cb..955ca80 100644
--- a/sysprep/main.ml
+++ b/sysprep/main.ml
@@ -229,8 +229,7 @@ read the man page virt-sysprep(1).
let mount_opts = !mount_opts in
let mount_opts =
List.map (string_split ":") (string_nsplit ";" mount_opts) in
- let mount_opts mp =
- try List.assoc mp mount_opts with Not_found -> "" in
+ let mount_opts mp = assoc ~default:"" mp mount_opts in
let msg fs = make_message_function ~quiet fs in
msg (f_"Examining the guest ...");
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-libvirt/libguestfs.git
More information about the Pkg-libvirt-commits
mailing list