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