R/string-utils.R

Defines functions str_rep blank_where ncharw

#' Count characters or width of strings
#'
#' Uses stringi to ensure consistent character width calculations.
#'
#' @noRd
ncharw <- function(x, type = "width") {
  if (requireNamespace("crayon", quietly = TRUE)) {
    x <- crayon::strip_style(x)
  }
  # we use stringi throughout to keep the same concept of width
  switch(type,
    width = stringi::stri_width(x),
    chars = stringi::stri_length(x),
    stop("Unrecognized type in ncharw")
  )
}


#' Replace elements with blanks where a condition is TRUE
#'
#' @noRd
blank_where <- function(text, cond) {
  stopifnot(length(text) == length(cond))
  text[cond] <- ""
  text
}


#' Repeat strings elementwise
#'
#' @noRd
str_rep <- function(x, times) {
  mapply(function(s, t) paste0(rep(s, t), collapse = ""), x, times)
}

Try the huxtable package in your browser

Any scripts or data that you put into this service are public.

huxtable documentation built on Aug. 19, 2025, 1:12 a.m.