add hasInfix to nib.strings

This commit is contained in:
Emile Clark-Boman 2026-01-15 14:00:46 +10:00
parent cec7087816
commit f5d2dc8766

View file

@ -2,6 +2,7 @@
inherit inherit
(builtins) (builtins)
isPath isPath
genList
replaceStrings replaceStrings
substring substring
stringLength stringLength
@ -19,6 +20,21 @@ in rec {
stringLength stringLength
; ;
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);
removeSuffix = suffix: str: removeSuffix = suffix: str:
# Before 23.05, paths would be copied to the store before converting them # Before 23.05, paths would be copied to the store before converting them
# to strings and comparing. This was surprising and confusing. # to strings and comparing. This was surprising and confusing.
@ -37,4 +53,6 @@ in rec {
then substring 0 (sLen - sufLen) str then substring 0 (sLen - sufLen) str
else str else str
); );
stringToCharacters = s: genList (p: substring p 1 s) (stringLength s);
} }