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

Tue, 24 Sep 2013 14:20:33 -0700

author
mfang
date
Tue, 24 Sep 2013 14:20:33 -0700
changeset 2057
1332a99572c5
parent 1770
0928f2cfbf8e
child 2525
2eb010b6cb22
child 2628
a5eb8f677bd4
permissions
-rw-r--r--

8025215: jdk8 l10n resource file translation update 4
Reviewed-by: naoto, yhuang

duke@1 1 /*
jjg@1770 2 * Copyright (c) 1999, 2013, Oracle and/or its affiliates. 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
ohair@554 7 * published by the Free Software Foundation. Oracle designates this
duke@1 8 * particular file as subject to the "Classpath" exception as provided
ohair@554 9 * by Oracle 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 *
ohair@554 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
ohair@554 22 * or visit www.oracle.com if you need additional information or have any
ohair@554 23 * 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 *
jjg@1770 31 * <p>Note regarding UTF-8.
jjg@1770 32 * The JVMS defines its own version of the UTF-8 format so that it
jjg@1770 33 * contains no zero bytes (modified UTF-8). This is not actually the same
jjg@1770 34 * as Charset.forName("UTF-8").
jjg@1770 35 *
jjg@1770 36 * <p>
jjg@1770 37 * See also:
jjg@1770 38 * <ul>
jjg@1770 39 * <li><a href="http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4.7">
jjg@1770 40 * JVMS 4.4.7 </a></li>
jjg@1770 41 * <li><a href="http://docs.oracle.com/javase/7/docs/api/java/io/DataInput.html#modified-utf-8">
jjg@1770 42 java.io.DataInput: Modified UTF-8 </a></li>
jjg@1770 43 <li><a href="https://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8">
jjg@1770 44 Modified UTF-8 (wikipedia) </a></li>
jjg@1770 45 * </ul>
jjg@1770 46 *
jjg@1770 47 * The methods here support modified UTF-8.
jjg@1770 48 *
jjg@581 49 * <p><b>This is NOT part of any supported API.
jjg@581 50 * If you write code that depends on this, you do so at your own risk.
duke@1 51 * This code and its internal interfaces are subject to change or
duke@1 52 * deletion without notice.</b>
duke@1 53 */
duke@1 54 public class Convert {
duke@1 55
duke@1 56 /** Convert string to integer.
duke@1 57 */
duke@1 58 public static int string2int(String s, int radix)
duke@1 59 throws NumberFormatException {
duke@1 60 if (radix == 10) {
duke@1 61 return Integer.parseInt(s, radix);
duke@1 62 } else {
duke@1 63 char[] cs = s.toCharArray();
duke@1 64 int limit = Integer.MAX_VALUE / (radix/2);
duke@1 65 int n = 0;
duke@1 66 for (int i = 0; i < cs.length; i++) {
duke@1 67 int d = Character.digit(cs[i], radix);
duke@1 68 if (n < 0 ||
duke@1 69 n > limit ||
duke@1 70 n * radix > Integer.MAX_VALUE - d)
duke@1 71 throw new NumberFormatException();
duke@1 72 n = n * radix + d;
duke@1 73 }
duke@1 74 return n;
duke@1 75 }
duke@1 76 }
duke@1 77
duke@1 78 /** Convert string to long integer.
duke@1 79 */
duke@1 80 public static long string2long(String s, int radix)
duke@1 81 throws NumberFormatException {
duke@1 82 if (radix == 10) {
duke@1 83 return Long.parseLong(s, radix);
duke@1 84 } else {
duke@1 85 char[] cs = s.toCharArray();
duke@1 86 long limit = Long.MAX_VALUE / (radix/2);
duke@1 87 long n = 0;
duke@1 88 for (int i = 0; i < cs.length; i++) {
duke@1 89 int d = Character.digit(cs[i], radix);
duke@1 90 if (n < 0 ||
duke@1 91 n > limit ||
duke@1 92 n * radix > Long.MAX_VALUE - d)
duke@1 93 throw new NumberFormatException();
duke@1 94 n = n * radix + d;
duke@1 95 }
duke@1 96 return n;
duke@1 97 }
duke@1 98 }
duke@1 99
duke@1 100 /* Conversion routines between names, strings, and byte arrays in Utf8 format
duke@1 101 */
duke@1 102
duke@1 103 /** Convert `len' bytes from utf8 to characters.
duke@1 104 * Parameters are as in System.arraycopy
duke@1 105 * Return first index in `dst' past the last copied char.
duke@1 106 * @param src The array holding the bytes to convert.
duke@1 107 * @param sindex The start index from which bytes are converted.
duke@1 108 * @param dst The array holding the converted characters..
duke@1 109 * @param dindex The start index from which converted characters
duke@1 110 * are written.
duke@1 111 * @param len The maximum number of bytes to convert.
duke@1 112 */
duke@1 113 public static int utf2chars(byte[] src, int sindex,
duke@1 114 char[] dst, int dindex,
duke@1 115 int len) {
duke@1 116 int i = sindex;
duke@1 117 int j = dindex;
duke@1 118 int limit = sindex + len;
duke@1 119 while (i < limit) {
duke@1 120 int b = src[i++] & 0xFF;
duke@1 121 if (b >= 0xE0) {
duke@1 122 b = (b & 0x0F) << 12;
duke@1 123 b = b | (src[i++] & 0x3F) << 6;
duke@1 124 b = b | (src[i++] & 0x3F);
duke@1 125 } else if (b >= 0xC0) {
duke@1 126 b = (b & 0x1F) << 6;
duke@1 127 b = b | (src[i++] & 0x3F);
duke@1 128 }
duke@1 129 dst[j++] = (char)b;
duke@1 130 }
duke@1 131 return j;
duke@1 132 }
duke@1 133
duke@1 134 /** Return bytes in Utf8 representation as an array of characters.
duke@1 135 * @param src The array holding the bytes.
duke@1 136 * @param sindex The start index from which bytes are converted.
duke@1 137 * @param len The maximum number of bytes to convert.
duke@1 138 */
duke@1 139 public static char[] utf2chars(byte[] src, int sindex, int len) {
duke@1 140 char[] dst = new char[len];
duke@1 141 int len1 = utf2chars(src, sindex, dst, 0, len);
duke@1 142 char[] result = new char[len1];
duke@1 143 System.arraycopy(dst, 0, result, 0, len1);
duke@1 144 return result;
duke@1 145 }
duke@1 146
duke@1 147 /** Return all bytes of a given array in Utf8 representation
duke@1 148 * as an array of characters.
duke@1 149 * @param src The array holding the bytes.
duke@1 150 */
duke@1 151 public static char[] utf2chars(byte[] src) {
duke@1 152 return utf2chars(src, 0, src.length);
duke@1 153 }
duke@1 154
duke@1 155 /** Return bytes in Utf8 representation as a string.
duke@1 156 * @param src The array holding the bytes.
duke@1 157 * @param sindex The start index from which bytes are converted.
duke@1 158 * @param len The maximum number of bytes to convert.
duke@1 159 */
duke@1 160 public static String utf2string(byte[] src, int sindex, int len) {
duke@1 161 char dst[] = new char[len];
duke@1 162 int len1 = utf2chars(src, sindex, dst, 0, len);
duke@1 163 return new String(dst, 0, len1);
duke@1 164 }
duke@1 165
duke@1 166 /** Return all bytes of a given array in Utf8 representation
duke@1 167 * as a string.
duke@1 168 * @param src The array holding the bytes.
duke@1 169 */
duke@1 170 public static String utf2string(byte[] src) {
duke@1 171 return utf2string(src, 0, src.length);
duke@1 172 }
duke@1 173
duke@1 174 /** Copy characters in source array to bytes in target array,
duke@1 175 * converting them to Utf8 representation.
duke@1 176 * The target array must be large enough to hold the result.
duke@1 177 * returns first index in `dst' past the last copied byte.
duke@1 178 * @param src The array holding the characters to convert.
duke@1 179 * @param sindex The start index from which characters are converted.
duke@1 180 * @param dst The array holding the converted characters..
duke@1 181 * @param dindex The start index from which converted bytes
duke@1 182 * are written.
duke@1 183 * @param len The maximum number of characters to convert.
duke@1 184 */
duke@1 185 public static int chars2utf(char[] src, int sindex,
duke@1 186 byte[] dst, int dindex,
duke@1 187 int len) {
duke@1 188 int j = dindex;
duke@1 189 int limit = sindex + len;
duke@1 190 for (int i = sindex; i < limit; i++) {
duke@1 191 char ch = src[i];
duke@1 192 if (1 <= ch && ch <= 0x7F) {
duke@1 193 dst[j++] = (byte)ch;
duke@1 194 } else if (ch <= 0x7FF) {
duke@1 195 dst[j++] = (byte)(0xC0 | (ch >> 6));
duke@1 196 dst[j++] = (byte)(0x80 | (ch & 0x3F));
duke@1 197 } else {
duke@1 198 dst[j++] = (byte)(0xE0 | (ch >> 12));
duke@1 199 dst[j++] = (byte)(0x80 | ((ch >> 6) & 0x3F));
duke@1 200 dst[j++] = (byte)(0x80 | (ch & 0x3F));
duke@1 201 }
duke@1 202 }
duke@1 203 return j;
duke@1 204 }
duke@1 205
duke@1 206 /** Return characters as an array of bytes in Utf8 representation.
duke@1 207 * @param src The array holding the characters.
duke@1 208 * @param sindex The start index from which characters are converted.
duke@1 209 * @param len The maximum number of characters to convert.
duke@1 210 */
duke@1 211 public static byte[] chars2utf(char[] src, int sindex, int len) {
duke@1 212 byte[] dst = new byte[len * 3];
duke@1 213 int len1 = chars2utf(src, sindex, dst, 0, len);
duke@1 214 byte[] result = new byte[len1];
duke@1 215 System.arraycopy(dst, 0, result, 0, len1);
duke@1 216 return result;
duke@1 217 }
duke@1 218
duke@1 219 /** Return all characters in given array as an array of bytes
duke@1 220 * in Utf8 representation.
duke@1 221 * @param src The array holding the characters.
duke@1 222 */
duke@1 223 public static byte[] chars2utf(char[] src) {
duke@1 224 return chars2utf(src, 0, src.length);
duke@1 225 }
duke@1 226
duke@1 227 /** Return string as an array of bytes in in Utf8 representation.
duke@1 228 */
duke@1 229 public static byte[] string2utf(String s) {
duke@1 230 return chars2utf(s.toCharArray());
duke@1 231 }
duke@1 232
duke@1 233 /**
duke@1 234 * Escapes each character in a string that has an escape sequence or
duke@1 235 * is non-printable ASCII. Leaves non-ASCII characters alone.
duke@1 236 */
duke@1 237 public static String quote(String s) {
duke@1 238 StringBuilder buf = new StringBuilder();
duke@1 239 for (int i = 0; i < s.length(); i++) {
duke@1 240 buf.append(quote(s.charAt(i)));
duke@1 241 }
duke@1 242 return buf.toString();
duke@1 243 }
duke@1 244
duke@1 245 /**
duke@1 246 * Escapes a character if it has an escape sequence or is
duke@1 247 * non-printable ASCII. Leaves non-ASCII characters alone.
duke@1 248 */
duke@1 249 public static String quote(char ch) {
duke@1 250 switch (ch) {
duke@1 251 case '\b': return "\\b";
duke@1 252 case '\f': return "\\f";
duke@1 253 case '\n': return "\\n";
duke@1 254 case '\r': return "\\r";
duke@1 255 case '\t': return "\\t";
duke@1 256 case '\'': return "\\'";
duke@1 257 case '\"': return "\\\"";
duke@1 258 case '\\': return "\\\\";
duke@1 259 default:
darcy@414 260 return (isPrintableAscii(ch))
duke@1 261 ? String.valueOf(ch)
darcy@414 262 : String.format("\\u%04x", (int) ch);
duke@1 263 }
duke@1 264 }
duke@1 265
duke@1 266 /**
duke@1 267 * Is a character printable ASCII?
duke@1 268 */
duke@1 269 private static boolean isPrintableAscii(char ch) {
duke@1 270 return ch >= ' ' && ch <= '~';
duke@1 271 }
duke@1 272
duke@1 273 /** Escape all unicode characters in string.
duke@1 274 */
duke@1 275 public static String escapeUnicode(String s) {
duke@1 276 int len = s.length();
duke@1 277 int i = 0;
duke@1 278 while (i < len) {
duke@1 279 char ch = s.charAt(i);
duke@1 280 if (ch > 255) {
jjg@1362 281 StringBuilder buf = new StringBuilder();
duke@1 282 buf.append(s.substring(0, i));
duke@1 283 while (i < len) {
duke@1 284 ch = s.charAt(i);
duke@1 285 if (ch > 255) {
duke@1 286 buf.append("\\u");
duke@1 287 buf.append(Character.forDigit((ch >> 12) % 16, 16));
duke@1 288 buf.append(Character.forDigit((ch >> 8) % 16, 16));
duke@1 289 buf.append(Character.forDigit((ch >> 4) % 16, 16));
duke@1 290 buf.append(Character.forDigit((ch ) % 16, 16));
duke@1 291 } else {
duke@1 292 buf.append(ch);
duke@1 293 }
duke@1 294 i++;
duke@1 295 }
duke@1 296 s = buf.toString();
duke@1 297 } else {
duke@1 298 i++;
duke@1 299 }
duke@1 300 }
duke@1 301 return s;
duke@1 302 }
duke@1 303
duke@1 304 /* Conversion routines for qualified name splitting
duke@1 305 */
duke@1 306 /** Return the last part of a class name.
duke@1 307 */
duke@1 308 public static Name shortName(Name classname) {
duke@1 309 return classname.subName(
jjg@113 310 classname.lastIndexOf((byte)'.') + 1, classname.getByteLength());
duke@1 311 }
duke@1 312
duke@1 313 public static String shortName(String classname) {
duke@1 314 return classname.substring(classname.lastIndexOf('.') + 1);
duke@1 315 }
duke@1 316
duke@1 317 /** Return the package name of a class name, excluding the trailing '.',
duke@1 318 * "" if not existent.
duke@1 319 */
duke@1 320 public static Name packagePart(Name classname) {
duke@1 321 return classname.subName(0, classname.lastIndexOf((byte)'.'));
duke@1 322 }
duke@1 323
duke@1 324 public static String packagePart(String classname) {
duke@1 325 int lastDot = classname.lastIndexOf('.');
duke@1 326 return (lastDot < 0 ? "" : classname.substring(0, lastDot));
duke@1 327 }
duke@1 328
duke@1 329 public static List<Name> enclosingCandidates(Name name) {
duke@1 330 List<Name> names = List.nil();
duke@1 331 int index;
duke@1 332 while ((index = name.lastIndexOf((byte)'$')) > 0) {
duke@1 333 name = name.subName(0, index);
duke@1 334 names = names.prepend(name);
duke@1 335 }
duke@1 336 return names;
duke@1 337 }
duke@1 338 }

mercurial