make/jdk/src/classes/build/tools/generatecharacter/PrintCharacterRanges.java
author pliden
Thu, 26 Sep 2019 13:56:58 +0200
changeset 58355 de246fd65587
parent 47216 71c04702a3d5
permissions -rw-r--r--
8231294: ZGC: vmTestbase/nsk/jvmti/ResourceExhausted/resexhausted002 fails Reviewed-by: shade, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21805
diff changeset
     2
 * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package build.tools.generatecharacter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.lang.reflect.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
/** Recovers and prints ranges for certain java.lang.Character
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    properties. Useful for generating fast-path Latin-1 code. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
public class PrintCharacterRanges {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
  static class BooleanRange {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
    private int begin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private int end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    BooleanRange(int begin, int end) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
      this.begin = begin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
      this.end   = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    int begin() { return begin; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    int end()   { return end;   }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
  private static List/*<BooleanRange>*/ recoverBooleanRanges(String methodName) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
    List result = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
    int currentRangeStart = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
    Method method = Character.class.getDeclaredMethod(methodName, new Class[] { Character.TYPE });
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
    if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
      throw new RuntimeException("No method \"" + methodName + "\"(C) found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
    for (int i = 0; i <= 255; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
      boolean methodRes = ((Boolean) method.invoke(null, new Object[] { new Character((char) i) })).booleanValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
      if (methodRes) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        if (currentRangeStart < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
          currentRangeStart = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        if (i == 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
          result.add(new BooleanRange(currentRangeStart, i));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        if (currentRangeStart >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
          result.add(new BooleanRange(currentRangeStart, i - 1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
          currentRangeStart = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
  private static String describe(int num) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
    StringBuffer s = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    s.append(num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
    s.append(" ('");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
    if (num > 32 && num < 123) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
      s.append((char) num);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
      s.append("\\u");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
      String hex = Long.toHexString(num).toUpperCase();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
      for (int i = 0; i < (4 - hex.length()); i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        s.append('0');
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
      s.append(hex);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    s.append("')");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
    return s.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
  private static void printBooleanRanges(List/*<BooleanRange>*/ ranges, String methodName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
    System.out.print(methodName + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    for (Iterator iter = ranges.iterator(); iter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
      BooleanRange range = (BooleanRange) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
      System.out.print(" [ " + describe(range.begin()) + ", " + describe(range.end()) + " ]");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
    System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
  private static void recoverAndPrintBooleanRanges(String methodName) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    List ranges = recoverBooleanRanges(methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
    printBooleanRanges(ranges, methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
  static class ShiftRange {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
    private int begin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
    private int end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
    private int offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
    ShiftRange(int begin, int end, int offset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
      this.begin  = begin;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
      this.end    = end;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
      this.offset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
    int begin()  { return begin;  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
    int end()    { return end;    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
    int offset() { return offset; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
  private static List/*<ShiftRange>*/ recoverShiftRanges(String methodName) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
    List result = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
    int currentRangeStart = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
    int currentRangeOffset = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
    Method method = Character.class.getDeclaredMethod(methodName, new Class[] { Character.TYPE });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
    if (method == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
      throw new RuntimeException("No method \"" + methodName + "\"(C) found");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
    for (int i = 0; i <= 255; i++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
      char methodRes = ((Character) method.invoke(null, new Object[] { new Character((char) i) })).charValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
      if (methodRes != i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
        int offset = methodRes - i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
        if (currentRangeStart < 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
          currentRangeStart = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
        } else if (offset != currentRangeOffset) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
          result.add(new ShiftRange(currentRangeStart, i - 1, currentRangeOffset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
          currentRangeStart = i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        currentRangeOffset = offset;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        if (i == 255) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
          result.add(new ShiftRange(currentRangeStart, i, currentRangeOffset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
        if (currentRangeStart >= 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
          result.add(new ShiftRange(currentRangeStart, i - 1, currentRangeOffset));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
          currentRangeStart = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
    return result;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
  private static void printShiftRanges(List/*<ShiftRange>*/ ranges, String methodName) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
    System.out.print(methodName + ":");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
    boolean isFirst = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
    for (Iterator iter = ranges.iterator(); iter.hasNext();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
      ShiftRange range = (ShiftRange) iter.next();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
      if (isFirst) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
        isFirst = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
      } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        System.out.print(", ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
      }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
      System.out.print(" [ " + describe(range.begin()) + ", " + describe(range.end()) + " ] -> [ " +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
                       describe((range.begin() + range.offset())) + ", " + describe((range.end() + range.offset())) + " ] (" +
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
                       range.offset() + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
    System.out.println("");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
  private static void recoverAndPrintShiftRanges(String methodName) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    List ranges = recoverShiftRanges(methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
    printShiftRanges(ranges, methodName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
  public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
      recoverAndPrintBooleanRanges("isDefined");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
      recoverAndPrintBooleanRanges("isDigit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
      recoverAndPrintBooleanRanges("isIdentifierIgnorable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
      recoverAndPrintBooleanRanges("isISOControl");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
      recoverAndPrintBooleanRanges("isJavaIdentifierPart");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
      recoverAndPrintBooleanRanges("isJavaIdentifierStart");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
      recoverAndPrintBooleanRanges("isLetter");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
      recoverAndPrintBooleanRanges("isLetterOrDigit");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
      recoverAndPrintBooleanRanges("isLowerCase");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
      recoverAndPrintBooleanRanges("isMirrored");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
      recoverAndPrintBooleanRanges("isSpaceChar");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
      recoverAndPrintBooleanRanges("isTitleCase");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
      recoverAndPrintBooleanRanges("isUnicodeIdentifierPart");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
      recoverAndPrintBooleanRanges("isUnicodeIdentifierStart");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
      recoverAndPrintBooleanRanges("isUpperCase");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
      recoverAndPrintBooleanRanges("isWhitespace");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
      recoverAndPrintShiftRanges("toUpperCase");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
      recoverAndPrintShiftRanges("toLowerCase");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
      e.printStackTrace();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
}