summary refs log tree commit diff
path: root/lib/strings.nix
diff options
context:
space:
mode:
authorPatrick Hilhorst <git@hilhorst.be>2018-06-06 22:04:35 +0200
committerPatrick Hilhorst <git@hilhorst.be>2018-11-06 00:04:21 +0100
commit320c9c10de8489604c459127079f59a3ef4d8475 (patch)
tree1e8568c447c03ca9a21148630667accee8d2454b /lib/strings.nix
parent48caae504502d36c65e3aa9d9b6aee7d06085057 (diff)
downloadnixpkgs-320c9c10de8489604c459127079f59a3ef4d8475.tar
nixpkgs-320c9c10de8489604c459127079f59a3ef4d8475.tar.gz
nixpkgs-320c9c10de8489604c459127079f59a3ef4d8475.tar.bz2
nixpkgs-320c9c10de8489604c459127079f59a3ef4d8475.tar.lz
nixpkgs-320c9c10de8489604c459127079f59a3ef4d8475.tar.xz
nixpkgs-320c9c10de8489604c459127079f59a3ef4d8475.tar.zst
nixpkgs-320c9c10de8489604c459127079f59a3ef4d8475.zip
make-derivation: use pname-version as default name if both are present
Diffstat (limited to 'lib/strings.nix')
-rw-r--r--lib/strings.nix20
1 files changed, 20 insertions, 0 deletions
diff --git a/lib/strings.nix b/lib/strings.nix
index 4d7fa1e774d..48420a36781 100644
--- a/lib/strings.nix
+++ b/lib/strings.nix
@@ -236,6 +236,26 @@ rec {
     in lenContent >= lenSuffix &&
        substring (lenContent - lenSuffix) lenContent content == suffix;
 
+  /* Determine whether a string contains the given infix
+
+    Type: hasInfix :: string -> string -> bool
+
+    Example:
+      hasInfix "bc" "abcd"
+      => true
+      hasInfix "ab" "abcd"
+      => true
+      hasInfix "cd" "abcd"
+      => true
+      hasInfix "foo" "abcd"
+      => false
+  */
+  hasInfix = infix: content:
+    let
+      drop = x: substring 1 (stringLength x) x;
+    in hasPrefix infix content
+      || content != "" && hasInfix infix (drop content);
+
   /* Convert a string to a list of characters (i.e. singleton strings).
      This allows you to, e.g., map a function over each character.  However,
      note that this will likely be horribly inefficient; Nix is not a