Skip to content

trim() mishandles indentation-only lines shorter than min_indent #163

@alandipert

Description

@alandipert

trim() calculates the "minimum indent" of every line after the first, and then removes that number of characters from the beginning of every line after the first. I encountered this while working on the related #162.

min_indent is calculated by a routine that ignores indentation (spaces and tabs) on blank lines - lines containing only indentation. Consequently, the newline that terminates these lines can get trimmed, which causes the following line to include spurious leading whitespace.

Failing example:

library(glue)
library(testthat)
expect_identical(
  trim("
       \ta
       \tb
      \t
       \tc"),
  "a\nb\n\nc"
)

In the character vector passed to trim(), the penultimate line contains only indentation (6 spaces followed by a tab).

When trim() is copying parts of the old string to a new one, it skips over the newline that delimits the penultimate and last lines, and the whitespace from the penultimate line is included in the last line.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions