Code with Finding: |
class ReplaceStrings {
/**
* Convert the provide string into a Config. The string can be a static function:
* foo(,,?)
* foo.bar(?)
* or a class method:
* foo.prototype.bar(?)
* And is allowed to either replace all parameters using "*" or one parameter "?".
* "," is used as a placeholder for ignored parameters.
*/
private Config parseConfiguration(String function) {
// Looks like this function_name(,$,)
int first = function.indexOf('(');
int last = function.indexOf(')');
// TODO(johnlenz): Make parsing precondition checks JSErrors reports.
Preconditions.checkState(first != -1 && last != -1);
String name = function.substring(0, first);
String params = function.substring(first+1, last);
int paramCount = 0;
int replacementParameter = -1;
String[] parts = params.split(",");
for (String param : parts) {
paramCount++;
if (param.equals(REPLACE_ALL_MARKER)) {
Preconditions.checkState(paramCount == 1 && parts.length == 1);
replacementParameter = Config.REPLACE_ALL_VALUE;
} else if (param.equals(REPLACE_ONE_MARKER)) {
// TODO(johnlenz): Support multiple.
Preconditions.checkState(replacementParameter == -1);
replacementParameter = paramCount;
} else {
// TODO(johnlenz): report an error.
Preconditions.checkState(param.isEmpty(), "Unknown marker", param);
}
}
Preconditions.checkState(replacementParameter != -1);
return new Config(name, replacementParameter);
}
}
|