[Pkg-libvirt-commits] [libguestfs] 61/156: mllib: Rewrite text wrapping function so it can handle newlines within the text.

Hilko Bengen bengen at moszumanska.debian.org
Sat Aug 30 08:25:52 UTC 2014


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

bengen pushed a commit to branch master
in repository libguestfs.

commit 6398259a49b17ea156af0206c1f7d0f15b7633b6
Author: Richard W.M. Jones <rjones at redhat.com>
Date:   Tue Jun 24 16:18:36 2014 +0100

    mllib: Rewrite text wrapping function so it can handle newlines within the text.
    
    (cherry picked from commit 5d3ec4474c955672c761ac11a178f29581985cd2)
---
 mllib/common_utils.ml  | 50 +++++++++++++++++++++++++++++++++-----------------
 mllib/common_utils.mli |  2 +-
 resize/resize.ml       |  4 ++--
 3 files changed, 36 insertions(+), 20 deletions(-)

diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index ba0d72b..a92cafd 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -57,25 +57,41 @@ let le32_of_int i =
   String.unsafe_set s 3 (Char.unsafe_chr (Int64.to_int c3));
   s
 
-let output_spaces chan n = for i = 0 to n-1 do output_char chan ' ' done
-
-let wrap ?(chan = stdout) ?(hanging = 0) str =
-  let rec _wrap col str =
-    let n = String.length str in
-    let i = try String.index str ' ' with Not_found -> n in
-    let col =
-      if col+i >= 72 then (
+type wrap_break_t = WrapEOS | WrapSpace | WrapNL
+
+let rec wrap ?(chan = stdout) ?(indent = 0) str =
+  let len = String.length str in
+  _wrap chan indent 0 0 len str
+
+and _wrap chan indent column i len str =
+  if i < len then (
+    let (j, break) = _wrap_find_next_break i len str in
+    let next_column =
+      if column + (j-i) >= 72 then (
         output_char chan '\n';
-        output_spaces chan hanging;
-        i+hanging+1
-      ) else col+i+1 in
-    output_string chan (String.sub str 0 i);
-    if i < n then (
+        output_spaces chan indent;
+        indent + (j-i) + 1
+      )
+      else column + (j-i) + 1 in
+    output chan str i (j-i);
+    match break with
+    | WrapEOS -> ()
+    | WrapSpace ->
       output_char chan ' ';
-      _wrap col (String.sub str (i+1) (n-(i+1)))
-    )
-  in
-  _wrap 0 str
+      _wrap chan indent next_column (j+1) len str
+    | WrapNL ->
+      output_char chan '\n';
+      output_spaces chan indent;
+      _wrap chan indent indent (j+1) len str
+  )
+
+and _wrap_find_next_break i len str =
+  if i >= len then (len, WrapEOS)
+  else if String.unsafe_get str i = ' ' then (i, WrapSpace)
+  else if String.unsafe_get str i = '\n' then (i, WrapNL)
+  else _wrap_find_next_break (i+1) len str
+
+and output_spaces chan n = for i = 0 to n-1 do output_char chan ' ' done
 
 let string_prefix str prefix =
   let n = String.length prefix in
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index 48e5f6c..1ebf8dd 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -31,7 +31,7 @@ val roundup64 : int64 -> int64 -> int64
 val int_of_le32 : string -> int64
 val le32_of_int : int64 -> string
 
-val wrap : ?chan:out_channel -> ?hanging:int -> string -> unit
+val wrap : ?chan:out_channel -> ?indent:int -> string -> unit
 (** Wrap text. *)
 
 val string_prefix : string -> string -> bool
diff --git a/resize/resize.ml b/resize/resize.ml
index dec23b1..7ae8c37 100644
--- a/resize/resize.ml
+++ b/resize/resize.ml
@@ -833,7 +833,7 @@ read the man page virt-resize(1).
                      (expand_content_method p.p_type))
               ) else "" in
 
-        wrap ~hanging:4 (text ^ "\n\n")
+        wrap ~indent:4 (text ^ "\n\n")
     ) partitions;
 
     List.iter (
@@ -852,7 +852,7 @@ read the man page virt-resize(1).
                      (expand_content_method lv.lv_type))
               ) else "" in
 
-            wrap ~hanging:4 (text ^ "\n\n")
+            wrap ~indent:4 (text ^ "\n\n")
     ) lvs;
 
     if surplus > 0L then (

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