Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/src/share/classes/sun/text/normalizer/Utility.java
38830 views
/*1* Copyright (c) 2005, 2009, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation. Oracle designates this7* particular file as subject to the "Classpath" exception as provided8* by Oracle in the LICENSE file that accompanied this code.9*10* This code is distributed in the hope that it will be useful, but WITHOUT11* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or12* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License13* version 2 for more details (a copy is included in the LICENSE file that14* accompanied this code).15*16* You should have received a copy of the GNU General Public License version17* 2 along with this work; if not, write to the Free Software Foundation,18* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.19*20* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA21* or visit www.oracle.com if you need additional information or have any22* questions.23*/24/*25*******************************************************************************26* (C) Copyright IBM Corp. and others, 1996-2009 - All Rights Reserved *27* *28* The original version of this source code and documentation is copyrighted *29* and owned by IBM, These materials are provided under terms of a License *30* Agreement between IBM and Sun. This technology is protected by multiple *31* US and International patents. This notice and attribution to IBM may not *32* to removed. *33*******************************************************************************34*/3536package sun.text.normalizer;3738public final class Utility {3940/**41* Convenience utility to compare two Object[]s42* Ought to be in System.43* @param len the length to compare.44* The start indices and start+len must be valid.45*/46public final static boolean arrayRegionMatches(char[] source, int sourceStart,47char[] target, int targetStart,48int len)49{50int sourceEnd = sourceStart + len;51int delta = targetStart - sourceStart;52for (int i = sourceStart; i < sourceEnd; i++) {53if (source[i]!=target[i + delta])54return false;55}56return true;57}5859/**60* Convert characters outside the range U+0020 to U+007F to61* Unicode escapes, and convert backslash to a double backslash.62*/63public static final String escape(String s) {64StringBuffer buf = new StringBuffer();65for (int i=0; i<s.length(); ) {66int c = UTF16.charAt(s, i);67i += UTF16.getCharCount(c);68if (c >= ' ' && c <= 0x007F) {69if (c == '\\') {70buf.append("\\\\"); // That is, "\\"71} else {72buf.append((char)c);73}74} else {75boolean four = c <= 0xFFFF;76buf.append(four ? "\\u" : "\\U");77hex(c, four ? 4 : 8, buf);78}79}80return buf.toString();81}8283/* This map must be in ASCENDING ORDER OF THE ESCAPE CODE */84static private final char[] UNESCAPE_MAP = {85/*" 0x22, 0x22 */86/*' 0x27, 0x27 */87/*? 0x3F, 0x3F */88/*\ 0x5C, 0x5C */89/*a*/ 0x61, 0x07,90/*b*/ 0x62, 0x08,91/*e*/ 0x65, 0x1b,92/*f*/ 0x66, 0x0c,93/*n*/ 0x6E, 0x0a,94/*r*/ 0x72, 0x0d,95/*t*/ 0x74, 0x09,96/*v*/ 0x76, 0x0b97};9899/**100* Convert an escape to a 32-bit code point value. We attempt101* to parallel the icu4c unescapeAt() function.102* @param offset16 an array containing offset to the character103* <em>after</em> the backslash. Upon return offset16[0] will104* be updated to point after the escape sequence.105* @return character value from 0 to 10FFFF, or -1 on error.106*/107public static int unescapeAt(String s, int[] offset16) {108int c;109int result = 0;110int n = 0;111int minDig = 0;112int maxDig = 0;113int bitsPerDigit = 4;114int dig;115int i;116boolean braces = false;117118/* Check that offset is in range */119int offset = offset16[0];120int length = s.length();121if (offset < 0 || offset >= length) {122return -1;123}124125/* Fetch first UChar after '\\' */126c = UTF16.charAt(s, offset);127offset += UTF16.getCharCount(c);128129/* Convert hexadecimal and octal escapes */130switch (c) {131case 'u':132minDig = maxDig = 4;133break;134case 'U':135minDig = maxDig = 8;136break;137case 'x':138minDig = 1;139if (offset < length && UTF16.charAt(s, offset) == 0x7B /*{*/) {140++offset;141braces = true;142maxDig = 8;143} else {144maxDig = 2;145}146break;147default:148dig = UCharacter.digit(c, 8);149if (dig >= 0) {150minDig = 1;151maxDig = 3;152n = 1; /* Already have first octal digit */153bitsPerDigit = 3;154result = dig;155}156break;157}158if (minDig != 0) {159while (offset < length && n < maxDig) {160c = UTF16.charAt(s, offset);161dig = UCharacter.digit(c, (bitsPerDigit == 3) ? 8 : 16);162if (dig < 0) {163break;164}165result = (result << bitsPerDigit) | dig;166offset += UTF16.getCharCount(c);167++n;168}169if (n < minDig) {170return -1;171}172if (braces) {173if (c != 0x7D /*}*/) {174return -1;175}176++offset;177}178if (result < 0 || result >= 0x110000) {179return -1;180}181// If an escape sequence specifies a lead surrogate, see182// if there is a trail surrogate after it, either as an183// escape or as a literal. If so, join them up into a184// supplementary.185if (offset < length &&186UTF16.isLeadSurrogate((char) result)) {187int ahead = offset+1;188c = s.charAt(offset); // [sic] get 16-bit code unit189if (c == '\\' && ahead < length) {190int o[] = new int[] { ahead };191c = unescapeAt(s, o);192ahead = o[0];193}194if (UTF16.isTrailSurrogate((char) c)) {195offset = ahead;196result = UCharacterProperty.getRawSupplementary(197(char) result, (char) c);198}199}200offset16[0] = offset;201return result;202}203204/* Convert C-style escapes in table */205for (i=0; i<UNESCAPE_MAP.length; i+=2) {206if (c == UNESCAPE_MAP[i]) {207offset16[0] = offset;208return UNESCAPE_MAP[i+1];209} else if (c < UNESCAPE_MAP[i]) {210break;211}212}213214/* Map \cX to control-X: X & 0x1F */215if (c == 'c' && offset < length) {216c = UTF16.charAt(s, offset);217offset16[0] = offset + UTF16.getCharCount(c);218return 0x1F & c;219}220221/* If no special forms are recognized, then consider222* the backslash to generically escape the next character. */223offset16[0] = offset;224return c;225}226227/**228* Convert a integer to size width hex uppercase digits.229* E.g., hex('a', 4, str) => "0041".230* Append the output to the given StringBuffer.231* If width is too small to fit, nothing will be appended to output.232*/233public static StringBuffer hex(int ch, int width, StringBuffer output) {234return appendNumber(output, ch, 16, width);235}236237/**238* Convert a integer to size width (minimum) hex uppercase digits.239* E.g., hex('a', 4, str) => "0041". If the integer requires more240* than width digits, more will be used.241*/242public static String hex(int ch, int width) {243StringBuffer buf = new StringBuffer();244return appendNumber(buf, ch, 16, width).toString();245}246247/**248* Skip over a sequence of zero or more white space characters249* at pos. Return the index of the first non-white-space character250* at or after pos, or str.length(), if there is none.251*/252public static int skipWhitespace(String str, int pos) {253while (pos < str.length()) {254int c = UTF16.charAt(str, pos);255if (!UCharacterProperty.isRuleWhiteSpace(c)) {256break;257}258pos += UTF16.getCharCount(c);259}260return pos;261}262263static final char DIGITS[] = {264'0', '1', '2', '3', '4', '5', '6', '7', '8', '9',265'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',266'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T',267'U', 'V', 'W', 'X', 'Y', 'Z'268};269270/**271* Append the digits of a positive integer to the given272* <code>StringBuffer</code> in the given radix. This is273* done recursively since it is easiest to generate the low-274* order digit first, but it must be appended last.275*276* @param result is the <code>StringBuffer</code> to append to277* @param n is the positive integer278* @param radix is the radix, from 2 to 36 inclusive279* @param minDigits is the minimum number of digits to append.280*/281private static void recursiveAppendNumber(StringBuffer result, int n,282int radix, int minDigits)283{284int digit = n % radix;285286if (n >= radix || minDigits > 1) {287recursiveAppendNumber(result, n / radix, radix, minDigits - 1);288}289290result.append(DIGITS[digit]);291}292293/**294* Append a number to the given StringBuffer in the given radix.295* Standard digits '0'-'9' are used and letters 'A'-'Z' for296* radices 11 through 36.297* @param result the digits of the number are appended here298* @param n the number to be converted to digits; may be negative.299* If negative, a '-' is prepended to the digits.300* @param radix a radix from 2 to 36 inclusive.301* @param minDigits the minimum number of digits, not including302* any '-', to produce. Values less than 2 have no effect. One303* digit is always emitted regardless of this parameter.304* @return a reference to result305*/306public static StringBuffer appendNumber(StringBuffer result, int n,307int radix, int minDigits)308throws IllegalArgumentException309{310if (radix < 2 || radix > 36) {311throw new IllegalArgumentException("Illegal radix " + radix);312}313314315int abs = n;316317if (n < 0) {318abs = -n;319result.append("-");320}321322recursiveAppendNumber(result, abs, radix, minDigits);323324return result;325}326327/**328* Return true if the character is NOT printable ASCII. The tab,329* newline and linefeed characters are considered unprintable.330*/331public static boolean isUnprintable(int c) {332return !(c >= 0x20 && c <= 0x7E);333}334335/**336* Escape unprintable characters using <backslash>uxxxx notation337* for U+0000 to U+FFFF and <backslash>Uxxxxxxxx for U+10000 and338* above. If the character is printable ASCII, then do nothing339* and return FALSE. Otherwise, append the escaped notation and340* return TRUE.341*/342public static boolean escapeUnprintable(StringBuffer result, int c) {343if (isUnprintable(c)) {344result.append('\\');345if ((c & ~0xFFFF) != 0) {346result.append('U');347result.append(DIGITS[0xF&(c>>28)]);348result.append(DIGITS[0xF&(c>>24)]);349result.append(DIGITS[0xF&(c>>20)]);350result.append(DIGITS[0xF&(c>>16)]);351} else {352result.append('u');353}354result.append(DIGITS[0xF&(c>>12)]);355result.append(DIGITS[0xF&(c>>8)]);356result.append(DIGITS[0xF&(c>>4)]);357result.append(DIGITS[0xF&c]);358return true;359}360return false;361}362363/**364* Similar to StringBuffer.getChars, version 1.3.365* Since JDK 1.2 implements StringBuffer.getChars differently, this method366* is here to provide consistent results.367* To be removed after JDK 1.2 ceased to be the reference platform.368* @param src source string buffer369* @param srcBegin offset to the start of the src to retrieve from370* @param srcEnd offset to the end of the src to retrieve from371* @param dst char array to store the retrieved chars372* @param dstBegin offset to the start of the destination char array to373* store the retrieved chars374*/375public static void getChars(StringBuffer src, int srcBegin, int srcEnd,376char dst[], int dstBegin)377{378if (srcBegin == srcEnd) {379return;380}381src.getChars(srcBegin, srcEnd, dst, dstBegin);382}383384}385386387