MCPcopy Create free account
hub / github.com/google/re2j / appendReplacementInternal

Method appendReplacementInternal

java/com/google/re2j/Matcher.java:485–545  ·  view source on GitHub ↗
(StringBuilder sb, String replacement)

Source from the content-addressed store, hash-verified

483 }
484
485 private void appendReplacementInternal(StringBuilder sb, String replacement) {
486 int last = 0;
487 int i = 0;
488 int m = replacement.length();
489 for (; i < m - 1; i++) {
490 if (replacement.charAt(i) == '\\') {
491 if (last < i) {
492 sb.append(replacement.substring(last, i));
493 }
494 i++;
495 last = i;
496 continue;
497 }
498 if (replacement.charAt(i) == '$') {
499 int c = replacement.charAt(i + 1);
500 if ('0' <= c && c <= '9') {
501 int n = c - '0';
502 if (last < i) {
503 sb.append(replacement.substring(last, i));
504 }
505 for (i += 2; i < m; i++) {
506 c = replacement.charAt(i);
507 if (c < '0' || c > '9' || n * 10 + c - '0' > groupCount) {
508 break;
509 }
510 n = n * 10 + c - '0';
511 }
512 if (n > groupCount) {
513 throw new IndexOutOfBoundsException("n > number of groups: " + n);
514 }
515 String group = group(n);
516 if (group != null) {
517 sb.append(group);
518 }
519 last = i;
520 i--;
521 continue;
522 } else if (c == '{') {
523 if (last < i) {
524 sb.append(replacement.substring(last, i));
525 }
526 i++; // skip {
527 int j = i + 1;
528 while (j < replacement.length()
529 && replacement.charAt(j) != '}'
530 && replacement.charAt(j) != ' ') {
531 j++;
532 }
533 if (j == replacement.length() || replacement.charAt(j) != '}') {
534 throw new IllegalArgumentException("named capture group is missing trailing '}'");
535 }
536 String groupName = replacement.substring(i + 1, j);
537 sb.append(group(groupName));
538 last = j + 1;
539 }
540 }
541 }
542 if (last < m) {

Callers 1

appendReplacementMethod · 0.95

Calls 4

groupMethod · 0.95
appendMethod · 0.80
substringMethod · 0.80
lengthMethod · 0.45

Tested by

no test coverage detected