This is like the Perl chomp function, it removes a single \n from the
end of a string if present, else leaves the string alone.
I believe I found the only (two) places where such a function is used,
but there may be a few more lurking.
---
common/mlstdutils/std_utils.ml | 7 +++++++
common/mlstdutils/std_utils.mli | 2 ++
common/mlstdutils/std_utils_tests.ml | 10 ++++++++++
mllib/common_utils.ml | 10 ++--------
v2v/linux_bootloaders.ml | 8 ++------
5 files changed, 23 insertions(+), 14 deletions(-)
diff --git a/common/mlstdutils/std_utils.ml b/common/mlstdutils/std_utils.ml
index 374507662..b731b8fd5 100644
--- a/common/mlstdutils/std_utils.ml
+++ b/common/mlstdutils/std_utils.ml
@@ -229,6 +229,13 @@ module String = struct
let trim ?(test = Char.isspace) str =
trimr ~test (triml ~test str)
+ let chomp str =
+ let n = String.length str in
+ if n > 0 && str.[n-1] = '\n' then
+ String.sub str 0 (n-1)
+ else
+ str
+
let count_chars c str =
let count = ref 0 in
for i = 0 to String.length str - 1 do
diff --git a/common/mlstdutils/std_utils.mli b/common/mlstdutils/std_utils.mli
index 0b7d90736..d217e48d4 100644
--- a/common/mlstdutils/std_utils.mli
+++ b/common/mlstdutils/std_utils.mli
@@ -107,6 +107,8 @@ module String : sig
(** Trim right. *)
val trim : ?test:(char -> bool) -> string -> string
(** Trim left and right. *)
+ val chomp : string -> string
+ (** If the string ends with [\n], remove it. *)
val count_chars : char -> string -> int
(** Count number of times the character occurs in string. *)
val explode : string -> char list
diff --git a/common/mlstdutils/std_utils_tests.ml b/common/mlstdutils/std_utils_tests.ml
index 2789766c6..ce49c7606 100644
--- a/common/mlstdutils/std_utils_tests.ml
+++ b/common/mlstdutils/std_utils_tests.ml
@@ -110,6 +110,15 @@ let test_string_span ctx =
assert_equal_int 3 (String.cspan "def" "ab");
assert_equal_int 0 (String.cspan "" "ab")
+(* Test Std_utils.String.chomp. *)
+let test_string_chomp ctx =
+ assert_equal_string "a" (String.chomp "a");
+ assert_equal_string "a" (String.chomp "a\n");
+ assert_equal_string "a\nb" (String.chomp "a\nb");
+ assert_equal_string "" (String.chomp "");
+ assert_equal_string "" (String.chomp "\n");
+ assert_equal_string "\n" (String.chomp "\n\n") (* only removes one
*)
+
(* Suites declaration. *)
let suite =
"mllib Std_utils" >:::
@@ -122,6 +131,7 @@ let suite =
"strings.find" >:: test_string_find;
"strings.lines_split" >:: test_string_lines_split;
"strings.span" >:: test_string_span;
+ "strings.chomp" >:: test_string_chomp;
]
let () =
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index 73c6e2473..597128967 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -376,14 +376,8 @@ let shell_command ?(echo_cmd = true) cmd =
let uuidgen () =
let lines = external_command "uuidgen -r" in
assert (List.length lines >= 1);
- let uuid = List.hd lines in
- let len = String.length uuid in
- let uuid, len =
- if len > 0 && uuid.[len-1] = '\n' then
- String.sub uuid 0 (len-1), len-1
- else
- uuid, len in
- if len < 10 then assert false; (* sanity check on uuidgen *)
+ let uuid = String.chomp (List.hd lines) in
+ if String.length uuid < 10 then assert false; (* sanity check on uuidgen *)
uuid
(* Remove a temporary directory on exit. *)
diff --git a/v2v/linux_bootloaders.ml b/v2v/linux_bootloaders.ml
index d76407670..210cce762 100644
--- a/v2v/linux_bootloaders.ml
+++ b/v2v/linux_bootloaders.ml
@@ -307,12 +307,8 @@ object (self)
match res with
| None -> None
| Some k ->
- let len = String.length k in
- let k =
- if len > 0 && k.[len-1] = '\n' then
- String.sub k 0 (len-1)
- else k in
- Some (remove_hd_prefix k)
+ let k = String.chomp k in
+ Some (remove_hd_prefix k)
in
let vmlinuzes =
--
2.13.2