[Pkg-libvirt-commits] [libguestfs] 192/384: mllib: add string_lines_split

Hilko Bengen bengen at moszumanska.debian.org
Sun Mar 29 16:56:55 UTC 2015


This is an automated email from the git hooks/post-receive script.

bengen pushed a commit to branch experimental
in repository libguestfs.

commit 09f76fd2a9a699df5f72f3a8b1955b158b574e56
Author: Pino Toscano <ptoscano at redhat.com>
Date:   Thu Jan 22 17:05:30 2015 +0100

    mllib: add string_lines_split
    
    Introduce an helper function to split a text into lines, keeping into
    accout continuation lines (i.e. with \ at the end).
---
 mllib/common_utils.ml  | 31 +++++++++++++++++++++++++++++++
 mllib/common_utils.mli |  1 +
 2 files changed, 32 insertions(+)

diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index 39b5653..37a547e 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -171,6 +171,37 @@ let string_random8 =
       ) [1;2;3;4;5;6;7;8]
     )
 
+(* Split a string into lines, keeping continuation characters
+ * (i.e. \ at the end of lines) into account. *)
+let rec string_lines_split str =
+  let buf = Buffer.create 16 in
+  let len = String.length str in
+  let rec loop start len =
+    try
+      let i = String.index_from str start '\n' in
+      if i > 0 && str.[i-1] = '\\' then (
+        Buffer.add_substring buf str start (i-start-1);
+        Buffer.add_char buf '\n';
+        loop (i+1) len
+      ) else (
+        Buffer.add_substring buf str start (i-start);
+        i+1
+      )
+    with Not_found ->
+      if len > 0 && str.[len-1] = '\\' then (
+        Buffer.add_substring buf str start (len-start-1);
+        Buffer.add_char buf '\n'
+      ) else
+        Buffer.add_substring buf str start (len-start);
+      len+1
+  in
+  let endi = loop 0 len in
+  let line = Buffer.contents buf in
+  if endi > len then
+    [line]
+  else
+    line :: string_lines_split (String.sub str endi (len-endi))
+
 (* Drop elements from a list while a predicate is true. *)
 let rec dropwhile f = function
   | [] -> []
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index 5661cab..adb436d 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -45,6 +45,7 @@ val replace_str : string -> string -> string -> string
 val string_nsplit : string -> string -> string list
 val string_split : string -> string -> string * string
 val string_random8 : unit -> string
+val string_lines_split : string -> string list
 (** Various string functions. *)
 
 val dropwhile : ('a -> bool) -> 'a list -> 'a list

-- 
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