1#' Create a character map.
2#'
3#' Create a list of character mappings usable for \code{\link{pystr_translate}}.
4#'
5#' The two arguments must be strings of equal length, and in the resulting
6#' dictionary, each character in \code{x} will be mapped to the character at the
7#' same position in \code{y}.
8#'
9#' @param x A string.
10#' @param y A string.
11#'
12#' @return A list of character mappings for use in \code{\link{pystr_translate}}.
13#'
14#' @references \url{https://docs.python.org/3/library/stdtypes.html#str.maketrans}
15#'
16#' @seealso \code{\link{pystr_translate}}
17#'
18#' @examples
19#' map = pystr_maketrans("abc", "123")
20#' pystr_translate("a blue cat", map)
21#'
22#' @export
23pystr_maketrans <- function(x, y) {
24  if(nchar(x) != nchar(y)) {
25    stop("x and y have inequal lengths.")
26  }
27
28  map = list()
29
30  for(i in 1:nchar(x)) {
31    xletter = substr(x, i, i)
32    yletter = substr(y, i, i)
33    map[xletter] = yletter
34  }
35
36  return(map)
37}
38