@@ -267,6 +267,15 @@ static inline void skip_white(const char **pch)
267267 * pch = ch ;
268268}
269269
270+ static inline const char * skip_to_comment_or_nonwhite (const char * ch )
271+ {
272+ while (ch < eof && (* ch == ' ' || * ch == '\t' || * ch == '\0' )) {
273+ if (commentChar && * ch == commentChar ) break ;
274+ ch ++ ;
275+ }
276+ return ch ;
277+ }
278+
270279/**
271280 * eol() accepts a position and, if any of the following line endings, moves to the end of that sequence
272281 * and returns true. Repeated \\r around \n are considered one. At most one \\n will be moved over, though.
@@ -2238,8 +2247,7 @@ int freadMain(freadMainArgs _args)
22382247 ((lenOff * * ) fctx .targets )[8 ]++ ;
22392248 if (commentChar ) {
22402249 // skip leading whitespace to detect inline comment marker in header row
2241- const char * commentPos = ch ;
2242- while (commentPos < eof && (* commentPos == ' ' || * commentPos == '\t' || * commentPos == '\0' )) commentPos ++ ;
2250+ const char * commentPos = skip_to_comment_or_nonwhite (ch );
22432251 if (commentPos < eof && * commentPos == commentChar ) {
22442252 ch = commentPos ;
22452253 while (ch < eof && * ch != '\n' && * ch != '\r' ) ch ++ ;
@@ -2254,8 +2262,7 @@ int freadMain(freadMainArgs _args)
22542262 }
22552263 if (commentChar ) {
22562264 // fast-trim trailing comment text after the header names
2257- const char * commentPos = ch ;
2258- while (commentPos < eof && (* commentPos == ' ' || * commentPos == '\t' || * commentPos == '\0' )) commentPos ++ ;
2265+ const char * commentPos = skip_to_comment_or_nonwhite (ch );
22592266 if (commentPos < eof && * commentPos == commentChar ) {
22602267 ch = commentPos ;
22612268 while (ch < eof && * ch != '\n' && * ch != '\r' ) ch ++ ;
@@ -2504,8 +2511,7 @@ int freadMain(freadMainArgs _args)
25042511
25052512 if (commentChar ) {
25062513 // treat lines whose first non-space character is the comment marker as empty
2507- const char * afterWhite = tLineStart ;
2508- while (afterWhite < eof && (* afterWhite == ' ' || * afterWhite == '\t' || * afterWhite == '\0' )) afterWhite ++ ;
2514+ const char * afterWhite = skip_to_comment_or_nonwhite (tLineStart );
25092515 if (afterWhite < eof && * afterWhite == commentChar ) {
25102516 const char * skip = afterWhite ;
25112517 while (skip < eof && * skip != '\n' && * skip != '\r' ) skip ++ ;
@@ -2659,8 +2665,7 @@ int freadMain(freadMainArgs _args)
26592665 if (thisSize ) ((char * * ) targets )[size [j ]] += size [j ]; // 'if' to avoid undefined NULL+=0 when rereading
26602666 j ++ ;
26612667 if (commentChar ) {
2662- const char * commentPtr = tch ;
2663- while (commentPtr < eof && (* commentPtr == ' ' || * commentPtr == '\t' || * commentPtr == '\0' )) commentPtr ++ ;
2668+ const char * commentPtr = skip_to_comment_or_nonwhite (tch );
26642669 if (commentPtr < eof && * commentPtr == commentChar ) {
26652670 tch = commentPtr ;
26662671 while (tch < eof && * tch != '\n' && * tch != '\r' ) tch ++ ;
0 commit comments