1 /*
   2  * Copyright (c) 2002, 2018, 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 java.lang;
  27 
  28 import jdk.internal.HotSpotIntrinsicCandidate;
  29 
  30 /** The CharacterData class encapsulates the large tables found in
  31     Java.lang.Character. */
  32 
  33 class CharacterDataLatin1 extends CharacterData {
  34 
  35     /* The character properties are currently encoded into 32 bits in the following manner:
  36         1 bit   mirrored property
  37         4 bits  directionality property
  38         9 bits  signed offset used for converting case
  39         1 bit   if 1, adding the signed offset converts the character to lowercase
  40         1 bit   if 1, subtracting the signed offset converts the character to uppercase
  41         1 bit   if 1, this character has a titlecase equivalent (possibly itself)
  42         3 bits  0  may not be part of an identifier
  43                 1  ignorable control; may continue a Unicode identifier or Java identifier
  44                 2  may continue a Java identifier but not a Unicode identifier (unused)
  45                 3  may continue a Unicode identifier or Java identifier
  46                 4  is a Java whitespace character
  47                 5  may start or continue a Java identifier;
  48                    may continue but not start a Unicode identifier (underscores)
  49                 6  may start or continue a Java identifier but not a Unicode identifier ($)
  50                 7  may start or continue a Unicode identifier or Java identifier
  51                 Thus:
  52                    5, 6, 7 may start a Java identifier
  53                    1, 2, 3, 5, 6, 7 may continue a Java identifier
  54                    7 may start a Unicode identifier
  55                    1, 3, 5, 7 may continue a Unicode identifier
  56                    1 is ignorable within an identifier
  57                    4 is Java whitespace
  58         2 bits  0  this character has no numeric property
  59                 1  adding the digit offset to the character code and then
  60                    masking with 0x1F will produce the desired numeric value
  61                 2  this character has a "strange" numeric value
  62                 3  a Java supradecimal digit: adding the digit offset to the
  63                    character code, then masking with 0x1F, then adding 10
  64                    will produce the desired numeric value
  65         5 bits  digit offset
  66         5 bits  character type
  67 
  68         The encoding of character properties is subject to change at any time.
  69      */
  70 
  71     int getProperties(int ch) {
  72         char offset = (char)ch;
  73         int props = $$Lookup(offset);
  74         return props;
  75     }
  76 
  77     int getPropertiesEx(int ch) {
  78         char offset = (char)ch;
  79         int props = $$LookupEx(offset);
  80         return props;
  81     }
  82 
  83     @HotSpotIntrinsicCandidate
  84     boolean isDigit(int ch) {
  85         return getType(ch) == Character.DECIMAL_DIGIT_NUMBER;
  86     }
  87 
  88     @HotSpotIntrinsicCandidate
  89     boolean isLowerCase(int ch) {
  90         return getType(ch) == Character.LOWERCASE_LETTER;
  91     }
  92 
  93     @HotSpotIntrinsicCandidate
  94     boolean isUpperCase(int ch) {
  95         return getType(ch) == Character.UPPERCASE_LETTER;
  96     }
  97 
  98     boolean isOtherLowercase(int ch) {
  99         int props = getPropertiesEx(ch);
 100         return (props & $$maskOtherLowercase) != 0;
 101     }
 102 
 103     boolean isOtherUppercase(int ch) {
 104         int props = getPropertiesEx(ch);
 105         return (props & $$maskOtherUppercase) != 0;
 106     }
 107 
 108     boolean isOtherAlphabetic(int ch) {
 109         int props = getPropertiesEx(ch);
 110         return (props & $$maskOtherAlphabetic) != 0;
 111     }
 112 
 113     boolean isIdeographic(int ch) {
 114         int props = getPropertiesEx(ch);
 115         return (props & $$maskIdeographic) != 0;
 116     }
 117 
 118     int getType(int ch) {
 119         int props = getProperties(ch);
 120         return (props & $$maskType);
 121     }
 122 
 123     boolean isJavaIdentifierStart(int ch) {
 124         int props = getProperties(ch);
 125         return ((props & $$maskIdentifierInfo) >= $$lowJavaStart);
 126     }
 127 
 128     boolean isJavaIdentifierPart(int ch) {
 129         int props = getProperties(ch);
 130         return ((props & $$nonzeroJavaPart) != 0);
 131     }
 132 
 133     boolean isUnicodeIdentifierStart(int ch) {
 134         int props = getProperties(ch);
 135         return ((props & $$maskIdentifierInfo) == $$valueUnicodeStart);
 136     }
 137 
 138     boolean isUnicodeIdentifierPart(int ch) {
 139         int props = getProperties(ch);
 140         return ((props & $$maskUnicodePart) != 0);
 141     }
 142 
 143     boolean isIdentifierIgnorable(int ch) {
 144         int props = getProperties(ch);
 145         return ((props & $$maskIdentifierInfo) == $$valueIgnorable);
 146     }
 147 
 148     int toLowerCase(int ch) {
 149         int mapChar = ch;
 150         int val = getProperties(ch);
 151 
 152         if (((val & $$maskLowerCase) != 0) && 
 153                 ((val & $$maskCaseOffset) != $$maskCaseOffset)) { 
 154             int offset = val << $$shiftCaseOffsetSign >> ($$shiftCaseOffsetSign+$$shiftCaseOffset);
 155             mapChar = ch + offset;
 156         }
 157         return mapChar;
 158     }
 159 
 160     int toUpperCase(int ch) {
 161         int mapChar = ch;
 162         int val = getProperties(ch);
 163 
 164         if ((val & $$maskUpperCase) != 0) {
 165             if ((val & $$maskCaseOffset) != $$maskCaseOffset) {
 166                 int offset = val  << $$shiftCaseOffsetSign >> ($$shiftCaseOffsetSign+$$shiftCaseOffset);
 167                 mapChar =  ch - offset;
 168             } else if (ch == 0x00B5) {
 169                 mapChar = 0x039C;
 170             }
 171         }
 172         return mapChar;
 173     }
 174 
 175     int toTitleCase(int ch) {
 176         return toUpperCase(ch);
 177     }
 178 
 179     // Digit values for codePoints in the 0-255 range. Contents generated using:
 180     // for (char i = 0; i < 256; i++) {
 181     //     int v = -1;
 182     //     if (i >= '0' && i <= '9') { v = i - '0'; } 
 183     //     else if (i >= 'A' && i <= 'Z') { v = i - 'A' + 10; }
 184     //     else if (i >= 'a' && i <= 'z') { v = i - 'a' + 10; }
 185     //     if (i % 20 == 0) System.out.println();
 186     //     System.out.printf("%2d, ", v);
 187     // }
 188     //
 189     // Analysis has shown that generating the whole array allows the JIT to generate
 190     // better code compared to a slimmed down array, such as one cutting off after 'z'
 191     private static final byte[] DIGITS = new byte[] {
 192         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 193         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 194         -1, -1, -1, -1, -1, -1, -1, -1,  0,  1,  2,  3,  4,  5,  6,  7,  8,  9, -1, -1,
 195         -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
 196         25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1, 10, 11, 12,
 197         13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
 198         33, 34, 35, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 199         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 200         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 201         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 202         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 203         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
 204         -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 };
 205 
 206     int digit(int ch, int radix) {
 207         int value = DIGITS[ch];
 208         return (value >= 0 && value < radix && radix >= Character.MIN_RADIX
 209                 && radix <= Character.MAX_RADIX) ? value : -1;
 210     }
 211 
 212     int getNumericValue(int ch) {
 213         int val = getProperties(ch);
 214         int retval = -1;
 215 
 216         switch (val & $$maskNumericType) {
 217             default: // cannot occur
 218             case ($$valueNotNumeric):         // not numeric
 219                 retval = -1;
 220                 break;
 221             case ($$valueDigit):              // simple numeric
 222                 retval = ch + ((val & $$maskDigitOffset) >> $$shiftDigitOffset) & $$maskDigit;
 223                 break;
 224             case ($$valueStrangeNumeric)      :       // "strange" numeric
 225                  retval = -2; 
 226                  break;
 227             case ($$valueJavaSupradecimal):           // Java supradecimal
 228                 retval = (ch + ((val & $$maskDigitOffset) >> $$shiftDigitOffset) & $$maskDigit) + 10;
 229                 break;
 230         }
 231         return retval;
 232     }
 233 
 234     @HotSpotIntrinsicCandidate
 235     boolean isWhitespace(int ch) {
 236         int props = getProperties(ch);
 237         return ((props & $$maskIdentifierInfo) == $$valueJavaWhitespace);
 238     }
 239 
 240     byte getDirectionality(int ch) {
 241         int val = getProperties(ch);
 242         byte directionality = (byte)((val & $$maskBidi) >> $$shiftBidi);
 243 
 244         if (directionality == 0xF ) {
 245             directionality = -1;
 246         }
 247         return directionality;
 248     }
 249 
 250     boolean isMirrored(int ch) {
 251         int props = getProperties(ch);
 252         return ((props & $$maskMirrored) != 0);
 253     }
 254 
 255     int toUpperCaseEx(int ch) {
 256         int mapChar = ch;
 257         int val = getProperties(ch);
 258 
 259         if ((val & $$maskUpperCase) != 0) {
 260             if ((val & $$maskCaseOffset) != $$maskCaseOffset) {
 261                 int offset = val  << $$shiftCaseOffsetSign >> ($$shiftCaseOffsetSign+$$shiftCaseOffset);
 262                 mapChar =  ch - offset;
 263             }
 264             else {
 265                 switch(ch) {
 266                     // map overflow characters
 267                     case 0x00B5 : mapChar = 0x039C; break;
 268                     default       : mapChar = Character.ERROR; break;
 269                 }
 270             }
 271         }
 272         return mapChar;
 273     }
 274 
 275     static char[] sharpsMap = new char[] {'S', 'S'};
 276 
 277     char[] toUpperCaseCharArray(int ch) {
 278         char[] upperMap = {(char)ch};
 279         if (ch == 0x00DF) {
 280             upperMap = sharpsMap;
 281         }
 282         return upperMap;
 283     }
 284 
 285     static final CharacterDataLatin1 instance = new CharacterDataLatin1();
 286     private CharacterDataLatin1() {};
 287 
 288     $$Tables
 289 
 290     static {
 291         $$Initializers
 292     }        
 293 }
 294 
--- EOF ---