diff --git a/NAMESPACE b/NAMESPACE index 07f842ab..60742f92 100644 --- a/NAMESPACE +++ b/NAMESPACE @@ -19,6 +19,7 @@ export(regex) export(str_c) export(str_conv) export(str_count) +export(str_dedent) export(str_detect) export(str_dup) export(str_ends) diff --git a/R/remove.R b/R/remove.R index a943cede..80825f45 100644 --- a/R/remove.R +++ b/R/remove.R @@ -19,3 +19,56 @@ str_remove <- function(string, pattern) { str_remove_all <- function(string, pattern) { str_replace_all(string, pattern, "") } + + +#' 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. +#' @export +#' @examples +#' str_dedent(" Hello\n World") +#' +#' str_dedent(" Line 1\n Line 2\n Line 3") +#' +#' str_dedent("No indentation") +#' +#' str_dedent( +#' " +#' this +#' is +#' a +#' test +#' " +#' ) +str_dedent <- function(text) { + lines <- str_split_1(text, fixed("\n")) + + # Determine the common leading whitespace + leading_ws <- "" + for (line in lines) { + # Ignore completely blank lines + if (str_detect(line, "^\\s*$")) { + next + } + + ws <- str_extract(line, "^\\s+") + if (!is.na(ws)) { + leading_ws <- ws + break + } + } + + if (is.null(leading_ws)) { + return(text) + } + + # Remove the common leading whitespace from each line + dedented_lines <- str_replace_all(lines, paste0("^", leading_ws), "") + + # Combine the lines back into a single string + paste(dedented_lines, collapse = "\n") +} diff --git a/_pkgdown.yml b/_pkgdown.yml index 741e9dee..19dc1905 100644 --- a/_pkgdown.yml +++ b/_pkgdown.yml @@ -21,6 +21,7 @@ reference: - subtitle: String contents: - str_count + - str_dedent - str_detect - str_escape - str_extract diff --git a/man/str_dedent.Rd b/man/str_dedent.Rd new file mode 100644 index 00000000..9584a20b --- /dev/null +++ b/man/str_dedent.Rd @@ -0,0 +1,34 @@ +% Generated by roxygen2: do not edit by hand +% Please edit documentation in R/remove.R +\name{str_dedent} +\alias{str_dedent} +\title{Remove common leading indentation from strings} +\usage{ +str_dedent(text) +} +\arguments{ +\item{text}{\code{character} The input string or character vector.} +} +\value{ +The input string or character vector with leading indentation removed. +} +\description{ +This function is similar to Python's \code{dedent} function in the \code{textwrap} +library. It removes common leading indentation from strings. +} +\examples{ +str_dedent(" Hello\n World") + +str_dedent(" Line 1\n Line 2\n Line 3") + +str_dedent("No indentation") + +str_dedent( + " + this + is + a + test + " +) +} diff --git a/tests/testthat/test-remove.R b/tests/testthat/test-remove.R index 04f6aa52..fc027428 100644 --- a/tests/testthat/test-remove.R +++ b/tests/testthat/test-remove.R @@ -2,3 +2,17 @@ test_that("succesfully wraps str_replace_all", { expect_equal(str_remove_all("abababa", "ba"), "a") expect_equal(str_remove("abababa", "ba"), "ababa") }) + +test_that("successfully dedent str_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") +})