lib/strings: add hasPrefix and simplify hasSuffix
It was discussed as a part of #2570.
This commit is contained in:
parent
180bd65cd0
commit
bf2bc0f0dc
|
@ -56,12 +56,14 @@ rec {
|
||||||
optionalString = cond: string: if cond then string else "";
|
optionalString = cond: string: if cond then string else "";
|
||||||
|
|
||||||
|
|
||||||
# Determine whether a filename ends in the given suffix.
|
# Determine whether a string has given prefix/suffix.
|
||||||
hasSuffix = ext: fileName:
|
hasPrefix = pref: str:
|
||||||
let lenFileName = stringLength fileName;
|
substring 0 (stringLength pref) str == pref;
|
||||||
lenExt = stringLength ext;
|
hasSuffix = suff: str:
|
||||||
in !(lessThan lenFileName lenExt) &&
|
let lenStr = stringLength str;
|
||||||
substring (sub lenFileName lenExt) lenFileName fileName == ext;
|
lenSuff = stringLength suff;
|
||||||
|
in lenStr >= lenSuff &&
|
||||||
|
substring (lenStr - lenSuff) lenStr str == suff;
|
||||||
|
|
||||||
|
|
||||||
# Convert a string to a list of characters (i.e. singleton strings).
|
# Convert a string to a list of characters (i.e. singleton strings).
|
||||||
|
|
Loading…
Reference in a new issue