本文整理汇总了Java中com.android.tools.lint.checks.StringFormatDetector类的典型用法代码示例。如果您正苦于以下问题:Java StringFormatDetector类的具体用法?Java StringFormatDetector怎么用?Java StringFormatDetector使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
StringFormatDetector类属于com.android.tools.lint.checks包,在下文中一共展示了StringFormatDetector类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: getStringArgumentTypes
import com.android.tools.lint.checks.StringFormatDetector; //导入依赖的package包/类
private static List<String> getStringArgumentTypes(String formatString) {
List<String> types = new ArrayList<>();
Matcher matcher = StringFormatDetector.FORMAT.matcher(formatString);
int index = 0;
int prevIndex = 0;
while (true) {
if (matcher.find(index)) {
int matchStart = matcher.start();
while (prevIndex < matchStart) {
char c = formatString.charAt(prevIndex);
if (c == '\\') {
prevIndex++;
}
prevIndex++;
}
if (prevIndex > matchStart) {
index = prevIndex;
continue;
}
index = matcher.end();
String str = formatString.substring(matchStart, matcher.end());
if ("%%".equals(str) || "%n".equals(str)) {
continue;
}
String time = matcher.group(5);
if ("t".equalsIgnoreCase(time)) {
types.add(time + matcher.group(6));
} else {
types.add(matcher.group(6));
}
} else {
break;
}
}
return types;
}
示例2: getFormatArgumentCount
import com.android.tools.lint.checks.StringFormatDetector; //导入依赖的package包/类
private static int getFormatArgumentCount(@NonNull String s) {
Matcher matcher = StringFormatDetector.FORMAT.matcher(s);
int index = 0;
int prevIndex = 0;
int nextNumber = 1;
int max = 0;
while (true) {
if (matcher.find(index)) {
String value = matcher.group(6);
if ("%".equals(value) || "n".equals(value)) {
index = matcher.end();
continue;
}
int matchStart = matcher.start();
for (; prevIndex < matchStart; prevIndex++) {
char c = s.charAt(prevIndex);
if (c == '\\') {
prevIndex++;
}
}
if (prevIndex > matchStart) {
index = prevIndex;
continue;
}
int number;
String numberString = matcher.group(1);
if (numberString != null) {
// Strip off trailing $
numberString = numberString.substring(0, numberString.length() - 1);
number = Integer.parseInt(numberString);
nextNumber = number + 1;
} else {
number = nextNumber++;
}
if (number > max) {
max = number;
}
index = matcher.end();
} else {
break;
}
}
return max;
}
示例3: convertFormatStringToRegexp
import com.android.tools.lint.checks.StringFormatDetector; //导入依赖的package包/类
@VisibleForTesting
static String convertFormatStringToRegexp(String formatString) {
StringBuilder regexp = new StringBuilder();
int from = 0;
boolean hasEscapedLetters = false;
Matcher matcher = StringFormatDetector.FORMAT.matcher(formatString);
int length = formatString.length();
while (matcher.find(from)) {
int start = matcher.start();
int end = matcher.end();
if (start == 0 && end == length) {
// Don't match if the entire string literal starts with % and ends with
// the a formatting character, such as just "%d": this just matches absolutely
// everything and is unlikely to be used in a resource lookup
return NO_MATCH;
}
if (start > from) {
hasEscapedLetters |= appendEscapedPattern(formatString, regexp, from, start);
}
// If the wildcard follows a previous wildcard, just skip it
// (e.g. don't convert %s%s into .*.*; .* is enough.
int regexLength = regexp.length();
if (regexLength < 2
|| regexp.charAt(regexLength - 1) != '*'
|| regexp.charAt(regexLength - 2) != '.') {
regexp.append(".*");
}
from = end;
}
if (from < length) {
hasEscapedLetters |= appendEscapedPattern(formatString, regexp, from, length);
}
if (!hasEscapedLetters) {
// If the regexp contains *only* formatting characters, e.g. "%.0f%d", or
// if it contains only formatting characters and punctuation, e.g. "%s_%d",
// don't treat this as a possible resource name pattern string: it is unlikely
// to be intended for actual resource names, and has the side effect of matching
// most names.
return NO_MATCH;
}
return regexp.toString();
}