Skip to content

Commit a25dd37

Browse files
committed
control skipping white spaces before comments with strip.white
1 parent 29bdb3d commit a25dd37

File tree

2 files changed

+17
-3
lines changed

2 files changed

+17
-3
lines changed

inst/tests/tests.Rraw

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21769,3 +21769,11 @@ test(2341.21, fread('# meta1 # meta2
2176921769
a,b
2177021770
1,2', comment.char = '#'), data.table(a=1L, b=2L))
2177121771
test(2341.22, fread('a,b # inline header comment\r\n1,2\r\n', comment.char = '#'), data.table(a=1L, b=2L))
21772+
# control skipping white space before comments with strip.white
21773+
test(2341.23, fread('a
21774+
b # trailing cmnt
21775+
', comment.char = '#', strip.white = FALSE, sep = ","), data.table(a="b "))
21776+
test(2341.24, fread('a
21777+
# leading cmnt
21778+
b
21779+
', comment.char = '#', strip.white = FALSE, sep = ","), data.table(a=c(" ", "b")))

src/fread.c

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -273,9 +273,15 @@ static inline void skip_white(const char **pch)
273273
*/
274274
static inline const char *skip_to_comment_or_nonwhite(const char *ch)
275275
{
276-
while (ch < eof && (*ch == ' ' || *ch == '\t' || *ch == '\0')) {
277-
if (commentChar && *ch == commentChar) break; // comment char might be space or tab
278-
ch++;
276+
while (ch < eof && *ch == '\0') ++ch;
277+
if (!stripWhite) return ch;
278+
279+
const unsigned char stopSpace = (commentChar == ' ');
280+
const unsigned char stopTab = (commentChar == '\t');
281+
282+
while (ch < eof && (*ch == ' ' || *ch == '\t')) {
283+
if ((stopSpace && *ch == ' ') || (stopTab && *ch == '\t')) break;
284+
++ch;
279285
}
280286
return ch;
281287
}

0 commit comments

Comments
 (0)