Skip to content

Commit

Permalink
trim(): fix handling of indentation-only lines shorter than min_… (#164)
Browse files Browse the repository at this point in the history
* Improve handling of lines containing only indentation

* Update NEWS

* Handle short indentation-only lines; fixes #163

* Add test for empty intermediate line handling

Co-authored-by: Jim Hester <james.f.hester@gmail.com>
  • Loading branch information
alandipert and jimhester authored Mar 3, 2020
1 parent 7131fc3 commit 2e3a4e0
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 4 deletions.
2 changes: 1 addition & 1 deletion NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* `single_quote()` `double_quote()` and `backtick()` all return `NA` for `NA`
inputs (#135).

* Improve `trim()`'s handling of lines containing only indentation (#162)
* Improve `trim()`'s handling of lines containing only indentation (#162, #163)

# glue 1.3.1

Expand Down
24 changes: 21 additions & 3 deletions src/trim.c
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
#include "Rinternals.h"
#include <stdbool.h>
#include <stdlib.h>
#include <string.h> // for strlen()
#include <string.h> // for strlen(), strchr(), strncpy()

const char* skip(const char* s, const char* delim) {
while (strchr(delim, s[0])) s++;
return s;
}

SEXP trim_(SEXP x) {
size_t len = LENGTH(x);
Expand Down Expand Up @@ -80,8 +85,21 @@ SEXP trim_(SEXP x) {
i += 2;
continue;
} else if (new_line) {
if (i + min_indent < str_len && (xx[i] == ' ' || xx[i] == '\t')) {
i += min_indent;
const char* after = skip(xx + i, "\t ");
const size_t skipped = after - (xx + i);
/*
* if the line consists only of tabs and spaces, and if the line is
* shorter than min_indent, copy the entire line and proceed to the
* next
*/
if (after[0] == '\n' && skipped < min_indent) {
strncpy(str + j, xx + i, skipped);
i += skipped;
j += skipped;
} else {
if (i + min_indent < str_len && (xx[i] == ' ' || xx[i] == '\t')) {
i += min_indent;
}
}
new_line = false;
continue;
Expand Down
19 changes: 19 additions & 0 deletions tests/testthat/test-trim.R
Original file line number Diff line number Diff line change
Expand Up @@ -136,4 +136,23 @@ test_that("lines containing only indentation are handled properly", {
\tc"),
" \ta\nb\n \t\n \tc"
)
# A line shorter than min_indent that contains only indentation should not be
# trimmed, removed, or prepended to the next line.
expect_identical(
trim("
\ta
\tb
\t
\tc"),
"a\nb\n \t\nc"
)
# Ensure empty intermedite lines are handled properly
expect_identical(
trim("
\ta
\tb
\tc"),
"a\nb\n\nc"
)
})

0 comments on commit 2e3a4e0

Please sign in to comment.