0N/A/*
553N/A * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.javac.util;
0N/A
0N/A/** Utility class for static conversion methods between numbers
0N/A * and strings in various formats.
0N/A *
580N/A * <p><b>This is NOT part of any supported API.
580N/A * If you write code that depends on this, you do so at your own risk.
0N/A * This code and its internal interfaces are subject to change or
0N/A * deletion without notice.</b>
0N/A */
0N/Apublic class Convert {
0N/A
0N/A /** Convert string to integer.
0N/A */
0N/A public static int string2int(String s, int radix)
0N/A throws NumberFormatException {
0N/A if (radix == 10) {
0N/A return Integer.parseInt(s, radix);
0N/A } else {
0N/A char[] cs = s.toCharArray();
0N/A int limit = Integer.MAX_VALUE / (radix/2);
0N/A int n = 0;
0N/A for (int i = 0; i < cs.length; i++) {
0N/A int d = Character.digit(cs[i], radix);
0N/A if (n < 0 ||
0N/A n > limit ||
0N/A n * radix > Integer.MAX_VALUE - d)
0N/A throw new NumberFormatException();
0N/A n = n * radix + d;
0N/A }
0N/A return n;
0N/A }
0N/A }
0N/A
0N/A /** Convert string to long integer.
0N/A */
0N/A public static long string2long(String s, int radix)
0N/A throws NumberFormatException {
0N/A if (radix == 10) {
0N/A return Long.parseLong(s, radix);
0N/A } else {
0N/A char[] cs = s.toCharArray();
0N/A long limit = Long.MAX_VALUE / (radix/2);
0N/A long n = 0;
0N/A for (int i = 0; i < cs.length; i++) {
0N/A int d = Character.digit(cs[i], radix);
0N/A if (n < 0 ||
0N/A n > limit ||
0N/A n * radix > Long.MAX_VALUE - d)
0N/A throw new NumberFormatException();
0N/A n = n * radix + d;
0N/A }
0N/A return n;
0N/A }
0N/A }
0N/A
0N/A/* Conversion routines between names, strings, and byte arrays in Utf8 format
0N/A */
0N/A
0N/A /** Convert `len' bytes from utf8 to characters.
0N/A * Parameters are as in System.arraycopy
0N/A * Return first index in `dst' past the last copied char.
0N/A * @param src The array holding the bytes to convert.
0N/A * @param sindex The start index from which bytes are converted.
0N/A * @param dst The array holding the converted characters..
0N/A * @param dindex The start index from which converted characters
0N/A * are written.
0N/A * @param len The maximum number of bytes to convert.
0N/A */
0N/A public static int utf2chars(byte[] src, int sindex,
0N/A char[] dst, int dindex,
0N/A int len) {
0N/A int i = sindex;
0N/A int j = dindex;
0N/A int limit = sindex + len;
0N/A while (i < limit) {
0N/A int b = src[i++] & 0xFF;
0N/A if (b >= 0xE0) {
0N/A b = (b & 0x0F) << 12;
0N/A b = b | (src[i++] & 0x3F) << 6;
0N/A b = b | (src[i++] & 0x3F);
0N/A } else if (b >= 0xC0) {
0N/A b = (b & 0x1F) << 6;
0N/A b = b | (src[i++] & 0x3F);
0N/A }
0N/A dst[j++] = (char)b;
0N/A }
0N/A return j;
0N/A }
0N/A
0N/A /** Return bytes in Utf8 representation as an array of characters.
0N/A * @param src The array holding the bytes.
0N/A * @param sindex The start index from which bytes are converted.
0N/A * @param len The maximum number of bytes to convert.
0N/A */
0N/A public static char[] utf2chars(byte[] src, int sindex, int len) {
0N/A char[] dst = new char[len];
0N/A int len1 = utf2chars(src, sindex, dst, 0, len);
0N/A char[] result = new char[len1];
0N/A System.arraycopy(dst, 0, result, 0, len1);
0N/A return result;
0N/A }
0N/A
0N/A /** Return all bytes of a given array in Utf8 representation
0N/A * as an array of characters.
0N/A * @param src The array holding the bytes.
0N/A */
0N/A public static char[] utf2chars(byte[] src) {
0N/A return utf2chars(src, 0, src.length);
0N/A }
0N/A
0N/A /** Return bytes in Utf8 representation as a string.
0N/A * @param src The array holding the bytes.
0N/A * @param sindex The start index from which bytes are converted.
0N/A * @param len The maximum number of bytes to convert.
0N/A */
0N/A public static String utf2string(byte[] src, int sindex, int len) {
0N/A char dst[] = new char[len];
0N/A int len1 = utf2chars(src, sindex, dst, 0, len);
0N/A return new String(dst, 0, len1);
0N/A }
0N/A
0N/A /** Return all bytes of a given array in Utf8 representation
0N/A * as a string.
0N/A * @param src The array holding the bytes.
0N/A */
0N/A public static String utf2string(byte[] src) {
0N/A return utf2string(src, 0, src.length);
0N/A }
0N/A
0N/A /** Copy characters in source array to bytes in target array,
0N/A * converting them to Utf8 representation.
0N/A * The target array must be large enough to hold the result.
0N/A * returns first index in `dst' past the last copied byte.
0N/A * @param src The array holding the characters to convert.
0N/A * @param sindex The start index from which characters are converted.
0N/A * @param dst The array holding the converted characters..
0N/A * @param dindex The start index from which converted bytes
0N/A * are written.
0N/A * @param len The maximum number of characters to convert.
0N/A */
0N/A public static int chars2utf(char[] src, int sindex,
0N/A byte[] dst, int dindex,
0N/A int len) {
0N/A int j = dindex;
0N/A int limit = sindex + len;
0N/A for (int i = sindex; i < limit; i++) {
0N/A char ch = src[i];
0N/A if (1 <= ch && ch <= 0x7F) {
0N/A dst[j++] = (byte)ch;
0N/A } else if (ch <= 0x7FF) {
0N/A dst[j++] = (byte)(0xC0 | (ch >> 6));
0N/A dst[j++] = (byte)(0x80 | (ch & 0x3F));
0N/A } else {
0N/A dst[j++] = (byte)(0xE0 | (ch >> 12));
0N/A dst[j++] = (byte)(0x80 | ((ch >> 6) & 0x3F));
0N/A dst[j++] = (byte)(0x80 | (ch & 0x3F));
0N/A }
0N/A }
0N/A return j;
0N/A }
0N/A
0N/A /** Return characters as an array of bytes in Utf8 representation.
0N/A * @param src The array holding the characters.
0N/A * @param sindex The start index from which characters are converted.
0N/A * @param len The maximum number of characters to convert.
0N/A */
0N/A public static byte[] chars2utf(char[] src, int sindex, int len) {
0N/A byte[] dst = new byte[len * 3];
0N/A int len1 = chars2utf(src, sindex, dst, 0, len);
0N/A byte[] result = new byte[len1];
0N/A System.arraycopy(dst, 0, result, 0, len1);
0N/A return result;
0N/A }
0N/A
0N/A /** Return all characters in given array as an array of bytes
0N/A * in Utf8 representation.
0N/A * @param src The array holding the characters.
0N/A */
0N/A public static byte[] chars2utf(char[] src) {
0N/A return chars2utf(src, 0, src.length);
0N/A }
0N/A
0N/A /** Return string as an array of bytes in in Utf8 representation.
0N/A */
0N/A public static byte[] string2utf(String s) {
0N/A return chars2utf(s.toCharArray());
0N/A }
0N/A
0N/A /**
0N/A * Escapes each character in a string that has an escape sequence or
0N/A * is non-printable ASCII. Leaves non-ASCII characters alone.
0N/A */
0N/A public static String quote(String s) {
0N/A StringBuilder buf = new StringBuilder();
0N/A for (int i = 0; i < s.length(); i++) {
0N/A buf.append(quote(s.charAt(i)));
0N/A }
0N/A return buf.toString();
0N/A }
0N/A
0N/A /**
0N/A * Escapes a character if it has an escape sequence or is
0N/A * non-printable ASCII. Leaves non-ASCII characters alone.
0N/A */
0N/A public static String quote(char ch) {
0N/A switch (ch) {
0N/A case '\b': return "\\b";
0N/A case '\f': return "\\f";
0N/A case '\n': return "\\n";
0N/A case '\r': return "\\r";
0N/A case '\t': return "\\t";
0N/A case '\'': return "\\'";
0N/A case '\"': return "\\\"";
0N/A case '\\': return "\\\\";
0N/A default:
413N/A return (isPrintableAscii(ch))
0N/A ? String.valueOf(ch)
413N/A : String.format("\\u%04x", (int) ch);
0N/A }
0N/A }
0N/A
0N/A /**
0N/A * Is a character printable ASCII?
0N/A */
0N/A private static boolean isPrintableAscii(char ch) {
0N/A return ch >= ' ' && ch <= '~';
0N/A }
0N/A
0N/A /** Escape all unicode characters in string.
0N/A */
0N/A public static String escapeUnicode(String s) {
0N/A int len = s.length();
0N/A int i = 0;
0N/A while (i < len) {
0N/A char ch = s.charAt(i);
0N/A if (ch > 255) {
0N/A StringBuffer buf = new StringBuffer();
0N/A buf.append(s.substring(0, i));
0N/A while (i < len) {
0N/A ch = s.charAt(i);
0N/A if (ch > 255) {
0N/A buf.append("\\u");
0N/A buf.append(Character.forDigit((ch >> 12) % 16, 16));
0N/A buf.append(Character.forDigit((ch >> 8) % 16, 16));
0N/A buf.append(Character.forDigit((ch >> 4) % 16, 16));
0N/A buf.append(Character.forDigit((ch ) % 16, 16));
0N/A } else {
0N/A buf.append(ch);
0N/A }
0N/A i++;
0N/A }
0N/A s = buf.toString();
0N/A } else {
0N/A i++;
0N/A }
0N/A }
0N/A return s;
0N/A }
0N/A
0N/A/* Conversion routines for qualified name splitting
0N/A */
0N/A /** Return the last part of a class name.
0N/A */
0N/A public static Name shortName(Name classname) {
0N/A return classname.subName(
112N/A classname.lastIndexOf((byte)'.') + 1, classname.getByteLength());
0N/A }
0N/A
0N/A public static String shortName(String classname) {
0N/A return classname.substring(classname.lastIndexOf('.') + 1);
0N/A }
0N/A
0N/A /** Return the package name of a class name, excluding the trailing '.',
0N/A * "" if not existent.
0N/A */
0N/A public static Name packagePart(Name classname) {
0N/A return classname.subName(0, classname.lastIndexOf((byte)'.'));
0N/A }
0N/A
0N/A public static String packagePart(String classname) {
0N/A int lastDot = classname.lastIndexOf('.');
0N/A return (lastDot < 0 ? "" : classname.substring(0, lastDot));
0N/A }
0N/A
0N/A public static List<Name> enclosingCandidates(Name name) {
0N/A List<Name> names = List.nil();
0N/A int index;
0N/A while ((index = name.lastIndexOf((byte)'$')) > 0) {
0N/A name = name.subName(0, index);
0N/A names = names.prepend(name);
0N/A }
0N/A return names;
0N/A }
0N/A}