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

Matcher.hitEnd returns false for incomplete surrogate pairs

XMLWordPrintable

    • Fix Understood
    • generic
    • generic

      A DESCRIPTION OF THE PROBLEM :
      Complete summary: "Matcher.hitEnd returns false for incomplete surrogate pairs after matches() and lookingAt()"

      When using a Matcher for an incomplete surrogate pair as input, Matcher.hitEnd() returns false after matches() or lookingAt(). Interestingly using it after Matcher.find() works as expected (returning true).


      ---------- BEGIN SOURCE ----------
      import java.util.function.Consumer;
      import java.util.function.Predicate;
      import java.util.regex.Matcher;
      import java.util.regex.Pattern;

      public class MatcherTest {
          public static void main(String[] args) {
              checkHitEnd("normal", MatcherTest::checkHitEndNormal);
              checkHitEnd("incomplete surrogate pair", MatcherTest::checkHitEndIncompleteSurrogatePair);
          }
          
          private static void checkHitEnd(String checkName, Predicate<Consumer<Matcher>> checkMethod) {
              System.out.println("hitEnd(): " + checkName);
              System.out.println(" matches: " + checkMethod.test(Matcher::matches));
              System.out.println(" lookingAt: " + checkMethod.test(Matcher::lookingAt));
              System.out.println(" find: " + checkMethod.test(Matcher::find));
          }

          private static boolean checkHitEndNormal(Consumer<Matcher> matcherAction) {
              final String input = "abc";
              final Pattern pattern = Pattern.compile("abcd");
              final Matcher matcher = pattern.matcher(input);
              matcherAction.accept(matcher);
              return matcher.hitEnd();
          }
          
          private static boolean checkHitEndIncompleteSurrogatePair(Consumer<Matcher> matcherAction) {
              final String hexString = "1F30C";
              final char[] chars = Character.toChars(Integer.parseInt(hexString, 16));
              final Pattern pattern = Pattern.compile("\\x{" + hexString + "}");
              final Matcher matcher = pattern.matcher(String.valueOf(chars[0]));
              matcherAction.accept(matcher);
              return matcher.hitEnd();
          }
      }

      ---------- END SOURCE ----------

      FREQUENCY : always


            igraves Ian Graves
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated: