From 0ffba474fa3ebd554af7f0e776286d86903dd1b1 Mon Sep 17 00:00:00 2001 From: Zsombor Date: Sun, 13 Jul 2008 20:33:26 -0600 Subject: [PATCH] implement additional Unicode support in Character --- classpath/java/lang/Character.java | 75 ++++++++++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/classpath/java/lang/Character.java b/classpath/java/lang/Character.java index a51008cf7d..6627ae3170 100644 --- a/classpath/java/lang/Character.java +++ b/classpath/java/lang/Character.java @@ -58,6 +58,14 @@ public final class Character implements Comparable { } } + public static int toLowerCase(int codePoint) { + if (isSupplementaryCodePoint(codePoint)) { + return codePoint; + } else { + return toLowerCase((char) codePoint); + } + } + public static char toUpperCase(char c) { if (c >= 'a' && c <= 'z') { return (char) ((c - 'a') + 'A'); @@ -66,10 +74,22 @@ public final class Character implements Comparable { } } + public static int toUpperCase(int codePoint) { + if (isSupplementaryCodePoint(codePoint)) { + return codePoint; + } else { + return toUpperCase((char) codePoint); + } + } + public static boolean isDigit(char c) { return c >= '0' && c <= '9'; } + public static boolean isDigit(int c) { + return c >= '0' && c <= '9'; + } + public static int digit(char c, int radix) { int digit = 0; if ((c >= '0') && (c <= '9')) { @@ -87,6 +107,10 @@ public final class Character implements Comparable { } } + public static boolean isLetter(int c) { + return canCastToChar(c) && isLetter((char) c); + } + public static boolean isLetter(char c) { return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'); } @@ -95,6 +119,14 @@ public final class Character implements Comparable { return isDigit(c) || isLetter(c); } + public static boolean isLetterOrDigit(int c) { + return canCastToChar(c) && (isDigit((char) c) || isLetter((char) c)); + } + + public static boolean isLowerCase(int c) { + return canCastToChar(c) && isLowerCase((char) c); + } + public static boolean isLowerCase(char c) { return (c >= 'a' && c <= 'z'); } @@ -103,6 +135,14 @@ public final class Character implements Comparable { return (c >= 'A' && c <= 'Z'); } + public static boolean isUpperCase(int c) { + return canCastToChar(c) && isUpperCase((char) c); + } + + public static boolean isWhitespace(int c) { + return canCastToChar(c) && isWhitespace((char) c); + } + public static boolean isWhitespace(char c) { return c == ' ' || c == '\t' || c == '\n' || c == '\r'; } @@ -110,4 +150,39 @@ public final class Character implements Comparable { public static boolean isSpaceChar(char c) { return isWhitespace(c); } + + public static boolean isHighSurrogate(char ch) { + return ch >= '\uD800' && ch <= '\uDBFF'; + } + + public static boolean isLowSurrogate(char ch) { + return ch >= '\uDC00' && ch <= '\uDFFF'; + } + + public static int toCodePoint(char high, char low) { + return (((high & 0x3FF) << 10) | (low & 0x3FF)) + 0x10000; + } + + public static boolean isSupplementaryCodePoint(int codePoint) { + return codePoint >= 0x10000 && codePoint <= 0x10FFFF; + } + + private static boolean canCastToChar(int codePoint) { + return (codePoint >= 0 && codePoint <= 0xFFFF); + } + + public static char[] toChars(int codePoint) { + if (isSupplementaryCodePoint(codePoint)) { + int cpPrime = codePoint - 0x10000; + int high = 0xD800 | ((cpPrime >> 10) & 0x3FF); + int low = 0xDC00 | (cpPrime & 0x3FF); + return new char[] { (char) high, (char) low }; + } + return new char[] { (char) codePoint }; + } + + public static boolean isSurrogatePair(char high, char low) { + return isHighSurrogate(high) && isLowSurrogate(low); + } + }