R/count_trailing_spaces.R

Defines functions count_trailing_spaces

Documented in count_trailing_spaces

#' Count the number of spaces before the first character
#' @param line line of text
#' @return the number of spaces before the first character
#' @author Richèl J.C. Bilderbeek
#' @examples
#' check_empty_beautier_folder()
#'
#' # 0
#' count_trailing_spaces("x")
#' # 1
#' count_trailing_spaces(" y")
#' # 2
#' count_trailing_spaces("  <")
#' # 0
#' count_trailing_spaces("")
#' # 1
#' count_trailing_spaces(" ")
#' # 2
#' count_trailing_spaces("  ")
#'
#' check_empty_beautier_folder()
#' @export
count_trailing_spaces <- function(
  line
) {
  for (i in 1:nchar(line)) {
    char <- substring(line, i, i)
    if (char != " ") return(i - 1)
  }
  nchar(line)
}

Try the beautier package in your browser

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

beautier documentation built on Nov. 2, 2023, 5:08 p.m.