lib.strings: add trim and trimWith

`strings.trim` returns a copy of the string with all leading and trailing
whitespace removed.

`strings.trimWith` does the same thing, but calling code can decide
whether to trim the start and/or end of the string.
This commit is contained in:
Matt Sturgeon 2024-05-28 18:22:43 +01:00
parent d1e70dde57
commit aad87c2aa8
No known key found for this signature in database
GPG key ID: 4F91844CED1A8299
3 changed files with 129 additions and 1 deletions

View file

@ -157,6 +157,68 @@ rec {
*/
replicate = n: s: concatStrings (lib.lists.replicate n s);
/*
Remove leading and trailing whitespace from a string.
Whitespace is defined as any of the following characters:
" ", "\t" "\r" "\n"
Type: trim :: string -> string
Example:
trim " hello, world! "
=> "hello, world!"
*/
trim = trimWith {
start = true;
end = true;
};
/*
Remove leading and/or trailing whitespace from a string.
Whitespace is defined as any of the following characters:
" ", "\t" "\r" "\n"
Type: trimWith :: Attrs -> string -> string
Example:
trimWith { start = true; } " hello, world! "}
=> "hello, world! "
trimWith { end = true; } " hello, world! "}
=> " hello, world!"
*/
trimWith =
{
# Trim leading whitespace
start ? false,
# Trim trailing whitespace
end ? false,
}:
s:
let
# Define our own whitespace character class instead of using
# `[:space:]`, which is not well-defined.
chars = " \t\r\n";
# To match up until trailing whitespace, we need to capture a
# group that ends with a non-whitespace character.
regex =
if start && end then
"[${chars}]*(.*[^${chars}])[${chars}]*"
else if start then
"[${chars}]*(.*)"
else if end then
"(.*[^${chars}])[${chars}]*"
else
"(.*)";
# If the string was empty or entirely whitespace,
# then the regex may not match and `res` will be `null`.
res = match regex s;
in
optionalString (res != null) (head res);
/* Construct a Unix-style, colon-separated search path consisting of
the given `subDir` appended to each of the given paths.