| 1 32 package net.sf.retrotranslator.runtime.java.lang; 33 34 37 public class _Character { 38 39 private static final int SURROGATE_MASK = 0x3FF; 40 private static final Character [] cache = new Character [128]; 41 42 static { 43 for (int i = 0; i < cache.length; i++) { 44 cache[i] = new Character ((char) i); 45 } 46 } 47 48 public static int charCount(int codePoint) { 49 return codePoint < Character.MIN_SUPPLEMENTARY_CODE_POINT ? 1 : 2; 50 } 51 52 public static int codePointAt(char[] chars, int index) { 53 return codePointAt(chars, index, chars.length); 54 } 55 56 public static int codePointAt(char[] chars, int index, int limit) { 57 if (index >= limit || limit < 0 || limit > chars.length) { 58 throw new IndexOutOfBoundsException (); 59 } 60 char highChar = chars[index]; 61 if (isHighSurrogate(highChar) && ++index < limit) { 62 char lowChar = chars[index]; 63 if (isLowSurrogate(lowChar)) { 64 return toCodePoint(highChar, lowChar); 65 } 66 } 67 return highChar; 68 } 69 70 public static int codePointAt(CharSequence sequence, int index) { 71 char highChar = sequence.charAt(index); 72 if (isHighSurrogate(highChar) && ++index < sequence.length()) { 73 char lowChar = sequence.charAt(index); 74 if (isLowSurrogate(lowChar)) { 75 return toCodePoint(highChar, lowChar); 76 } 77 } 78 return highChar; 79 } 80 81 public static int codePointBefore(char[] chars, int index) { 82 return codePointBefore(chars, index, 0); 83 } 84 85 public static int codePointBefore(char[] chars, int index, int start) { 86 if (index <= start || start < 0 || start >= chars.length) { 87 throw new IndexOutOfBoundsException (); 88 } 89 char lowChar = chars[--index]; 90 if (isLowSurrogate(lowChar) && index > start) { 91 char highChar = chars[index - 1]; 92 if (isHighSurrogate(highChar)) { 93 return toCodePoint(highChar, lowChar); 94 } 95 } 96 return lowChar; 97 } 98 99 public static int codePointBefore(CharSequence sequence, int index) { 100 char lowChar = sequence.charAt(--index); 101 if (isLowSurrogate(lowChar) && index > 0) { 102 char highChar = sequence.charAt(index - 1); 103 if (isHighSurrogate(highChar)) { 104 return toCodePoint(highChar, lowChar); 105 } 106 } 107 return lowChar; 108 } 109 110 public static int codePointCount(char[] chars, int offset, int count) { 111 int endIndex = offset + count; 112 if (offset < 0 || offset > endIndex || endIndex > chars.length) { 113 throw new IndexOutOfBoundsException (); 114 } 115 int result = 0; 116 for (int i = offset; i < endIndex; result++) { 117 if (isHighSurrogate(chars[i++]) && i < endIndex && isLowSurrogate(chars[i])) { 118 i++; 119 } 120 } 121 return result; 122 } 123 124 public static int codePointCount(CharSequence sequence, int beginIndex, int endIndex) { 125 if (beginIndex < 0 || beginIndex > endIndex || endIndex > sequence.length()) { 126 throw new IndexOutOfBoundsException (); 127 } 128 int result = 0; 129 for (int i = beginIndex; i < endIndex; result++) { 130 if (isHighSurrogate(sequence.charAt(i++)) && i < endIndex && isLowSurrogate(sequence.charAt(i))) { 131 i++; 132 } 133 } 134 return result; 135 } 136 137 public static int digit(int codePoint, int radix) { 138 return isBasic(codePoint) ? Character.digit((char) codePoint, radix) : -1; 139 } 140 141 public static byte getDirectionality(int codePoint) { 142 return isBasic(codePoint) ? Character.getDirectionality((char) codePoint) : Character.DIRECTIONALITY_UNDEFINED; 143 } 144 145 public static int getNumericValue(int codePoint) { 146 return isBasic(codePoint) ? Character.getNumericValue((char) codePoint) : -1; 147 } 148 149 public static int getType(int codePoint) { 150 return isBasic(codePoint) ? Character.getType((char) codePoint) : Character.UNASSIGNED; 151 } 152 153 public static boolean isDefined(int codePoint) { 154 return isBasic(codePoint) && Character.isDefined((char) codePoint); 155 } 156 157 public static boolean isDigit(int codePoint) { 158 return isBasic(codePoint) && Character.isDigit((char) codePoint); 159 } 160 161 public static boolean isHighSurrogate(char aChar) { 162 return aChar >= Character.MIN_HIGH_SURROGATE && aChar <= Character.MAX_HIGH_SURROGATE; 163 } 164 165 public static boolean isIdentifierIgnorable(int codePoint) { 166 return isBasic(codePoint) && Character.isIdentifierIgnorable((char) codePoint); 167 } 168 169 public static boolean isISOControl(int codePoint) { 170 return isBasic(codePoint) && Character.isISOControl((char) codePoint); 171 } 172 173 public static boolean isJavaIdentifierPart(int codePoint) { 174 return isBasic(codePoint) && Character.isJavaIdentifierPart((char) codePoint); 175 } 176 177 public static boolean isJavaIdentifierStart(int codePoint) { 178 return isBasic(codePoint) && Character.isJavaIdentifierStart((char) codePoint); 179 } 180 181 public static boolean isLetter(int codePoint) { 182 return isBasic(codePoint) && Character.isLetter((char) codePoint); 183 } 184 185 public static boolean isLetterOrDigit(int codePoint) { 186 return isBasic(codePoint) && Character.isLetterOrDigit((char) codePoint); 187 } 188 189 public static boolean isLowerCase(int codePoint) { 190 return isBasic(codePoint) && Character.isLowerCase((char) codePoint); 191 } 192 193 public static boolean isLowSurrogate(char aChar) { 194 return aChar >= Character.MIN_LOW_SURROGATE && aChar <= Character.MAX_LOW_SURROGATE; 195 } 196 197 public static boolean isMirrored(int codePoint) { 198 return isBasic(codePoint) && Character.isMirrored((char) codePoint); 199 } 200 201 public static boolean isSpaceChar(int codePoint) { 202 return isBasic(codePoint) && Character.isSpaceChar((char) codePoint); 203 } 204 205 public static boolean isSupplementaryCodePoint(int codePoint) { 206 return codePoint >= Character.MIN_SUPPLEMENTARY_CODE_POINT && codePoint <= Character.MAX_CODE_POINT; 207 } 208 209 public static boolean isSurrogatePair(char highChar, char lowChar) { 210 return isHighSurrogate(highChar) && isLowSurrogate(lowChar); 211 } 212 213 public static boolean isTitleCase(int codePoint) { 214 return isBasic(codePoint) && Character.isTitleCase((char) codePoint); 215 } 216 217 public static boolean isUnicodeIdentifierPart(int codePoint) { 218 return isBasic(codePoint) && Character.isUnicodeIdentifierPart((char) codePoint); 219 } 220 221 public static boolean isUnicodeIdentifierStart(int codePoint) { 222 return isBasic(codePoint) && Character.isUnicodeIdentifierStart((char) codePoint); 223 } 224 225 public static boolean isUpperCase(int codePoint) { 226 return isBasic(codePoint) && Character.isUpperCase((char) codePoint); 227 } 228 229 public static boolean isValidCodePoint(int codePoint) { 230 return codePoint >= Character.MIN_CODE_POINT && codePoint <= Character.MAX_CODE_POINT; 231 } 232 233 public static boolean isWhitespace(int codePoint) { 234 return isBasic(codePoint) && Character.isWhitespace((char) codePoint); 235 } 236 237 public static int offsetByCodePoints(char[] chars, int start, int count, int index, int codePointOffset) { 238 int endIndex = start + count; 239 if (start < 0 || start > endIndex || endIndex > chars.length || index < start || index > endIndex) { 240 throw new IndexOutOfBoundsException (); 241 } 242 if (codePointOffset >= 0) { 243 for (int i = 0; i < codePointOffset; i++) { 244 if (index >= endIndex) { 245 throw new IndexOutOfBoundsException (); 246 } 247 if (isHighSurrogate(chars[index++]) && index < endIndex && isLowSurrogate(chars[index])) { 248 index++; 249 } 250 } 251 } else { 252 for (int i = codePointOffset; i < 0; i++) { 253 if (index <= start) { 254 throw new IndexOutOfBoundsException (); 255 } 256 if (isLowSurrogate(chars[--index]) && index > start && isHighSurrogate(chars[index - 1])) { 257 index--; 258 } 259 } 260 } 261 return index; 262 } 263 264 public static int offsetByCodePoints(CharSequence sequence, int index, int codePointOffset) { 265 if (index < 0 || index > sequence.length()) { 266 throw new IndexOutOfBoundsException (); 267 } 268 if (codePointOffset >= 0) { 269 for (int i = 0; i < codePointOffset; i++) { 270 if (isHighSurrogate(sequence.charAt(index++)) && 271 index < sequence.length() && isLowSurrogate(sequence.charAt(index))) { 272 index++; 273 } 274 } 275 } else { 276 for (int i = codePointOffset; i < 0; i++) { 277 if (isLowSurrogate(sequence.charAt(--index)) && 278 index > 0 && isHighSurrogate(sequence.charAt(index - 1))) { 279 index--; 280 } 281 } 282 } 283 return index; 284 } 285 286 public static char reverseBytes(char ch) { 287 return (char) (ch >> 8 & 0xFF | ch << 8); 288 } 289 290 public static char[] toChars(int codePoint) { 291 char[] chars = new char[charCount(codePoint)]; 292 toChars(codePoint, chars, 0); 293 return chars; 294 } 295 296 public static int toChars(int codePoint, char[] chars, int index) { 297 if (!isValidCodePoint(codePoint)) { 298 throw new IllegalArgumentException (); 299 } 300 if (codePoint < Character.MIN_SUPPLEMENTARY_CODE_POINT) { 301 chars[index] = (char) codePoint; 302 return 1; 303 } else { 304 chars[index] = (char) (((codePoint - Character.MIN_SUPPLEMENTARY_CODE_POINT) >>> 10) | 305 Character.MIN_HIGH_SURROGATE); 306 chars[index + 1] = (char) ((codePoint & SURROGATE_MASK) | Character.MIN_LOW_SURROGATE); 307 return 2; 308 } 309 } 310 311 public static int toCodePoint(char highChar, char lowChar) { 312 return ((highChar & SURROGATE_MASK) << 10 | (lowChar & SURROGATE_MASK)) + 313 Character.MIN_SUPPLEMENTARY_CODE_POINT; 314 } 315 316 public static int toLowerCase(int codePoint) { 317 return isBasic(codePoint) ? Character.toLowerCase((char) codePoint) : codePoint; 318 } 319 320 public static int toTitleCase(int codePoint) { 321 return isBasic(codePoint) ? Character.toTitleCase((char) codePoint) : codePoint; 322 } 323 324 public static int toUpperCase(int codePoint) { 325 return isBasic(codePoint) ? Character.toUpperCase((char) codePoint) : codePoint; 326 } 327 328 public static Character valueOf(char c) { 329 return c <= 127 ? cache[c] : new Character (c); 330 } 331 332 private static boolean isBasic(int codePoint) { 333 return codePoint >= 0 && codePoint < Character.MIN_SUPPLEMENTARY_CODE_POINT; 334 } 335 336 } 337 | Popular Tags |