am 9f491e34: Merge "Filter out empty entry from more keys CSV"

* commit '9f491e34ac2c96c3d18f30f9710b1a3ecab173e7':
  Filter out empty entry from more keys CSV
main
Tadashi G. Takaoka 2012-02-03 01:56:42 -08:00 committed by Android Git Automerger
commit 303a0f3f15
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");