Skip to content

Commit

Permalink
export OpamStd.Char.is_whitespace
Browse files Browse the repository at this point in the history
  • Loading branch information
kit-ty-kate committed Jan 6, 2025
1 parent 26290a4 commit a8c3fd1
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 7 deletions.
1 change: 1 addition & 0 deletions master_changes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
19 changes: 12 additions & 7 deletions src/core/opamStd.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1898,6 +1902,7 @@ module Config = struct
end

module List = OpamList
module Char = OpamChar
module String = OpamString
module Sys = OpamSys
module Format = OpamFormat
Expand Down
6 changes: 6 additions & 0 deletions src/core/opamStd.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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} *)
Expand Down

0 comments on commit a8c3fd1

Please sign in to comment.