duke@1: /* ohair@554: * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.util; duke@1: duke@1: /** Utility class for static conversion methods between numbers duke@1: * and strings in various formats. duke@1: * jjg@581: *

This is NOT part of any supported API. jjg@581: * If you write code that depends on this, you do so at your own risk. duke@1: * This code and its internal interfaces are subject to change or duke@1: * deletion without notice. duke@1: */ duke@1: public class Convert { duke@1: duke@1: /** Convert string to integer. duke@1: */ duke@1: public static int string2int(String s, int radix) duke@1: throws NumberFormatException { duke@1: if (radix == 10) { duke@1: return Integer.parseInt(s, radix); duke@1: } else { duke@1: char[] cs = s.toCharArray(); duke@1: int limit = Integer.MAX_VALUE / (radix/2); duke@1: int n = 0; duke@1: for (int i = 0; i < cs.length; i++) { duke@1: int d = Character.digit(cs[i], radix); duke@1: if (n < 0 || duke@1: n > limit || duke@1: n * radix > Integer.MAX_VALUE - d) duke@1: throw new NumberFormatException(); duke@1: n = n * radix + d; duke@1: } duke@1: return n; duke@1: } duke@1: } duke@1: duke@1: /** Convert string to long integer. duke@1: */ duke@1: public static long string2long(String s, int radix) duke@1: throws NumberFormatException { duke@1: if (radix == 10) { duke@1: return Long.parseLong(s, radix); duke@1: } else { duke@1: char[] cs = s.toCharArray(); duke@1: long limit = Long.MAX_VALUE / (radix/2); duke@1: long n = 0; duke@1: for (int i = 0; i < cs.length; i++) { duke@1: int d = Character.digit(cs[i], radix); duke@1: if (n < 0 || duke@1: n > limit || duke@1: n * radix > Long.MAX_VALUE - d) duke@1: throw new NumberFormatException(); duke@1: n = n * radix + d; duke@1: } duke@1: return n; duke@1: } duke@1: } duke@1: duke@1: /* Conversion routines between names, strings, and byte arrays in Utf8 format duke@1: */ duke@1: duke@1: /** Convert `len' bytes from utf8 to characters. duke@1: * Parameters are as in System.arraycopy duke@1: * Return first index in `dst' past the last copied char. duke@1: * @param src The array holding the bytes to convert. duke@1: * @param sindex The start index from which bytes are converted. duke@1: * @param dst The array holding the converted characters.. duke@1: * @param dindex The start index from which converted characters duke@1: * are written. duke@1: * @param len The maximum number of bytes to convert. duke@1: */ duke@1: public static int utf2chars(byte[] src, int sindex, duke@1: char[] dst, int dindex, duke@1: int len) { duke@1: int i = sindex; duke@1: int j = dindex; duke@1: int limit = sindex + len; duke@1: while (i < limit) { duke@1: int b = src[i++] & 0xFF; duke@1: if (b >= 0xE0) { duke@1: b = (b & 0x0F) << 12; duke@1: b = b | (src[i++] & 0x3F) << 6; duke@1: b = b | (src[i++] & 0x3F); duke@1: } else if (b >= 0xC0) { duke@1: b = (b & 0x1F) << 6; duke@1: b = b | (src[i++] & 0x3F); duke@1: } duke@1: dst[j++] = (char)b; duke@1: } duke@1: return j; duke@1: } duke@1: duke@1: /** Return bytes in Utf8 representation as an array of characters. duke@1: * @param src The array holding the bytes. duke@1: * @param sindex The start index from which bytes are converted. duke@1: * @param len The maximum number of bytes to convert. duke@1: */ duke@1: public static char[] utf2chars(byte[] src, int sindex, int len) { duke@1: char[] dst = new char[len]; duke@1: int len1 = utf2chars(src, sindex, dst, 0, len); duke@1: char[] result = new char[len1]; duke@1: System.arraycopy(dst, 0, result, 0, len1); duke@1: return result; duke@1: } duke@1: duke@1: /** Return all bytes of a given array in Utf8 representation duke@1: * as an array of characters. duke@1: * @param src The array holding the bytes. duke@1: */ duke@1: public static char[] utf2chars(byte[] src) { duke@1: return utf2chars(src, 0, src.length); duke@1: } duke@1: duke@1: /** Return bytes in Utf8 representation as a string. duke@1: * @param src The array holding the bytes. duke@1: * @param sindex The start index from which bytes are converted. duke@1: * @param len The maximum number of bytes to convert. duke@1: */ duke@1: public static String utf2string(byte[] src, int sindex, int len) { duke@1: char dst[] = new char[len]; duke@1: int len1 = utf2chars(src, sindex, dst, 0, len); duke@1: return new String(dst, 0, len1); duke@1: } duke@1: duke@1: /** Return all bytes of a given array in Utf8 representation duke@1: * as a string. duke@1: * @param src The array holding the bytes. duke@1: */ duke@1: public static String utf2string(byte[] src) { duke@1: return utf2string(src, 0, src.length); duke@1: } duke@1: duke@1: /** Copy characters in source array to bytes in target array, duke@1: * converting them to Utf8 representation. duke@1: * The target array must be large enough to hold the result. duke@1: * returns first index in `dst' past the last copied byte. duke@1: * @param src The array holding the characters to convert. duke@1: * @param sindex The start index from which characters are converted. duke@1: * @param dst The array holding the converted characters.. duke@1: * @param dindex The start index from which converted bytes duke@1: * are written. duke@1: * @param len The maximum number of characters to convert. duke@1: */ duke@1: public static int chars2utf(char[] src, int sindex, duke@1: byte[] dst, int dindex, duke@1: int len) { duke@1: int j = dindex; duke@1: int limit = sindex + len; duke@1: for (int i = sindex; i < limit; i++) { duke@1: char ch = src[i]; duke@1: if (1 <= ch && ch <= 0x7F) { duke@1: dst[j++] = (byte)ch; duke@1: } else if (ch <= 0x7FF) { duke@1: dst[j++] = (byte)(0xC0 | (ch >> 6)); duke@1: dst[j++] = (byte)(0x80 | (ch & 0x3F)); duke@1: } else { duke@1: dst[j++] = (byte)(0xE0 | (ch >> 12)); duke@1: dst[j++] = (byte)(0x80 | ((ch >> 6) & 0x3F)); duke@1: dst[j++] = (byte)(0x80 | (ch & 0x3F)); duke@1: } duke@1: } duke@1: return j; duke@1: } duke@1: duke@1: /** Return characters as an array of bytes in Utf8 representation. duke@1: * @param src The array holding the characters. duke@1: * @param sindex The start index from which characters are converted. duke@1: * @param len The maximum number of characters to convert. duke@1: */ duke@1: public static byte[] chars2utf(char[] src, int sindex, int len) { duke@1: byte[] dst = new byte[len * 3]; duke@1: int len1 = chars2utf(src, sindex, dst, 0, len); duke@1: byte[] result = new byte[len1]; duke@1: System.arraycopy(dst, 0, result, 0, len1); duke@1: return result; duke@1: } duke@1: duke@1: /** Return all characters in given array as an array of bytes duke@1: * in Utf8 representation. duke@1: * @param src The array holding the characters. duke@1: */ duke@1: public static byte[] chars2utf(char[] src) { duke@1: return chars2utf(src, 0, src.length); duke@1: } duke@1: duke@1: /** Return string as an array of bytes in in Utf8 representation. duke@1: */ duke@1: public static byte[] string2utf(String s) { duke@1: return chars2utf(s.toCharArray()); duke@1: } duke@1: duke@1: /** duke@1: * Escapes each character in a string that has an escape sequence or duke@1: * is non-printable ASCII. Leaves non-ASCII characters alone. duke@1: */ duke@1: public static String quote(String s) { duke@1: StringBuilder buf = new StringBuilder(); duke@1: for (int i = 0; i < s.length(); i++) { duke@1: buf.append(quote(s.charAt(i))); duke@1: } duke@1: return buf.toString(); duke@1: } duke@1: duke@1: /** duke@1: * Escapes a character if it has an escape sequence or is duke@1: * non-printable ASCII. Leaves non-ASCII characters alone. duke@1: */ duke@1: public static String quote(char ch) { duke@1: switch (ch) { duke@1: case '\b': return "\\b"; duke@1: case '\f': return "\\f"; duke@1: case '\n': return "\\n"; duke@1: case '\r': return "\\r"; duke@1: case '\t': return "\\t"; duke@1: case '\'': return "\\'"; duke@1: case '\"': return "\\\""; duke@1: case '\\': return "\\\\"; duke@1: default: darcy@414: return (isPrintableAscii(ch)) duke@1: ? String.valueOf(ch) darcy@414: : String.format("\\u%04x", (int) ch); duke@1: } duke@1: } duke@1: duke@1: /** duke@1: * Is a character printable ASCII? duke@1: */ duke@1: private static boolean isPrintableAscii(char ch) { duke@1: return ch >= ' ' && ch <= '~'; duke@1: } duke@1: duke@1: /** Escape all unicode characters in string. duke@1: */ duke@1: public static String escapeUnicode(String s) { duke@1: int len = s.length(); duke@1: int i = 0; duke@1: while (i < len) { duke@1: char ch = s.charAt(i); duke@1: if (ch > 255) { duke@1: StringBuffer buf = new StringBuffer(); duke@1: buf.append(s.substring(0, i)); duke@1: while (i < len) { duke@1: ch = s.charAt(i); duke@1: if (ch > 255) { duke@1: buf.append("\\u"); duke@1: buf.append(Character.forDigit((ch >> 12) % 16, 16)); duke@1: buf.append(Character.forDigit((ch >> 8) % 16, 16)); duke@1: buf.append(Character.forDigit((ch >> 4) % 16, 16)); duke@1: buf.append(Character.forDigit((ch ) % 16, 16)); duke@1: } else { duke@1: buf.append(ch); duke@1: } duke@1: i++; duke@1: } duke@1: s = buf.toString(); duke@1: } else { duke@1: i++; duke@1: } duke@1: } duke@1: return s; duke@1: } duke@1: duke@1: /* Conversion routines for qualified name splitting duke@1: */ duke@1: /** Return the last part of a class name. duke@1: */ duke@1: public static Name shortName(Name classname) { duke@1: return classname.subName( jjg@113: classname.lastIndexOf((byte)'.') + 1, classname.getByteLength()); duke@1: } duke@1: duke@1: public static String shortName(String classname) { duke@1: return classname.substring(classname.lastIndexOf('.') + 1); duke@1: } duke@1: duke@1: /** Return the package name of a class name, excluding the trailing '.', duke@1: * "" if not existent. duke@1: */ duke@1: public static Name packagePart(Name classname) { duke@1: return classname.subName(0, classname.lastIndexOf((byte)'.')); duke@1: } duke@1: duke@1: public static String packagePart(String classname) { duke@1: int lastDot = classname.lastIndexOf('.'); duke@1: return (lastDot < 0 ? "" : classname.substring(0, lastDot)); duke@1: } duke@1: duke@1: public static List enclosingCandidates(Name name) { duke@1: List names = List.nil(); duke@1: int index; duke@1: while ((index = name.lastIndexOf((byte)'$')) > 0) { duke@1: name = name.subName(0, index); duke@1: names = names.prepend(name); duke@1: } duke@1: return names; duke@1: } duke@1: }