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

changeset 1
9a66ca7c79fa
child 113
eff38cc97183
     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	Sat Dec 01 00:00:00 2007 +0000
     1.3 @@ -0,0 +1,320 @@
     1.4 +/*
     1.5 + * Copyright 1999-2006 Sun Microsystems, Inc.  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.  Sun designates this
    1.11 + * particular file as subject to the "Classpath" exception as provided
    1.12 + * by Sun 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    1.25 + * CA 95054 USA or visit www.sun.com if you need additional information or
    1.26 + * have any 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><b>This is NOT part of any API supported by Sun Microsystems.  If
    1.35 + *  you write code that depends on this, you do so at your own risk.
    1.36 + *  This code and its internal interfaces are subject to change or
    1.37 + *  deletion without notice.</b>
    1.38 + */
    1.39 +public class Convert {
    1.40 +
    1.41 +    /** Convert string to integer.
    1.42 +     */
    1.43 +    public static int string2int(String s, int radix)
    1.44 +        throws NumberFormatException {
    1.45 +        if (radix == 10) {
    1.46 +            return Integer.parseInt(s, radix);
    1.47 +        } else {
    1.48 +            char[] cs = s.toCharArray();
    1.49 +            int limit = Integer.MAX_VALUE / (radix/2);
    1.50 +            int n = 0;
    1.51 +            for (int i = 0; i < cs.length; i++) {
    1.52 +                int d = Character.digit(cs[i], radix);
    1.53 +                if (n < 0 ||
    1.54 +                    n > limit ||
    1.55 +                    n * radix > Integer.MAX_VALUE - d)
    1.56 +                    throw new NumberFormatException();
    1.57 +                n = n * radix + d;
    1.58 +            }
    1.59 +            return n;
    1.60 +        }
    1.61 +    }
    1.62 +
    1.63 +    /** Convert string to long integer.
    1.64 +     */
    1.65 +    public static long string2long(String s, int radix)
    1.66 +        throws NumberFormatException {
    1.67 +        if (radix == 10) {
    1.68 +            return Long.parseLong(s, radix);
    1.69 +        } else {
    1.70 +            char[] cs = s.toCharArray();
    1.71 +            long limit = Long.MAX_VALUE / (radix/2);
    1.72 +            long n = 0;
    1.73 +            for (int i = 0; i < cs.length; i++) {
    1.74 +                int d = Character.digit(cs[i], radix);
    1.75 +                if (n < 0 ||
    1.76 +                    n > limit ||
    1.77 +                    n * radix > Long.MAX_VALUE - d)
    1.78 +                    throw new NumberFormatException();
    1.79 +                n = n * radix + d;
    1.80 +            }
    1.81 +            return n;
    1.82 +        }
    1.83 +    }
    1.84 +
    1.85 +/* Conversion routines between names, strings, and byte arrays in Utf8 format
    1.86 + */
    1.87 +
    1.88 +    /** Convert `len' bytes from utf8 to characters.
    1.89 +     *  Parameters are as in System.arraycopy
    1.90 +     *  Return first index in `dst' past the last copied char.
    1.91 +     *  @param src        The array holding the bytes to convert.
    1.92 +     *  @param sindex     The start index from which bytes are converted.
    1.93 +     *  @param dst        The array holding the converted characters..
    1.94 +     *  @param dindex     The start index from which converted characters
    1.95 +     *                    are written.
    1.96 +     *  @param len        The maximum number of bytes to convert.
    1.97 +     */
    1.98 +    public static int utf2chars(byte[] src, int sindex,
    1.99 +                                char[] dst, int dindex,
   1.100 +                                int len) {
   1.101 +        int i = sindex;
   1.102 +        int j = dindex;
   1.103 +        int limit = sindex + len;
   1.104 +        while (i < limit) {
   1.105 +            int b = src[i++] & 0xFF;
   1.106 +            if (b >= 0xE0) {
   1.107 +                b = (b & 0x0F) << 12;
   1.108 +                b = b | (src[i++] & 0x3F) << 6;
   1.109 +                b = b | (src[i++] & 0x3F);
   1.110 +            } else if (b >= 0xC0) {
   1.111 +                b = (b & 0x1F) << 6;
   1.112 +                b = b | (src[i++] & 0x3F);
   1.113 +            }
   1.114 +            dst[j++] = (char)b;
   1.115 +        }
   1.116 +        return j;
   1.117 +    }
   1.118 +
   1.119 +    /** Return bytes in Utf8 representation as an array of characters.
   1.120 +     *  @param src        The array holding the bytes.
   1.121 +     *  @param sindex     The start index from which bytes are converted.
   1.122 +     *  @param len        The maximum number of bytes to convert.
   1.123 +     */
   1.124 +    public static char[] utf2chars(byte[] src, int sindex, int len) {
   1.125 +        char[] dst = new char[len];
   1.126 +        int len1 = utf2chars(src, sindex, dst, 0, len);
   1.127 +        char[] result = new char[len1];
   1.128 +        System.arraycopy(dst, 0, result, 0, len1);
   1.129 +        return result;
   1.130 +    }
   1.131 +
   1.132 +    /** Return all bytes of a given array in Utf8 representation
   1.133 +     *  as an array of characters.
   1.134 +     *  @param src        The array holding the bytes.
   1.135 +     */
   1.136 +    public static char[] utf2chars(byte[] src) {
   1.137 +        return utf2chars(src, 0, src.length);
   1.138 +    }
   1.139 +
   1.140 +    /** Return bytes in Utf8 representation as a string.
   1.141 +     *  @param src        The array holding the bytes.
   1.142 +     *  @param sindex     The start index from which bytes are converted.
   1.143 +     *  @param len        The maximum number of bytes to convert.
   1.144 +     */
   1.145 +    public static String utf2string(byte[] src, int sindex, int len) {
   1.146 +        char dst[] = new char[len];
   1.147 +        int len1 = utf2chars(src, sindex, dst, 0, len);
   1.148 +        return new String(dst, 0, len1);
   1.149 +    }
   1.150 +
   1.151 +    /** Return all bytes of a given array in Utf8 representation
   1.152 +     *  as a string.
   1.153 +     *  @param src        The array holding the bytes.
   1.154 +     */
   1.155 +    public static String utf2string(byte[] src) {
   1.156 +        return utf2string(src, 0, src.length);
   1.157 +    }
   1.158 +
   1.159 +    /** Copy characters in source array to bytes in target array,
   1.160 +     *  converting them to Utf8 representation.
   1.161 +     *  The target array must be large enough to hold the result.
   1.162 +     *  returns first index in `dst' past the last copied byte.
   1.163 +     *  @param src        The array holding the characters to convert.
   1.164 +     *  @param sindex     The start index from which characters are converted.
   1.165 +     *  @param dst        The array holding the converted characters..
   1.166 +     *  @param dindex     The start index from which converted bytes
   1.167 +     *                    are written.
   1.168 +     *  @param len        The maximum number of characters to convert.
   1.169 +     */
   1.170 +    public static int chars2utf(char[] src, int sindex,
   1.171 +                                byte[] dst, int dindex,
   1.172 +                                int len) {
   1.173 +        int j = dindex;
   1.174 +        int limit = sindex + len;
   1.175 +        for (int i = sindex; i < limit; i++) {
   1.176 +            char ch = src[i];
   1.177 +            if (1 <= ch && ch <= 0x7F) {
   1.178 +                dst[j++] = (byte)ch;
   1.179 +            } else if (ch <= 0x7FF) {
   1.180 +                dst[j++] = (byte)(0xC0 | (ch >> 6));
   1.181 +                dst[j++] = (byte)(0x80 | (ch & 0x3F));
   1.182 +            } else {
   1.183 +                dst[j++] = (byte)(0xE0 | (ch >> 12));
   1.184 +                dst[j++] = (byte)(0x80 | ((ch >> 6) & 0x3F));
   1.185 +                dst[j++] = (byte)(0x80 | (ch & 0x3F));
   1.186 +            }
   1.187 +        }
   1.188 +        return j;
   1.189 +    }
   1.190 +
   1.191 +    /** Return characters as an array of bytes in Utf8 representation.
   1.192 +     *  @param src        The array holding the characters.
   1.193 +     *  @param sindex     The start index from which characters are converted.
   1.194 +     *  @param len        The maximum number of characters to convert.
   1.195 +     */
   1.196 +    public static byte[] chars2utf(char[] src, int sindex, int len) {
   1.197 +        byte[] dst = new byte[len * 3];
   1.198 +        int len1 = chars2utf(src, sindex, dst, 0, len);
   1.199 +        byte[] result = new byte[len1];
   1.200 +        System.arraycopy(dst, 0, result, 0, len1);
   1.201 +        return result;
   1.202 +    }
   1.203 +
   1.204 +    /** Return all characters in given array as an array of bytes
   1.205 +     *  in Utf8 representation.
   1.206 +     *  @param src        The array holding the characters.
   1.207 +     */
   1.208 +    public static byte[] chars2utf(char[] src) {
   1.209 +        return chars2utf(src, 0, src.length);
   1.210 +    }
   1.211 +
   1.212 +    /** Return string as an array of bytes in in Utf8 representation.
   1.213 +     */
   1.214 +    public static byte[] string2utf(String s) {
   1.215 +        return chars2utf(s.toCharArray());
   1.216 +    }
   1.217 +
   1.218 +    /**
   1.219 +     * Escapes each character in a string that has an escape sequence or
   1.220 +     * is non-printable ASCII.  Leaves non-ASCII characters alone.
   1.221 +     */
   1.222 +    public static String quote(String s) {
   1.223 +        StringBuilder buf = new StringBuilder();
   1.224 +        for (int i = 0; i < s.length(); i++) {
   1.225 +            buf.append(quote(s.charAt(i)));
   1.226 +        }
   1.227 +        return buf.toString();
   1.228 +    }
   1.229 +
   1.230 +    /**
   1.231 +     * Escapes a character if it has an escape sequence or is
   1.232 +     * non-printable ASCII.  Leaves non-ASCII characters alone.
   1.233 +     */
   1.234 +    public static String quote(char ch) {
   1.235 +        switch (ch) {
   1.236 +        case '\b':  return "\\b";
   1.237 +        case '\f':  return "\\f";
   1.238 +        case '\n':  return "\\n";
   1.239 +        case '\r':  return "\\r";
   1.240 +        case '\t':  return "\\t";
   1.241 +        case '\'':  return "\\'";
   1.242 +        case '\"':  return "\\\"";
   1.243 +        case '\\':  return "\\\\";
   1.244 +        default:
   1.245 +            return (ch > 127 || isPrintableAscii(ch))
   1.246 +                ? String.valueOf(ch)
   1.247 +                : String.format("\\%03o", (int) ch);
   1.248 +        }
   1.249 +    }
   1.250 +
   1.251 +    /**
   1.252 +     * Is a character printable ASCII?
   1.253 +     */
   1.254 +    private static boolean isPrintableAscii(char ch) {
   1.255 +        return ch >= ' ' && ch <= '~';
   1.256 +    }
   1.257 +
   1.258 +    /** Escape all unicode characters in string.
   1.259 +     */
   1.260 +    public static String escapeUnicode(String s) {
   1.261 +        int len = s.length();
   1.262 +        int i = 0;
   1.263 +        while (i < len) {
   1.264 +            char ch = s.charAt(i);
   1.265 +            if (ch > 255) {
   1.266 +                StringBuffer buf = new StringBuffer();
   1.267 +                buf.append(s.substring(0, i));
   1.268 +                while (i < len) {
   1.269 +                    ch = s.charAt(i);
   1.270 +                    if (ch > 255) {
   1.271 +                        buf.append("\\u");
   1.272 +                        buf.append(Character.forDigit((ch >> 12) % 16, 16));
   1.273 +                        buf.append(Character.forDigit((ch >>  8) % 16, 16));
   1.274 +                        buf.append(Character.forDigit((ch >>  4) % 16, 16));
   1.275 +                        buf.append(Character.forDigit((ch      ) % 16, 16));
   1.276 +                    } else {
   1.277 +                        buf.append(ch);
   1.278 +                    }
   1.279 +                    i++;
   1.280 +                }
   1.281 +                s = buf.toString();
   1.282 +            } else {
   1.283 +                i++;
   1.284 +            }
   1.285 +        }
   1.286 +        return s;
   1.287 +    }
   1.288 +
   1.289 +/* Conversion routines for qualified name splitting
   1.290 + */
   1.291 +    /** Return the last part of a class name.
   1.292 +     */
   1.293 +    public static Name shortName(Name classname) {
   1.294 +        return classname.subName(
   1.295 +            classname.lastIndexOf((byte)'.') + 1, classname.len);
   1.296 +    }
   1.297 +
   1.298 +    public static String shortName(String classname) {
   1.299 +        return classname.substring(classname.lastIndexOf('.') + 1);
   1.300 +    }
   1.301 +
   1.302 +    /** Return the package name of a class name, excluding the trailing '.',
   1.303 +     *  "" if not existent.
   1.304 +     */
   1.305 +    public static Name packagePart(Name classname) {
   1.306 +        return classname.subName(0, classname.lastIndexOf((byte)'.'));
   1.307 +    }
   1.308 +
   1.309 +    public static String packagePart(String classname) {
   1.310 +        int lastDot = classname.lastIndexOf('.');
   1.311 +        return (lastDot < 0 ? "" : classname.substring(0, lastDot));
   1.312 +    }
   1.313 +
   1.314 +    public static List<Name> enclosingCandidates(Name name) {
   1.315 +        List<Name> names = List.nil();
   1.316 +        int index;
   1.317 +        while ((index = name.lastIndexOf((byte)'$')) > 0) {
   1.318 +            name = name.subName(0, index);
   1.319 +            names = names.prepend(name);
   1.320 +        }
   1.321 +        return names;
   1.322 +    }
   1.323 +}

mercurial