query
On this page

removeSuffix

lib.strings.removeSuffix

Docs pulled from | This Revision | 28 minutes ago


Nixpkgs manual

Returns a string without the specified suffix, if the suffix matches.

Inputs

suffix
Suffix to remove if it matches
str
Input string

Type

removeSuffix :: String -> String -> String

Examples

lib.strings.removeSuffix usage example

removeSuffix "front" "homefront"
=> "home"
removeSuffix "xxx" "homefront"
=> "homefront"

Noogle detected

Aliases

Implementation

The following is the current implementation of this function.

removeSuffix =
    suffix:
    let
      sufLen = stringLength suffix;
    in
    if isPath suffix then
      # Before 23.05, paths would be copied to the store before converting them
      # to strings and comparing. This was surprising and confusing.
      throw ''
        lib.strings.removeSuffix: 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 never removes any suffix in such a case.
            This function also copies the path to the Nix store, which may not be what you want.''
    else
      str:
      let
        sLen = stringLength str;
      in
      if sufLen <= sLen && suffix == substring (sLen - sufLen) sufLen str then
        substring 0 (sLen - sufLen) str
      else
        str;