dedent: Remove Common Leading Whitespace

dedentR Documentation

Remove Common Leading Whitespace

Description

Remove the common leading whitespace from each line. This is useful in multi-line strings to make them line up with the left edge of the display, while still presenting them in indented form within the source code.

Usage

dedent(x, strip = TRUE)

Arguments

x

character vector. The strings from which to remove common leading whitespace.

strip

logical. Should the leading and trailing whitespace line be removed from each element of x? This will not remove the leading indent.

Details

strip is TRUE by default because the standard use case is to dedent a multi-line string that appears in the source code. Refer to section Examples.

Tabs and spaces are both whitespace, but they are not treated equally. This is because tab is a different width depending on the context.

Value

character vector, the same length as x, and with the dim, dimnames, and names attributes of x (after possible coercion to character).

Examples

cat(dedent("
    here is a multi-line string that appears in the source code. we wish to
    remove the common indent from each line, and dedent should do this for us!
    ---- hopefully this works ----
"), sep = "\n")


# for me, a tab prints as eight spaces when preceded by a newline
# but since tab isn't always eight spaces, we treat this as unsolveable :(
# there is no common leading whitespace since "        " != "\t"
cat(dedent("
        another multi-line string, this time with no common leading whitespace
\tanother multi-line string, this time with no common leading whitespace
"), sep = "\n")

ArcadeAntics/essentials documentation built on Nov. 7, 2024, 4:33 p.m.