Skip to content

Commit

Permalink
StringLiner fix.
Browse files Browse the repository at this point in the history
  • Loading branch information
matejonnet committed Jul 14, 2020
1 parent d6d79b1 commit 8bcd720
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ public void append(String string) {
}

public synchronized String nextLine() {
int nlLength = 1;
int nlPosition = stringBuffer.indexOf("\n");
if (nlPosition == -1) {
nlPosition = stringBuffer.indexOf("\r");
}
if (nlPosition == -1) {
nlPosition = stringBuffer.indexOf("\r\n");
int nlLength;
int nlPosition = stringBuffer.indexOf("\r\n");
if (nlPosition > -1) {
nlLength = 2;
} else {
nlLength = 1;
nlPosition = stringBuffer.indexOf("\n");
if (nlPosition == -1) {
nlPosition = stringBuffer.indexOf("\r");
}
}
if (nlPosition > -1) {
String line = stringBuffer.substring(0, nlPosition);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package org.jboss.pnc.buildagent.common;

import org.junit.Assert;
import org.junit.Test;

public class StringLinerTest {
@Test
public void shouldSplitStringOnLines() {
StringLiner stringLiner = new StringLiner();

stringLiner.append("aa");
String line = stringLiner.nextLine();
Assert.assertNull(line);

stringLiner.append("\r\n");
line = stringLiner.nextLine();
Assert.assertEquals("aa", line);

stringLiner.append("bb\n");
line = stringLiner.nextLine();
Assert.assertEquals("bb", line);

stringLiner.append("cc\r");
line = stringLiner.nextLine();
Assert.assertEquals("cc", line);

stringLiner.append("dd");
line = stringLiner.nextLine();
Assert.assertNull(line);

stringLiner.append("\n");
line = stringLiner.nextLine();
Assert.assertEquals("dd", line);
}
}

0 comments on commit 8bcd720

Please sign in to comment.