Permalink
Browse files

Fix #1190

Before this fix, splitting a single character input string with a
Splitter.onPattern instance created with a zero-width regex pattern,
would have caused the input string to be dropped from the output,
resulting in an empty iterable being returned rather than a single
element one.

This fix ensures that the input passes through untouched.

For example, whereas before in this code snippet, 'words' would have
been initialized as an empty iterable...

   String string = "f";
   Iterable<String> words =
       Splitter.on(Pattern.compile("\\b")).split(string);
   // words is empty!

Fixes #2615, #2086

-------------
Created by MOE: https://github.com/google/moe
MOE_MIGRATED_REVID=137723825
  • Loading branch information...
jbduncan authored and cpovirk committed Oct 31, 2016
1 parent 5367a2d commit 55524c66de8db4c2e44727b69421c7d0e4f30be0
@@ -382,6 +382,21 @@ public void testPatternSplitWordBoundary() {
assertThat(words).containsExactly("foo", "<", "bar", ">", "bletch").inOrder();
}
@GwtIncompatible // java.util.regex.Pattern
public void testPatternSplitWordBoundary_singleCharInput() {
String string = "f";
Iterable<String> words = Splitter.on(Pattern.compile("\\b")).split(string);
assertThat(words).containsExactly("f").inOrder();
}
@AndroidIncompatible // Apparently Gingerbread's regex API is buggy.
@GwtIncompatible // java.util.regex.Pattern
public void testPatternSplitWordBoundary_singleWordInput() {
String string = "foo";
Iterable<String> words = Splitter.on(Pattern.compile("\\b")).split(string);
assertThat(words).containsExactly("foo").inOrder();
}
@GwtIncompatible // java.util.regex.Pattern
public void testPatternSplitEmptyToken() {
String emptyToken = "a. .c";
@@ -557,7 +557,7 @@ protected String computeNext() {
* of the next returned substring -- so nextStart stays the same.
*/
offset++;
if (offset >= toSplit.length()) {
if (offset > toSplit.length()) {
offset = -1;
}
continue;

1 comment on commit 55524c6

@jbduncan

This comment has been minimized.

Show comment
Hide comment
@jbduncan

jbduncan Oct 31, 2016

Contributor

@cpovirk Great! Thanks for taking the time to review this. 👍

Contributor

jbduncan commented on 55524c6 Oct 31, 2016

@cpovirk Great! Thanks for taking the time to review this. 👍

Please sign in to comment.