1 /* 2 * Copyright (c) 2002, 2019, 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 '0' <= ch && ch <= '9'; 86 } 87 88 @HotSpotIntrinsicCandidate 89 boolean isLowerCase(int ch) { 90 int props = getProperties(ch); 91 return (props & $$maskType) == Character.LOWERCASE_LETTER; 92 } 93 94 @HotSpotIntrinsicCandidate 95 boolean isUpperCase(int ch) { 96 int props = getProperties(ch); 97 return (props & $$maskType) == Character.UPPERCASE_LETTER; 98 } 99 100 boolean isOtherLowercase(int ch) { 101 int props = getPropertiesEx(ch); 102 return (props & $$maskOtherLowercase) != 0; 103 } 104 105 boolean isOtherUppercase(int ch) { 106 int props = getPropertiesEx(ch); 107 return (props & $$maskOtherUppercase) != 0; 108 } 109 110 boolean isOtherAlphabetic(int ch) { 111 int props = getPropertiesEx(ch); 112 return (props & $$maskOtherAlphabetic) != 0; 113 } 114 115 boolean isIdeographic(int ch) { 116 int props = getPropertiesEx(ch); 117 return (props & $$maskIdeographic) != 0; 118 } 119 120 int getType(int ch) { 121 int props = getProperties(ch); 122 return (props & $$maskType); 123 } 124 125 boolean isJavaIdentifierStart(int ch) { 126 int props = getProperties(ch); 127 return ((props & $$maskIdentifierInfo) >= $$lowJavaStart); 128 } 129 130 boolean isJavaIdentifierPart(int ch) { 131 int props = getProperties(ch); 132 return ((props & $$nonzeroJavaPart) != 0); 133 } 134 135 boolean isUnicodeIdentifierStart(int ch) { 136 return (getPropertiesEx(ch) & $$maskIDStart) != 0 || 137 ch == 0x2E2F; 138 } 139 140 boolean isUnicodeIdentifierPart(int ch) { 141 return (getPropertiesEx(ch) & $$maskIDContinue) != 0 || 142 isIdentifierIgnorable(ch) || 143 ch == 0x2E2F; 144 } 145 146 boolean isIdentifierIgnorable(int ch) { 147 int props = getProperties(ch); 148 return ((props & $$maskIdentifierInfo) == $$valueIgnorable); 149 } 150 151 int toLowerCase(int ch) { 152 int mapChar = ch; 153 int val = getProperties(ch); 154 155 if (((val & $$maskLowerCase) != 0) && 156 ((val & $$maskCaseOffset) != $$maskCaseOffset)) { 157 int offset = val << $$shiftCaseOffsetSign >> ($$shiftCaseOffsetSign+$$shiftCaseOffset); 158 mapChar = ch + offset; 159 } 160 return mapChar; 161 } 162 163 int toUpperCase(int ch) { 164 int mapChar = ch; 165 int val = getProperties(ch); 166 167 if ((val & $$maskUpperCase) != 0) { 168 if ((val & $$maskCaseOffset) != $$maskCaseOffset) { 169 int offset = val << $$shiftCaseOffsetSign >> ($$shiftCaseOffsetSign+$$shiftCaseOffset); 170 mapChar = ch - offset; 171 } else if (ch == 0x00B5) { 172 mapChar = 0x039C; 173 } 174 } 175 return mapChar; 176 } 177 178 int toTitleCase(int ch) { 179 return toUpperCase(ch); 180 } 181 182 // Digit values for codePoints in the 0-255 range. Contents generated using: 183 // for (char i = 0; i < 256; i++) { 184 // int v = -1; 185 // if (i >= '0' && i <= '9') { v = i - '0'; } 186 // else if (i >= 'A' && i <= 'Z') { v = i - 'A' + 10; } 187 // else if (i >= 'a' && i <= 'z') { v = i - 'a' + 10; } 188 // if (i % 20 == 0) System.out.println(); 189 // System.out.printf("%2d, ", v); 190 // } 191 // 192 // Analysis has shown that generating the whole array allows the JIT to generate 193 // better code compared to a slimmed down array, such as one cutting off after 'z' 194 private static final byte[] DIGITS = new byte[] { 195 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 196 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 197 -1, -1, -1, -1, -1, -1, -1, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, 198 -1, -1, -1, -1, -1, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 199 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, -1, -1, -1, -1, -1, -1, 10, 11, 12, 200 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 201 33, 34, 35, -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, -1, -1, -1, -1, 205 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 206 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 207 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1 }; 208 209 int digit(int ch, int radix) { 210 int value = DIGITS[ch]; 211 return (value >= 0 && value < radix && radix >= Character.MIN_RADIX 212 && radix <= Character.MAX_RADIX) ? value : -1; 213 } 214 215 int getNumericValue(int ch) { 216 int val = getProperties(ch); 217 int retval = -1; 218 219 switch (val & $$maskNumericType) { 220 default: // cannot occur 221 case ($$valueNotNumeric): // not numeric 222 retval = -1; 223 break; 224 case ($$valueDigit): // simple numeric 225 retval = ch + ((val & $$maskDigitOffset) >> $$shiftDigitOffset) & $$maskDigit; 226 break; 227 case ($$valueStrangeNumeric) : // "strange" numeric 228 retval = -2; 229 break; 230 case ($$valueJavaSupradecimal): // Java supradecimal 231 retval = (ch + ((val & $$maskDigitOffset) >> $$shiftDigitOffset) & $$maskDigit) + 10; 232 break; 233 } 234 return retval; 235 } 236 237 @HotSpotIntrinsicCandidate 238 boolean isWhitespace(int ch) { 239 int props = getProperties(ch); 240 return ((props & $$maskIdentifierInfo) == $$valueJavaWhitespace); 241 } 242 243 byte getDirectionality(int ch) { 244 int val = getProperties(ch); 245 byte directionality = (byte)((val & $$maskBidi) >> $$shiftBidi); 246 247 if (directionality == 0xF ) { 248 directionality = -1; 249 } 250 return directionality; 251 } 252 253 boolean isMirrored(int ch) { 254 int props = getProperties(ch); 255 return ((props & $$maskMirrored) != 0); 256 } 257 258 int toUpperCaseEx(int ch) { 259 int mapChar = ch; 260 int val = getProperties(ch); 261 262 if ((val & $$maskUpperCase) != 0) { 263 if ((val & $$maskCaseOffset) != $$maskCaseOffset) { 264 int offset = val << $$shiftCaseOffsetSign >> ($$shiftCaseOffsetSign+$$shiftCaseOffset); 265 mapChar = ch - offset; 266 } 267 else { 268 switch(ch) { 269 // map overflow characters 270 case 0x00B5 : mapChar = 0x039C; break; 271 default : mapChar = Character.ERROR; break; 272 } 273 } 274 } 275 return mapChar; 276 } 277 278 static char[] sharpsMap = new char[] {'S', 'S'}; 279 280 char[] toUpperCaseCharArray(int ch) { 281 char[] upperMap = {(char)ch}; 282 if (ch == 0x00DF) { 283 upperMap = sharpsMap; 284 } 285 return upperMap; 286 } 287 288 static final CharacterDataLatin1 instance = new CharacterDataLatin1(); 289 private CharacterDataLatin1() {}; 290 291 $$Tables 292 293 static { 294 $$Initializers 295 } 296 } 297