code: public static int parsePattern(String pat,
              Replaceable text,
              int index,
              int limit) {
          int ipat = 0;
  
          // empty pattern matches immediately
          if (ipat == pat.length()) {
              return index;
          }
  
          int cpat = Character.codePointAt(pat, ipat);
  
          while (index < limit) {
              int c = text.char32At(index);
  
              // parse \s*
              if (cpat == '~') {
                  if (PatternProps.isWhiteSpace(c)) {
                      index += UTF16.getCharCount(c);
                      continue;
                  } else {
                      if (++ipat == pat.length()) {
                          return index; // success; c unparsed
                      }
                      // fall thru; process c again with next cpat
                  }
              }
  
              // parse literal
              else if (c == cpat) {
                  int n = UTF16.getCharCount(c);
                  index += n;
                  ipat += n;
                  if (ipat == pat.length()) {
                      return index; // success; c parsed
                  }
                  // fall thru; get next cpat
              }
  
              // match failure of literal
              else {
                  return -1;
              }
  
              cpat = UTF16.charAt(pat, ipat);
          }
  
          return -1; // text ended before end of pat
      }
docs: Parse a pattern string within the given Replaceable and a parsing
  pattern.  Characters are matched literally and case-sensitively
  except for the following special characters:
  
  ~  zero or more Pattern_White_Space chars
  
  If end of pattern is reached with all matches along the way,
  pos is advanced to the first unparsed index and returned.
  Otherwise -1 is returned.
  @param pat pattern that controls parsing
  @param text text to be parsed, starting at index
  @param index offset to first character to parse
  @param limit offset after last character to parse
  @return index after last parsed character, or -1 on parse failure.