|
| 1 | +/* |
| 2 | + * Copyright (c) 2025, Oracle and/or its affiliates. All rights reserved. |
| 3 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
| 4 | + * |
| 5 | + * This code is free software; you can redistribute it and/or modify it |
| 6 | + * under the terms of the GNU General Public License version 2 only, as |
| 7 | + * published by the Free Software Foundation. Oracle designates this |
| 8 | + * particular file as subject to the "Classpath" exception as provided |
| 9 | + * by Oracle in the LICENSE file that accompanied this code. |
| 10 | + * |
| 11 | + * This code is distributed in the hope that it will be useful, but WITHOUT |
| 12 | + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
| 13 | + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
| 14 | + * version 2 for more details (a copy is included in the LICENSE file that |
| 15 | + * accompanied this code). |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU General Public License version |
| 18 | + * 2 along with this work; if not, write to the Free Software Foundation, |
| 19 | + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
| 20 | + * |
| 21 | + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
| 22 | + * or visit www.oracle.com if you need additional information or have any |
| 23 | + * questions. |
| 24 | + */ |
| 25 | + |
| 26 | +package jdk.internal.util.regex; |
| 27 | + |
| 28 | +import java.util.Arrays; |
| 29 | +import java.util.Map; |
| 30 | +import java.util.Objects; |
| 31 | + |
| 32 | +import static java.util.Map.entry; |
| 33 | + |
| 34 | +public final class CaseFolding { |
| 35 | + |
| 36 | + private static final Map<Integer, Integer> expanded_case_map = Map.ofEntries( |
| 37 | +%%%Entries |
| 38 | + ); |
| 39 | + |
| 40 | + private static final int[] expanded_case_cps = expanded_case_map.keySet() |
| 41 | + .stream() |
| 42 | + .mapToInt(Integer::intValue) |
| 43 | + .toArray(); |
| 44 | + |
| 45 | + private CaseFolding() {} |
| 46 | + |
| 47 | + /** |
| 48 | + * Returns an expansion set to "close" a given regex Unicode character class range for case-sensitive |
| 49 | + * matching, according to the |
| 50 | + * <a href="https://www.unicode.org/reports/tr18/#Simple_Loose_Matches">Simple Loose Matches</a> |
| 51 | + * rule defined in Unicode Technical Standard #18: Unicode Regular Expressions. |
| 52 | + * <p> |
| 53 | + * To conform with Level 1 of UTS #18, specifically RL1.5: Simple Loose Matches, simple case folding must |
| 54 | + * be applied to literals and (optionally) to character classes. When applied to character classes, each |
| 55 | + * character class is expected to be closed under simple case folding. See the standard for the |
| 56 | + * detailed explanation and example of "closed". |
| 57 | + * <p> |
| 58 | + * RL1.5 states: To meet this requirement, an implementation that supports case-sensitive matching should |
| 59 | + * <ol> |
| 60 | + * <li>Provide at least the simple, default Unicode case-insensitive matching, and</li> |
| 61 | + * <li>Specify which character properties or constructs are closed under the matching.</li> |
| 62 | + * </ol> |
| 63 | + * <p> |
| 64 | + * In the {@code Pattern} implementation, 5 types of constructs maybe case-sensitive when matching: |
| 65 | + * back-refs, string slice (sequences), single, family(char-property) and class range. Single and |
| 66 | + * family may appears independently or within a class. |
| 67 | + * <p> |
| 68 | + * For loose/case-insensitive matching, the back-refs, slices and singles apply {code toUpperCase} and |
| 69 | + * {@code toLowerCase} to both the pattern and the input string. This effectively 'close' the class for |
| 70 | + * matching. |
| 71 | + * <p> |
| 72 | + * The family/char-properties are not "closed" and should remain unchanged. This is acceptable per RL1.5, |
| 73 | + * if their behavior is clearly specified. |
| 74 | + * <p> |
| 75 | + * This method addresses that requirement for the "range" construct within in character class by computing |
| 76 | + * the additional characters that should be included to close the range under simple case folding: |
| 77 | + * <p> |
| 78 | + * For each character in the input range {@code [start, end]} (inclusive), if the character has a simple |
| 79 | + * case folding mapping in Unicode's CaseFolding.txt, the mapping is not a round-trip map, and the mapped |
| 80 | + * character is not already in the range, then that mapped character (typically lowercase) is added to |
| 81 | + * the expansion set. |
| 82 | + * <p> |
| 83 | + * This allows regex character class "range" implementation to use the returned expansion set to support |
| 84 | + * additional case-insensitive matching, without duplicating characters already covered by the existing |
| 85 | + * regex range implementation. The expectation is the matching is done using both the uppercase and |
| 86 | + * lowercase forms of the input character, for example |
| 87 | + * |
| 88 | + * <pre>{@code |
| 89 | + * |
| 90 | + * ch -> inRange(lower, Character.toUpperCase(ch), upper) || |
| 91 | + * inRange(lower, Character.toLower(ch), upper) || |
| 92 | + * additionalClosingCharacters.contains(Character.toUpperCase(ch)) || |
| 93 | + * additionalClosingCharacters.contains(Character.toUpperCase(ch)) |
| 94 | + * }</pre> |
| 95 | + * |
| 96 | + * <p> |
| 97 | + * @spec https://www.unicode.org/reports/tr18/#Simple_Loose_Matches |
| 98 | + * @param start the starting code point of the character range |
| 99 | + * @param end the ending code point of the character range |
| 100 | + * @return a {@code int[]} containing the all simple case equivalents of characters in the range, excluding |
| 101 | + * those already in the range |
| 102 | + */ |
| 103 | + public static int[] getClassRangeClosingCharacters(int start, int end) { |
| 104 | + int[] expanded = new int[expanded_case_cps.length]; |
| 105 | + int off = 0; |
| 106 | + for (int cp : expanded_case_cps) { |
| 107 | + if (cp >= start && cp <= end) { |
| 108 | + int folding = expanded_case_map.get(cp); |
| 109 | + if (folding < start || folding > end) { |
| 110 | + expanded[off++] = folding; |
| 111 | + } |
| 112 | + } |
| 113 | + } |
| 114 | + return Arrays.copyOf(expanded, off); |
| 115 | + } |
| 116 | +} |
0 commit comments