lib.strings.remove{Prefix,Suffix}: Deprecate for path prefix/suffix arguments

See also parent commits
This commit is contained in:
Silvan Mosberger 2023-03-15 19:49:33 +01:00
parent 5e8b9de728
commit 61012f6daf

View File

@ -601,14 +601,23 @@ rec {
prefix: prefix:
# Input string # Input string
str: str:
let # Before 23.05, paths would be copied to the store before converting them
# to strings and comparing. This was surprising and confusing.
warnIf
(isPath prefix)
''
lib.strings.removePrefix: The first argument (${toString prefix}) 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 prefix 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
preLen = stringLength prefix; preLen = stringLength prefix;
sLen = stringLength str; sLen = stringLength str;
in in
if hasPrefix prefix str then if substring 0 preLen str == prefix then
substring preLen (sLen - preLen) str substring preLen (sLen - preLen) str
else else
str; str);
/* Return a string without the specified suffix, if the suffix matches. /* Return a string without the specified suffix, if the suffix matches.
@ -625,14 +634,23 @@ rec {
suffix: suffix:
# Input string # Input string
str: str:
let # 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; sufLen = stringLength suffix;
sLen = stringLength str; sLen = stringLength str;
in in
if sufLen <= sLen && suffix == substring (sLen - sufLen) sufLen str then if sufLen <= sLen && suffix == substring (sLen - sufLen) sufLen str then
substring 0 (sLen - sufLen) str substring 0 (sLen - sufLen) str
else else
str; str);
/* Return true if string v1 denotes a version older than v2. /* Return true if string v1 denotes a version older than v2.