1#' Partition a string from the right.
2#'
3#' Split the string at the last occurrence of \code{sep}, and return a list of character vectors containing the part before the separator,
4#' the separator itself, and the part after the separator.
5#'
6#' If the separator is not found, return a character vector containing two empty strings, followed by the string itself.
7#'
8#' @param str A character vector.
9#' @param sep A character string.
10#'
11#' @return A character vector.
12#'
13#' @references \url{https://docs.python.org/3/library/stdtypes.html#str.rpartition}
14#'
15#' @seealso \code{\link{pystr_partition}}
16#'
17#' @examples
18#' pystr_rpartition("onetwothreeonetwothree", "two")
19#'
20#' @export
21pystr_rpartition <- function(str, sep) {
22  return(mapply(pystr_rpartition_, str, sep, SIMPLIFY=FALSE, USE.NAMES=FALSE))
23}
24
25pystr_rpartition_ <- function(str, sep) {
26  if(sep == "") {
27    stop("Empty separator.")
28  }
29
30  idx = pystr_rfind(str, sep)
31
32  if(idx == -1) {
33    return(c("", "", str))
34  }
35
36  before = substr(str, 1, idx - 1)
37  after = substr(str, idx + nchar(sep), nchar(str))
38  return(c(before, sep, after))
39}
40