Skip to content
Snippets Groups Projects
Commit 9f491e34 authored by Tadashi G. Takaoka's avatar Tadashi G. Takaoka Committed by Android (Google) Code Review
Browse files

Merge "Filter out empty entry from more keys CSV"

parents 0de2fbd7 24cd2617
No related branches found
No related tags found
No related merge requests found
...@@ -325,7 +325,7 @@ public class KeySpecParser { ...@@ -325,7 +325,7 @@ public class KeySpecParser {
return null; return null;
} }
if (Utils.codePointCount(text) == 1) { if (Utils.codePointCount(text) == 1) {
return new String[] { text }; return text.codePointAt(0) == COMMA ? null : new String[] { text };
} }
ArrayList<String> list = null; ArrayList<String> list = null;
...@@ -333,10 +333,13 @@ public class KeySpecParser { ...@@ -333,10 +333,13 @@ public class KeySpecParser {
for (int pos = 0; pos < size; pos++) { for (int pos = 0; pos < size; pos++) {
final char c = text.charAt(pos); final char c = text.charAt(pos);
if (c == COMMA) { if (c == COMMA) {
if (list == null) { // Skip empty entry.
list = new ArrayList<String>(); if (pos - start > 0) {
if (list == null) {
list = new ArrayList<String>();
}
list.add(text.substring(start, pos));
} }
list.add(text.substring(start, pos));
// Skip comma // Skip comma
start = pos + 1; start = pos + 1;
} else if (c == ESCAPE_CHAR) { } else if (c == ESCAPE_CHAR) {
...@@ -344,10 +347,13 @@ public class KeySpecParser { ...@@ -344,10 +347,13 @@ public class KeySpecParser {
pos++; pos++;
} }
} }
final String remain = (size - start > 0) ? text.substring(start) : null;
if (list == null) { if (list == null) {
return new String[] { text.substring(start) }; return remain != null ? new String[] { remain } : null;
} else { } else {
list.add(text.substring(start)); if (remain != null) {
list.add(remain);
}
return list.toArray(new String[list.size()]); return list.toArray(new String[list.size()]);
} }
} }
......
...@@ -42,7 +42,8 @@ public class KeySpecParserCsvTests extends AndroidTestCase { ...@@ -42,7 +42,8 @@ public class KeySpecParserCsvTests extends AndroidTestCase {
final String actual[] = KeySpecParser.parseCsvString(value, mTestResources, final String actual[] = KeySpecParser.parseCsvString(value, mTestResources,
R.string.empty_string); R.string.empty_string);
if (expected.length == 0) { if (expected.length == 0) {
assertNull(message, actual); assertNull(message + ": expected=null actual=" + Arrays.toString(actual),
actual);
return; return;
} }
assertEquals(message + ": expected=" + Arrays.toString(expected) assertEquals(message + ": expected=" + Arrays.toString(expected)
...@@ -74,6 +75,11 @@ public class KeySpecParserCsvTests extends AndroidTestCase { ...@@ -74,6 +75,11 @@ public class KeySpecParserCsvTests extends AndroidTestCase {
public void testParseCsvTextZero() { public void testParseCsvTextZero() {
assertTextArray("Empty string", ""); assertTextArray("Empty string", "");
assertTextArray("Empty entry", ",");
assertTextArray("Empty entry at beginning", ",a", "a");
assertTextArray("Empty entry at end", "a,", "a");
assertTextArray("Empty entry at middle", "a,,b", "a", "b");
assertTextArray("Empty entries with escape", ",a,b\\,c,,d,", "a", "b\\,c", "d");
} }
public void testParseCsvTextSingle() { public void testParseCsvTextSingle() {
...@@ -82,7 +88,7 @@ public class KeySpecParserCsvTests extends AndroidTestCase { ...@@ -82,7 +88,7 @@ public class KeySpecParserCsvTests extends AndroidTestCase {
assertTextArray("Single escape", "\\", "\\"); assertTextArray("Single escape", "\\", "\\");
assertTextArray("Space", " ", " "); assertTextArray("Space", " ", " ");
assertTextArray("Single label", "abc", "abc"); assertTextArray("Single label", "abc", "abc");
assertTextArray("Single srrogate pairs label", SURROGATE2, SURROGATE2); assertTextArray("Single surrogate pairs label", SURROGATE2, SURROGATE2);
assertTextArray("Spaces", " ", " "); assertTextArray("Spaces", " ", " ");
assertTextArray("Spaces in label", "a b c", "a b c"); assertTextArray("Spaces in label", "a b c", "a b c");
assertTextArray("Spaces at beginning of label", " abc", " abc"); assertTextArray("Spaces at beginning of label", " abc", " abc");
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment