Handle following "real" line breaks correctly

This commit is contained in:
René Pfeuffer
2020-05-29 16:41:33 +02:00
parent ebfc267b93
commit 83145d953b
2 changed files with 41 additions and 6 deletions

View File

@@ -32,7 +32,7 @@ class LineFilteredOutputStream extends OutputStream {
private final int start; private final int start;
private final Integer end; private final Integer end;
private boolean inLineBreak; private Character lastLineBreakCharacter;
private int currentLine = 0; private int currentLine = 0;
LineFilteredOutputStream(OutputStream target, Integer start, Integer end) { LineFilteredOutputStream(OutputStream target, Integer start, Integer end) {
@@ -46,25 +46,39 @@ class LineFilteredOutputStream extends OutputStream {
switch (b) { switch (b) {
case '\n': case '\n':
case '\r': case '\r':
if (!inLineBreak) { if (lastLineBreakCharacter == null) {
inLineBreak = true; keepLineBreakInMind((char) b);
} else if (lastLineBreakCharacter == b) {
if (currentLine > start && currentLine <= end) {
target.write('\n');
}
++currentLine; ++currentLine;
} else {
if (currentLine > start && currentLine <= end) {
target.write('\n');
}
lastLineBreakCharacter = null;
} }
break; break;
default: default:
if (inLineBreak && currentLine > start && currentLine <= end) { if (lastLineBreakCharacter != null && currentLine > start && currentLine <= end) {
target.write('\n'); target.write('\n');
} }
inLineBreak = false; lastLineBreakCharacter = null;
if (currentLine >= start && currentLine < end) { if (currentLine >= start && currentLine < end) {
target.write(b); target.write(b);
} }
} }
} }
public void keepLineBreakInMind(char b) {
lastLineBreakCharacter = b;
++currentLine;
}
@Override @Override
public void close() throws IOException { public void close() throws IOException {
if (inLineBreak && currentLine >= start && currentLine < end) { if (lastLineBreakCharacter != null && currentLine >= start && currentLine < end) {
target.write('\n'); target.write('\n');
} }
target.close(); target.close();

View File

@@ -24,6 +24,7 @@
package sonia.scm.api.v2.resources; package sonia.scm.api.v2.resources;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource; import org.junit.jupiter.params.provider.ValueSource;
@@ -79,4 +80,24 @@ class LineFilteredOutputStreamTest {
assertThat(target.toString()).isEqualTo("line 1\nline 2\n"); assertThat(target.toString()).isEqualTo("line 1\nline 2\n");
} }
@ParameterizedTest
@ValueSource(strings = {"line 1\n\nline 2\n\nline 3", "line 1\r\n\r\nline 2\r\n\r\nline 3"})
void shouldHandleDoubleBlankLinesCorrectly(String input) throws IOException {
LineFilteredOutputStream filtered = new LineFilteredOutputStream(target, 4, null);
filtered.write(input.getBytes());
assertThat(target.toString()).isEqualTo("line 3");
}
@ParameterizedTest
@ValueSource(strings = {"line 1\n\n\nline 2\n\n\nline 3", "line 1\r\n\r\n\r\nline 2\r\n\r\n\r\nline 3"})
void shouldHandleTripleBlankLinesCorrectly(String input) throws IOException {
LineFilteredOutputStream filtered = new LineFilteredOutputStream(target, 4, 6);
filtered.write(input.getBytes());
assertThat(target.toString()).isEqualTo("\n\n");
}
} }