in tools/device_broker/java/com/google/android/apps/common/testing/broker/shell/ShellUtils.java [90:143]
public static void tokenize(List<String> options, String optionString)
throws TokenizationException {
// See test suite for examples.
//
// Note: backslash escapes the following character, except within a
// single-quoted region where it is literal.
StringBuilder token = new StringBuilder();
boolean forceToken = false;
char quotation = '\0'; // NUL, '\'' or '"'
for (int ii = 0, len = optionString.length(); ii < len; ii++) {
char c = optionString.charAt(ii);
if (quotation != '\0') { // in quotation
if (c == quotation) { // end of quotation
quotation = '\0';
} else if (c == '\\' && quotation == '"') { // backslash in "-quotation
if (++ii == len) {
throw new TokenizationException("backslash at end of string");
}
c = optionString.charAt(ii);
if (c != '\\' && c != '"') {
token.append('\\');
}
token.append(c);
} else { // regular char, in quotation
token.append(c);
}
} else { // not in quotation
if (c == '\'' || c == '"') { // begin single/double quotation
quotation = c;
forceToken = true;
} else if (c == ' ' || c == '\t') { // space, not quoted
if (forceToken || token.length() > 0) {
options.add(token.toString());
token = new StringBuilder();
forceToken = false;
}
} else if (c == '\\') { // backslash, not quoted
if (++ii == len) {
throw new TokenizationException("backslash at end of string");
}
token.append(optionString.charAt(ii));
} else { // regular char, not quoted
token.append(c);
}
}
}
if (quotation != '\0') {
throw new TokenizationException("unterminated quotation");
}
if (forceToken || token.length() > 0) {
options.add(token.toString());
}
}