src/share/classes/com/sun/tools/javac/util/Convert.java

Thu, 02 Oct 2008 19:58:40 -0700

author
xdono
date
Thu, 02 Oct 2008 19:58:40 -0700
changeset 117
24a47c3062fe
parent 113
eff38cc97183
child 414
e992e602788e
permissions
-rw-r--r--

6754988: Update copyright year
Summary: Update for files that have been modified starting July 2008
Reviewed-by: ohair, tbell

duke@1 1 /*
xdono@117 2 * Copyright 1999-2008 Sun Microsystems, Inc. All Rights Reserved.
duke@1 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@1 4 *
duke@1 5 * This code is free software; you can redistribute it and/or modify it
duke@1 6 * under the terms of the GNU General Public License version 2 only, as
duke@1 7 * published by the Free Software Foundation. Sun designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
duke@1 9 * by Sun in the LICENSE file that accompanied this code.
duke@1 10 *
duke@1 11 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@1 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@1 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@1 14 * version 2 for more details (a copy is included in the LICENSE file that
duke@1 15 * accompanied this code).
duke@1 16 *
duke@1 17 * You should have received a copy of the GNU General Public License version
duke@1 18 * 2 along with this work; if not, write to the Free Software Foundation,
duke@1 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@1 20 *
duke@1 21 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
duke@1 22 * CA 95054 USA or visit www.sun.com if you need additional information or
duke@1 23 * have any questions.
duke@1 24 */
duke@1 25
duke@1 26 package com.sun.tools.javac.util;
duke@1 27
duke@1 28 /** Utility class for static conversion methods between numbers
duke@1 29 * and strings in various formats.
duke@1 30 *
duke@1 31 * <p><b>This is NOT part of any API supported by Sun Microsystems. If
duke@1 32 * you write code that depends on this, you do so at your own risk.
duke@1 33 * This code and its internal interfaces are subject to change or
duke@1 34 * deletion without notice.</b>
duke@1 35 */
duke@1 36 public class Convert {
duke@1 37
duke@1 38 /** Convert string to integer.
duke@1 39 */
duke@1 40 public static int string2int(String s, int radix)
duke@1 41 throws NumberFormatException {
duke@1 42 if (radix == 10) {
duke@1 43 return Integer.parseInt(s, radix);
duke@1 44 } else {
duke@1 45 char[] cs = s.toCharArray();
duke@1 46 int limit = Integer.MAX_VALUE / (radix/2);
duke@1 47 int n = 0;
duke@1 48 for (int i = 0; i < cs.length; i++) {
duke@1 49 int d = Character.digit(cs[i], radix);
duke@1 50 if (n < 0 ||
duke@1 51 n > limit ||
duke@1 52 n * radix > Integer.MAX_VALUE - d)
duke@1 53 throw new NumberFormatException();
duke@1 54 n = n * radix + d;
duke@1 55 }
duke@1 56 return n;
duke@1 57 }
duke@1 58 }
duke@1 59
duke@1 60 /** Convert string to long integer.
duke@1 61 */
duke@1 62 public static long string2long(String s, int radix)
duke@1 63 throws NumberFormatException {
duke@1 64 if (radix == 10) {
duke@1 65 return Long.parseLong(s, radix);
duke@1 66 } else {
duke@1 67 char[] cs = s.toCharArray();
duke@1 68 long limit = Long.MAX_VALUE / (radix/2);
duke@1 69 long n = 0;
duke@1 70 for (int i = 0; i < cs.length; i++) {
duke@1 71 int d = Character.digit(cs[i], radix);
duke@1 72 if (n < 0 ||
duke@1 73 n > limit ||
duke@1 74 n * radix > Long.MAX_VALUE - d)
duke@1 75 throw new NumberFormatException();
duke@1 76 n = n * radix + d;
duke@1 77 }
duke@1 78 return n;
duke@1 79 }
duke@1 80 }
duke@1 81
duke@1 82 /* Conversion routines between names, strings, and byte arrays in Utf8 format
duke@1 83 */
duke@1 84
duke@1 85 /** Convert `len' bytes from utf8 to characters.
duke@1 86 * Parameters are as in System.arraycopy
duke@1 87 * Return first index in `dst' past the last copied char.
duke@1 88 * @param src The array holding the bytes to convert.
duke@1 89 * @param sindex The start index from which bytes are converted.
duke@1 90 * @param dst The array holding the converted characters..
duke@1 91 * @param dindex The start index from which converted characters
duke@1 92 * are written.
duke@1 93 * @param len The maximum number of bytes to convert.
duke@1 94 */
duke@1 95 public static int utf2chars(byte[] src, int sindex,
duke@1 96 char[] dst, int dindex,
duke@1 97 int len) {
duke@1 98 int i = sindex;
duke@1 99 int j = dindex;
duke@1 100 int limit = sindex + len;
duke@1 101 while (i < limit) {
duke@1 102 int b = src[i++] & 0xFF;
duke@1 103 if (b >= 0xE0) {
duke@1 104 b = (b & 0x0F) << 12;
duke@1 105 b = b | (src[i++] & 0x3F) << 6;
duke@1 106 b = b | (src[i++] & 0x3F);
duke@1 107 } else if (b >= 0xC0) {
duke@1 108 b = (b & 0x1F) << 6;
duke@1 109 b = b | (src[i++] & 0x3F);
duke@1 110 }
duke@1 111 dst[j++] = (char)b;
duke@1 112 }
duke@1 113 return j;
duke@1 114 }
duke@1 115
duke@1 116 /** Return bytes in Utf8 representation as an array of characters.
duke@1 117 * @param src The array holding the bytes.
duke@1 118 * @param sindex The start index from which bytes are converted.
duke@1 119 * @param len The maximum number of bytes to convert.
duke@1 120 */
duke@1 121 public static char[] utf2chars(byte[] src, int sindex, int len) {
duke@1 122 char[] dst = new char[len];
duke@1 123 int len1 = utf2chars(src, sindex, dst, 0, len);
duke@1 124 char[] result = new char[len1];
duke@1 125 System.arraycopy(dst, 0, result, 0, len1);
duke@1 126 return result;
duke@1 127 }
duke@1 128
duke@1 129 /** Return all bytes of a given array in Utf8 representation
duke@1 130 * as an array of characters.
duke@1 131 * @param src The array holding the bytes.
duke@1 132 */
duke@1 133 public static char[] utf2chars(byte[] src) {
duke@1 134 return utf2chars(src, 0, src.length);
duke@1 135 }
duke@1 136
duke@1 137 /** Return bytes in Utf8 representation as a string.
duke@1 138 * @param src The array holding the bytes.
duke@1 139 * @param sindex The start index from which bytes are converted.
duke@1 140 * @param len The maximum number of bytes to convert.
duke@1 141 */
duke@1 142 public static String utf2string(byte[] src, int sindex, int len) {
duke@1 143 char dst[] = new char[len];
duke@1 144 int len1 = utf2chars(src, sindex, dst, 0, len);
duke@1 145 return new String(dst, 0, len1);
duke@1 146 }
duke@1 147
duke@1 148 /** Return all bytes of a given array in Utf8 representation
duke@1 149 * as a string.
duke@1 150 * @param src The array holding the bytes.
duke@1 151 */
duke@1 152 public static String utf2string(byte[] src) {
duke@1 153 return utf2string(src, 0, src.length);
duke@1 154 }
duke@1 155
duke@1 156 /** Copy characters in source array to bytes in target array,
duke@1 157 * converting them to Utf8 representation.
duke@1 158 * The target array must be large enough to hold the result.
duke@1 159 * returns first index in `dst' past the last copied byte.
duke@1 160 * @param src The array holding the characters to convert.
duke@1 161 * @param sindex The start index from which characters are converted.
duke@1 162 * @param dst The array holding the converted characters..
duke@1 163 * @param dindex The start index from which converted bytes
duke@1 164 * are written.
duke@1 165 * @param len The maximum number of characters to convert.
duke@1 166 */
duke@1 167 public static int chars2utf(char[] src, int sindex,
duke@1 168 byte[] dst, int dindex,
duke@1 169 int len) {
duke@1 170 int j = dindex;
duke@1 171 int limit = sindex + len;
duke@1 172 for (int i = sindex; i < limit; i++) {
duke@1 173 char ch = src[i];
duke@1 174 if (1 <= ch && ch <= 0x7F) {
duke@1 175 dst[j++] = (byte)ch;
duke@1 176 } else if (ch <= 0x7FF) {
duke@1 177 dst[j++] = (byte)(0xC0 | (ch >> 6));
duke@1 178 dst[j++] = (byte)(0x80 | (ch & 0x3F));
duke@1 179 } else {
duke@1 180 dst[j++] = (byte)(0xE0 | (ch >> 12));
duke@1 181 dst[j++] = (byte)(0x80 | ((ch >> 6) & 0x3F));
duke@1 182 dst[j++] = (byte)(0x80 | (ch & 0x3F));
duke@1 183 }
duke@1 184 }
duke@1 185 return j;
duke@1 186 }
duke@1 187
duke@1 188 /** Return characters as an array of bytes in Utf8 representation.
duke@1 189 * @param src The array holding the characters.
duke@1 190 * @param sindex The start index from which characters are converted.
duke@1 191 * @param len The maximum number of characters to convert.
duke@1 192 */
duke@1 193 public static byte[] chars2utf(char[] src, int sindex, int len) {
duke@1 194 byte[] dst = new byte[len * 3];
duke@1 195 int len1 = chars2utf(src, sindex, dst, 0, len);
duke@1 196 byte[] result = new byte[len1];
duke@1 197 System.arraycopy(dst, 0, result, 0, len1);
duke@1 198 return result;
duke@1 199 }
duke@1 200
duke@1 201 /** Return all characters in given array as an array of bytes
duke@1 202 * in Utf8 representation.
duke@1 203 * @param src The array holding the characters.
duke@1 204 */
duke@1 205 public static byte[] chars2utf(char[] src) {
duke@1 206 return chars2utf(src, 0, src.length);
duke@1 207 }
duke@1 208
duke@1 209 /** Return string as an array of bytes in in Utf8 representation.
duke@1 210 */
duke@1 211 public static byte[] string2utf(String s) {
duke@1 212 return chars2utf(s.toCharArray());
duke@1 213 }
duke@1 214
duke@1 215 /**
duke@1 216 * Escapes each character in a string that has an escape sequence or
duke@1 217 * is non-printable ASCII. Leaves non-ASCII characters alone.
duke@1 218 */
duke@1 219 public static String quote(String s) {
duke@1 220 StringBuilder buf = new StringBuilder();
duke@1 221 for (int i = 0; i < s.length(); i++) {
duke@1 222 buf.append(quote(s.charAt(i)));
duke@1 223 }
duke@1 224 return buf.toString();
duke@1 225 }
duke@1 226
duke@1 227 /**
duke@1 228 * Escapes a character if it has an escape sequence or is
duke@1 229 * non-printable ASCII. Leaves non-ASCII characters alone.
duke@1 230 */
duke@1 231 public static String quote(char ch) {
duke@1 232 switch (ch) {
duke@1 233 case '\b': return "\\b";
duke@1 234 case '\f': return "\\f";
duke@1 235 case '\n': return "\\n";
duke@1 236 case '\r': return "\\r";
duke@1 237 case '\t': return "\\t";
duke@1 238 case '\'': return "\\'";
duke@1 239 case '\"': return "\\\"";
duke@1 240 case '\\': return "\\\\";
duke@1 241 default:
duke@1 242 return (ch > 127 || isPrintableAscii(ch))
duke@1 243 ? String.valueOf(ch)
duke@1 244 : String.format("\\%03o", (int) ch);
duke@1 245 }
duke@1 246 }
duke@1 247
duke@1 248 /**
duke@1 249 * Is a character printable ASCII?
duke@1 250 */
duke@1 251 private static boolean isPrintableAscii(char ch) {
duke@1 252 return ch >= ' ' && ch <= '~';
duke@1 253 }
duke@1 254
duke@1 255 /** Escape all unicode characters in string.
duke@1 256 */
duke@1 257 public static String escapeUnicode(String s) {
duke@1 258 int len = s.length();
duke@1 259 int i = 0;
duke@1 260 while (i < len) {
duke@1 261 char ch = s.charAt(i);
duke@1 262 if (ch > 255) {
duke@1 263 StringBuffer buf = new StringBuffer();
duke@1 264 buf.append(s.substring(0, i));
duke@1 265 while (i < len) {
duke@1 266 ch = s.charAt(i);
duke@1 267 if (ch > 255) {
duke@1 268 buf.append("\\u");
duke@1 269 buf.append(Character.forDigit((ch >> 12) % 16, 16));
duke@1 270 buf.append(Character.forDigit((ch >> 8) % 16, 16));
duke@1 271 buf.append(Character.forDigit((ch >> 4) % 16, 16));
duke@1 272 buf.append(Character.forDigit((ch ) % 16, 16));
duke@1 273 } else {
duke@1 274 buf.append(ch);
duke@1 275 }
duke@1 276 i++;
duke@1 277 }
duke@1 278 s = buf.toString();
duke@1 279 } else {
duke@1 280 i++;
duke@1 281 }
duke@1 282 }
duke@1 283 return s;
duke@1 284 }
duke@1 285
duke@1 286 /* Conversion routines for qualified name splitting
duke@1 287 */
duke@1 288 /** Return the last part of a class name.
duke@1 289 */
duke@1 290 public static Name shortName(Name classname) {
duke@1 291 return classname.subName(
jjg@113 292 classname.lastIndexOf((byte)'.') + 1, classname.getByteLength());
duke@1 293 }
duke@1 294
duke@1 295 public static String shortName(String classname) {
duke@1 296 return classname.substring(classname.lastIndexOf('.') + 1);
duke@1 297 }
duke@1 298
duke@1 299 /** Return the package name of a class name, excluding the trailing '.',
duke@1 300 * "" if not existent.
duke@1 301 */
duke@1 302 public static Name packagePart(Name classname) {
duke@1 303 return classname.subName(0, classname.lastIndexOf((byte)'.'));
duke@1 304 }
duke@1 305
duke@1 306 public static String packagePart(String classname) {
duke@1 307 int lastDot = classname.lastIndexOf('.');
duke@1 308 return (lastDot < 0 ? "" : classname.substring(0, lastDot));
duke@1 309 }
duke@1 310
duke@1 311 public static List<Name> enclosingCandidates(Name name) {
duke@1 312 List<Name> names = List.nil();
duke@1 313 int index;
duke@1 314 while ((index = name.lastIndexOf((byte)'$')) > 0) {
duke@1 315 name = name.subName(0, index);
duke@1 316 names = names.prepend(name);
duke@1 317 }
duke@1 318 return names;
duke@1 319 }
duke@1 320 }

mercurial