1#' Translate a string. 2#' 3#' Return a copy of \code{str} where all characters have been mapped through 4#' \code{map}, where \code{map} can be created with \code{\link{pystr_maketrans}}. 5#' 6#' @param str A character vector. 7#' @param map A list of character mappings. 8#' 9#' @return A character vector. 10#' 11#' @references \url{https://docs.python.org/3/library/stdtypes.html#str.translate} 12#' 13#' @seealso \code{\link{pystr_maketrans}} 14#' 15#' @examples 16#' map = pystr_maketrans("abc", "123") 17#' pystr_translate("a blue cat", map) 18#' 19#' @export 20pystr_translate <- function(str, map) { 21 return(vapply(str, function(x) pystr_translate_(x, map), character(1), USE.NAMES = FALSE)) 22} 23 24pystr_translate_ <- function(str, map) { 25 translated = str 26 27 for(i in 1:length(map)) { 28 translated = pystr_replace(translated, names(map[i]), map[[i]]) 29 } 30 31 return(translated) 32} 33