Richard W.M. Jones
2016-Jun-16 13:44 UTC
[Libguestfs] [PATCH] mllib: Add isspace, triml, trimr and trim functions.
---
mllib/common_utils.ml | 29 +++++++++++++++++++++++++++++
mllib/common_utils.mli | 8 ++++++++
2 files changed, 37 insertions(+)
diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml
index 64bf3d3..34e1285 100644
--- a/mllib/common_utils.ml
+++ b/mllib/common_utils.ml
@@ -49,6 +49,35 @@ module String = struct
and len = length str in
len >= sufflen && sub str (len - sufflen) sufflen = suffix
+ (* Note OCaml stdlib has an "is_space" function. *)
+ let isspace c + c = ' '
+ (* || c = '\f' *) || c = '\n' || c = '\r' || c =
'\t' (* || c = '\v' *)
+
+ let triml ?(test = isspace) str + let i = ref 0 in
+ let n = ref (String.length str) in
+ while !n > 0 && test str.[!i]; do
+ decr n;
+ incr i
+ done;
+ if !i = 0 then str
+ else String.sub str !i !n
+
+ let trimr ?(test = isspace) str + let n = ref (String.length str) in
+ while !n > 0 && test str.[!n-1]; do
+ decr n
+ done;
+ if !n = String.length str then str
+ else String.sub str 0 !n
+
+ (* Note OCaml stdlib has a function with the same name and
+ * different signature.
+ *)
+ let trim ?(test = isspace) str + trimr ~test (triml ~test str)
+
let rec find s sub let len = length s in
let sublen = length sub in
diff --git a/mllib/common_utils.mli b/mllib/common_utils.mli
index 5b0b9bb..a04a784 100644
--- a/mllib/common_utils.mli
+++ b/mllib/common_utils.mli
@@ -56,6 +56,14 @@ module String : sig
(** [is_prefix str prefix] returns true if [prefix] is a prefix of [str].
*)
val is_suffix : string -> string -> bool
(** [is_suffix str suffix] returns true if [suffix] is a suffix of [str].
*)
+ val isspace : char -> bool
+ (** Return true if char is a whitespace character. *)
+ val triml : ?test:(char -> bool) -> string -> string
+ (** Trim left. *)
+ val trimr : ?test:(char -> bool) -> string -> string
+ (** Trim right. *)
+ val trim : ?test:(char -> bool) -> string -> string
+ (** Trim left and right. *)
val find : string -> string -> int
(** [find str sub] searches for [sub] as a substring of [str]. If
found it returns the index. If not found, it returns [-1]. *)
--
2.7.4
Pino Toscano
2016-Jun-29 08:58 UTC
Re: [Libguestfs] [PATCH] mllib: Add isspace, triml, trimr and trim functions.
On Thursday 16 June 2016 14:44:19 Richard W.M. Jones wrote:> --- > mllib/common_utils.ml | 29 +++++++++++++++++++++++++++++ > mllib/common_utils.mli | 8 ++++++++ > 2 files changed, 37 insertions(+)The patch makes sense to me, I'd like to use these in the customize code. Just one note below.> diff --git a/mllib/common_utils.ml b/mllib/common_utils.ml > index 64bf3d3..34e1285 100644 > --- a/mllib/common_utils.ml > +++ b/mllib/common_utils.ml > @@ -49,6 +49,35 @@ module String = struct > and len = length str in > len >= sufflen && sub str (len - sufflen) sufflen = suffix > > + (* Note OCaml stdlib has an "is_space" function. *) > + let isspace c > + c = ' ' > + (* || c = '\f' *) || c = '\n' || c = '\r' || c = '\t' (* || c = '\v' *)Since it's new code for Common_utils, why not just use the standard is_space here, and in case provide an own function only where needed? This way we can easily drop these implementations when bumping the required OCaml version to 4.00. Thanks, -- Pino Toscano
Possibly Parallel Threads
- Re: [PATCH] generator: Share Common_utils code.
- [PATCH v6 04/41] mllib: Split ‘Common_utils’ into ‘Std_utils’ + ‘Common_utils’.
- [PATCH] generator: Share Common_utils code.
- Re: [PATCH v2] mlcustomize: Trim whitespaces from commands read from file (RHBZ#1351000)
- [PATCH] common/mlstdutils: Add chomp function to remove \n from end of strings.