diff --git a/nib/std/default.nix b/nib/std/default.nix index 718979f..354b7ef 100644 --- a/nib/std/default.nix +++ b/nib/std/default.nix @@ -2,11 +2,13 @@ attrs = import ./attrs.nix args; functions = import ./functions.nix args; lists = import ./lists.nix args; + strings = import ./strings.nix args; trivial = import ./trivial.nix args; in attrs.mergeAttrsList [ attrs functions lists + strings trivial ] diff --git a/nib/std/strings.nix b/nib/std/strings.nix new file mode 100644 index 0000000..a017ed0 --- /dev/null +++ b/nib/std/strings.nix @@ -0,0 +1,38 @@ +{nib, ...}: let + inherit + (builtins) + isPath + substring + stringLength + ; + + inherit + (nib.trivial) + warnIf + ; +in rec { + # re-export builtin string methods + inherit + substring + stringLength + ; + + 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 + ); +}