about summary refs log tree commit diff
path: root/lib/strings.nix
diff options
context:
space:
mode:
authorSilvan Mosberger <silvan.mosberger@tweag.io>2023-03-15 19:49:33 +0100
committerSilvan Mosberger <silvan.mosberger@tweag.io>2023-03-15 19:51:38 +0100
commit61012f6daf61e2cca664c333453a8b868908dc97 (patch)
treef1d2ad6163bbb4420758997bd68d4fd411f58624 /lib/strings.nix
parent5e8b9de7285497e4ef749eb9c55916dd26fecb88 (diff)
lib.strings.remove{Prefix,Suffix}: Deprecate for path prefix/suffix arguments
See also parent commits
Diffstat (limited to 'lib/strings.nix')
-rw-r--r--lib/strings.nix28
1 files changed, 23 insertions, 5 deletions
diff --git a/lib/strings.nix b/lib/strings.nix
index be4e8ece59361..46a62522e1f47 100644
--- a/lib/strings.nix
+++ b/lib/strings.nix
@@ -601,14 +601,23 @@ rec {
     prefix:
     # Input string
     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;
       sLen = stringLength str;
     in
-      if hasPrefix prefix str then
+      if substring 0 preLen str == prefix then
         substring preLen (sLen - preLen) str
       else
-        str;
+        str);
 
   /* Return a string without the specified suffix, if the suffix matches.
 
@@ -625,14 +634,23 @@ rec {
     suffix:
     # Input string
     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;
       sLen = stringLength str;
     in
       if sufLen <= sLen && suffix == substring (sLen - sufLen) sufLen str then
         substring 0 (sLen - sufLen) str
       else
-        str;
+        str);
 
   /* Return true if string v1 denotes a version older than v2.