当前位置: 首页>>代码示例>>Java>>正文


Java StringFormatDetector类代码示例

本文整理汇总了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;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:38,代码来源:WrongTimberUsageDetector.java

示例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;
}
 
开发者ID:weiwenqiang,项目名称:GitHub,代码行数:47,代码来源:WrongTimberUsageDetector.java

示例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();
}
 
开发者ID:bazelbuild,项目名称:bazel,代码行数:43,代码来源:ResourceUsageAnalyzer.java


注:本文中的com.android.tools.lint.checks.StringFormatDetector类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。