query
On this page

hasSuffix

lib.hasSuffix

Docs pulled from | This Revision | about 1 hour ago


Determine whether a string has given suffix.

Inputs

suffix
Suffix to check for
content
Input string

Type

hasSuffix :: string -> string -> bool

Examples

lib.strings.hasSuffix usage example

hasSuffix "foo" "foobar"
=> false
hasSuffix "foo" "barfoo"
=> true
(lib.strings.hasSuffix)

Noogle detected

Aliases

Implementation

The following is the current implementation of this function.

hasSuffix =
    suffix:
    content:
    let
      lenContent = stringLength content;
      lenSuffix = stringLength suffix;
    in
    # 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.hasSuffix: 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 always returns `false` 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.''
      (
        lenContent >= lenSuffix
        && substring (lenContent - lenSuffix) lenContent content == suffix
      );