Richard W.M. Jones
2017-Jul-21 13:56 UTC
[Libguestfs] [PATCH] common/mlstdutils: Add chomp function to remove \n from end of strings.
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
Pino Toscano
2017-Jul-21 14:29 UTC
Re: [Libguestfs] [PATCH] common/mlstdutils: Add chomp function to remove \n from end of strings.
On Friday, 21 July 2017 15:56:17 CEST Richard W.M. Jones wrote:> 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. > ---I did not find more either -- LGTM. -- Pino Toscano
Richard Jones
2017-Jul-21 17:16 UTC
[Libguestfs] check-syntax FAILED (was: Re: [PATCH] common/mlstdutils: Add chomp function to remove \n from end of strings.)
Checking out sources from https://github.com/libguestfs/libguestfs ... /var/tmp/tmpZ6wS6s/libguestfs /var/tmp/tmpZ6wS6s Reset branch 'master' Branch master set up to track remote branch master from origin. Your branch is up-to-date with 'origin/master'. Already up-to-date. /var/tmp/tmpZ6wS6s Applying patches ... /var/tmp/tmpZ6wS6s/libguestfs /var/tmp/tmpZ6wS6s error: patch failed: common/mlstdutils/std_utils.ml:229 error: common/mlstdutils/std_utils.ml: patch does not apply error: patch failed: common/mlstdutils/std_utils.mli:107 error: common/mlstdutils/std_utils.mli: patch does not apply error: patch failed: common/mlstdutils/std_utils_tests.ml:110 error: common/mlstdutils/std_utils_tests.ml: patch does not apply error: patch failed: mllib/common_utils.ml:376 error: mllib/common_utils.ml: patch does not apply error: patch failed: v2v/linux_bootloaders.ml:307 error: v2v/linux_bootloaders.ml: patch does not apply Applying: common/mlstdutils: Add chomp function to remove \n from end of strings. Patch failed at 0001 common/mlstdutils: Add chomp function to remove \n from end of strings. The copy of the patch that failed is found in: .git/rebase-apply/patch When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort".
Richard Jones
2017-Jul-21 17:16 UTC
[Libguestfs] check-release FAILED (was: Re: [PATCH] common/mlstdutils: Add chomp function to remove \n from end of strings.)
Checking out sources from https://github.com/libguestfs/libguestfs ... /var/tmp/tmpCbDvGx/libguestfs /var/tmp/tmpCbDvGx Reset branch 'master' Branch master set up to track remote branch master from origin. Your branch is up-to-date with 'origin/master'. Already up-to-date. /var/tmp/tmpCbDvGx Applying patches ... /var/tmp/tmpCbDvGx/libguestfs /var/tmp/tmpCbDvGx error: patch failed: common/mlstdutils/std_utils.ml:229 error: common/mlstdutils/std_utils.ml: patch does not apply error: patch failed: common/mlstdutils/std_utils.mli:107 error: common/mlstdutils/std_utils.mli: patch does not apply error: patch failed: common/mlstdutils/std_utils_tests.ml:110 error: common/mlstdutils/std_utils_tests.ml: patch does not apply error: patch failed: mllib/common_utils.ml:376 error: mllib/common_utils.ml: patch does not apply error: patch failed: v2v/linux_bootloaders.ml:307 error: v2v/linux_bootloaders.ml: patch does not apply Applying: common/mlstdutils: Add chomp function to remove \n from end of strings. Patch failed at 0001 common/mlstdutils: Add chomp function to remove \n from end of strings. The copy of the patch that failed is found in: .git/rebase-apply/patch When you have resolved this problem, run "git am --continue". If you prefer to skip this patch, run "git am --skip" instead. To restore the original branch and stop patching, run "git am --abort".
Possibly Parallel Threads
- [PATCH 1/3] mlstdutils: add a very simple test for Std_utils.which
- [PATCH v6 04/41] mllib: Split ‘Common_utils’ into ‘Std_utils’ + ‘Common_utils’.
- [common PATCH] build: stop shipping files generated by configure
- [v2v PATCH] po: do not extract tests
- [PATCH v2 2/6] common/mlstdutils: Remove unused Libdir module.