From bf2bc0f0dc5177bfe5833520b06ae2830031de85 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20=C4=8Cun=C3=A1t?= Date: Tue, 13 May 2014 11:05:37 +0200 Subject: [PATCH] lib/strings: add hasPrefix and simplify hasSuffix It was discussed as a part of #2570. --- lib/strings.nix | 14 ++++++++------ 1 file 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).