nt/nib/std/strings.nix

59 lines
2 KiB
Nix
Raw Normal View History

2026-01-15 13:34:07 +10:00
{nib, ...}: let
inherit
(builtins)
isPath
2026-01-15 14:00:46 +10:00
genList
2026-01-15 13:43:44 +10:00
replaceStrings
2026-01-15 13:34:07 +10:00
substring
stringLength
;
inherit
2026-01-15 14:23:12 +10:00
(nib.std)
2026-01-15 13:34:07 +10:00
warnIf
;
in rec {
# re-export builtin string methods
inherit
2026-01-15 13:43:44 +10:00
replaceStrings
2026-01-15 13:34:07 +10:00
substring
stringLength
;
2026-01-15 14:00:46 +10:00
escape = list: replaceStrings list (map (c: "\\${c}") list);
escapeRegex = escape (stringToCharacters "\\[{()^$?*+|.");
hasInfix = infix: content:
# Before 23.05, paths would be copied to the store before converting them
# to strings and comparing. This was surprising and confusing.
warnIf (isPath infix)
''
lib.strings.hasInfix: The first argument (${toString infix}) is a path value, but only strings are supported.
There is almost certainly a bug in the calling code, since this function always returns `false` in such a case.
This function also copies the path to the Nix store, which may not be what you want.
This behavior is deprecated and will throw an error in the future.''
(builtins.match ".*${escapeRegex infix}.*" "${content}" != null);
2026-01-15 13:34:07 +10:00
removeSuffix = suffix: str:
# Before 23.05, paths would be copied to the store before converting them
# to strings and comparing. This was surprising and confusing.
warnIf (isPath suffix)
''
lib.strings.removeSuffix: The first argument (${toString suffix}) is a path value, but only strings are supported.
There is almost certainly a bug in the calling code, since this function never removes any suffix in such a case.
This function also copies the path to the Nix store, which may not be what you want.
This behavior is deprecated and will throw an error in the future.''
(
let
sufLen = stringLength suffix;
sLen = stringLength str;
in
if sufLen <= sLen && suffix == substring (sLen - sufLen) sufLen str
then substring 0 (sLen - sufLen) str
else str
);
2026-01-15 14:00:46 +10:00
stringToCharacters = s: genList (p: substring p 1 s) (stringLength s);
2026-01-15 13:34:07 +10:00
}