Skip to content

Commit

Permalink
Merge pull request #31 from abersnaze/byLine
Browse files Browse the repository at this point in the history
Make byLine handle carriage newline and just newline equally.
  • Loading branch information
abersnaze committed Mar 16, 2016
2 parents f49c858 + 62096fd commit 0a5fb5d
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 4 deletions.
15 changes: 13 additions & 2 deletions src/main/java/rx/observables/StringObservable.java
Original file line number Diff line number Diff line change
Expand Up @@ -551,15 +551,26 @@ public void onNext(String t) {
}

/**
* Splits the {@link Observable} of Strings by lines and numbers them (zero based index)
* Splits the {@link Observable} of Strings by line ending characters in a platform independent way. It is equivalent to
* <pre>split(src, "(\\r\\n)|\\n|\\r|\\u0085|\\u2028|\\u2029")</pre>
* <p>
* <img width="640" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/St.byLine.png" alt="">
*
* @param source
* @return the Observable conaining the split lines of the source
* @see StringObservable#split(Observable, Pattern)
*/
public static Observable<String> byLine(Observable<String> source) {
return split(source, System.getProperty("line.separator"));
return split(source, ByLinePatternHolder.BY_LINE);
}

/**
* Lazy initialization of the pattern.
*
* https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#lt
*/
private static final class ByLinePatternHolder {
private static final Pattern BY_LINE = Pattern.compile("(\\r\\n)|\\n|\\r|\\u0085|\\u2028|\\u2029");
}

/**
Expand Down
37 changes: 35 additions & 2 deletions src/test/java/rx/observables/StringObservableTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,42 @@ public synchronized int read(byte[] b, int off, int len) {
}

@Test
public void testByLine() {
String newLine = System.getProperty("line.separator");
public void testByLine_Newline() {
String newLine = String.valueOf(new char[] { 0x0A });
testByLineGeneric(newLine);
}

@Test
public void testByLine_CarriageNewline() {
String newLine = String.valueOf(new char[] { 0x0D, 0x0A });
testByLineGeneric(newLine);
}

@Test
public void testByLine_Carriage() {
String newLine = String.valueOf(new char[] { 0x0D });
testByLineGeneric(newLine);
}

@Test
public void testByLine_NextLine() {
String newLine = String.valueOf(new char[] { 0x0085 });
testByLineGeneric(newLine);
}

@Test
public void testByLine_LineSeparator() {
String newLine = String.valueOf(new char[] { 0x2028 });
testByLineGeneric(newLine);
}

@Test
public void testByLine_ParagraphSeparator() {
String newLine = String.valueOf(new char[] { 0x2029 });
testByLineGeneric(newLine);
}

private void testByLineGeneric(String newLine) {
List<String> lines = byLine(
Observable.from(Arrays.asList("qwer", newLine + "asdf" + newLine, "zx", "cv")))
.toList().toBlocking().single();
Expand Down

0 comments on commit 0a5fb5d

Please sign in to comment.