diff --git a/master_changes.md b/master_changes.md index 45b56deb343..c38ca7d6816 100644 --- a/master_changes.md +++ b/master_changes.md @@ -204,6 +204,7 @@ users) ## opam-core * `OpamStd.List.split`: Improve performance [#6210 @kit-ty-kate] + * `OpamStd.Char`: Create the module and export `is_whitespace` [#5492 @kit-ty-kate] * `OpamStd.Sys.{get_terminal_columns,uname,getconf,guess_shell_compat}`: Harden the process calls to account for failures [#6230 @kit-ty-kate - fix #6215] * `OpamStd.Sys.getconf`: was removed, replaced by `get_long_bit` [#6217 @kit-ty-kate] * `OpamStd.Sys.get_long_bit`: was added, which returns the output of the `getconf LONG_BIT` command [#6217 @kit-ty-kate] diff --git a/src/core/opamStd.ml b/src/core/opamStd.ml index 2b766533742..14a6ef3f975 100644 --- a/src/core/opamStd.ml +++ b/src/core/opamStd.ml @@ -550,7 +550,15 @@ module Option = struct end end +module OpamChar = struct + (* TODO: Replace by Stdlib.Char.Ascii.is_space + (to be introduced in OCaml 5.4) *) + let is_whitespace = function + | ' ' | '\t' | '\r' | '\n' -> true + | _ -> false + +end module OpamString = struct @@ -630,26 +638,22 @@ module OpamString = struct for i = 0 to len - 1 do Bytes.set b i (f s.[i]) done; Bytes.to_string b - let is_whitespace = function - | ' ' | '\t' | '\r' | '\n' -> true - | _ -> false - let strip str = let p = ref 0 in let l = String.length str in - while !p < l && is_whitespace (String.unsafe_get str !p) do + while !p < l && OpamChar.is_whitespace (String.unsafe_get str !p) do incr p; done; let p = !p in let l = ref (l - 1) in - while !l >= p && is_whitespace (String.unsafe_get str !l) do + while !l >= p && OpamChar.is_whitespace (String.unsafe_get str !l) do decr l; done; String.sub str p (!l - p + 1) let strip_right str = let rec aux i = - if i < 0 || not (is_whitespace str.[i]) then i else aux (i-1) + if i < 0 || not (OpamChar.is_whitespace str.[i]) then i else aux (i-1) in let l = String.length str in let i = aux (l-1) in @@ -1898,6 +1902,7 @@ module Config = struct end module List = OpamList +module Char = OpamChar module String = OpamString module Sys = OpamSys module Format = OpamFormat diff --git a/src/core/opamStd.mli b/src/core/opamStd.mli index 74b06acb2de..7680d56bffd 100644 --- a/src/core/opamStd.mli +++ b/src/core/opamStd.mli @@ -260,6 +260,12 @@ module List : sig val fold_left_map: ('s -> 'a -> ('s * 'b)) -> 's -> 'a list -> 's * 'b list end +module Char : sig + + val is_whitespace : char -> bool + +end + module String : sig (** {3 Collections} *)