Java Code Examples for com.google.re2j.Matcher#appendTail()

The following examples show how to use com.google.re2j.Matcher#appendTail() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: FilterReplace.java    From copybara with Apache License 2.0 6 votes vote down vote up
private String replaceString(String originalContent) {
  Pattern pattern = Pattern.compile(before.pattern());
  Matcher matcher = pattern.matcher(originalContent);

  boolean anyReplace = false;
  StringBuilder result = new StringBuilder(originalContent.length());
  while (matcher.find()) {
    String val = matcher.group(FilterReplace.this.group);
    String res = mapping.apply(val);
    anyReplace |= !val.equals(res);
    if (group == 0) {
      matcher.appendReplacement(result, Matcher.quoteReplacement(res));
    } else {
      String prefix = originalContent.substring(matcher.start(), matcher.start(group));
      String suffix = originalContent.substring(matcher.end(group), matcher.end());
      matcher.appendReplacement(result, Matcher.quoteReplacement((prefix + res + suffix)));
    }
  }

  if (!anyReplace) {
    return originalContent;
  }

  matcher.appendTail(result);
  return result.toString();
}
 
Example 2
Source File: SqlTemplate.java    From nomulus with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the freshly substituted SQL code.
 *
 * @throws IllegalArgumentException if any substitution variable is not found in the template,
 *         or if there are any variable-like strings (%something%) left after substitution.
 */
public String build() {
  StringBuffer result = new StringBuffer(template.length());
  Set<String> found = new HashSet<>();
  Matcher matcher = SEARCH_PATTERN.matcher(template);
  while (matcher.find()) {
    String wholeMatch = matcher.group(0);
    String leftQuote = matcher.group(1);
    String key = matcher.group(2);
    String rightQuote = matcher.group(3);
    String value = substitutions.get(key);
    checkArgumentNotNull(value, "%%s% found in template but no substitution specified", key);
    checkArgument(leftQuote.equals(rightQuote), "Quote mismatch: %s", wholeMatch);
    matcher.appendReplacement(result, String.format("%s%s%s", leftQuote, value, rightQuote));
    found.add(key);
  }
  matcher.appendTail(result);
  Set<String> remaining = difference(substitutions.keySet(), found);
  checkArgument(remaining.isEmpty(),
      "Not found in template: %s", Joiner.on(", ").join(remaining));
  return result.toString();
}
 
Example 3
Source File: CamelCase.java    From PacketProxy with Apache License 2.0 5 votes vote down vote up
static public String toCamelCase(String s) {
	Pattern pattern = Pattern.compile("([a-zA-Z][a-zA-Z0-9]*)");
	Matcher matcher = pattern.matcher(s);
	StringBuffer sb = new StringBuffer();
	while (matcher.find()) {
		matcher.appendReplacement(sb, toProperCase(matcher.group()));
	}
	matcher.appendTail(sb);
	return sb.toString();
}
 
Example 4
Source File: TodoReplace.java    From copybara with Apache License 2.0 5 votes vote down vote up
private Set<FileState> run(Iterable<FileState> files, Console console)
    throws IOException, ValidationException {
  Set<FileState> modifiedFiles = new HashSet<>();
  // TODO(malcon): Remove reconstructing pattern once RE2J doesn't synchronize on matching.
  Pattern batchPattern = Pattern.compile(pattern.pattern(), pattern.flags());
  for (FileState file : files) {
    if (Files.isSymbolicLink(file.getPath())) {
      continue;
    }
    String content = new String(Files.readAllBytes(file.getPath()), UTF_8);
    Matcher matcher = batchPattern.matcher(content);
    StringBuffer sb = new StringBuffer();
    boolean modified = false;
    while (matcher.find()) {
      if (matcher.group(2).trim().isEmpty()){
        matcher.appendReplacement(sb, matcher.group(0));
        continue;
      }
      List<String> users = Splitter.on(",").splitToList(matcher.group(2));
      List<String> mappedUsers = mapUsers(users, matcher.group(0), file.getPath(), console);
      modified |= !users.equals(mappedUsers);
      String result = matcher.group(1);
      if (!mappedUsers.isEmpty()) {
        result += "(" + Joiner.on(",").join(mappedUsers) + ")";
      }
      matcher.appendReplacement(sb, Matcher.quoteReplacement(result));
    }
    matcher.appendTail(sb);

    if (modified) {
      modifiedFiles.add(file);
      Files.write(file.getPath(), sb.toString().getBytes(UTF_8));
    }
  }
  return modifiedFiles;
}
 
Example 5
Source File: MarkdownGenerator.java    From copybara with Apache License 2.0 5 votes vote down vote up
private String simplerJavaTypes(TypeMirror typeMirror) {
  String s = typeMirror.toString();
  Matcher m = Pattern.compile("(?:[A-z.]*\\.)*([A-z]+)").matcher(s);
  StringBuilder sb = new StringBuilder();
  while(m.find()) {
    String replacement = deCapitalize(m.group(1));
    m.appendReplacement(sb, replacement);
  }
  m.appendTail(sb);

  return HtmlEscapers.htmlEscaper().escape(sb.toString());
}
 
Example 6
Source File: RegexTemplateTokens.java    From copybara with Apache License 2.0 4 votes vote down vote up
private String replaceLine(String line) {
  if (patternsToIgnore != null) {
    for (Pattern patternToIgnore : patternsToIgnore) {
      if (patternToIgnore.matches(line)) {
        return line;
      }
    }
  }

  Matcher matcher = before.matcher(line);
  StringBuilder sb = new StringBuilder(line.length());
  while (matcher.find()) {
    for (Collection<Integer> groupIndexes : repeatedGroups.asMap().values()) {
      // Check that all the references of the repeated group match the same string
      Iterator<Integer> iterator = groupIndexes.iterator();
      String value = matcher.group(iterator.next());
      while (iterator.hasNext()) {
        if (!value.equals(matcher.group(iterator.next()))) {
          return line;
        }
      }
    }
    String replaceTemplate;
    if (callback != null) {
      ImmutableMap.Builder<Integer, String> groupValues =
          ImmutableMap.builder();
      for (int i = 0; i <= matcher.groupCount(); i++) {
        groupValues.put(i, matcher.group(i));
      }
      replaceTemplate = callback.alter(groupValues.build(), afterReplaceTemplate);
    } else {
      replaceTemplate = afterReplaceTemplate;
    }

    matcher.appendReplacement(sb, replaceTemplate);
    if (firstOnly) {
      break;
    }
  }
  matcher.appendTail(sb);
  return sb.toString();
}