3636import com .igormaznitsa .jcp .utils .ResetablePrinter ;
3737import java .io .File ;
3838import java .io .IOException ;
39+ import java .util .ArrayList ;
3940import java .util .Collection ;
4041import java .util .HashSet ;
4142import java .util .List ;
4546import java .util .regex .Matcher ;
4647import java .util .regex .Pattern ;
4748import java .util .stream .Collectors ;
49+ import java .util .stream .IntStream ;
4850import lombok .Data ;
4951
5052/**
@@ -434,10 +436,12 @@ public PreprocessingState preprocessFile(final PreprocessingState state,
434436 return this .preprocessFileWithNotification (state , context , true );
435437 }
436438
439+ private static final String EOL_MARKER = "-=$$$$$$$$__EOL__$$$$$$$$=-" ;
440+
437441 private void flushTextBufferForRemovedComments (
438442 final AtomicReference <Map .Entry <String , String >> firstDetectedUncommentLinePtr ,
439443 final int stringIndex ,
440- final StringBuilder textBuffer ,
444+ final List < String > textPieces ,
441445 final ResetablePrinter resetablePrinter ,
442446 final PreprocessingState state ,
443447 final PreprocessorContext context )
@@ -446,11 +450,22 @@ private void flushTextBufferForRemovedComments(
446450 final Map .Entry <String , String > firstUncommentLine =
447451 firstDetectedUncommentLinePtr .getAndSet (null );
448452
449- if (textBuffer .length () > 0 ) {
453+ final boolean lastEol = !textPieces .isEmpty () && textPieces .get (textPieces .size () - 1 ) ==
454+ EOL_MARKER ;
455+ final String accumulated = (lastEol ? IntStream .range (0 , textPieces .size () - 1 ) :
456+ IntStream .range (0 , textPieces .size ()))
457+ .mapToObj (textPieces ::get )
458+ .map (x -> (x == EOL_MARKER ? context .getEol () : x ))
459+ .collect (Collectors .joining ());
460+ textPieces .clear ();
461+
462+ if (accumulated .isEmpty ()) {
463+ if (lastEol ) {
464+ resetablePrinter .print (context .getEol ());
465+ }
466+ } else {
450467 final List <CommentTextProcessor > processors = context .getCommentTextProcessors ();
451- final String textToProcess = textBuffer .toString ();
452- textBuffer .setLength (0 );
453- String text = textToProcess ;
468+ String text = accumulated ;
454469
455470 if (!processors .isEmpty ()) {
456471 final FilePositionInfo filePositionInfo =
@@ -459,12 +474,12 @@ private void flushTextBufferForRemovedComments(
459474
460475 final List <String > results = processors
461476 .stream ()
462- .filter (x -> x .isEnabled (this , filePositionInfo , context , state ))
477+ .filter (x -> x .isAllowed (this , filePositionInfo , context , state ))
463478 .map (x -> {
464479 try {
465480 return x .processUncommentedText (
466481 indent ,
467- textToProcess ,
482+ accumulated ,
468483 this ,
469484 filePositionInfo ,
470485 context ,
@@ -474,18 +489,18 @@ private void flushTextBufferForRemovedComments(
474489 throw new PreprocessorException (
475490 "Error during external comment text processor call: " +
476491 x .getClass ().getCanonicalName (),
477- textToProcess , state .makeIncludeStack (), ex );
492+ accumulated , state .makeIncludeStack (), ex );
478493 }
479494 }).collect (Collectors .toList ());
480495
481496 if (results .isEmpty ()) {
482497 context .logDebug ("No any result from processors for text block at " + filePositionInfo );
483- text = textToProcess ;
498+ text = accumulated ;
484499 } else {
485- text = String . join ( "" , results );
500+ text = results . stream (). collect ( Collectors . joining ( context . getEol ()) );
486501 }
487502 }
488- resetablePrinter .print (text );
503+ resetablePrinter .print (text + ( lastEol ? context . getEol () : "" ) );
489504 }
490505 }
491506
@@ -526,7 +541,7 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
526541 String leftTrimmedString = null ;
527542
528543 TextFileDataContainer lastTextFileDataContainer = null ;
529- final StringBuilder textBlockBuffer = new StringBuilder ();
544+ final List < String > textPieces = new ArrayList <> ();
530545
531546 Integer firstBlockLineIndex = null ;
532547 try {
@@ -560,7 +575,8 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
560575 this .flushTextBufferForRemovedComments (
561576 firstUncommentLine ,
562577 requireNonNullElse (firstBlockLineIndex , findLastReadLineIndex (theState )),
563- textBlockBuffer , thePrinter ,
578+ textPieces ,
579+ thePrinter ,
564580 theState ,
565581 context );
566582 firstBlockLineIndex = null ;
@@ -588,12 +604,12 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
588604 }
589605
590606 String stringToBeProcessed = leftTrimmedString ;
591- final boolean doPrintLn = presentedNextLine || !context .isCareForLastEol ();
607+ final boolean doPrintEol = presentedNextLine || !context .isCareForLastEol ();
592608
593609 if (isHashPrefixed (stringToBeProcessed , context )) {
594610 this .flushTextBufferForRemovedComments (firstUncommentLine ,
595611 requireNonNullElse (firstBlockLineIndex , findLastReadLineIndex (theState )),
596- textBlockBuffer ,
612+ textPieces ,
597613 thePrinter ,
598614 theState ,
599615 context );
@@ -607,7 +623,7 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
607623 final String text = stringPrefix +
608624 AbstractDirectiveHandler .PREFIX_FOR_KEEPING_LINES_PROCESSED_DIRECTIVES +
609625 extractedDirective ;
610- if (doPrintLn ) {
626+ if (doPrintEol ) {
611627 thePrinter .println (text , context .getEol ());
612628 } else {
613629 thePrinter .print (text );
@@ -619,7 +635,7 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
619635 final String text = stringPrefix +
620636 AbstractDirectiveHandler .PREFIX_FOR_KEEPING_LINES_PROCESSED_DIRECTIVES +
621637 extractedDirective ;
622- if (doPrintLn ) {
638+ if (doPrintEol ) {
623639 thePrinter .println (text , context .getEol ());
624640 } else {
625641 thePrinter .print (text );
@@ -658,26 +674,27 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
658674 firstBlockLineIndex = findLastReadLineIndex (theState );
659675 firstUncommentLine .set (indentText );
660676 }
661- textBlockBuffer . append (indentText .getValue ());
662- if (doPrintLn ) {
663- textBlockBuffer . append ( context . getEol () );
677+ textPieces . add (indentText .getValue ());
678+ if (doPrintEol ) {
679+ textPieces . add ( EOL_MARKER );
664680 }
665681 } else {
666682 this .flushTextBufferForRemovedComments (firstUncommentLine ,
667683 requireNonNullElse (firstBlockLineIndex , findLastReadLineIndex (theState )),
668- textBlockBuffer ,
684+ textPieces ,
669685 thePrinter ,
670686 theState , context );
671687 firstBlockLineIndex = null ;
672688
673- textBlockBuffer .append (stringPrefix ).append (indentText .getKey ())
674- .append (indentText .getValue ());
675- if (doPrintLn ) {
676- textBlockBuffer .append (context .getEol ());
689+ textPieces .add (stringPrefix );
690+ textPieces .add (indentText .getKey ());
691+ textPieces .add (indentText .getValue ());
692+ if (doPrintEol ) {
693+ textPieces .add (EOL_MARKER );
677694 }
678695 this .flushTextBufferForRemovedComments (firstUncommentLine ,
679696 requireNonNullElse (firstBlockLineIndex , findLastReadLineIndex (theState )),
680- textBlockBuffer ,
697+ textPieces ,
681698 thePrinter ,
682699 theState , context );
683700 firstBlockLineIndex = null ;
@@ -701,27 +718,29 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
701718 firstUncommentLine .set (indentText );
702719 }
703720
704- textBlockBuffer . append (indentText .getValue ());
705- if (doPrintLn ) {
706- textBlockBuffer . append ( context . getEol () );
721+ textPieces . add (indentText .getValue ());
722+ if (doPrintEol ) {
723+ textPieces . add ( EOL_MARKER );
707724 }
708725 } else {
709726 this .flushTextBufferForRemovedComments (firstUncommentLine ,
710727 requireNonNullElse (firstBlockLineIndex , findLastReadLineIndex (theState )),
711- textBlockBuffer ,
728+ textPieces ,
712729 thePrinter ,
713730 theState , context );
714731 firstBlockLineIndex = null ;
715732
716- textBlockBuffer .append (stringPrefix ).append (indentText .getKey ())
717- .append (indentText .getValue ());
718- if (doPrintLn ) {
719- textBlockBuffer .append (context .getEol ());
733+ textPieces .add (stringPrefix );
734+ textPieces .add (indentText .getKey ());
735+ textPieces .add (indentText .getValue ());
736+
737+ if (doPrintEol ) {
738+ textPieces .add (EOL_MARKER );
720739 }
721740 this .flushTextBufferForRemovedComments (
722741 firstUncommentLine ,
723742 requireNonNullElse (firstBlockLineIndex , findLastReadLineIndex (theState )),
724- textBlockBuffer ,
743+ textPieces ,
725744 thePrinter ,
726745 theState , context );
727746 firstBlockLineIndex = null ;
@@ -730,7 +749,7 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
730749 // Just string
731750 this .flushTextBufferForRemovedComments (firstUncommentLine ,
732751 requireNonNullElse (firstBlockLineIndex , findLastReadLineIndex (theState )),
733- textBlockBuffer ,
752+ textPieces ,
734753 thePrinter ,
735754 theState , context );
736755 firstBlockLineIndex = null ;
@@ -745,24 +764,23 @@ public PreprocessingState preprocessFileWithNotification(final PreprocessingStat
745764 }
746765
747766 thePrinter .print (stringPrefix );
748- if (doPrintLn ) {
767+ if (doPrintEol ) {
749768 thePrinter .println (strToOut , context .getEol ());
750769 } else {
751770 thePrinter .print (strToOut );
752771 }
753772 }
754773 } else if (context .isKeepLines ()) {
755-
756774 flushTextBufferForRemovedComments (firstUncommentLine ,
757775 requireNonNullElse (firstBlockLineIndex , findLastReadLineIndex (theState )),
758- textBlockBuffer ,
776+ textPieces ,
759777 thePrinter ,
760778 theState ,
761779 context );
762780 firstBlockLineIndex = null ;
763781
764782 final String text = AbstractDirectiveHandler .PREFIX_FOR_KEEPING_LINES + rawString ;
765- if (doPrintLn ) {
783+ if (doPrintEol ) {
766784 thePrinter .println (text , context .getEol ());
767785 } else {
768786 thePrinter .print (text );
0 commit comments