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

Wed, 27 Apr 2016 01:34:52 +0800

author
aoqi
date
Wed, 27 Apr 2016 01:34:52 +0800
changeset 0
959103a6100f
child 2525
2eb010b6cb22
permissions
-rw-r--r--

Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17

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

mercurial