diff -r 07b686da11c4 -r c7d7946239de jdk/make/src/classes/build/tools/generatecharacter/Utility.java --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/jdk/make/src/classes/build/tools/generatecharacter/Utility.java Thu Nov 14 11:19:32 2013 +0100 @@ -0,0 +1,407 @@ +/* + * Copyright (c) 2002, 2003, Oracle and/or its affiliates. All rights reserved. + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. + * + * This code is free software; you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 2 only, as + * published by the Free Software Foundation. Oracle designates this + * particular file as subject to the "Classpath" exception as provided + * by Oracle in the LICENSE file that accompanied this code. + * + * This code is distributed in the hope that it will be useful, but WITHOUT + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License + * version 2 for more details (a copy is included in the LICENSE file that + * accompanied this code). + * + * You should have received a copy of the GNU General Public License version + * 2 along with this work; if not, write to the Free Software Foundation, + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. + * + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA + * or visit www.oracle.com if you need additional information or have any + * questions. + */ + +package build.tools.generatecharacter; + +import java.text.*; +import java.util.*; + +public class Utility { + static byte peekByte(String s, int index) { + char c = s.charAt(index/2); + return ((index&1)==0)?(byte)(c>>8):(byte)c; + } + + static short peekShort(String s, int index) { + return (short)s.charAt(index); + } + + static int peekInt(String s, int index) { + index *= 2; + return (((int)s.charAt(index)) << 16) | s.charAt(index+1); + } + + static void poke(String s, int index, byte value) { + int mask = 0xFF00; + int ivalue = value; + if ((index&1)==0) { + ivalue <<= 8; + mask = 0x00FF; + } + index /= 2; + if (index == s.length()) { + s = s + (char)ivalue; + } + else if (index == 0) { + s = (char)(ivalue|(s.charAt(0)&mask)) + s.substring(1); + } + else { + s = s.substring(0, index) + (char)(ivalue|(s.charAt(index)&mask)) + + s.substring(index+1); + } + } + + static void poke(String s, int index, short value) { + if (index == s.length()) { + s = s + (char)value; + } + else if (index == 0) { + s = (char)value + s.substring(1); + } + else { + s = s.substring(0, index) + (char)value + s.substring(index+1); + } + } + + static void poke(String s, int index, int value) { + index *= 2; + char hi = (char)(value >> 16); + if (index == s.length()) { + s = s + hi + (char)value; + } + else if (index == 0) { + s = hi + (char)value + s.substring(2); + } + else { + s = s.substring(0, index) + hi + (char)value + s.substring(index+2); + } + } + + /** + * The ESCAPE character is used during run-length encoding. It signals + * a run of identical chars. + */ + static final char ESCAPE = '\uA5A5'; + + /** + * The ESCAPE_BYTE character is used during run-length encoding. It signals + * a run of identical bytes. + */ + static final byte ESCAPE_BYTE = (byte)0xA5; + + /** + * Construct a string representing a short array. Use run-length encoding. + * A character represents itself, unless it is the ESCAPE character. Then + * the following notations are possible: + * ESCAPE ESCAPE ESCAPE literal + * ESCAPE n c n instances of character c + * Since an encoded run occupies 3 characters, we only encode runs of 4 or + * more characters. Thus we have n > 0 and n != ESCAPE and n <= 0xFFFF. + * If we encounter a run where n == ESCAPE, we represent this as: + * c ESCAPE n-1 c + * The ESCAPE value is chosen so as not to collide with commonly + * seen values. + */ + static final String arrayToRLEString(short[] a) { + StringBuffer buffer = new StringBuffer(); + // for (int i=0; i> 16)); + buffer.append((char) a.length); + short runValue = a[0]; + int runLength = 1; + for (int i=1; i 0 and n != ESCAPE_BYTE and n <= 0xFF. + * If we encounter a run where n == ESCAPE_BYTE, we represent this as: + * b ESCAPE_BYTE n-1 b + * The ESCAPE_BYTE value is chosen so as not to collide with commonly + * seen values. + */ + static final String arrayToRLEString(byte[] a) { + StringBuffer buffer = new StringBuffer(); + buffer.append((char) (a.length >> 16)); + buffer.append((char) a.length); + byte runValue = a[0]; + int runLength = 1; + byte[] state = new byte[2]; + for (int i=1; i 0 && <= 0xFFFF. + */ + private static final void encodeRun(StringBuffer buffer, short value, int length) { + if (length < 4) { + for (int j=0; j 0 && <= 0xFF. + */ + private static final void encodeRun(StringBuffer buffer, byte value, int length, + byte[] state) { + if (length < 4) { + for (int j=0; j> 8); + nextChar = false; + } + else { + b = (byte) (c & 0xFF); + nextChar = true; + } + + // This part of the loop is a tiny state machine which handles + // the parsing of the run-length encoding. This would be simpler + // if we could look ahead, but we can't, so we use 'node' to + // move between three nodes in the state machine. + switch (node) { + case 0: + // Normal idle node + if (b == ESCAPE_BYTE) { + node = 1; + } + else { + array[ai++] = b; + } + break; + case 1: + // We have seen one ESCAPE_BYTE; we expect either a second + // one, or a run length and value. + if (b == ESCAPE_BYTE) { + array[ai++] = ESCAPE_BYTE; + node = 0; + } + else { + runLength = b; + // Interpret signed byte as unsigned + if (runLength < 0) runLength += 0x100; + node = 2; + } + break; + case 2: + // We have seen an ESCAPE_BYTE and length byte. We interpret + // the next byte as the value to be repeated. + for (int j=0; j 0) buffer.append("+\n"); + int limit = buffer.length() + 78; // Leave 2 for trailing <"+> + buffer.append(indent + '"'); + while (i= '\u0020' && c <= '\u007E') { + // Printable ASCII ranges from ' ' to '~' + buffer.append(c); + } + else + */ + if (c <= '\377') { + // Represent control characters + // using octal notation; otherwise the string we form + // won't compile, since Unicode escape sequences are + // processed before tokenization. + buffer.append('\\'); + buffer.append(HEX_DIGIT[(c & 0700) >> 6]); // HEX_DIGIT works for octal + buffer.append(HEX_DIGIT[(c & 0070) >> 3]); + buffer.append(HEX_DIGIT[(c & 0007)]); + } + else { + // Handle the rest with Unicode + buffer.append("\\u"); + buffer.append(HEX_DIGIT[(c & 0xF000) >> 12]); + buffer.append(HEX_DIGIT[(c & 0x0F00) >> 8]); + buffer.append(HEX_DIGIT[(c & 0x00F0) >> 4]); + buffer.append(HEX_DIGIT[(c & 0x000F)]); + } + } + buffer.append('"'); + } + return buffer.toString(); + } + + static final char[] HEX_DIGIT = {'0','1','2','3','4','5','6','7', + '8','9','A','B','C','D','E','F'}; +}