Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/make/src/classes/build/tools/generatecharacter/PrintCharacterRanges.java
32287 views
/*1* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/2425package build.tools.generatecharacter;2627import java.lang.reflect.*;28import java.util.*;2930/** Recovers and prints ranges for certain java.lang.Character31properties. Useful for generating fast-path Latin-1 code. */3233public class PrintCharacterRanges {34static class BooleanRange {35private int begin;36private int end;3738BooleanRange(int begin, int end) {39this.begin = begin;40this.end = end;41}4243int begin() { return begin; }44int end() { return end; }45}4647private static List/*<BooleanRange>*/ recoverBooleanRanges(String methodName) throws Exception {48List result = new ArrayList();49int currentRangeStart = -1;50Method method = Character.class.getDeclaredMethod(methodName, new Class[] { Character.TYPE });51if (method == null) {52throw new RuntimeException("No method \"" + methodName + "\"(C) found");53}5455for (int i = 0; i <= 255; i++) {56boolean methodRes = ((Boolean) method.invoke(null, new Object[] { new Character((char) i) })).booleanValue();57if (methodRes) {58if (currentRangeStart < 0) {59currentRangeStart = i;60}61if (i == 255) {62result.add(new BooleanRange(currentRangeStart, i));63}64} else {65if (currentRangeStart >= 0) {66result.add(new BooleanRange(currentRangeStart, i - 1));67currentRangeStart = -1;68}69}70}7172return result;73}7475private static String describe(int num) {76StringBuffer s = new StringBuffer();77s.append(num);78s.append(" ('");79if (num > 32 && num < 123) {80s.append((char) num);81} else {82s.append("\\u");83String hex = Long.toHexString(num).toUpperCase();84for (int i = 0; i < (4 - hex.length()); i++) {85s.append('0');86}87s.append(hex);88}89s.append("')");90return s.toString();91}9293private static void printBooleanRanges(List/*<BooleanRange>*/ ranges, String methodName) {94System.out.print(methodName + ":");95for (Iterator iter = ranges.iterator(); iter.hasNext();) {96BooleanRange range = (BooleanRange) iter.next();97System.out.print(" [ " + describe(range.begin()) + ", " + describe(range.end()) + " ]");98}99System.out.println("");100}101102private static void recoverAndPrintBooleanRanges(String methodName) throws Exception {103List ranges = recoverBooleanRanges(methodName);104printBooleanRanges(ranges, methodName);105}106107static class ShiftRange {108private int begin;109private int end;110private int offset;111112ShiftRange(int begin, int end, int offset) {113this.begin = begin;114this.end = end;115this.offset = offset;116}117118int begin() { return begin; }119int end() { return end; }120int offset() { return offset; }121}122123private static List/*<ShiftRange>*/ recoverShiftRanges(String methodName) throws Exception {124List result = new ArrayList();125int currentRangeStart = -1;126int currentRangeOffset = -1;127Method method = Character.class.getDeclaredMethod(methodName, new Class[] { Character.TYPE });128if (method == null) {129throw new RuntimeException("No method \"" + methodName + "\"(C) found");130}131132for (int i = 0; i <= 255; i++) {133char methodRes = ((Character) method.invoke(null, new Object[] { new Character((char) i) })).charValue();134if (methodRes != i) {135int offset = methodRes - i;136if (currentRangeStart < 0) {137currentRangeStart = i;138} else if (offset != currentRangeOffset) {139result.add(new ShiftRange(currentRangeStart, i - 1, currentRangeOffset));140currentRangeStart = i;141}142currentRangeOffset = offset;143if (i == 255) {144result.add(new ShiftRange(currentRangeStart, i, currentRangeOffset));145}146} else {147if (currentRangeStart >= 0) {148result.add(new ShiftRange(currentRangeStart, i - 1, currentRangeOffset));149currentRangeStart = -1;150}151}152}153154return result;155}156157private static void printShiftRanges(List/*<ShiftRange>*/ ranges, String methodName) {158System.out.print(methodName + ":");159boolean isFirst = true;160for (Iterator iter = ranges.iterator(); iter.hasNext();) {161ShiftRange range = (ShiftRange) iter.next();162if (isFirst) {163isFirst = false;164} else {165System.out.print(", ");166}167System.out.print(" [ " + describe(range.begin()) + ", " + describe(range.end()) + " ] -> [ " +168describe((range.begin() + range.offset())) + ", " + describe((range.end() + range.offset())) + " ] (" +169range.offset() + ")");170}171System.out.println("");172}173174private static void recoverAndPrintShiftRanges(String methodName) throws Exception {175List ranges = recoverShiftRanges(methodName);176printShiftRanges(ranges, methodName);177}178179public static void main(String[] args) {180try {181recoverAndPrintBooleanRanges("isDefined");182recoverAndPrintBooleanRanges("isDigit");183recoverAndPrintBooleanRanges("isIdentifierIgnorable");184recoverAndPrintBooleanRanges("isISOControl");185recoverAndPrintBooleanRanges("isJavaIdentifierPart");186recoverAndPrintBooleanRanges("isJavaIdentifierStart");187recoverAndPrintBooleanRanges("isLetter");188recoverAndPrintBooleanRanges("isLetterOrDigit");189recoverAndPrintBooleanRanges("isLowerCase");190recoverAndPrintBooleanRanges("isMirrored");191recoverAndPrintBooleanRanges("isSpaceChar");192recoverAndPrintBooleanRanges("isTitleCase");193recoverAndPrintBooleanRanges("isUnicodeIdentifierPart");194recoverAndPrintBooleanRanges("isUnicodeIdentifierStart");195recoverAndPrintBooleanRanges("isUpperCase");196recoverAndPrintBooleanRanges("isWhitespace");197198recoverAndPrintShiftRanges("toUpperCase");199recoverAndPrintShiftRanges("toLowerCase");200} catch (Exception e) {201e.printStackTrace();202}203}204}205206207