1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/classes/com/sun/tools/javac/util/Convert.java Wed Apr 27 01:34:52 2016 +0800 1.3 @@ -0,0 +1,338 @@ 1.4 +/* 1.5 + * Copyright (c) 1999, 2013, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.javac.util; 1.30 + 1.31 +/** Utility class for static conversion methods between numbers 1.32 + * and strings in various formats. 1.33 + * 1.34 + * <p>Note regarding UTF-8. 1.35 + * The JVMS defines its own version of the UTF-8 format so that it 1.36 + * contains no zero bytes (modified UTF-8). This is not actually the same 1.37 + * as Charset.forName("UTF-8"). 1.38 + * 1.39 + * <p> 1.40 + * See also: 1.41 + * <ul> 1.42 + * <li><a href="http://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html#jvms-4.4.7"> 1.43 + * JVMS 4.4.7 </a></li> 1.44 + * <li><a href="http://docs.oracle.com/javase/7/docs/api/java/io/DataInput.html#modified-utf-8"> 1.45 + java.io.DataInput: Modified UTF-8 </a></li> 1.46 + <li><a href="https://en.wikipedia.org/wiki/UTF-8#Modified_UTF-8"> 1.47 + Modified UTF-8 (wikipedia) </a></li> 1.48 + * </ul> 1.49 + * 1.50 + * The methods here support modified UTF-8. 1.51 + * 1.52 + * <p><b>This is NOT part of any supported API. 1.53 + * If you write code that depends on this, you do so at your own risk. 1.54 + * This code and its internal interfaces are subject to change or 1.55 + * deletion without notice.</b> 1.56 + */ 1.57 +public class Convert { 1.58 + 1.59 + /** Convert string to integer. 1.60 + */ 1.61 + public static int string2int(String s, int radix) 1.62 + throws NumberFormatException { 1.63 + if (radix == 10) { 1.64 + return Integer.parseInt(s, radix); 1.65 + } else { 1.66 + char[] cs = s.toCharArray(); 1.67 + int limit = Integer.MAX_VALUE / (radix/2); 1.68 + int n = 0; 1.69 + for (int i = 0; i < cs.length; i++) { 1.70 + int d = Character.digit(cs[i], radix); 1.71 + if (n < 0 || 1.72 + n > limit || 1.73 + n * radix > Integer.MAX_VALUE - d) 1.74 + throw new NumberFormatException(); 1.75 + n = n * radix + d; 1.76 + } 1.77 + return n; 1.78 + } 1.79 + } 1.80 + 1.81 + /** Convert string to long integer. 1.82 + */ 1.83 + public static long string2long(String s, int radix) 1.84 + throws NumberFormatException { 1.85 + if (radix == 10) { 1.86 + return Long.parseLong(s, radix); 1.87 + } else { 1.88 + char[] cs = s.toCharArray(); 1.89 + long limit = Long.MAX_VALUE / (radix/2); 1.90 + long n = 0; 1.91 + for (int i = 0; i < cs.length; i++) { 1.92 + int d = Character.digit(cs[i], radix); 1.93 + if (n < 0 || 1.94 + n > limit || 1.95 + n * radix > Long.MAX_VALUE - d) 1.96 + throw new NumberFormatException(); 1.97 + n = n * radix + d; 1.98 + } 1.99 + return n; 1.100 + } 1.101 + } 1.102 + 1.103 +/* Conversion routines between names, strings, and byte arrays in Utf8 format 1.104 + */ 1.105 + 1.106 + /** Convert `len' bytes from utf8 to characters. 1.107 + * Parameters are as in System.arraycopy 1.108 + * Return first index in `dst' past the last copied char. 1.109 + * @param src The array holding the bytes to convert. 1.110 + * @param sindex The start index from which bytes are converted. 1.111 + * @param dst The array holding the converted characters.. 1.112 + * @param dindex The start index from which converted characters 1.113 + * are written. 1.114 + * @param len The maximum number of bytes to convert. 1.115 + */ 1.116 + public static int utf2chars(byte[] src, int sindex, 1.117 + char[] dst, int dindex, 1.118 + int len) { 1.119 + int i = sindex; 1.120 + int j = dindex; 1.121 + int limit = sindex + len; 1.122 + while (i < limit) { 1.123 + int b = src[i++] & 0xFF; 1.124 + if (b >= 0xE0) { 1.125 + b = (b & 0x0F) << 12; 1.126 + b = b | (src[i++] & 0x3F) << 6; 1.127 + b = b | (src[i++] & 0x3F); 1.128 + } else if (b >= 0xC0) { 1.129 + b = (b & 0x1F) << 6; 1.130 + b = b | (src[i++] & 0x3F); 1.131 + } 1.132 + dst[j++] = (char)b; 1.133 + } 1.134 + return j; 1.135 + } 1.136 + 1.137 + /** Return bytes in Utf8 representation as an array of characters. 1.138 + * @param src The array holding the bytes. 1.139 + * @param sindex The start index from which bytes are converted. 1.140 + * @param len The maximum number of bytes to convert. 1.141 + */ 1.142 + public static char[] utf2chars(byte[] src, int sindex, int len) { 1.143 + char[] dst = new char[len]; 1.144 + int len1 = utf2chars(src, sindex, dst, 0, len); 1.145 + char[] result = new char[len1]; 1.146 + System.arraycopy(dst, 0, result, 0, len1); 1.147 + return result; 1.148 + } 1.149 + 1.150 + /** Return all bytes of a given array in Utf8 representation 1.151 + * as an array of characters. 1.152 + * @param src The array holding the bytes. 1.153 + */ 1.154 + public static char[] utf2chars(byte[] src) { 1.155 + return utf2chars(src, 0, src.length); 1.156 + } 1.157 + 1.158 + /** Return bytes in Utf8 representation as a string. 1.159 + * @param src The array holding the bytes. 1.160 + * @param sindex The start index from which bytes are converted. 1.161 + * @param len The maximum number of bytes to convert. 1.162 + */ 1.163 + public static String utf2string(byte[] src, int sindex, int len) { 1.164 + char dst[] = new char[len]; 1.165 + int len1 = utf2chars(src, sindex, dst, 0, len); 1.166 + return new String(dst, 0, len1); 1.167 + } 1.168 + 1.169 + /** Return all bytes of a given array in Utf8 representation 1.170 + * as a string. 1.171 + * @param src The array holding the bytes. 1.172 + */ 1.173 + public static String utf2string(byte[] src) { 1.174 + return utf2string(src, 0, src.length); 1.175 + } 1.176 + 1.177 + /** Copy characters in source array to bytes in target array, 1.178 + * converting them to Utf8 representation. 1.179 + * The target array must be large enough to hold the result. 1.180 + * returns first index in `dst' past the last copied byte. 1.181 + * @param src The array holding the characters to convert. 1.182 + * @param sindex The start index from which characters are converted. 1.183 + * @param dst The array holding the converted characters.. 1.184 + * @param dindex The start index from which converted bytes 1.185 + * are written. 1.186 + * @param len The maximum number of characters to convert. 1.187 + */ 1.188 + public static int chars2utf(char[] src, int sindex, 1.189 + byte[] dst, int dindex, 1.190 + int len) { 1.191 + int j = dindex; 1.192 + int limit = sindex + len; 1.193 + for (int i = sindex; i < limit; i++) { 1.194 + char ch = src[i]; 1.195 + if (1 <= ch && ch <= 0x7F) { 1.196 + dst[j++] = (byte)ch; 1.197 + } else if (ch <= 0x7FF) { 1.198 + dst[j++] = (byte)(0xC0 | (ch >> 6)); 1.199 + dst[j++] = (byte)(0x80 | (ch & 0x3F)); 1.200 + } else { 1.201 + dst[j++] = (byte)(0xE0 | (ch >> 12)); 1.202 + dst[j++] = (byte)(0x80 | ((ch >> 6) & 0x3F)); 1.203 + dst[j++] = (byte)(0x80 | (ch & 0x3F)); 1.204 + } 1.205 + } 1.206 + return j; 1.207 + } 1.208 + 1.209 + /** Return characters as an array of bytes in Utf8 representation. 1.210 + * @param src The array holding the characters. 1.211 + * @param sindex The start index from which characters are converted. 1.212 + * @param len The maximum number of characters to convert. 1.213 + */ 1.214 + public static byte[] chars2utf(char[] src, int sindex, int len) { 1.215 + byte[] dst = new byte[len * 3]; 1.216 + int len1 = chars2utf(src, sindex, dst, 0, len); 1.217 + byte[] result = new byte[len1]; 1.218 + System.arraycopy(dst, 0, result, 0, len1); 1.219 + return result; 1.220 + } 1.221 + 1.222 + /** Return all characters in given array as an array of bytes 1.223 + * in Utf8 representation. 1.224 + * @param src The array holding the characters. 1.225 + */ 1.226 + public static byte[] chars2utf(char[] src) { 1.227 + return chars2utf(src, 0, src.length); 1.228 + } 1.229 + 1.230 + /** Return string as an array of bytes in in Utf8 representation. 1.231 + */ 1.232 + public static byte[] string2utf(String s) { 1.233 + return chars2utf(s.toCharArray()); 1.234 + } 1.235 + 1.236 + /** 1.237 + * Escapes each character in a string that has an escape sequence or 1.238 + * is non-printable ASCII. Leaves non-ASCII characters alone. 1.239 + */ 1.240 + public static String quote(String s) { 1.241 + StringBuilder buf = new StringBuilder(); 1.242 + for (int i = 0; i < s.length(); i++) { 1.243 + buf.append(quote(s.charAt(i))); 1.244 + } 1.245 + return buf.toString(); 1.246 + } 1.247 + 1.248 + /** 1.249 + * Escapes a character if it has an escape sequence or is 1.250 + * non-printable ASCII. Leaves non-ASCII characters alone. 1.251 + */ 1.252 + public static String quote(char ch) { 1.253 + switch (ch) { 1.254 + case '\b': return "\\b"; 1.255 + case '\f': return "\\f"; 1.256 + case '\n': return "\\n"; 1.257 + case '\r': return "\\r"; 1.258 + case '\t': return "\\t"; 1.259 + case '\'': return "\\'"; 1.260 + case '\"': return "\\\""; 1.261 + case '\\': return "\\\\"; 1.262 + default: 1.263 + return (isPrintableAscii(ch)) 1.264 + ? String.valueOf(ch) 1.265 + : String.format("\\u%04x", (int) ch); 1.266 + } 1.267 + } 1.268 + 1.269 + /** 1.270 + * Is a character printable ASCII? 1.271 + */ 1.272 + private static boolean isPrintableAscii(char ch) { 1.273 + return ch >= ' ' && ch <= '~'; 1.274 + } 1.275 + 1.276 + /** Escape all unicode characters in string. 1.277 + */ 1.278 + public static String escapeUnicode(String s) { 1.279 + int len = s.length(); 1.280 + int i = 0; 1.281 + while (i < len) { 1.282 + char ch = s.charAt(i); 1.283 + if (ch > 255) { 1.284 + StringBuilder buf = new StringBuilder(); 1.285 + buf.append(s.substring(0, i)); 1.286 + while (i < len) { 1.287 + ch = s.charAt(i); 1.288 + if (ch > 255) { 1.289 + buf.append("\\u"); 1.290 + buf.append(Character.forDigit((ch >> 12) % 16, 16)); 1.291 + buf.append(Character.forDigit((ch >> 8) % 16, 16)); 1.292 + buf.append(Character.forDigit((ch >> 4) % 16, 16)); 1.293 + buf.append(Character.forDigit((ch ) % 16, 16)); 1.294 + } else { 1.295 + buf.append(ch); 1.296 + } 1.297 + i++; 1.298 + } 1.299 + s = buf.toString(); 1.300 + } else { 1.301 + i++; 1.302 + } 1.303 + } 1.304 + return s; 1.305 + } 1.306 + 1.307 +/* Conversion routines for qualified name splitting 1.308 + */ 1.309 + /** Return the last part of a class name. 1.310 + */ 1.311 + public static Name shortName(Name classname) { 1.312 + return classname.subName( 1.313 + classname.lastIndexOf((byte)'.') + 1, classname.getByteLength()); 1.314 + } 1.315 + 1.316 + public static String shortName(String classname) { 1.317 + return classname.substring(classname.lastIndexOf('.') + 1); 1.318 + } 1.319 + 1.320 + /** Return the package name of a class name, excluding the trailing '.', 1.321 + * "" if not existent. 1.322 + */ 1.323 + public static Name packagePart(Name classname) { 1.324 + return classname.subName(0, classname.lastIndexOf((byte)'.')); 1.325 + } 1.326 + 1.327 + public static String packagePart(String classname) { 1.328 + int lastDot = classname.lastIndexOf('.'); 1.329 + return (lastDot < 0 ? "" : classname.substring(0, lastDot)); 1.330 + } 1.331 + 1.332 + public static List<Name> enclosingCandidates(Name name) { 1.333 + List<Name> names = List.nil(); 1.334 + int index; 1.335 + while ((index = name.lastIndexOf((byte)'$')) > 0) { 1.336 + name = name.subName(0, index); 1.337 + names = names.prepend(name); 1.338 + } 1.339 + return names; 1.340 + } 1.341 +}