|
6 | 6 | #' @param width Minimum width of padded strings. |
7 | 7 | #' @param side Side on which padding character is added (left, right or both). |
8 | 8 | #' @param pad Single padding character (default is a space). |
| 9 | +#' @param use_length If `TRUE`, use the number of characters instead of the |
| 10 | +#' total of character widths (see [stringi::stri_width]). |
9 | 11 | #' @return A character vector. |
10 | 12 | #' @seealso [str_trim()] to remove whitespace; |
11 | 13 | #' [str_trunc()] to decrease the maximum width of a string. |
|
24 | 26 | #' |
25 | 27 | #' # Longer strings are returned unchanged |
26 | 28 | #' str_pad("hadley", 3) |
27 | | -str_pad <- function(string, width, side = c("left", "right", "both"), pad = " ") { |
| 29 | +str_pad <- function(string, width, side = c("left", "right", "both"), pad = " ", use_length = FALSE) { |
28 | 30 | side <- match.arg(side) |
29 | 31 |
|
30 | 32 | switch(side, |
31 | | - left = stri_pad_left(string, width, pad = pad), |
32 | | - right = stri_pad_right(string, width, pad = pad), |
33 | | - both = stri_pad_both(string, width, pad = pad) |
| 33 | + left = stri_pad_left(string, width, pad = pad, use_length = use_length), |
| 34 | + right = stri_pad_right(string, width, pad = pad, use_length = use_length), |
| 35 | + both = stri_pad_both(string, width, pad = pad, use_length = use_length) |
34 | 36 | ) |
35 | 37 | } |
0 commit comments