diff options
author | Vladimír Čunát <vcunat@gmail.com> | 2014-05-13 11:05:37 +0200 |
---|---|---|
committer | Vladimír Čunát <vcunat@gmail.com> | 2014-05-13 11:05:37 +0200 |
commit | bf2bc0f0dc5177bfe5833520b06ae2830031de85 (patch) | |
tree | ff2a8a68fe976b1912a42804da592aef2acf99f8 /lib/strings.nix | |
parent | 180bd65cd048c328dd8123fef7d0ba757d93a23c (diff) | |
download | nixpkgs-bf2bc0f0dc5177bfe5833520b06ae2830031de85.tar nixpkgs-bf2bc0f0dc5177bfe5833520b06ae2830031de85.tar.gz nixpkgs-bf2bc0f0dc5177bfe5833520b06ae2830031de85.tar.bz2 nixpkgs-bf2bc0f0dc5177bfe5833520b06ae2830031de85.tar.lz nixpkgs-bf2bc0f0dc5177bfe5833520b06ae2830031de85.tar.xz nixpkgs-bf2bc0f0dc5177bfe5833520b06ae2830031de85.tar.zst nixpkgs-bf2bc0f0dc5177bfe5833520b06ae2830031de85.zip |
lib/strings: add hasPrefix and simplify hasSuffix
It was discussed as a part of #2570.
Diffstat (limited to 'lib/strings.nix')
-rw-r--r-- | lib/strings.nix | 14 |
1 files changed, 8 insertions, 6 deletions
diff --git a/lib/strings.nix b/lib/strings.nix index fa3cdd9711e..5f76da5c33c 100644 --- a/lib/strings.nix +++ b/lib/strings.nix @@ -56,12 +56,14 @@ rec { optionalString = cond: string: if cond then string else ""; - # Determine whether a filename ends in the given suffix. - hasSuffix = ext: fileName: - let lenFileName = stringLength fileName; - lenExt = stringLength ext; - in !(lessThan lenFileName lenExt) && - substring (sub lenFileName lenExt) lenFileName fileName == ext; + # Determine whether a string has given prefix/suffix. + hasPrefix = pref: str: + substring 0 (stringLength pref) str == pref; + hasSuffix = suff: str: + let lenStr = stringLength str; + lenSuff = stringLength suff; + in lenStr >= lenSuff && + substring (lenStr - lenSuff) lenStr str == suff; # Convert a string to a list of characters (i.e. singleton strings). |