langtools/src/share/classes/com/sun/tools/javac/util/Convert.java
author jjg
Thu, 10 Jun 2010 16:08:01 -0700
changeset 5847 1908176fd6e3
parent 5520 86e4b9a9da40
child 14263 473b1eaede64
permissions -rw-r--r--
6944312: Potential rebranding issues in openjdk/langtools repository sources Reviewed-by: darcy
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
10
06bc494ca11e Initial load
duke
parents:
diff changeset
     1
/*
5520
86e4b9a9da40 6943119: Rebrand source copyright notices
ohair
parents: 3994
diff changeset
     2
 * Copyright (c) 1999, 2008, 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
 *
5847
1908176fd6e3 6944312: Potential rebranding issues in openjdk/langtools repository sources
jjg
parents: 5520
diff changeset
    31
 *  <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
    32
 *  If you write code that depends on this, you do so at your own risk.
10
06bc494ca11e Initial load
duke
parents:
diff changeset
    33
 *  This code and its internal interfaces are subject to change or
06bc494ca11e Initial load
duke
parents:
diff changeset
    34
 *  deletion without notice.</b>
06bc494ca11e Initial load
duke
parents:
diff changeset
    35
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    36
public class Convert {
06bc494ca11e Initial load
duke
parents:
diff changeset
    37
06bc494ca11e Initial load
duke
parents:
diff changeset
    38
    /** Convert string to integer.
06bc494ca11e Initial load
duke
parents:
diff changeset
    39
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    40
    public static int string2int(String s, int radix)
06bc494ca11e Initial load
duke
parents:
diff changeset
    41
        throws NumberFormatException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    42
        if (radix == 10) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    43
            return Integer.parseInt(s, radix);
06bc494ca11e Initial load
duke
parents:
diff changeset
    44
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
    45
            char[] cs = s.toCharArray();
06bc494ca11e Initial load
duke
parents:
diff changeset
    46
            int limit = Integer.MAX_VALUE / (radix/2);
06bc494ca11e Initial load
duke
parents:
diff changeset
    47
            int n = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    48
            for (int i = 0; i < cs.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    49
                int d = Character.digit(cs[i], radix);
06bc494ca11e Initial load
duke
parents:
diff changeset
    50
                if (n < 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
    51
                    n > limit ||
06bc494ca11e Initial load
duke
parents:
diff changeset
    52
                    n * radix > Integer.MAX_VALUE - d)
06bc494ca11e Initial load
duke
parents:
diff changeset
    53
                    throw new NumberFormatException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    54
                n = n * radix + d;
06bc494ca11e Initial load
duke
parents:
diff changeset
    55
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    56
            return n;
06bc494ca11e Initial load
duke
parents:
diff changeset
    57
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    58
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    59
06bc494ca11e Initial load
duke
parents:
diff changeset
    60
    /** Convert string to long integer.
06bc494ca11e Initial load
duke
parents:
diff changeset
    61
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    62
    public static long string2long(String s, int radix)
06bc494ca11e Initial load
duke
parents:
diff changeset
    63
        throws NumberFormatException {
06bc494ca11e Initial load
duke
parents:
diff changeset
    64
        if (radix == 10) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    65
            return Long.parseLong(s, radix);
06bc494ca11e Initial load
duke
parents:
diff changeset
    66
        } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
    67
            char[] cs = s.toCharArray();
06bc494ca11e Initial load
duke
parents:
diff changeset
    68
            long limit = Long.MAX_VALUE / (radix/2);
06bc494ca11e Initial load
duke
parents:
diff changeset
    69
            long n = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
    70
            for (int i = 0; i < cs.length; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    71
                int d = Character.digit(cs[i], radix);
06bc494ca11e Initial load
duke
parents:
diff changeset
    72
                if (n < 0 ||
06bc494ca11e Initial load
duke
parents:
diff changeset
    73
                    n > limit ||
06bc494ca11e Initial load
duke
parents:
diff changeset
    74
                    n * radix > Long.MAX_VALUE - d)
06bc494ca11e Initial load
duke
parents:
diff changeset
    75
                    throw new NumberFormatException();
06bc494ca11e Initial load
duke
parents:
diff changeset
    76
                n = n * radix + d;
06bc494ca11e Initial load
duke
parents:
diff changeset
    77
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
    78
            return n;
06bc494ca11e Initial load
duke
parents:
diff changeset
    79
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
    80
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
    81
06bc494ca11e Initial load
duke
parents:
diff changeset
    82
/* Conversion routines between names, strings, and byte arrays in Utf8 format
06bc494ca11e Initial load
duke
parents:
diff changeset
    83
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
    84
06bc494ca11e Initial load
duke
parents:
diff changeset
    85
    /** Convert `len' bytes from utf8 to characters.
06bc494ca11e Initial load
duke
parents:
diff changeset
    86
     *  Parameters are as in System.arraycopy
06bc494ca11e Initial load
duke
parents:
diff changeset
    87
     *  Return first index in `dst' past the last copied char.
06bc494ca11e Initial load
duke
parents:
diff changeset
    88
     *  @param src        The array holding the bytes to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
    89
     *  @param sindex     The start index from which bytes are converted.
06bc494ca11e Initial load
duke
parents:
diff changeset
    90
     *  @param dst        The array holding the converted characters..
06bc494ca11e Initial load
duke
parents:
diff changeset
    91
     *  @param dindex     The start index from which converted characters
06bc494ca11e Initial load
duke
parents:
diff changeset
    92
     *                    are written.
06bc494ca11e Initial load
duke
parents:
diff changeset
    93
     *  @param len        The maximum number of bytes to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
    94
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
    95
    public static int utf2chars(byte[] src, int sindex,
06bc494ca11e Initial load
duke
parents:
diff changeset
    96
                                char[] dst, int dindex,
06bc494ca11e Initial load
duke
parents:
diff changeset
    97
                                int len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
    98
        int i = sindex;
06bc494ca11e Initial load
duke
parents:
diff changeset
    99
        int j = dindex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   100
        int limit = sindex + len;
06bc494ca11e Initial load
duke
parents:
diff changeset
   101
        while (i < limit) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   102
            int b = src[i++] & 0xFF;
06bc494ca11e Initial load
duke
parents:
diff changeset
   103
            if (b >= 0xE0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   104
                b = (b & 0x0F) << 12;
06bc494ca11e Initial load
duke
parents:
diff changeset
   105
                b = b | (src[i++] & 0x3F) << 6;
06bc494ca11e Initial load
duke
parents:
diff changeset
   106
                b = b | (src[i++] & 0x3F);
06bc494ca11e Initial load
duke
parents:
diff changeset
   107
            } else if (b >= 0xC0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   108
                b = (b & 0x1F) << 6;
06bc494ca11e Initial load
duke
parents:
diff changeset
   109
                b = b | (src[i++] & 0x3F);
06bc494ca11e Initial load
duke
parents:
diff changeset
   110
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   111
            dst[j++] = (char)b;
06bc494ca11e Initial load
duke
parents:
diff changeset
   112
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   113
        return j;
06bc494ca11e Initial load
duke
parents:
diff changeset
   114
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   115
06bc494ca11e Initial load
duke
parents:
diff changeset
   116
    /** Return bytes in Utf8 representation as an array of characters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   117
     *  @param src        The array holding the bytes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   118
     *  @param sindex     The start index from which bytes are converted.
06bc494ca11e Initial load
duke
parents:
diff changeset
   119
     *  @param len        The maximum number of bytes to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   120
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   121
    public static char[] utf2chars(byte[] src, int sindex, int len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   122
        char[] dst = new char[len];
06bc494ca11e Initial load
duke
parents:
diff changeset
   123
        int len1 = utf2chars(src, sindex, dst, 0, len);
06bc494ca11e Initial load
duke
parents:
diff changeset
   124
        char[] result = new char[len1];
06bc494ca11e Initial load
duke
parents:
diff changeset
   125
        System.arraycopy(dst, 0, result, 0, len1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   126
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   127
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   128
06bc494ca11e Initial load
duke
parents:
diff changeset
   129
    /** Return all bytes of a given array in Utf8 representation
06bc494ca11e Initial load
duke
parents:
diff changeset
   130
     *  as an array of characters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   131
     *  @param src        The array holding the bytes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   132
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   133
    public static char[] utf2chars(byte[] src) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   134
        return utf2chars(src, 0, src.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
   135
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   136
06bc494ca11e Initial load
duke
parents:
diff changeset
   137
    /** Return bytes in Utf8 representation as a string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   138
     *  @param src        The array holding the bytes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   139
     *  @param sindex     The start index from which bytes are converted.
06bc494ca11e Initial load
duke
parents:
diff changeset
   140
     *  @param len        The maximum number of bytes to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   141
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   142
    public static String utf2string(byte[] src, int sindex, int len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   143
        char dst[] = new char[len];
06bc494ca11e Initial load
duke
parents:
diff changeset
   144
        int len1 = utf2chars(src, sindex, dst, 0, len);
06bc494ca11e Initial load
duke
parents:
diff changeset
   145
        return new String(dst, 0, len1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   146
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   147
06bc494ca11e Initial load
duke
parents:
diff changeset
   148
    /** Return all bytes of a given array in Utf8 representation
06bc494ca11e Initial load
duke
parents:
diff changeset
   149
     *  as a string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   150
     *  @param src        The array holding the bytes.
06bc494ca11e Initial load
duke
parents:
diff changeset
   151
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   152
    public static String utf2string(byte[] src) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   153
        return utf2string(src, 0, src.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
   154
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   155
06bc494ca11e Initial load
duke
parents:
diff changeset
   156
    /** Copy characters in source array to bytes in target array,
06bc494ca11e Initial load
duke
parents:
diff changeset
   157
     *  converting them to Utf8 representation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   158
     *  The target array must be large enough to hold the result.
06bc494ca11e Initial load
duke
parents:
diff changeset
   159
     *  returns first index in `dst' past the last copied byte.
06bc494ca11e Initial load
duke
parents:
diff changeset
   160
     *  @param src        The array holding the characters to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   161
     *  @param sindex     The start index from which characters are converted.
06bc494ca11e Initial load
duke
parents:
diff changeset
   162
     *  @param dst        The array holding the converted characters..
06bc494ca11e Initial load
duke
parents:
diff changeset
   163
     *  @param dindex     The start index from which converted bytes
06bc494ca11e Initial load
duke
parents:
diff changeset
   164
     *                    are written.
06bc494ca11e Initial load
duke
parents:
diff changeset
   165
     *  @param len        The maximum number of characters to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   166
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   167
    public static int chars2utf(char[] src, int sindex,
06bc494ca11e Initial load
duke
parents:
diff changeset
   168
                                byte[] dst, int dindex,
06bc494ca11e Initial load
duke
parents:
diff changeset
   169
                                int len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   170
        int j = dindex;
06bc494ca11e Initial load
duke
parents:
diff changeset
   171
        int limit = sindex + len;
06bc494ca11e Initial load
duke
parents:
diff changeset
   172
        for (int i = sindex; i < limit; i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   173
            char ch = src[i];
06bc494ca11e Initial load
duke
parents:
diff changeset
   174
            if (1 <= ch && ch <= 0x7F) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   175
                dst[j++] = (byte)ch;
06bc494ca11e Initial load
duke
parents:
diff changeset
   176
            } else if (ch <= 0x7FF) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   177
                dst[j++] = (byte)(0xC0 | (ch >> 6));
06bc494ca11e Initial load
duke
parents:
diff changeset
   178
                dst[j++] = (byte)(0x80 | (ch & 0x3F));
06bc494ca11e Initial load
duke
parents:
diff changeset
   179
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   180
                dst[j++] = (byte)(0xE0 | (ch >> 12));
06bc494ca11e Initial load
duke
parents:
diff changeset
   181
                dst[j++] = (byte)(0x80 | ((ch >> 6) & 0x3F));
06bc494ca11e Initial load
duke
parents:
diff changeset
   182
                dst[j++] = (byte)(0x80 | (ch & 0x3F));
06bc494ca11e Initial load
duke
parents:
diff changeset
   183
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   184
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   185
        return j;
06bc494ca11e Initial load
duke
parents:
diff changeset
   186
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   187
06bc494ca11e Initial load
duke
parents:
diff changeset
   188
    /** Return characters as an array of bytes in Utf8 representation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   189
     *  @param src        The array holding the characters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   190
     *  @param sindex     The start index from which characters are converted.
06bc494ca11e Initial load
duke
parents:
diff changeset
   191
     *  @param len        The maximum number of characters to convert.
06bc494ca11e Initial load
duke
parents:
diff changeset
   192
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   193
    public static byte[] chars2utf(char[] src, int sindex, int len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   194
        byte[] dst = new byte[len * 3];
06bc494ca11e Initial load
duke
parents:
diff changeset
   195
        int len1 = chars2utf(src, sindex, dst, 0, len);
06bc494ca11e Initial load
duke
parents:
diff changeset
   196
        byte[] result = new byte[len1];
06bc494ca11e Initial load
duke
parents:
diff changeset
   197
        System.arraycopy(dst, 0, result, 0, len1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   198
        return result;
06bc494ca11e Initial load
duke
parents:
diff changeset
   199
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   200
06bc494ca11e Initial load
duke
parents:
diff changeset
   201
    /** Return all characters in given array as an array of bytes
06bc494ca11e Initial load
duke
parents:
diff changeset
   202
     *  in Utf8 representation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   203
     *  @param src        The array holding the characters.
06bc494ca11e Initial load
duke
parents:
diff changeset
   204
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   205
    public static byte[] chars2utf(char[] src) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   206
        return chars2utf(src, 0, src.length);
06bc494ca11e Initial load
duke
parents:
diff changeset
   207
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   208
06bc494ca11e Initial load
duke
parents:
diff changeset
   209
    /** Return string as an array of bytes in in Utf8 representation.
06bc494ca11e Initial load
duke
parents:
diff changeset
   210
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   211
    public static byte[] string2utf(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   212
        return chars2utf(s.toCharArray());
06bc494ca11e Initial load
duke
parents:
diff changeset
   213
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   214
06bc494ca11e Initial load
duke
parents:
diff changeset
   215
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   216
     * Escapes each character in a string that has an escape sequence or
06bc494ca11e Initial load
duke
parents:
diff changeset
   217
     * is non-printable ASCII.  Leaves non-ASCII characters alone.
06bc494ca11e Initial load
duke
parents:
diff changeset
   218
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   219
    public static String quote(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   220
        StringBuilder buf = new StringBuilder();
06bc494ca11e Initial load
duke
parents:
diff changeset
   221
        for (int i = 0; i < s.length(); i++) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   222
            buf.append(quote(s.charAt(i)));
06bc494ca11e Initial load
duke
parents:
diff changeset
   223
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   224
        return buf.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   225
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   226
06bc494ca11e Initial load
duke
parents:
diff changeset
   227
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   228
     * Escapes a character if it has an escape sequence or is
06bc494ca11e Initial load
duke
parents:
diff changeset
   229
     * non-printable ASCII.  Leaves non-ASCII characters alone.
06bc494ca11e Initial load
duke
parents:
diff changeset
   230
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   231
    public static String quote(char ch) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   232
        switch (ch) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   233
        case '\b':  return "\\b";
06bc494ca11e Initial load
duke
parents:
diff changeset
   234
        case '\f':  return "\\f";
06bc494ca11e Initial load
duke
parents:
diff changeset
   235
        case '\n':  return "\\n";
06bc494ca11e Initial load
duke
parents:
diff changeset
   236
        case '\r':  return "\\r";
06bc494ca11e Initial load
duke
parents:
diff changeset
   237
        case '\t':  return "\\t";
06bc494ca11e Initial load
duke
parents:
diff changeset
   238
        case '\'':  return "\\'";
06bc494ca11e Initial load
duke
parents:
diff changeset
   239
        case '\"':  return "\\\"";
06bc494ca11e Initial load
duke
parents:
diff changeset
   240
        case '\\':  return "\\\\";
06bc494ca11e Initial load
duke
parents:
diff changeset
   241
        default:
3994
7df1ecd5eadb 6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents: 1264
diff changeset
   242
            return (isPrintableAscii(ch))
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   243
                ? String.valueOf(ch)
3994
7df1ecd5eadb 6517779: javax.lang.model.util.Elements.getConstantExpression() doesn't throw any exception
darcy
parents: 1264
diff changeset
   244
                : String.format("\\u%04x", (int) ch);
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   245
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   246
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   247
06bc494ca11e Initial load
duke
parents:
diff changeset
   248
    /**
06bc494ca11e Initial load
duke
parents:
diff changeset
   249
     * Is a character printable ASCII?
06bc494ca11e Initial load
duke
parents:
diff changeset
   250
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   251
    private static boolean isPrintableAscii(char ch) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   252
        return ch >= ' ' && ch <= '~';
06bc494ca11e Initial load
duke
parents:
diff changeset
   253
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   254
06bc494ca11e Initial load
duke
parents:
diff changeset
   255
    /** Escape all unicode characters in string.
06bc494ca11e Initial load
duke
parents:
diff changeset
   256
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   257
    public static String escapeUnicode(String s) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   258
        int len = s.length();
06bc494ca11e Initial load
duke
parents:
diff changeset
   259
        int i = 0;
06bc494ca11e Initial load
duke
parents:
diff changeset
   260
        while (i < len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   261
            char ch = s.charAt(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
   262
            if (ch > 255) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   263
                StringBuffer buf = new StringBuffer();
06bc494ca11e Initial load
duke
parents:
diff changeset
   264
                buf.append(s.substring(0, i));
06bc494ca11e Initial load
duke
parents:
diff changeset
   265
                while (i < len) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   266
                    ch = s.charAt(i);
06bc494ca11e Initial load
duke
parents:
diff changeset
   267
                    if (ch > 255) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   268
                        buf.append("\\u");
06bc494ca11e Initial load
duke
parents:
diff changeset
   269
                        buf.append(Character.forDigit((ch >> 12) % 16, 16));
06bc494ca11e Initial load
duke
parents:
diff changeset
   270
                        buf.append(Character.forDigit((ch >>  8) % 16, 16));
06bc494ca11e Initial load
duke
parents:
diff changeset
   271
                        buf.append(Character.forDigit((ch >>  4) % 16, 16));
06bc494ca11e Initial load
duke
parents:
diff changeset
   272
                        buf.append(Character.forDigit((ch      ) % 16, 16));
06bc494ca11e Initial load
duke
parents:
diff changeset
   273
                    } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   274
                        buf.append(ch);
06bc494ca11e Initial load
duke
parents:
diff changeset
   275
                    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   276
                    i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   277
                }
06bc494ca11e Initial load
duke
parents:
diff changeset
   278
                s = buf.toString();
06bc494ca11e Initial load
duke
parents:
diff changeset
   279
            } else {
06bc494ca11e Initial load
duke
parents:
diff changeset
   280
                i++;
06bc494ca11e Initial load
duke
parents:
diff changeset
   281
            }
06bc494ca11e Initial load
duke
parents:
diff changeset
   282
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   283
        return s;
06bc494ca11e Initial load
duke
parents:
diff changeset
   284
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   285
06bc494ca11e Initial load
duke
parents:
diff changeset
   286
/* Conversion routines for qualified name splitting
06bc494ca11e Initial load
duke
parents:
diff changeset
   287
 */
06bc494ca11e Initial load
duke
parents:
diff changeset
   288
    /** Return the last part of a class name.
06bc494ca11e Initial load
duke
parents:
diff changeset
   289
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   290
    public static Name shortName(Name classname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   291
        return classname.subName(
1260
a772ba9ba43d 6574134: Allow for alternative implementation of Name Table with garbage collection of name bytes
jjg
parents: 10
diff changeset
   292
            classname.lastIndexOf((byte)'.') + 1, classname.getByteLength());
10
06bc494ca11e Initial load
duke
parents:
diff changeset
   293
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   294
06bc494ca11e Initial load
duke
parents:
diff changeset
   295
    public static String shortName(String classname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   296
        return classname.substring(classname.lastIndexOf('.') + 1);
06bc494ca11e Initial load
duke
parents:
diff changeset
   297
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   298
06bc494ca11e Initial load
duke
parents:
diff changeset
   299
    /** Return the package name of a class name, excluding the trailing '.',
06bc494ca11e Initial load
duke
parents:
diff changeset
   300
     *  "" if not existent.
06bc494ca11e Initial load
duke
parents:
diff changeset
   301
     */
06bc494ca11e Initial load
duke
parents:
diff changeset
   302
    public static Name packagePart(Name classname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   303
        return classname.subName(0, classname.lastIndexOf((byte)'.'));
06bc494ca11e Initial load
duke
parents:
diff changeset
   304
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   305
06bc494ca11e Initial load
duke
parents:
diff changeset
   306
    public static String packagePart(String classname) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   307
        int lastDot = classname.lastIndexOf('.');
06bc494ca11e Initial load
duke
parents:
diff changeset
   308
        return (lastDot < 0 ? "" : classname.substring(0, lastDot));
06bc494ca11e Initial load
duke
parents:
diff changeset
   309
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   310
06bc494ca11e Initial load
duke
parents:
diff changeset
   311
    public static List<Name> enclosingCandidates(Name name) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   312
        List<Name> names = List.nil();
06bc494ca11e Initial load
duke
parents:
diff changeset
   313
        int index;
06bc494ca11e Initial load
duke
parents:
diff changeset
   314
        while ((index = name.lastIndexOf((byte)'$')) > 0) {
06bc494ca11e Initial load
duke
parents:
diff changeset
   315
            name = name.subName(0, index);
06bc494ca11e Initial load
duke
parents:
diff changeset
   316
            names = names.prepend(name);
06bc494ca11e Initial load
duke
parents:
diff changeset
   317
        }
06bc494ca11e Initial load
duke
parents:
diff changeset
   318
        return names;
06bc494ca11e Initial load
duke
parents:
diff changeset
   319
    }
06bc494ca11e Initial load
duke
parents:
diff changeset
   320
}