-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #171 from atorus-research/gh_issue_170
Introduce `replace_leading_whitespace()` for #170
- Loading branch information
Showing
6 changed files
with
124 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
#' Reformat strings with leading whitespace for HTML | ||
#' | ||
#' @param x Target string | ||
#' @param tab_width Number of spaces to compensate for tabs | ||
#' | ||
#' @return String with replaced for leading whitespace | ||
#' @export | ||
#' | ||
#' @examples | ||
#' x <- c(" Hello there", " Goodbye Friend ", "\tNice to meet you", | ||
#' " \t What are you up to? \t \t ") | ||
#' replace_leading_whitespace(x) | ||
#' | ||
#' replace_leading_whitespace(x, tab=2) | ||
#' | ||
replace_leading_whitespace <- function(x, tab_width=4) { | ||
# Pull out the leading whitespace chunk | ||
leading_spaces <- str_match(x, "^([ \\t])+")[,1] | ||
# Count spaces and tabs, factor in tab width | ||
spaces <- str_count(leading_spaces, pattern = " ") | ||
tabs <- str_count(leading_spaces, pattern = "\\t") * tab_width | ||
leading_length <- as.integer(spaces + tabs) | ||
|
||
# Build the string and combine with the trimmed string | ||
nbsp_string <- map_chr(leading_length, \(.x) { | ||
if (!is.na(.x)) { | ||
paste(rep(" ", .x), collapse="") | ||
} else { | ||
"" | ||
}}) | ||
minus_whitespace <- str_trim(x, side='left') | ||
paste(nbsp_string, minus_whitespace, sep="") | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
test_that("Test replacement of leading whitespace", { | ||
x <- c("Hello there", " Goodbye Friend ", "\tNice to meet you", " \t What are you up to? \t \t ") | ||
|
||
expect_equal( | ||
replace_leading_whitespace(x), | ||
c("Hello there", " Goodbye Friend ", | ||
" Nice to meet you", " What are you up to? \t \t ") | ||
) | ||
|
||
expect_equal( | ||
replace_leading_whitespace(x, tab=2), | ||
c("Hello there", " Goodbye Friend ", | ||
" Nice to meet you", " What are you up to? \t \t ") | ||
) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters