diff --git a/src/main/java/rx/observables/StringObservable.java b/src/main/java/rx/observables/StringObservable.java index f474252..df7edcd 100644 --- a/src/main/java/rx/observables/StringObservable.java +++ b/src/main/java/rx/observables/StringObservable.java @@ -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 + *
split(src, "(\\r\\n)|\\n|\\r|\\u0085|\\u2028|\\u2029")
*

* * * @param source * @return the Observable conaining the split lines of the source + * @see StringObservable#split(Observable, Pattern) */ public static Observable byLine(Observable 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"); } /** diff --git a/src/test/java/rx/observables/StringObservableTest.java b/src/test/java/rx/observables/StringObservableTest.java index 9be8106..a0bd98a 100644 --- a/src/test/java/rx/observables/StringObservableTest.java +++ b/src/test/java/rx/observables/StringObservableTest.java @@ -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 lines = byLine( Observable.from(Arrays.asList("qwer", newLine + "asdf" + newLine, "zx", "cv"))) .toList().toBlocking().single();