Filter out empty entry from more keys CSV

Change-Id: I9a26aaa58f0f502b3bfef9de422370d2b0add8d8
main
Tadashi G. Takaoka 2012-02-03 18:37:30 +09:00
parent 909b2940db
commit 24cd2617f5
2 changed files with 20 additions and 8 deletions

View File

@ -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 {
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) {
// Skip empty entry.
if (pos - start > 0) {
if (list == null) { if (list == null) {
list = new ArrayList<String>(); 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 {
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()]);
} }
} }

View File

@ -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 {
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 {
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");