[Libguestfs] [libguestfs-common PATCH 1/3] Demote "Std_utils.wrap" to an internal function in Tools_utils

Richard W.M. Jones rjones at redhat.com
Sat Feb 12 18:11:21 UTC 2022


On Fri, Feb 11, 2022 at 04:32:23PM +0100, Laszlo Ersek wrote:
> At this point, no client project of the libguestfs-common submodule should
> be using "Std_utils.wrap", so we can remove its public declaration, and
> make it an internal function in the module (Tools_utils) where its callers
> are ("error", "warning", and "info").
> 
> Note: the "output_spaces" function is not moved; in addition to being
> called by "wrap", it is also called by virt-v2v, from commit b4afcf1dac35
> ("v2v: Implement -i vmx to read VMware vmx files directly
> (RHBZ#1441197).", 2017-04-11). "output_spaces" is generally useful for
> formatting indented (not wrapped) debug output.
> 
> Bugzilla: https://bugzilla.redhat.com/show_bug.cgi?id=1820221
> Suggested-by: Richard W.M. Jones <rjones at redhat.com>
> Signed-off-by: Laszlo Ersek <lersek at redhat.com>
> ---
>  mlstdutils/std_utils.mli |  3 ---
>  mlstdutils/std_utils.ml  | 36 +-----------------------------------
>  mltools/tools_utils.ml   | 35 +++++++++++++++++++++++++++++++++++
>  3 files changed, 36 insertions(+), 38 deletions(-)
> 
> diff --git a/mlstdutils/std_utils.mli b/mlstdutils/std_utils.mli
> index 8b6bcd1dd7ae..534caa80d6a7 100644
> --- a/mlstdutils/std_utils.mli
> +++ b/mlstdutils/std_utils.mli
> @@ -315,9 +315,6 @@ val be64_of_int : int64 -> string
>      On the OCaml side, 64 bit integers are always used so that you
>      can use the [.^] operators on them for bit manipulation. *)
>  
> -val wrap : ?chan:out_channel -> ?indent:int -> string -> unit
> -(** Wrap text. *)
> -
>  val output_spaces : out_channel -> int -> unit
>  (** Write [n] spaces to [out_channel]. *)
>  
> diff --git a/mlstdutils/std_utils.ml b/mlstdutils/std_utils.ml
> index 29add4a027bc..58ac058c1b3a 100644
> --- a/mlstdutils/std_utils.ml
> +++ b/mlstdutils/std_utils.ml
> @@ -536,41 +536,7 @@ let be64_of_int i =
>    Bytes.unsafe_set b 7 (Char.unsafe_chr (Int64.to_int c0));
>    Bytes.to_string b
>  
> -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) >= 76 then (
> -        output_char chan '\n';
> -        output_spaces chan indent;
> -        indent + (j-i) + 1
> -      )
> -      else column + (j-i) + 1 in
> -    output chan (Bytes.of_string str) i (j-i);
> -    match break with
> -    | WrapEOS -> ()
> -    | WrapSpace ->
> -      output_char chan ' ';
> -      _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 output_spaces chan n = for i = 0 to n-1 do output_char chan ' ' done
>  
>  let unique = let i = ref 0 in fun () -> incr i; !i
>  
> diff --git a/mltools/tools_utils.ml b/mltools/tools_utils.ml
> index 177876e421e6..06ba2f7ab295 100644
> --- a/mltools/tools_utils.ml
> +++ b/mltools/tools_utils.ml
> @@ -122,6 +122,41 @@ let message fs =
>    in
>    ksprintf display fs
>  
> +(* Wrap text. *)
> +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) >= 76 then (
> +        output_char chan '\n';
> +        output_spaces chan indent;
> +        indent + (j-i) + 1
> +      )
> +      else column + (j-i) + 1 in
> +    output chan (Bytes.of_string str) i (j-i);
> +    match break with
> +    | WrapEOS -> ()
> +    | WrapSpace ->
> +      output_char chan ' ';
> +      _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
> +
>  (* Error messages etc. *)
>  let error ?(exit_code = 1) fs =
>    let display str =

Obvious code motion & hiding a function which is no longer needed
outside this module.

Reviewed-by: Richard W.M. Jones <rjones at redhat.com>

Rich.

-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming and virtualization blog: http://rwmj.wordpress.com
libguestfs lets you edit virtual machines.  Supports shell scripting,
bindings from many languages.  http://libguestfs.org




More information about the Libguestfs mailing list