make/jdk/src/classes/build/tools/generatecharacter/Utility.java
author lana
Mon, 19 Mar 2018 21:52:50 +0000
changeset 49266 778e4516409c
parent 47216 71c04702a3d5
permissions -rw-r--r--
Merge
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
23010
6dadb192ad81 8029235: Update copyright year to match last edit in jdk8 jdk repository for 2013
lana
parents: 21805
diff changeset
     2
 * Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
package build.tools.generatecharacter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
public class Utility {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
    static byte peekByte(String s, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
        char c = s.charAt(index/2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
        return ((index&1)==0)?(byte)(c>>8):(byte)c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    static short peekShort(String s, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        return (short)s.charAt(index);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
    static int peekInt(String s, int index) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
        index *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
        return (((int)s.charAt(index)) << 16) | s.charAt(index+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    static void poke(String s, int index, byte value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        int mask = 0xFF00;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
        int ivalue = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        if ((index&1)==0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            ivalue <<= 8;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            mask = 0x00FF;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
        index /= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        if (index == s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            s = s + (char)ivalue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
        else if (index == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            s = (char)(ivalue|(s.charAt(0)&mask)) + s.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            s = s.substring(0, index) + (char)(ivalue|(s.charAt(index)&mask))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
                + s.substring(index+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
    static void poke(String s, int index, short value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
        if (index == s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
            s = s + (char)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        else if (index == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            s = (char)value + s.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
            s = s.substring(0, index) + (char)value + s.substring(index+1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
    static void poke(String s, int index, int value) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        index *= 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        char hi = (char)(value >> 16);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
        if (index == s.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
            s = s + hi + (char)value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        else if (index == 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            s = hi + (char)value + s.substring(2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            s = s.substring(0, index) + hi + (char)value + s.substring(index+2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
     * The ESCAPE character is used during run-length encoding.  It signals
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
     * a run of identical chars.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    static final char ESCAPE = '\uA5A5';
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
     * The ESCAPE_BYTE character is used during run-length encoding.  It signals
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
     * a run of identical bytes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
    static final byte ESCAPE_BYTE = (byte)0xA5;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
     * Construct a string representing a short array.  Use run-length encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
     * A character represents itself, unless it is the ESCAPE character.  Then
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
     * the following notations are possible:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
     *   ESCAPE ESCAPE   ESCAPE literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
     *   ESCAPE n c      n instances of character c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
     * Since an encoded run occupies 3 characters, we only encode runs of 4 or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
     * more characters.  Thus we have n > 0 and n != ESCAPE and n <= 0xFFFF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
     * If we encounter a run where n == ESCAPE, we represent this as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
     *   c ESCAPE n-1 c
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
     * The ESCAPE value is chosen so as not to collide with commonly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
     * seen values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
    static final String arrayToRLEString(short[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
        StringBuffer buffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
        // for (int i=0; i<a.length; ++i) buffer.append((char) a[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        buffer.append((char) (a.length >> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        buffer.append((char) a.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
        short runValue = a[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
        int runLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
        for (int i=1; i<a.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
            short s = a[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            if (s == runValue && runLength < 0xFFFF) ++runLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                encodeRun(buffer, runValue, runLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
                runValue = s;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                runLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        encodeRun(buffer, runValue, runLength);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
        return buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     * Construct a string representing a byte array.  Use run-length encoding.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
     * Two bytes are packed into a single char, with a single extra zero byte at
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
     * the end if needed.  A byte represents itself, unless it is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
     * ESCAPE_BYTE.  Then the following notations are possible:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
     *   ESCAPE_BYTE ESCAPE_BYTE   ESCAPE_BYTE literal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
     *   ESCAPE_BYTE n b           n instances of byte b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
     * Since an encoded run occupies 3 bytes, we only encode runs of 4 or
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
     * more bytes.  Thus we have n > 0 and n != ESCAPE_BYTE and n <= 0xFF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
     * If we encounter a run where n == ESCAPE_BYTE, we represent this as:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
     *   b ESCAPE_BYTE n-1 b
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
     * The ESCAPE_BYTE value is chosen so as not to collide with commonly
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
     * seen values.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   151
    static final String arrayToRLEString(byte[] a) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   152
        StringBuffer buffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
        buffer.append((char) (a.length >> 16));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
        buffer.append((char) a.length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
        byte runValue = a[0];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
        int runLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
        byte[] state = new byte[2];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
        for (int i=1; i<a.length; ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
            byte b = a[i];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
            if (b == runValue && runLength < 0xFF) ++runLength;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
                encodeRun(buffer, runValue, runLength, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
                runValue = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
                runLength = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
        encodeRun(buffer, runValue, runLength, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        // We must save the final byte, if there is one, by padding
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        // an extra zero.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
        if (state[0] != 0) appendEncodedByte(buffer, (byte)0, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
        return buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
     * Encode a run, possibly a degenerate run (of < 4 values).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
     * @param length The length of the run; must be > 0 && <= 0xFFFF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
    private static final void encodeRun(StringBuffer buffer, short value, int length) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
        if (length < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
            for (int j=0; j<length; ++j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
                if (value == (int) ESCAPE) buffer.append(ESCAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
                buffer.append((char) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            if (length == (int) ESCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
                if (value == (int) ESCAPE) buffer.append(ESCAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
                buffer.append((char) value);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
                --length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
            buffer.append(ESCAPE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
            buffer.append((char) length);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
            buffer.append((char) value); // Don't need to escape this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
     * Encode a run, possibly a degenerate run (of < 4 values).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
     * @param length The length of the run; must be > 0 && <= 0xFF.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    private static final void encodeRun(StringBuffer buffer, byte value, int length,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
                                        byte[] state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
        if (length < 4) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
            for (int j=0; j<length; ++j) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
                if (value == ESCAPE_BYTE) appendEncodedByte(buffer, ESCAPE_BYTE, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
                appendEncodedByte(buffer, value, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
            if (length == ESCAPE_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
                if (value == ESCAPE_BYTE) appendEncodedByte(buffer, ESCAPE_BYTE, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
                appendEncodedByte(buffer, value, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
                --length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
            appendEncodedByte(buffer, ESCAPE_BYTE, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
            appendEncodedByte(buffer, (byte)length, state);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
            appendEncodedByte(buffer, value, state); // Don't need to escape this value
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
90ce3da70b43 Initial load
duke
parents:
diff changeset
   223
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
     * Append a byte to the given StringBuffer, packing two bytes into each
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
     * character.  The state parameter maintains intermediary data between
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
     * calls.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
     * @param state A two-element array, with state[0] == 0 if this is the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
     * first byte of a pair, or state[0] != 0 if this is the second byte
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
     * of a pair, in which case state[1] is the first byte.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
    private static final void appendEncodedByte(StringBuffer buffer, byte value,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
                                                byte[] state) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
        if (state[0] != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
            char c = (char) ((state[1] << 8) | (((int) value) & 0xFF));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
            buffer.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
            state[0] = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
        else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
            state[0] = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
            state[1] = value;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   245
     * Construct an array of shorts from a run-length encoded string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    static final short[] RLEStringToShortArray(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        int length = (((int) s.charAt(0)) << 16) | ((int) s.charAt(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        short[] array = new short[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
        int ai = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
        for (int i=2; i<s.length(); ++i) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
            char c = s.charAt(i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
            if (c == ESCAPE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
                c = s.charAt(++i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
                if (c == ESCAPE) array[ai++] = (short) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
                    int runLength = (int) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
                    short runValue = (short) s.charAt(++i);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
                    for (int j=0; j<runLength; ++j) array[ai++] = runValue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
                array[ai++] = (short) c;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
        if (ai != length)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
            throw new InternalError("Bad run-length encoded short array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
        return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
     * Construct an array of bytes from a run-length encoded string.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
    static final byte[] RLEStringToByteArray(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
        int length = (((int) s.charAt(0)) << 16) | ((int) s.charAt(1));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
        byte[] array = new byte[length];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
        boolean nextChar = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
        char c = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
        int node = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        int runLength = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        int i = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   284
        for (int ai=0; ai<length; ) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
            // This part of the loop places the next byte into the local
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
            // variable 'b' each time through the loop.  It keeps the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
            // current character in 'c' and uses the boolean 'nextChar'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
            // to see if we've taken both bytes out of 'c' yet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
            byte b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
            if (nextChar) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
                c = s.charAt(i++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
                b = (byte) (c >> 8);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
                nextChar = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
            else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
                b = (byte) (c & 0xFF);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
                nextChar = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
            // This part of the loop is a tiny state machine which handles
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
            // the parsing of the run-length encoding.  This would be simpler
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
            // if we could look ahead, but we can't, so we use 'node' to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
            // move between three nodes in the state machine.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   304
            switch (node) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
            case 0:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
                // Normal idle node
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
                if (b == ESCAPE_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
                    node = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
                    array[ai++] = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
            case 1:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   315
                // We have seen one ESCAPE_BYTE; we expect either a second
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
                // one, or a run length and value.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
                if (b == ESCAPE_BYTE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
                    array[ai++] = ESCAPE_BYTE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
                    node = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
                    runLength = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
                    // Interpret signed byte as unsigned
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
                    if (runLength < 0) runLength += 0x100;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
                    node = 2;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
            case 2:
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
                // We have seen an ESCAPE_BYTE and length byte.  We interpret
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
                // the next byte as the value to be repeated.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
                for (int j=0; j<runLength; ++j) array[ai++] = b;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
                node = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
                break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
        if (node != 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
            throw new InternalError("Bad run-length encoded byte array");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        if (i != s.length())
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
            throw new InternalError("Excess data in RLE byte array string");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
        return array;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
     * Format a String for representation in a source file.  This includes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
     * breaking it into lines escaping characters using octal notation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
     * when necessary (control characters and double quotes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
    static final String formatForSource(String s) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
        return formatForSource(s, "        ");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
     * Format a String for representation in a source file.  This includes
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
     * breaking it into lines escaping characters using octal notation
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
     * when necessary (control characters and double quotes).
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    static final String formatForSource(String s, String indent) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
        StringBuffer buffer = new StringBuffer();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
        for (int i=0; i<s.length();) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
            if (i > 0) buffer.append("+\n");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
            int limit = buffer.length() + 78; // Leave 2 for trailing <"+>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
            buffer.append(indent + '"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
            while (i<s.length() && buffer.length()<limit) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
                char c = s.charAt(i++);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
                /* This works too but it's kind of unnecessary; might as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
                   well keep things simple.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
                if (c == '\\' || c == '"') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
                    // Escape backslash and double-quote.  Don't need to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
                    // escape single-quote.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
                    buffer.append("\\" + c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
                else if (c >= '\u0020' && c <= '\u007E') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
                    // Printable ASCII ranges from ' ' to '~'
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
                    buffer.append(c);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
                else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
                */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
                if (c <= '\377') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
                    // Represent control characters
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
                    // using octal notation; otherwise the string we form
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
                    // won't compile, since Unicode escape sequences are
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
                    // processed before tokenization.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
                    buffer.append('\\');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
                    buffer.append(HEX_DIGIT[(c & 0700) >> 6]); // HEX_DIGIT works for octal
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
                    buffer.append(HEX_DIGIT[(c & 0070) >> 3]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
                    buffer.append(HEX_DIGIT[(c & 0007)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
                else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
                    // Handle the rest with Unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
                    buffer.append("\\u");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
                    buffer.append(HEX_DIGIT[(c & 0xF000) >> 12]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
                    buffer.append(HEX_DIGIT[(c & 0x0F00) >> 8]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
                    buffer.append(HEX_DIGIT[(c & 0x00F0) >> 4]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
                    buffer.append(HEX_DIGIT[(c & 0x000F)]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
            buffer.append('"');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        return buffer.toString();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
    static final char[] HEX_DIGIT = {'0','1','2','3','4','5','6','7',
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
                                     '8','9','A','B','C','D','E','F'};
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
}