[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[Libguestfs] [PATCH 10/14] mllib: Move common code for comparing version strings to library.



---
 builder/get_kernel.ml | 26 --------------------------
 mllib/common_utils.ml | 27 +++++++++++++++++++++++++++
 2 files changed, 27 insertions(+), 26 deletions(-)

diff --git a/builder/get_kernel.ml b/builder/get_kernel.ml
index 25e4293..7f93728 100644
--- a/builder/get_kernel.ml
+++ b/builder/get_kernel.ml
@@ -23,9 +23,6 @@ module G = Guestfs
 
 open Printf
 
-let rex_numbers = Str.regexp "^\\([0-9]+\\)\\(.*\\)$"
-let rex_letters = Str.regexp_case_fold "^\\([a-z]+\\)\\(.*\\)$"
-
 (* Originally:
  * http://rwmj.wordpress.com/2013/09/13/get-kernel-and-initramfs-from-a-disk-image/
  *)
@@ -96,26 +93,3 @@ let rec get_kernel ~debug ?format ?output disk =
   (* Shutdown. *)
   g#shutdown ();
   g#close ()
-
-and compare_version v1 v2 =
-  compare (split_version v1) (split_version v2)
-
-and split_version = function
-  | "" -> []
-  | str ->
-    let first, rest =
-      if Str.string_match rex_numbers str 0 then (
-        let n = Str.matched_group 1 str in
-        let rest = Str.matched_group 2 str in
-        let n =
-          try `Number (int_of_string n)
-          with Failure "int_of_string" -> `String n in
-        n, rest
-      )
-      else if Str.string_match rex_letters str 0 then
-        `String (Str.matched_group 1 str), Str.matched_group 2 str
-      else (
-        let len = String.length str in
-        `Char str.[0], String.sub str 1 (len-1)
-      ) in
-    first :: split_version rest
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index 60e3812..d4a97a7 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -340,6 +340,33 @@ let display_long_options () =
   ) !long_options;
   exit 0
 
+(* Compare two version strings intelligently. *)
+let rex_numbers = Str.regexp "^\\([0-9]+\\)\\(.*\\)$"
+let rex_letters = Str.regexp_case_fold "^\\([a-z]+\\)\\(.*\\)$"
+
+let compare_version v1 v2 =
+  let rec split_version = function
+    | "" -> []
+    | str ->
+      let first, rest =
+        if Str.string_match rex_numbers str 0 then (
+          let n = Str.matched_group 1 str in
+          let rest = Str.matched_group 2 str in
+          let n =
+            try `Number (int_of_string n)
+            with Failure "int_of_string" -> `String n in
+          n, rest
+        )
+        else if Str.string_match rex_letters str 0 then
+          `String (Str.matched_group 1 str), Str.matched_group 2 str
+        else (
+          let len = String.length str in
+          `Char str.[0], String.sub str 1 (len-1)
+        ) in
+      first :: split_version rest
+  in
+  compare (split_version v1) (split_version v2)
+
 (* Run an external command, slurp up the output as a list of lines. *)
 let external_command ~prog cmd =
   let chan = Unix.open_process_in cmd in
-- 
1.9.0


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]