Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-6485981

Enhancement for methods in Matcher

XMLWordPrintable

    • Icon: Enhancement Enhancement
    • Resolution: Duplicate
    • Icon: P5 P5
    • None
    • 5.0
    • core-libs

      A DESCRIPTION OF THE REQUEST :
      We need the symmetrical methods in the Matcher to avoid the the synchronized methods of StringBuffer:

      (existed) public Matcher appendReplacement(StringBuffer sb, String replacement).
      (to,create) public Matcher appendReplacement(StringBuilder sb, String replacement).

      (existed) public StringBuffer appendTail(StringBuffer sb)
      (to creat) public StringBuffer appendTail(StringBuilder sb)

      JUSTIFICATION :
      to avoid the use of StringBuffer

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      more performant
      ACTUAL -
      less performant

      CUSTOMER SUBMITTED WORKAROUND :
      AbstractStringBuilder >>
          public Matcher appendReplacement(AbstractStringBuilder sb, String replacement) {

              // If no match, return error
              if (first < 0)
                  throw new IllegalStateException("No match available");

              // Process substitution string to replace group references with groups
              int cursor = 0;
              String s = replacement;
              StringBuffer result = new StringBuffer();

              while (cursor < replacement.length()) {
                  char nextChar = replacement.charAt(cursor);
                  if (nextChar == '\\') {
                      cursor++;
                      nextChar = replacement.charAt(cursor);
                      result.append(nextChar);
                      cursor++;
                  } else if (nextChar == '$') {
                      // Skip past $
                      cursor++;

                      // The first number is always a group
                      int refNum = (int)replacement.charAt(cursor) - '0';
                      if ((refNum < 0)||(refNum > 9))
                          throw new IllegalArgumentException(
                              "Illegal group reference");
                      cursor++;

                      // Capture the largest legal group string
                      boolean done = false;
                      while (!done) {
                          if (cursor >= replacement.length()) {
                              break;
                          }
                          int nextDigit = replacement.charAt(cursor) - '0';
                          if ((nextDigit < 0)||(nextDigit > 9)) { // not a number
                              break;
                          }
                          int newRefNum = (refNum * 10) + nextDigit;
                          if (groupCount() < newRefNum) {
                              done = true;
                          } else {
                              refNum = newRefNum;
                              cursor++;
                          }
                      }

                      // Append group
                      if (group(refNum) != null)
                          result.append(group(refNum));
                  } else {
                      result.append(nextChar);
                      cursor++;
                  }
              }

              // Append the intervening text
              sb.append(getSubSequence(lastAppendPosition, first));
              // Append the match substitution
              sb.append(result.toString());

              lastAppendPosition = last;
      return this;

            sherman Xueming Shen
            ndcosta Nelson Dcosta (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: