Skip to content

Commit

Permalink
Polish implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
hadley committed Jul 16, 2024
1 parent 60e6fe1 commit a8607e9
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 47 deletions.
48 changes: 22 additions & 26 deletions R/remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -21,58 +21,54 @@ str_remove_all <- function(string, pattern) {
}


#' @title str_dedent
#' @description Remove common leading indentation from strings
#' @note This function is similar to Python's `dedent` function in the `textwrap` library. It removes common leading indentation from strings.
#' Remove common leading indentation from strings
#'
#' This function is similar to Python's `dedent` function in the `textwrap`
#' library. It removes common leading indentation from strings.
#'
#' @param text `character` The input string or character vector.
#' @return The input string or character vector with leading indentation removed.
#' @author chrimaho
#' @importFrom stringr str_replace_all
#' @importFrom stringr str_extract
#' @importFrom stringr regex
#' @export
#' @examples
#' dedent(" Hello\n World")
#' # Expected Output: "Hello\n World"
#' str_dedent(" Hello\n World")
#'
#' dedent(" Line 1\n Line 2\n Line 3")
#' # Expected Output: "Line 1\nLine 2\nLine 3"
#' str_dedent(" Line 1\n Line 2\n Line 3")
#'
#' dedent("No indentation")
#' # Expected Output: "No indentation"
#' str_dedent("No indentation")
#'
#' dedent(
#' str_dedent(
#' "
#' this
#' is
#' a
#' test
#' "
#' )
#' # Expected Output: "\nthis\nis\n a\ntest\n"
#' @rdname str_dedent
#' @export
str_dedent <- function(text) {
lines <- strsplit(text, "\n", fixed=TRUE)[[1]]
lines <- str_split_1(text, fixed("\n"))

# Determine the common leading whitespace
leading_ws <- NULL
leading_ws <- ""
for (line in lines) {
if (str_extract(line, regex("^\\s*$"))) {
# Ignore completely blank lines
if (str_detect(line, "^\\s*$")) {
next
}
leading_ws <- str_extract(line, regex("^\\s+"))
break

ws <- str_extract(line, "^\\s+")
if (!is.na(ws)) {
leading_ws <- ws
break
}
}

if (is.null(leading_ws)) {
return(text)

Check warning on line 66 in R/remove.R

View check run for this annotation

Codecov / codecov/patch

R/remove.R#L66

Added line #L66 was not covered by tests
}

# Remove the common leading whitespace from each line
dedented_lines <- str_replace_all(lines, regex(sprintf("^%s", leading_ws)), "")
dedented_lines <- str_replace_all(lines, paste0("^", leading_ws), "")

# Combine the lines back into a single string
dedented_text <- paste(dedented_lines, collapse = "\n")

return(dedented_text)
paste(dedented_lines, collapse = "\n")
}
23 changes: 7 additions & 16 deletions man/str_dedent.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 5 additions & 5 deletions tests/testthat/test-remove.R
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ test_that("succesfully wraps str_replace_all", {
})

test_that("successfully dedent str_dedent",{
expect_equal(dedent(" Hello\n World"), "Hello\n World")
expect_equal(dedent(" Line 1\n Line 2\n Line 3"), "Line 1\nLine 2\nLine 3")
expect_equal(dedent("No indentation"), "No indentation")
expect_equal(dedent(
expect_equal(str_dedent(" Hello\n World"), "Hello\n World")
expect_equal(str_dedent(" Line 1\n Line 2\n Line 3"), "Line 1\nLine 2\nLine 3")
expect_equal(str_dedent("No indentation"), "No indentation")
expect_equal(str_dedent(
"
this
is
a
test
"
), "\nthis\nis\n a\ntest\n")
})
})

0 comments on commit a8607e9

Please sign in to comment.