Skip to content

Commit 62096fd

Browse files
committed
Make byLine handle all of the line terminators equally.
1 parent f49c858 commit 62096fd

File tree

2 files changed

+48
-4
lines changed

2 files changed

+48
-4
lines changed

src/main/java/rx/observables/StringObservable.java

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -551,15 +551,26 @@ public void onNext(String t) {
551551
}
552552

553553
/**
554-
* Splits the {@link Observable} of Strings by lines and numbers them (zero based index)
554+
* Splits the {@link Observable} of Strings by line ending characters in a platform independent way. It is equivalent to
555+
* <pre>split(src, "(\\r\\n)|\\n|\\r|\\u0085|\\u2028|\\u2029")</pre>
555556
* <p>
556557
* <img width="640" src="https://raw.github.com/wiki/ReactiveX/RxJava/images/rx-operators/St.byLine.png" alt="">
557558
*
558559
* @param source
559560
* @return the Observable conaining the split lines of the source
561+
* @see StringObservable#split(Observable, Pattern)
560562
*/
561563
public static Observable<String> byLine(Observable<String> source) {
562-
return split(source, System.getProperty("line.separator"));
564+
return split(source, ByLinePatternHolder.BY_LINE);
565+
}
566+
567+
/**
568+
* Lazy initialization of the pattern.
569+
*
570+
* https://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html#lt
571+
*/
572+
private static final class ByLinePatternHolder {
573+
private static final Pattern BY_LINE = Pattern.compile("(\\r\\n)|\\n|\\r|\\u0085|\\u2028|\\u2029");
563574
}
564575

565576
/**

src/test/java/rx/observables/StringObservableTest.java

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,9 +356,42 @@ public synchronized int read(byte[] b, int off, int len) {
356356
}
357357

358358
@Test
359-
public void testByLine() {
360-
String newLine = System.getProperty("line.separator");
359+
public void testByLine_Newline() {
360+
String newLine = String.valueOf(new char[] { 0x0A });
361+
testByLineGeneric(newLine);
362+
}
363+
364+
@Test
365+
public void testByLine_CarriageNewline() {
366+
String newLine = String.valueOf(new char[] { 0x0D, 0x0A });
367+
testByLineGeneric(newLine);
368+
}
369+
370+
@Test
371+
public void testByLine_Carriage() {
372+
String newLine = String.valueOf(new char[] { 0x0D });
373+
testByLineGeneric(newLine);
374+
}
375+
376+
@Test
377+
public void testByLine_NextLine() {
378+
String newLine = String.valueOf(new char[] { 0x0085 });
379+
testByLineGeneric(newLine);
380+
}
381+
382+
@Test
383+
public void testByLine_LineSeparator() {
384+
String newLine = String.valueOf(new char[] { 0x2028 });
385+
testByLineGeneric(newLine);
386+
}
387+
388+
@Test
389+
public void testByLine_ParagraphSeparator() {
390+
String newLine = String.valueOf(new char[] { 0x2029 });
391+
testByLineGeneric(newLine);
392+
}
361393

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

0 commit comments

Comments
 (0)