Merge "Filter out empty entry from more keys CSV"

This commit is contained in:
Tadashi G. Takaoka 2012-02-03 01:54:49 -08:00 committed by Android (Google) Code Review
commit 9f491e34ac
2 changed files with 20 additions and 8 deletions

View File

@ -325,7 +325,7 @@ public class KeySpecParser {
return null;
}
if (Utils.codePointCount(text) == 1) {
return new String[] { text };
return text.codePointAt(0) == COMMA ? null : new String[] { text };
}
ArrayList<String> list = null;
@ -333,10 +333,13 @@ public class KeySpecParser {
for (int pos = 0; pos < size; pos++) {
final char c = text.charAt(pos);
if (c == COMMA) {
if (list == null) {
list = new ArrayList<String>();
// Skip empty entry.
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
start = pos + 1;
} else if (c == ESCAPE_CHAR) {
@ -344,10 +347,13 @@ public class KeySpecParser {
pos++;
}
}
final String remain = (size - start > 0) ? text.substring(start) : null;
if (list == null) {
return new String[] { text.substring(start) };
return remain != null ? new String[] { remain } : null;
} else {
list.add(text.substring(start));
if (remain != null) {
list.add(remain);
}
return list.toArray(new String[list.size()]);
}
}

View File

@ -42,7 +42,8 @@ public class KeySpecParserCsvTests extends AndroidTestCase {
final String actual[] = KeySpecParser.parseCsvString(value, mTestResources,
R.string.empty_string);
if (expected.length == 0) {
assertNull(message, actual);
assertNull(message + ": expected=null actual=" + Arrays.toString(actual),
actual);
return;
}
assertEquals(message + ": expected=" + Arrays.toString(expected)
@ -74,6 +75,11 @@ public class KeySpecParserCsvTests extends AndroidTestCase {
public void testParseCsvTextZero() {
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() {
@ -82,7 +88,7 @@ public class KeySpecParserCsvTests extends AndroidTestCase {
assertTextArray("Single escape", "\\", "\\");
assertTextArray("Space", " ", " ");
assertTextArray("Single label", "abc", "abc");
assertTextArray("Single srrogate pairs label", SURROGATE2, SURROGATE2);
assertTextArray("Single surrogate pairs label", SURROGATE2, SURROGATE2);
assertTextArray("Spaces", " ", " ");
assertTextArray("Spaces in label", "a b c", "a b c");
assertTextArray("Spaces at beginning of label", " abc", " abc");