1#' Check the suffix of a string.
2#'
3#' Return \code{TRUE} if the string \code{str} ends with the specified
4#' \code{suffix}, otherwise return \code{FALSE}.
5#'
6#' @details
7#' With optional \code{start}, test beginning at that position.
8#' With optional \code{end}, stop comparing at that position.
9#'
10#' @param str A character vector.
11#' @param suffix A character vector.
12#' @param start A numeric vector.
13#' @param end A numeric vector.
14#'
15#' @return A logical vector.
16#'
17#' @references \url{https://docs.python.org/3/library/stdtypes.html#str.endswith}
18#'
19#' @seealso \code{\link{pystr_startswith}}
20#'
21#' @examples
22#' pystr_endswith("selfie.jpg", ".jpg")
23#' pystr_endswith("selfie.jpg", ".png")
24#' pystr_endswith("hello world", "ello", 1, 5)
25#'
26#' @export
27pystr_endswith <- function(str, suffix, start=1, end=nchar(str)) {
28  return(mapply(pystr_endswith_, str, suffix, start, end, USE.NAMES=FALSE))
29}
30
31pystr_endswith_ <- function(str, suffix, start, end) {
32  string_to_check = substr(str, start, end)
33  start_check = nchar(string_to_check) - nchar(suffix) + 1
34  end_check = nchar(string_to_check)
35  letters_to_check = substr(string_to_check, start_check, end_check)
36  return(letters_to_check == suffix)
37}
38