diff --git a/R/remove.R b/R/remove.R index f2292cff..80825f45 100644 --- a/R/remove.R +++ b/R/remove.R @@ -21,26 +21,22 @@ 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 @@ -48,20 +44,22 @@ str_remove_all <- function(string, pattern) { #' 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)) { @@ -69,10 +67,8 @@ str_dedent <- function(text) { } # 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") } diff --git a/man/str_dedent.Rd b/man/str_dedent.Rd index 0eefd1a5..9584a20b 100644 --- a/man/str_dedent.Rd +++ b/man/str_dedent.Rd @@ -2,7 +2,7 @@ % Please edit documentation in R/remove.R \name{str_dedent} \alias{str_dedent} -\title{str_dedent} +\title{Remove common leading indentation from strings} \usage{ str_dedent(text) } @@ -13,22 +13,17 @@ str_dedent(text) The input string or character vector with leading indentation removed. } \description{ -Remove common leading indentation from strings -} -\note{ -This function is similar to Python's \code{dedent} function in the \code{textwrap} library. It removes common leading indentation from strings. +This function is similar to Python's \code{dedent} function in the \code{textwrap} +library. It removes common leading indentation from strings. } \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 @@ -36,8 +31,4 @@ dedent( test " ) -# Expected Output: "\nthis\nis\n a\ntest\n" -} -\author{ -chrimaho } diff --git a/tests/testthat/test-remove.R b/tests/testthat/test-remove.R index ba3b650d..fc027428 100644 --- a/tests/testthat/test-remove.R +++ b/tests/testthat/test-remove.R @@ -4,10 +4,10 @@ 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 @@ -15,4 +15,4 @@ test_that("successfully dedent str_dedent",{ test " ), "\nthis\nis\n a\ntest\n") -}) \ No newline at end of file +})