0N/A/*
2362N/A * Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A/*
0N/A *******************************************************************************
1091N/A * (C) Copyright IBM Corp. and others, 1996-2009 - All Rights Reserved *
0N/A * *
0N/A * The original version of this source code and documentation is copyrighted *
0N/A * and owned by IBM, These materials are provided under terms of a License *
0N/A * Agreement between IBM and Sun. This technology is protected by multiple *
0N/A * US and International patents. This notice and attribution to IBM may not *
0N/A * to removed. *
0N/A *******************************************************************************
0N/A */
0N/A
0N/Apackage sun.text.normalizer;
0N/A
0N/Apublic final class Utility {
0N/A
0N/A /**
1091N/A * Convenience utility to compare two Object[]s
1091N/A * Ought to be in System.
1091N/A * @param len the length to compare.
1091N/A * The start indices and start+len must be valid.
1091N/A */
1091N/A public final static boolean arrayRegionMatches(char[] source, int sourceStart,
1091N/A char[] target, int targetStart,
1091N/A int len)
1091N/A {
1091N/A int sourceEnd = sourceStart + len;
1091N/A int delta = targetStart - sourceStart;
1091N/A for (int i = sourceStart; i < sourceEnd; i++) {
1091N/A if (source[i]!=target[i + delta])
1091N/A return false;
1091N/A }
1091N/A return true;
1091N/A }
1091N/A
1091N/A /**
0N/A * Convert characters outside the range U+0020 to U+007F to
0N/A * Unicode escapes, and convert backslash to a double backslash.
0N/A */
0N/A public static final String escape(String s) {
0N/A StringBuffer buf = new StringBuffer();
0N/A for (int i=0; i<s.length(); ) {
0N/A int c = UTF16.charAt(s, i);
0N/A i += UTF16.getCharCount(c);
0N/A if (c >= ' ' && c <= 0x007F) {
0N/A if (c == '\\') {
0N/A buf.append("\\\\"); // That is, "\\"
0N/A } else {
0N/A buf.append((char)c);
0N/A }
0N/A } else {
0N/A boolean four = c <= 0xFFFF;
0N/A buf.append(four ? "\\u" : "\\U");
0N/A hex(c, four ? 4 : 8, buf);
0N/A }
0N/A }
0N/A return buf.toString();
0N/A }
0N/A
0N/A /* This map must be in ASCENDING ORDER OF THE ESCAPE CODE */
0N/A static private final char[] UNESCAPE_MAP = {
0N/A /*" 0x22, 0x22 */
0N/A /*' 0x27, 0x27 */
0N/A /*? 0x3F, 0x3F */
0N/A /*\ 0x5C, 0x5C */
0N/A /*a*/ 0x61, 0x07,
0N/A /*b*/ 0x62, 0x08,
0N/A /*e*/ 0x65, 0x1b,
0N/A /*f*/ 0x66, 0x0c,
0N/A /*n*/ 0x6E, 0x0a,
0N/A /*r*/ 0x72, 0x0d,
0N/A /*t*/ 0x74, 0x09,
0N/A /*v*/ 0x76, 0x0b
0N/A };
0N/A
0N/A /**
0N/A * Convert an escape to a 32-bit code point value. We attempt
0N/A * to parallel the icu4c unescapeAt() function.
0N/A * @param offset16 an array containing offset to the character
0N/A * <em>after</em> the backslash. Upon return offset16[0] will
0N/A * be updated to point after the escape sequence.
0N/A * @return character value from 0 to 10FFFF, or -1 on error.
0N/A */
0N/A public static int unescapeAt(String s, int[] offset16) {
0N/A int c;
0N/A int result = 0;
0N/A int n = 0;
0N/A int minDig = 0;
0N/A int maxDig = 0;
0N/A int bitsPerDigit = 4;
0N/A int dig;
0N/A int i;
0N/A boolean braces = false;
0N/A
0N/A /* Check that offset is in range */
0N/A int offset = offset16[0];
0N/A int length = s.length();
0N/A if (offset < 0 || offset >= length) {
0N/A return -1;
0N/A }
0N/A
0N/A /* Fetch first UChar after '\\' */
0N/A c = UTF16.charAt(s, offset);
0N/A offset += UTF16.getCharCount(c);
0N/A
0N/A /* Convert hexadecimal and octal escapes */
0N/A switch (c) {
0N/A case 'u':
0N/A minDig = maxDig = 4;
0N/A break;
0N/A case 'U':
0N/A minDig = maxDig = 8;
0N/A break;
0N/A case 'x':
0N/A minDig = 1;
0N/A if (offset < length && UTF16.charAt(s, offset) == 0x7B /*{*/) {
0N/A ++offset;
0N/A braces = true;
0N/A maxDig = 8;
0N/A } else {
0N/A maxDig = 2;
0N/A }
0N/A break;
0N/A default:
0N/A dig = UCharacter.digit(c, 8);
0N/A if (dig >= 0) {
0N/A minDig = 1;
0N/A maxDig = 3;
0N/A n = 1; /* Already have first octal digit */
0N/A bitsPerDigit = 3;
0N/A result = dig;
0N/A }
0N/A break;
0N/A }
0N/A if (minDig != 0) {
0N/A while (offset < length && n < maxDig) {
0N/A c = UTF16.charAt(s, offset);
0N/A dig = UCharacter.digit(c, (bitsPerDigit == 3) ? 8 : 16);
0N/A if (dig < 0) {
0N/A break;
0N/A }
0N/A result = (result << bitsPerDigit) | dig;
0N/A offset += UTF16.getCharCount(c);
0N/A ++n;
0N/A }
0N/A if (n < minDig) {
0N/A return -1;
0N/A }
0N/A if (braces) {
0N/A if (c != 0x7D /*}*/) {
0N/A return -1;
0N/A }
0N/A ++offset;
0N/A }
0N/A if (result < 0 || result >= 0x110000) {
0N/A return -1;
0N/A }
0N/A // If an escape sequence specifies a lead surrogate, see
0N/A // if there is a trail surrogate after it, either as an
0N/A // escape or as a literal. If so, join them up into a
0N/A // supplementary.
0N/A if (offset < length &&
0N/A UTF16.isLeadSurrogate((char) result)) {
0N/A int ahead = offset+1;
0N/A c = s.charAt(offset); // [sic] get 16-bit code unit
0N/A if (c == '\\' && ahead < length) {
0N/A int o[] = new int[] { ahead };
0N/A c = unescapeAt(s, o);
0N/A ahead = o[0];
0N/A }
0N/A if (UTF16.isTrailSurrogate((char) c)) {
0N/A offset = ahead;
0N/A result = UCharacterProperty.getRawSupplementary(
0N/A (char) result, (char) c);
0N/A }
0N/A }
0N/A offset16[0] = offset;
0N/A return result;
0N/A }
0N/A
0N/A /* Convert C-style escapes in table */
0N/A for (i=0; i<UNESCAPE_MAP.length; i+=2) {
0N/A if (c == UNESCAPE_MAP[i]) {
0N/A offset16[0] = offset;
0N/A return UNESCAPE_MAP[i+1];
0N/A } else if (c < UNESCAPE_MAP[i]) {
0N/A break;
0N/A }
0N/A }
0N/A
0N/A /* Map \cX to control-X: X & 0x1F */
0N/A if (c == 'c' && offset < length) {
0N/A c = UTF16.charAt(s, offset);
0N/A offset16[0] = offset + UTF16.getCharCount(c);
0N/A return 0x1F & c;
0N/A }
0N/A
0N/A /* If no special forms are recognized, then consider
0N/A * the backslash to generically escape the next character. */
0N/A offset16[0] = offset;
0N/A return c;
0N/A }
0N/A
0N/A /**
0N/A * Convert a integer to size width hex uppercase digits.
0N/A * E.g., hex('a', 4, str) => "0041".
0N/A * Append the output to the given StringBuffer.
0N/A * If width is too small to fit, nothing will be appended to output.
0N/A */
0N/A public static StringBuffer hex(int ch, int width, StringBuffer output) {
0N/A return appendNumber(output, ch, 16, width);
0N/A }
0N/A
0N/A /**
0N/A * Convert a integer to size width (minimum) hex uppercase digits.
0N/A * E.g., hex('a', 4, str) => "0041". If the integer requires more
0N/A * than width digits, more will be used.
0N/A */
0N/A public static String hex(int ch, int width) {
0N/A StringBuffer buf = new StringBuffer();
0N/A return appendNumber(buf, ch, 16, width).toString();
0N/A }
0N/A
0N/A /**
0N/A * Skip over a sequence of zero or more white space characters
0N/A * at pos. Return the index of the first non-white-space character
0N/A * at or after pos, or str.length(), if there is none.
0N/A */
0N/A public static int skipWhitespace(String str, int pos) {
0N/A while (pos < str.length()) {
0N/A int c = UTF16.charAt(str, pos);
0N/A if (!UCharacterProperty.isRuleWhiteSpace(c)) {
0N/A break;
0N/A }
0N/A pos += UTF16.getCharCount(c);
0N/A }
0N/A return pos;
0N/A }
0N/A
0N/A static final char DIGITS[] = {
0N/A '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
0N/A 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
0N/A 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',
0N/A 'U', 'V', 'W', 'X', 'Y', 'Z'
0N/A };
0N/A
0N/A /**
0N/A * Append the digits of a positive integer to the given
0N/A * <code>StringBuffer</code> in the given radix. This is
0N/A * done recursively since it is easiest to generate the low-
0N/A * order digit first, but it must be appended last.
0N/A *
0N/A * @param result is the <code>StringBuffer</code> to append to
0N/A * @param n is the positive integer
0N/A * @param radix is the radix, from 2 to 36 inclusive
0N/A * @param minDigits is the minimum number of digits to append.
0N/A */
0N/A private static void recursiveAppendNumber(StringBuffer result, int n,
0N/A int radix, int minDigits)
0N/A {
0N/A int digit = n % radix;
0N/A
0N/A if (n >= radix || minDigits > 1) {
0N/A recursiveAppendNumber(result, n / radix, radix, minDigits - 1);
0N/A }
0N/A
0N/A result.append(DIGITS[digit]);
0N/A }
0N/A
0N/A /**
0N/A * Append a number to the given StringBuffer in the given radix.
0N/A * Standard digits '0'-'9' are used and letters 'A'-'Z' for
0N/A * radices 11 through 36.
0N/A * @param result the digits of the number are appended here
0N/A * @param n the number to be converted to digits; may be negative.
0N/A * If negative, a '-' is prepended to the digits.
0N/A * @param radix a radix from 2 to 36 inclusive.
0N/A * @param minDigits the minimum number of digits, not including
0N/A * any '-', to produce. Values less than 2 have no effect. One
0N/A * digit is always emitted regardless of this parameter.
0N/A * @return a reference to result
0N/A */
0N/A public static StringBuffer appendNumber(StringBuffer result, int n,
0N/A int radix, int minDigits)
0N/A throws IllegalArgumentException
0N/A {
0N/A if (radix < 2 || radix > 36) {
0N/A throw new IllegalArgumentException("Illegal radix " + radix);
0N/A }
0N/A
0N/A
0N/A int abs = n;
0N/A
0N/A if (n < 0) {
0N/A abs = -n;
0N/A result.append("-");
0N/A }
0N/A
0N/A recursiveAppendNumber(result, abs, radix, minDigits);
0N/A
0N/A return result;
0N/A }
0N/A
0N/A /**
0N/A * Return true if the character is NOT printable ASCII. The tab,
0N/A * newline and linefeed characters are considered unprintable.
0N/A */
0N/A public static boolean isUnprintable(int c) {
0N/A return !(c >= 0x20 && c <= 0x7E);
0N/A }
0N/A
0N/A /**
0N/A * Escape unprintable characters using <backslash>uxxxx notation
0N/A * for U+0000 to U+FFFF and <backslash>Uxxxxxxxx for U+10000 and
0N/A * above. If the character is printable ASCII, then do nothing
0N/A * and return FALSE. Otherwise, append the escaped notation and
0N/A * return TRUE.
0N/A */
0N/A public static boolean escapeUnprintable(StringBuffer result, int c) {
0N/A if (isUnprintable(c)) {
0N/A result.append('\\');
0N/A if ((c & ~0xFFFF) != 0) {
0N/A result.append('U');
0N/A result.append(DIGITS[0xF&(c>>28)]);
0N/A result.append(DIGITS[0xF&(c>>24)]);
0N/A result.append(DIGITS[0xF&(c>>20)]);
0N/A result.append(DIGITS[0xF&(c>>16)]);
0N/A } else {
0N/A result.append('u');
0N/A }
0N/A result.append(DIGITS[0xF&(c>>12)]);
0N/A result.append(DIGITS[0xF&(c>>8)]);
0N/A result.append(DIGITS[0xF&(c>>4)]);
0N/A result.append(DIGITS[0xF&c]);
0N/A return true;
0N/A }
0N/A return false;
0N/A }
0N/A
0N/A /**
0N/A * Similar to StringBuffer.getChars, version 1.3.
0N/A * Since JDK 1.2 implements StringBuffer.getChars differently, this method
0N/A * is here to provide consistent results.
0N/A * To be removed after JDK 1.2 ceased to be the reference platform.
0N/A * @param src source string buffer
0N/A * @param srcBegin offset to the start of the src to retrieve from
0N/A * @param srcEnd offset to the end of the src to retrieve from
0N/A * @param dst char array to store the retrieved chars
0N/A * @param dstBegin offset to the start of the destination char array to
0N/A * store the retrieved chars
0N/A */
0N/A public static void getChars(StringBuffer src, int srcBegin, int srcEnd,
0N/A char dst[], int dstBegin)
0N/A {
0N/A if (srcBegin == srcEnd) {
0N/A return;
0N/A }
0N/A src.getChars(srcBegin, srcEnd, dst, dstBegin);
0N/A }
0N/A
0N/A}