mrc0mmand / rpms / libguestfs

Forked from rpms/libguestfs 3 years ago
Clone

Blame SOURCES/0076-mllib-Add-Common_utils.string_suffix-function-and-ex.patch

0d20ef
From 91b771d57ef671253ce336d7ccb74e5c3ff08404 Mon Sep 17 00:00:00 2001
0d20ef
From: "Richard W.M. Jones" <rjones@redhat.com>
0d20ef
Date: Thu, 4 Dec 2014 22:00:35 +0000
0d20ef
Subject: [PATCH] mllib: Add Common_utils.string_suffix function and extend
0d20ef
 test coverage.
0d20ef
0d20ef
(cherry picked from commit c712f880db640163a0ce913e42115ecf4f6aa0c2)
0d20ef
---
0d20ef
 mllib/common_utils.ml       |  5 +++++
0d20ef
 mllib/common_utils.mli      |  1 +
0d20ef
 mllib/common_utils_tests.ml | 21 +++++++++++++++++++++
0d20ef
 3 files changed, 27 insertions(+)
0d20ef
0d20ef
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
0d20ef
index 295981c..7c64ad0 100644
0d20ef
--- a/mllib/common_utils.ml
0d20ef
+++ b/mllib/common_utils.ml
0d20ef
@@ -98,6 +98,11 @@ let string_prefix str prefix =
0d20ef
   let n = String.length prefix in
0d20ef
   String.length str >= n && String.sub str 0 n = prefix
0d20ef
 
0d20ef
+let string_suffix str suffix =
0d20ef
+  let sufflen = String.length suffix
0d20ef
+  and len = String.length str in
0d20ef
+  len >= sufflen && String.sub str (len - sufflen) sufflen = suffix
0d20ef
+
0d20ef
 let rec string_find s sub =
0d20ef
   let len = String.length s in
0d20ef
   let sublen = String.length sub in
0d20ef
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
0d20ef
index 112648a..6d0a0fc 100644
0d20ef
--- a/mllib/common_utils.mli
0d20ef
+++ b/mllib/common_utils.mli
0d20ef
@@ -39,6 +39,7 @@ val output_spaces : out_channel -> int -> unit
0d20ef
 (** Write [n] spaces to [out_channel]. *)
0d20ef
 
0d20ef
 val string_prefix : string -> string -> bool
0d20ef
+val string_suffix : string -> string -> bool
0d20ef
 val string_find : string -> string -> int
0d20ef
 val replace_str : string -> string -> string -> string
0d20ef
 val string_nsplit : string -> string -> string list
0d20ef
diff --git a/mllib/common_utils_tests.ml b/mllib/common_utils_tests.ml
0d20ef
index e06b3a4..e12297a 100644
0d20ef
--- a/mllib/common_utils_tests.ml
0d20ef
+++ b/mllib/common_utils_tests.ml
0d20ef
@@ -78,3 +78,24 @@ let () =
0d20ef
   assert (human_size (-1363149_L) = "-1.3M");
0d20ef
   assert (human_size 3650722201_L = "3.4G");
0d20ef
   assert (human_size (-3650722201_L) = "-3.4G")
0d20ef
+
0d20ef
+(* Test Common_utils.string_prefix, string_suffix, string_find. *)
0d20ef
+let () =
0d20ef
+  assert (string_prefix "" "");
0d20ef
+  assert (string_prefix "foo" "");
0d20ef
+  assert (string_prefix "foo" "foo");
0d20ef
+  assert (string_prefix "foo123" "foo");
0d20ef
+  assert (not (string_prefix "" "foo"));
0d20ef
+
0d20ef
+  assert (string_suffix "" "");
0d20ef
+  assert (string_suffix "foo" "");
0d20ef
+  assert (string_suffix "foo" "foo");
0d20ef
+  assert (string_suffix "123foo" "foo");
0d20ef
+  assert (not (string_suffix "" "foo"));
0d20ef
+
0d20ef
+  assert (string_find "" "" = 0);
0d20ef
+  assert (string_find "foo" "" = 0);
0d20ef
+  assert (string_find "foo" "o" = 1);
0d20ef
+  assert (string_find "foobar" "bar" = 3);
0d20ef
+  assert (string_find "" "baz" = -1);
0d20ef
+  assert (string_find "foobar" "baz" = -1)
0d20ef
-- 
0d20ef
1.8.3.1
0d20ef