Skip to content

Commit

Permalink
an internal helper function strip_blank() to remove blank elements fr…
Browse files Browse the repository at this point in the history
…om both ends of a char vector
  • Loading branch information
yihui committed Nov 24, 2024
1 parent 66838ac commit ae934e2
Showing 1 changed file with 9 additions and 0 deletions.
9 changes: 9 additions & 0 deletions R/string.R
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,15 @@
#' xfun::is_blank(c('', ' ', 'abc'))
is_blank = function(x) grepl('^\\s*$', x)

# remote blank elements from both ends
strip_blank = function(x) {
if (!(N <- length(x))) return(x)
r = rle(is_blank(x))
l = r$lengths; v = r$values; n = length(v)
i = c(if (v[1]) seq_len(l[1]), if (n > 1 && v[n]) N - seq_len(l[n]) + 1)
if (length(i)) x[-i] else x
}

#' Convert numbers to English words
#'
#' This can be helpful when writing reports with \pkg{knitr}/\pkg{rmarkdown} if
Expand Down

0 comments on commit ae934e2

Please sign in to comment.