src/java.base/share/classes/java/util/regex/Grapheme.java
author redestad
Sat, 01 Jun 2019 03:18:23 +0200
changeset 55141 db105c4c5776
parent 55013 8dae495a59e7
permissions -rw-r--r--
8225061: Performance regression in Regex Reviewed-by: naoto, alanb Contributed-by: claes.redestad@oracle.com, naoto.sato@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
     1
/*
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
     2
 * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
     4
 *
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    10
 *
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    15
 * accompanied this code).
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    16
 *
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    20
 *
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    23
 * questions.
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    24
 */
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    25
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    26
package java.util.regex;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    27
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    28
import java.util.Objects;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    29
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    30
final class Grapheme {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    31
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    32
    /**
55141
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    33
     * Determines if there is an extended  grapheme cluster boundary between two
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    34
     * continuing characters {@code cp1} and {@code cp2}.
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    35
     * <p>
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    36
     * See Unicode Standard Annex #29 Unicode Text Segmentation for the specification
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    37
     * for the extended grapheme cluster boundary rules
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    38
     * <p>
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    39
     * Note: this method does not take care of stateful breaking.
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    40
     */
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    41
    static boolean isBoundary(int cp1, int cp2) {
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    42
        return rules[getType(cp1)][getType(cp2)];
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    43
    }
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    44
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    45
    /**
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    46
     * Look for the next extended grapheme cluster boundary in a CharSequence. It assumes
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    47
     * the start of the char sequence is a boundary.
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    48
     * <p>
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    49
     * See Unicode Standard Annex #29 Unicode Text Segmentation for the specification
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    50
     * for the extended grapheme cluster boundary rules. The following implementation
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    51
     * is based on version 12.0 of the annex.
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    52
     * (http://www.unicode.org/reports/tr29/tr29-35.html)
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    53
     *
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    54
     * @param src the {@code CharSequence} to be scanned
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    55
     * @param off offset to start looking for the next boundary in the src
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    56
     * @param limit limit offset in the src (exclusive)
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    57
     * @return the next possible boundary
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    58
     */
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    59
    static int nextBoundary(CharSequence src, int off, int limit) {
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    60
        Objects.checkFromToIndex(off, limit, src.length());
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    61
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    62
        int ch0 = Character.codePointAt(src, 0);
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    63
        int ret = Character.charCount(ch0);
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    64
        int ch1;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    65
        // indicates whether gb11 or gb12 is underway
55141
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    66
        int t0 = getGraphemeType(ch0);
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    67
        int riCount = t0 == RI ? 1 : 0;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    68
        boolean gb11 = t0 == EXTENDED_PICTOGRAPHIC;
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    69
        while (ret < limit) {
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    70
            ch1 = Character.codePointAt(src, ret);
55141
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    71
            int t1 = getGraphemeType(ch1);
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    72
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    73
            if (gb11 && t0 == ZWJ && t1 == EXTENDED_PICTOGRAPHIC) {
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    74
                gb11 = false;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    75
            } else if (riCount % 2 == 1 && t0 == RI && t1 == RI) {
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    76
                // continue for gb12
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    77
            } else if (rules[t0][t1]) {
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    78
                if (ret > off) {
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    79
                    break;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    80
                } else {
55141
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    81
                    gb11 = t1 == EXTENDED_PICTOGRAPHIC;
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    82
                    riCount = 0;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    83
                }
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    84
            }
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    85
55141
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    86
            riCount += (t1 == RI) ? 1 : 0;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    87
            t0 = t1;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
    88
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    89
            ret += Character.charCount(ch1);
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    90
        }
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
    91
        return ret;
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    92
    }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    93
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    94
    // types
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    95
    private static final int OTHER = 0;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    96
    private static final int CR = 1;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    97
    private static final int LF = 2;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    98
    private static final int CONTROL = 3;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
    99
    private static final int EXTEND = 4;
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   100
    private static final int ZWJ = 5;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   101
    private static final int RI = 6;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   102
    private static final int PREPEND = 7;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   103
    private static final int SPACINGMARK = 8;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   104
    private static final int L = 9;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   105
    private static final int V = 10;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   106
    private static final int T = 11;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   107
    private static final int LV = 12;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   108
    private static final int LVT = 13;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   109
    private static final int EXTENDED_PICTOGRAPHIC = 14;
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   110
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   111
    private static final int FIRST_TYPE = 0;
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   112
    private static final int LAST_TYPE = 14;
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   113
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   114
    private static boolean[][] rules;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   115
    static {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   116
        rules = new boolean[LAST_TYPE + 1][LAST_TYPE + 1];
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   117
        // GB 999 Any + Any  -> default
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   118
        for (int i = FIRST_TYPE; i <= LAST_TYPE; i++)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   119
            for (int j = FIRST_TYPE; j <= LAST_TYPE; j++)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   120
                rules[i][j] = true;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   121
        // GB 6 L x (L | V | LV | VT)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   122
        rules[L][L] = false;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   123
        rules[L][V] = false;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   124
        rules[L][LV] = false;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   125
        rules[L][LVT] = false;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   126
        // GB 7 (LV | V) x (V | T)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   127
        rules[LV][V] = false;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   128
        rules[LV][T] = false;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   129
        rules[V][V] = false;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   130
        rules[V][T] = false;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   131
        // GB 8 (LVT | T) x T
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   132
        rules[LVT][T] = false;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   133
        rules[T][T] = false;
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   134
        // GB 9 x (Extend|ZWJ)
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   135
        // GB 9a x Spacing Mark
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   136
        // GB 9b Prepend x
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   137
        for (int i = FIRST_TYPE; i <= LAST_TYPE; i++) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   138
            rules[i][EXTEND] = false;
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   139
            rules[i][ZWJ] = false;
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   140
            rules[i][SPACINGMARK] = false;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   141
            rules[PREPEND][i] = false;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   142
        }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   143
        // GB 4  (Control | CR | LF) +
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   144
        // GB 5  + (Control | CR | LF)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   145
        for (int i = FIRST_TYPE; i <= LAST_TYPE; i++)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   146
            for (int j = CR; j <= CONTROL; j++) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   147
                rules[i][j] = true;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   148
                rules[j][i] = true;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   149
            }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   150
        // GB 3 CR x LF
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   151
        rules[CR][LF] = false;
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   152
        // GB 11 Exended_Pictographic x (Extend|ZWJ)
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   153
        rules[EXTENDED_PICTOGRAPHIC][EXTEND] = false;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   154
        rules[EXTENDED_PICTOGRAPHIC][ZWJ] = false;
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   155
    }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   156
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   157
    // Hangul syllables
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   158
    private static final int SYLLABLE_BASE = 0xAC00;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   159
    private static final int LCOUNT = 19;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   160
    private static final int VCOUNT = 21;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   161
    private static final int TCOUNT = 28;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   162
    private static final int NCOUNT = VCOUNT * TCOUNT; // 588
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   163
    private static final int SCOUNT = LCOUNT * NCOUNT; // 11172
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   164
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   165
    // #tr29: SpacingMark exceptions: The following (which have
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   166
    // General_Category = Spacing_Mark and would otherwise be included)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   167
    // are specifically excluded
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   168
    private static boolean isExcludedSpacingMark(int cp) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   169
       return  cp == 0x102B || cp == 0x102C || cp == 0x1038 ||
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   170
               cp >= 0x1062 && cp <= 0x1064 ||
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   171
               cp >= 0x1062 && cp <= 0x106D ||
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   172
               cp == 0x1083 ||
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   173
               cp >= 0x1087 && cp <= 0x108C ||
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   174
               cp == 0x108F ||
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   175
               cp >= 0x109A && cp <= 0x109C ||
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   176
               cp == 0x1A61 || cp == 0x1A63 || cp == 0x1A64 ||
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   177
               cp == 0xAA7B || cp == 0xAA7D;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   178
    }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   179
55141
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
   180
    private static int getGraphemeType(int cp) {
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
   181
        if (cp < 0x007F) { // ASCII
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
   182
            if (cp < 32) { // Control characters
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
   183
                if (cp == 0x000D)
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
   184
                    return CR;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
   185
                if (cp == 0x000A)
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
   186
                    return LF;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
   187
                return CONTROL;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
   188
            }
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
   189
            return OTHER;
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
   190
        }
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
   191
        return getType(cp);
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
   192
    }
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
   193
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   194
    @SuppressWarnings("fallthrough")
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   195
    private static int getType(int cp) {
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   196
        if (EmojiData.isExtendedPictographic(cp)) {
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   197
            return EXTENDED_PICTOGRAPHIC;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   198
        }
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   199
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   200
        int type = Character.getType(cp);
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   201
        switch(type) {
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   202
        case Character.UNASSIGNED:
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   203
            // NOTE: #tr29 lists "Unassigned and Default_Ignorable_Code_Point" as Control
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   204
            // but GraphemeBreakTest.txt lists u+0378/reserved-0378 as "Other"
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   205
            // so type it as "Other" to make the test happy
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   206
            if (cp == 0x0378)
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   207
                return OTHER;
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   208
55141
db105c4c5776 8225061: Performance regression in Regex
redestad
parents: 55013
diff changeset
   209
        case Character.CONTROL:
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   210
        case Character.LINE_SEPARATOR:
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   211
        case Character.PARAGRAPH_SEPARATOR:
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   212
        case Character.SURROGATE:
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   213
            return CONTROL;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   214
        case Character.FORMAT:
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   215
            if (cp == 0x200C ||
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   216
                cp >= 0xE0020 && cp <= 0xE007F)
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   217
                return EXTEND;
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   218
            if (cp == 0x200D)
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   219
                return ZWJ;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   220
            if (cp >= 0x0600 && cp <= 0x0605 ||
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   221
                cp == 0x06DD || cp == 0x070F || cp == 0x08E2 ||
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   222
                cp == 0x110BD || cp == 0x110CD)
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   223
                return PREPEND;
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   224
            return CONTROL;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   225
        case Character.NON_SPACING_MARK:
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   226
        case Character.ENCLOSING_MARK:
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   227
            // NOTE:
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   228
            // #tr29 "plus a few General_Category = Spacing_Mark needed for
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   229
            // canonical equivalence."
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   230
            // but for "extended grapheme clusters" support, there is no
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   231
            // need actually to diff "extend" and "spackmark" given GB9, GB9a
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   232
            return EXTEND;
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   233
        case  Character.COMBINING_SPACING_MARK:
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   234
            if (isExcludedSpacingMark(cp))
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   235
                return OTHER;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   236
            // NOTE:
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   237
            // 0x11720 and 0x11721 are mentioned in #tr29 as
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   238
            // OTHER_LETTER but it appears their category has been updated to
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   239
            // COMBING_SPACING_MARK already (verified in ver.8)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   240
            return SPACINGMARK;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   241
        case Character.OTHER_SYMBOL:
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   242
            if (cp >= 0x1F1E6 && cp <= 0x1F1FF)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   243
                return RI;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   244
            return OTHER;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   245
        case Character.MODIFIER_LETTER:
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   246
        case Character.MODIFIER_SYMBOL:
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   247
            // WARNING:
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   248
            // not mentioned in #tr29 but listed in GraphemeBreakProperty.txt
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   249
            if (cp == 0xFF9E || cp == 0xFF9F ||
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   250
                cp >= 0x1F3FB && cp <= 0x1F3FF)
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   251
                return EXTEND;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   252
            return OTHER;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   253
        case Character.OTHER_LETTER:
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   254
            if (cp == 0x0E33 || cp == 0x0EB3)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   255
                return SPACINGMARK;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   256
            // hangul jamo
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   257
            if (cp >= 0x1100 && cp <= 0x11FF) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   258
                if (cp <= 0x115F)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   259
                    return L;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   260
                if (cp <= 0x11A7)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   261
                    return V;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   262
                return T;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   263
            }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   264
            // hangul syllables
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   265
            int sindex = cp - SYLLABLE_BASE;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   266
            if (sindex >= 0 && sindex < SCOUNT) {
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   267
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   268
                if (sindex % TCOUNT == 0)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   269
                    return LV;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   270
                return LVT;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   271
            }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   272
            //  hangul jamo_extended A
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   273
            if (cp >= 0xA960 && cp <= 0xA97C)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   274
                return L;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   275
            //  hangul jamo_extended B
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   276
            if (cp >= 0xD7B0 && cp <= 0xD7C6)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   277
                return V;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   278
            if (cp >= 0xD7CB && cp <= 0xD7FB)
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   279
                return T;
55013
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   280
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   281
            // Prepend
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   282
            switch (cp) {
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   283
                case 0x0D4E:
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   284
                case 0x111C2:
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   285
                case 0x111C3:
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   286
                case 0x11A3A:
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   287
                case 0x11A84:
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   288
                case 0x11A85:
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   289
                case 0x11A86:
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   290
                case 0x11A87:
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   291
                case 0x11A88:
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   292
                case 0x11A89:
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   293
                case 0x11D46:
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   294
                    return PREPEND;
8dae495a59e7 8221431: Support for Unicode 12.1
naoto
parents: 47216
diff changeset
   295
            }
35783
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   296
        }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   297
        return OTHER;
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   298
    }
2690535d72cc 7071819: To support Extended Grapheme Clusters in Regex
sherman
parents:
diff changeset
   299
}