@@ -505,44 +505,68 @@ public boolean startNewLine() throws IOException
505505 * @since 2.10.1
506506 */
507507 public boolean skipLinesWhenNeeded () throws IOException {
508- if (!(_allowComments || _skipBlankLines )) {
508+ if (_allowComments ) {
509+ return _skipCommentLines ();
510+ }
511+ if (!_skipBlankLines ) {
509512 return hasMoreInput ();
510513 }
511- int firstCharacterPtr = _inputPtr ;
514+
515+ // only need to skip fully empty lines
512516 while (hasMoreInput ()) {
513- char ch = _inputBuffer [_inputPtr ++ ];
517+ char ch = _inputBuffer [_inputPtr ];
514518 if (ch == '\r' || ch == '\n' ) {
519+ ++_inputPtr ;
515520 _pendingLF = ch ;
516521 _handleLF ();
517- // track the start of the new line
518- firstCharacterPtr = _inputPtr ;
519522 continue ;
520523 }
521- if (ch == ' ' ) {
524+ if (ch != ' ' ) {
525+ return true ; // processing can go on
526+ }
527+ ++_inputPtr ;
528+ }
529+ return false ; // end of input
530+ }
531+
532+ public boolean _skipCommentLines () throws IOException
533+ {
534+ while ((_inputPtr < _inputEnd ) || loadMore ()) {
535+ char ch = _inputBuffer [_inputPtr ];
536+ switch (ch ) {
537+ case '#' :
538+ ++_inputPtr ;
539+ _skipCommentContents ();
540+ continue ;
541+ case '\r' :
542+ case '\n' :
543+ ++_inputPtr ;
544+ _pendingLF = ch ;
545+ _handleLF ();
546+ continue ;
547+ case ' ' :
522548 // skip all blanks (in both comments/blanks skip mode)
549+ ++_inputPtr ;
523550 continue ;
551+ default :
552+ return true ;
524553 }
525- if (_allowComments ) {
526- if (_inputBuffer [firstCharacterPtr ] == '#' ) {
527- // on a commented line, skip everything
528- continue ;
529- }
530- if (ch == '#' ) {
531- // we reach this point when whitespaces precedes the hash character
532- // move the firstCharacterPtr to the '#' location in order to skip the line completely
533- firstCharacterPtr = _inputPtr -1 ;
534- continue ;
535- }
536- }
537- // we reached a non skippable character, this line needs to be parsed
538- // rollback the input pointer to the beginning of the line
539- _inputPtr = firstCharacterPtr ;
540- return true ; // processing can go on
541554 }
542555 return false ; // end of input
543556 }
544557
545- // 12-Apr-2020, tatu: Not used any more (probably replaced by above?)
558+ private void _skipCommentContents () throws IOException
559+ {
560+ while ((_inputPtr < _inputEnd ) || loadMore ()) {
561+ char ch = _inputBuffer [_inputPtr ++];
562+ if (ch == '\r' || ch == '\n' ) {
563+ _pendingLF = ch ;
564+ _handleLF ();
565+ break ;
566+ }
567+ }
568+ }
569+
546570 /*
547571 private final static int INT_HASH = '#';
548572
@@ -559,7 +583,8 @@ protected int _skipCommentLines() throws IOException
559583 // Ok, skipped the end of the line. Check next one...
560584 int i = _nextChar();
561585 if (i != INT_HASH) {
562- return i;
586+ --_inputPtr;
587+ return true;
563588 }
564589 }
565590 return -1; // end of input
0 commit comments