langtools/src/jdk.compiler/share/classes/com/sun/tools/javac/util/Convert.java
author jlahoda
Fri, 20 Jan 2017 15:32:07 +0100
changeset 43272 421ae1e38d2d
parent 25874 83c19f00452c
child 45752 32eb45c44fc8
permissions -rw-r--r--
8173117: Compilation significantly slower after JDK-8169197 Summary: Only using recovery search when an error is inevitable. Reviewed-by: jjg, mcimadamore
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 25874
diff changeset
     2
 * Copyright (c) 1999, 2017, Oracle and/or its affiliates. All rights reserved.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
06bc494ca11e Initial load
duke
parents:
diff changeset
     4
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
06bc494ca11e Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3994
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3994
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    10
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
06bc494ca11e Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
06bc494ca11e Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
06bc494ca11e Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
06bc494ca11e Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
06bc494ca11e Initial load
duke
parents:
diff changeset
    16
 *
06bc494ca11e Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
06bc494ca11e Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
06bc494ca11e Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
06bc494ca11e Initial load
duke
parents:
diff changeset
    20
 *
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3994
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3994
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3994
diff changeset
    23
 * questions.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    24
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    25
06bc494ca11e Initial load
duke
parents:
diff changeset
    26
package com.sun.tools.javac.util;
06bc494ca11e Initial load
duke
parents:
diff changeset
    27
06bc494ca11e Initial load
duke
parents:
diff changeset
    28
/** Utility class for static conversion methods between numbers
06bc494ca11e Initial load
duke
parents:
diff changeset
    29
 *  and strings in various formats.
06bc494ca11e Initial load
duke
parents:
diff changeset
    30
 *
17795
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    31
 *  <p>Note regarding UTF-8.
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    32
 *  The JVMS defines its own version of the UTF-8 format so that it
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    33
 *  contains no zero bytes (modified UTF-8). This is not actually the same
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    34
 *  as Charset.forName("UTF-8").
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    35
 *
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    36
 *  <p>
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    37
 *  See also:
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    38
 *  <ul>
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    39
 *  <li><a href="http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4.7">
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    40
 *    JVMS 4.4.7 </a></li>
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    41
 *  <li><a href="http://docs.oracle.com/javase/7/docs/api/java/io/DataInput.html#modified-utf-8">
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    42
      java.io.DataInput: Modified UTF-8 </a></li>
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    43
    <li><a href="https://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8">
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    44
      Modified UTF-8 (wikipedia) </a></li>
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    45
 *  </ul>
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    46
 *
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    47
 *  The methods here support modified UTF-8.
94ae1f055153 6885876: add comments to javac/util/Convert.java
jjg
parents: 14263
diff changeset
    48
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    49
 *  <p><b>This is NOT part of any supported API.
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    50
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
public class Convert {
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
    /** Convert string to integer.
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    public static int string2int(String s, int radix)
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
        throws NumberFormatException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
        if (radix == 10) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
            return Integer.parseInt(s, radix);
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
            char[] cs = s.toCharArray();
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
            int limit = Integer.MAX_VALUE / (radix/2);
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
            int n = 0;
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17795
diff changeset
    66
            for (char c : cs) {
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17795
diff changeset
    67
                int d = Character.digit(c, radix);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
                if (n < 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
                    n > limit ||
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
                    n * radix > Integer.MAX_VALUE - d)
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
                    throw new NumberFormatException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
                n = n * radix + d;
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
            return n;
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
    /** Convert string to long integer.
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    public static long string2long(String s, int radix)
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
        throws NumberFormatException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
        if (radix == 10) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
            return Long.parseLong(s, radix);
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
            char[] cs = s.toCharArray();
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
            long limit = Long.MAX_VALUE / (radix/2);
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
            long n = 0;
22159
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17795
diff changeset
    88
            for (char c : cs) {
682da512ec17 8030253: Update langtools to use strings-in-switch
briangoetz
parents: 17795
diff changeset
    89
                int d = Character.digit(c, radix);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
                if (n < 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
                    n > limit ||
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
                    n * radix > Long.MAX_VALUE - d)
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
                    throw new NumberFormatException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
                n = n * radix + d;
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
            return n;
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
/* Conversion routines between names, strings, and byte arrays in Utf8 format
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
    /** Convert `len' bytes from utf8 to characters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
     *  Parameters are as in System.arraycopy
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
     *  Return first index in `dst' past the last copied char.
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
     *  @param src        The array holding the bytes to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
     *  @param sindex     The start index from which bytes are converted.
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
     *  @param dst        The array holding the converted characters..
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
     *  @param dindex     The start index from which converted characters
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
     *                    are written.
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
     *  @param len        The maximum number of bytes to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
    public static int utf2chars(byte[] src, int sindex,
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
                                char[] dst, int dindex,
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
                                int len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
        int i = sindex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
        int j = dindex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
        int limit = sindex + len;
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
        while (i < limit) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
            int b = src[i++] & 0xFF;
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
            if (b >= 0xE0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
                b = (b & 0x0F) << 12;
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
                b = b | (src[i++] & 0x3F) << 6;
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
                b = b | (src[i++] & 0x3F);
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
            } else if (b >= 0xC0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
                b = (b & 0x1F) << 6;
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
                b = b | (src[i++] & 0x3F);
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
            dst[j++] = (char)b;
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
        return j;
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
    /** Return bytes in Utf8 representation as an array of characters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
     *  @param src        The array holding the bytes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
     *  @param sindex     The start index from which bytes are converted.
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
     *  @param len        The maximum number of bytes to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
    public static char[] utf2chars(byte[] src, int sindex, int len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
        char[] dst = new char[len];
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
        int len1 = utf2chars(src, sindex, dst, 0, len);
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
        char[] result = new char[len1];
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        System.arraycopy(dst, 0, result, 0, len1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
    /** Return all bytes of a given array in Utf8 representation
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
     *  as an array of characters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     *  @param src        The array holding the bytes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
    public static char[] utf2chars(byte[] src) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
        return utf2chars(src, 0, src.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
    /** Return bytes in Utf8 representation as a string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
     *  @param src        The array holding the bytes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     *  @param sindex     The start index from which bytes are converted.
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     *  @param len        The maximum number of bytes to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
    public static String utf2string(byte[] src, int sindex, int len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
        char dst[] = new char[len];
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
        int len1 = utf2chars(src, sindex, dst, 0, len);
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
        return new String(dst, 0, len1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
    /** Return all bytes of a given array in Utf8 representation
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
     *  as a string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
     *  @param src        The array holding the bytes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
    public static String utf2string(byte[] src) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        return utf2string(src, 0, src.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
    /** Copy characters in source array to bytes in target array,
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
     *  converting them to Utf8 representation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
     *  The target array must be large enough to hold the result.
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
     *  returns first index in `dst' past the last copied byte.
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
     *  @param src        The array holding the characters to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
     *  @param sindex     The start index from which characters are converted.
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
     *  @param dst        The array holding the converted characters..
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
     *  @param dindex     The start index from which converted bytes
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
     *                    are written.
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
     *  @param len        The maximum number of characters to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
    public static int chars2utf(char[] src, int sindex,
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
                                byte[] dst, int dindex,
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
                                int len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
        int j = dindex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
        int limit = sindex + len;
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
        for (int i = sindex; i < limit; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
            char ch = src[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
            if (1 <= ch && ch <= 0x7F) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
                dst[j++] = (byte)ch;
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
            } else if (ch <= 0x7FF) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
                dst[j++] = (byte)(0xC0 | (ch >> 6));
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
                dst[j++] = (byte)(0x80 | (ch & 0x3F));
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
                dst[j++] = (byte)(0xE0 | (ch >> 12));
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
                dst[j++] = (byte)(0x80 | ((ch >> 6) & 0x3F));
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
                dst[j++] = (byte)(0x80 | (ch & 0x3F));
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
        return j;
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
    /** Return characters as an array of bytes in Utf8 representation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
     *  @param src        The array holding the characters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
     *  @param sindex     The start index from which characters are converted.
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
     *  @param len        The maximum number of characters to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
    public static byte[] chars2utf(char[] src, int sindex, int len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        byte[] dst = new byte[len * 3];
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
        int len1 = chars2utf(src, sindex, dst, 0, len);
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
        byte[] result = new byte[len1];
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
        System.arraycopy(dst, 0, result, 0, len1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
    /** Return all characters in given array as an array of bytes
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
     *  in Utf8 representation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
     *  @param src        The array holding the characters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
    public static byte[] chars2utf(char[] src) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        return chars2utf(src, 0, src.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
    /** Return string as an array of bytes in in Utf8 representation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
    public static byte[] string2utf(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
        return chars2utf(s.toCharArray());
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
     * Escapes each character in a string that has an escape sequence or
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
     * is non-printable ASCII.  Leaves non-ASCII characters alone.
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
    public static String quote(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        StringBuilder buf = new StringBuilder();
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        for (int i = 0; i < s.length(); i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
            buf.append(quote(s.charAt(i)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   242
        return buf.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   244
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
     * Escapes a character if it has an escape sequence or is
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
     * non-printable ASCII.  Leaves non-ASCII characters alone.
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
    public static String quote(char ch) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
        switch (ch) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
        case '\b':  return "\\b";
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        case '\f':  return "\\f";
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
        case '\n':  return "\\n";
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
        case '\r':  return "\\r";
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
        case '\t':  return "\\t";
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
        case '\'':  return "\\'";
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
        case '\"':  return "\\\"";
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
        case '\\':  return "\\\\";
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        default:
3994
7df1ecd5eadb 6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents: 1264
diff changeset
   260
            return (isPrintableAscii(ch))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
                ? String.valueOf(ch)
3994
7df1ecd5eadb 6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents: 1264
diff changeset
   262
                : String.format("\\u%04x", (int) ch);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
     * Is a character printable ASCII?
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
    private static boolean isPrintableAscii(char ch) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
        return ch >= ' ' && ch <= '~';
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
    /** Escape all unicode characters in string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
    public static String escapeUnicode(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
        int len = s.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
        while (i < len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            char ch = s.charAt(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
            if (ch > 255) {
14263
473b1eaede64 8000310: Clean up use of StringBuffer in langtools
jjg
parents: 5847
diff changeset
   281
                StringBuilder buf = new StringBuilder();
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
                buf.append(s.substring(0, i));
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
                while (i < len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
                    ch = s.charAt(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
                    if (ch > 255) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
                        buf.append("\\u");
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
                        buf.append(Character.forDigit((ch >> 12) % 16, 16));
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
                        buf.append(Character.forDigit((ch >>  8) % 16, 16));
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
                        buf.append(Character.forDigit((ch >>  4) % 16, 16));
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
                        buf.append(Character.forDigit((ch      ) % 16, 16));
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   292
                        buf.append(ch);
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
                    i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
                s = buf.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
                i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
        return s;
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
/* Conversion routines for qualified name splitting
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
    /** Return the last part of a class name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
    public static Name shortName(Name classname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
        return classname.subName(
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   310
            classname.lastIndexOf((byte)'.') + 1, classname.getByteLength());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
    public static String shortName(String classname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        return classname.substring(classname.lastIndexOf('.') + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
    /** Return the package name of a class name, excluding the trailing '.',
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
     *  "" if not existent.
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
    public static Name packagePart(Name classname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   321
        return classname.subName(0, classname.lastIndexOf((byte)'.'));
06bc494ca11e Initial load
duke
parents:
diff changeset
   322
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   323
06bc494ca11e Initial load
duke
parents:
diff changeset
   324
    public static String packagePart(String classname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   325
        int lastDot = classname.lastIndexOf('.');
06bc494ca11e Initial load
duke
parents:
diff changeset
   326
        return (lastDot < 0 ? "" : classname.substring(0, lastDot));
06bc494ca11e Initial load
duke
parents:
diff changeset
   327
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   328
06bc494ca11e Initial load
duke
parents:
diff changeset
   329
    public static List<Name> enclosingCandidates(Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   330
        List<Name> names = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   331
        int index;
06bc494ca11e Initial load
duke
parents:
diff changeset
   332
        while ((index = name.lastIndexOf((byte)'$')) > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   333
            name = name.subName(0, index);
06bc494ca11e Initial load
duke
parents:
diff changeset
   334
            names = names.prepend(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   335
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   336
        return names;
06bc494ca11e Initial load
duke
parents:
diff changeset
   337
    }
43272
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 25874
diff changeset
   338
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 25874
diff changeset
   339
    public static List<Name> classCandidates(Name name) {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 25874
diff changeset
   340
        List<Name> names = List.nil();
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 25874
diff changeset
   341
        String nameStr = name.toString();
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 25874
diff changeset
   342
        int index = -1;
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 25874
diff changeset
   343
        while ((index = nameStr.indexOf('.', index + 1)) > 0) {
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 25874
diff changeset
   344
            String pack = nameStr.substring(0, index + 1);
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 25874
diff changeset
   345
            String clz = nameStr.substring(index + 1).replace('.', '$');
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 25874
diff changeset
   346
            names = names.prepend(name.table.names.fromString(pack + clz));
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 25874
diff changeset
   347
        }
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 25874
diff changeset
   348
        return names.reverse();
421ae1e38d2d 8173117: Compilation significantly slower after JDK-8169197
jlahoda
parents: 25874
diff changeset
   349
    }
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   350
}