duke@1: /* duke@1: * Copyright 1999-2006 Sun Microsystems, Inc. All Rights Reserved. duke@1: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@1: * duke@1: * This code is free software; you can redistribute it and/or modify it duke@1: * under the terms of the GNU General Public License version 2 only, as duke@1: * published by the Free Software Foundation. Sun designates this duke@1: * particular file as subject to the "Classpath" exception as provided duke@1: * by Sun in the LICENSE file that accompanied this code. duke@1: * duke@1: * This code is distributed in the hope that it will be useful, but WITHOUT duke@1: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@1: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@1: * version 2 for more details (a copy is included in the LICENSE file that duke@1: * accompanied this code). duke@1: * duke@1: * You should have received a copy of the GNU General Public License version duke@1: * 2 along with this work; if not, write to the Free Software Foundation, duke@1: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@1: * duke@1: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara, duke@1: * CA 95054 USA or visit www.sun.com if you need additional information or duke@1: * have any questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.util; duke@1: duke@1: import java.lang.ref.SoftReference; duke@1: duke@1: duke@1: /** An abstraction for internal compiler strings. For efficiency reasons, duke@1: * GJC uses hashed strings that are stored in a common large buffer. duke@1: * duke@1: *
Names represent unique hashable strings. Two names are equal duke@1: * if their indices are equal. Utf8 representation is used duke@1: * for storing names internally. duke@1: * duke@1: *
This is NOT part of any API supported by Sun Microsystems. If
duke@1: * you write code that depends on this, you do so at your own risk.
duke@1: * This code and its internal interfaces are subject to change or
duke@1: * deletion without notice.
duke@1: */
duke@1: public class Name implements javax.lang.model.element.Name {
duke@1:
duke@1: /** The table structure where the name is stored
duke@1: */
duke@1: public Table table;
duke@1:
duke@1: /** The index where the bytes of this name are stored in the global name
duke@1: * buffer `names'.
duke@1: */
duke@1: public int index;
duke@1:
duke@1: /** The number of bytes in this name.
duke@1: */
duke@1: public int len;
duke@1:
duke@1: /** The next name occupying the same hash bucket.
duke@1: */
duke@1: Name next;
duke@1:
duke@1: /** The hashcode of a name.
duke@1: */
duke@1: private static int hashValue(byte cs[], int start, int len) {
duke@1: int h = 0;
duke@1: int off = start;
duke@1:
duke@1: for (int i = 0; i < len; i++) {
duke@1: h = (h << 5) - h + cs[off++];
duke@1: }
duke@1: return h;
duke@1: }
duke@1:
duke@1: /** Is (the utf8 representation of) name equal to
duke@1: * cs[start..start+len-1]?
duke@1: */
duke@1: private static boolean equals(byte[] names, int index,
duke@1: byte cs[], int start, int len) {
duke@1: int i = 0;
duke@1: while (i < len && names[index + i] == cs[start + i]) i++;
duke@1: return i == len;
duke@1: }
duke@1:
duke@1: /** Create a name from the bytes in cs[start..start+len-1].
duke@1: * Assume that bytes are in utf8 format.
duke@1: */
duke@1: public static Name fromUtf(Table table, byte cs[], int start, int len) {
duke@1: int h = hashValue(cs, start, len) & table.hashMask;
duke@1: Name n = table.hashes[h];
duke@1: byte[] names = table.names;
duke@1: while (n != null &&
duke@1: (n.len != len || !equals(names, n.index, cs, start, len)))
duke@1: n = n.next;
duke@1: if (n == null) {
duke@1: int nc = table.nc;
duke@1: while (nc + len > names.length) {
duke@1: // System.err.println("doubling name buffer of length + " + names.length + " to fit " + len + " bytes");//DEBUG
duke@1: byte[] newnames = new byte[names.length * 2];
duke@1: System.arraycopy(names, 0, newnames, 0, names.length);
duke@1: names = table.names = newnames;
duke@1: }
duke@1: System.arraycopy(cs, start, names, nc, len);
duke@1: n = new Name();
duke@1: n.table = table;
duke@1: n.index = nc;
duke@1: n.len = len;
duke@1: n.next = table.hashes[h];
duke@1: table.hashes[h] = n;
duke@1: table.nc = nc + len;
duke@1: if (len == 0) table.nc++;
duke@1: }
duke@1: return n;
duke@1: }
duke@1:
duke@1: /** Create a name from the bytes in array cs.
duke@1: * Assume that bytes are in utf8 format.
duke@1: */
duke@1: public static Name fromUtf(Table table, byte cs[]) {
duke@1: return fromUtf(table, cs, 0, cs.length);
duke@1: }
duke@1:
duke@1: /** Create a name from the characters in cs[start..start+len-1].
duke@1: */
duke@1: public static Name fromChars(Table table, char[] cs, int start, int len) {
duke@1: int nc = table.nc;
duke@1: byte[] names = table.names;
duke@1: while (nc + len * 3 >= names.length) {
duke@1: // System.err.println("doubling name buffer of length " + names.length + " to fit " + len + " chars");//DEBUG
duke@1: byte[] newnames = new byte[names.length * 2];
duke@1: System.arraycopy(names, 0, newnames, 0, names.length);
duke@1: names = table.names = newnames;
duke@1: }
duke@1: int nbytes =
duke@1: Convert.chars2utf(cs, start, names, nc, len) - nc;
duke@1: int h = hashValue(names, nc, nbytes) & table.hashMask;
duke@1: Name n = table.hashes[h];
duke@1: while (n != null &&
duke@1: (n.len != nbytes ||
duke@1: !equals(names, n.index, names, nc, nbytes)))
duke@1: n = n.next;
duke@1: if (n == null) {
duke@1: n = new Name();
duke@1: n.table = table;
duke@1: n.index = nc;
duke@1: n.len = nbytes;
duke@1: n.next = table.hashes[h];
duke@1: table.hashes[h] = n;
duke@1: table.nc = nc + nbytes;
duke@1: if (nbytes == 0) table.nc++;
duke@1: }
duke@1: return n;
duke@1: }
duke@1:
duke@1: /** Create a name from the characters in string s.
duke@1: */
duke@1: public static Name fromString(Table table, String s) {
duke@1: char[] cs = s.toCharArray();
duke@1: return fromChars(table, cs, 0, cs.length);
duke@1: }
duke@1:
duke@1: /** Create a name from the characters in char sequence s.
duke@1: */
duke@1: public static Name fromString(Table table, CharSequence s) {
duke@1: return fromString(table, s.toString());
duke@1: }
duke@1:
duke@1: /** Return the Utf8 representation of this name.
duke@1: */
duke@1: public byte[] toUtf() {
duke@1: byte[] bs = new byte[len];
duke@1: System.arraycopy(table.names, index, bs, 0, len);
duke@1: return bs;
duke@1: }
duke@1:
duke@1: /** Return the string representation of this name.
duke@1: */
duke@1: public String toString() {
duke@1: return Convert.utf2string(table.names, index, len);
duke@1: }
duke@1:
duke@1: /** Copy all bytes of this name to buffer cs, starting at start.
duke@1: */
duke@1: public void getBytes(byte cs[], int start) {
duke@1: System.arraycopy(table.names, index, cs, start, len);
duke@1: }
duke@1:
duke@1: /** Return the hash value of this name.
duke@1: */
duke@1: public int hashCode() {
duke@1: return index;
duke@1: }
duke@1:
duke@1: /** Is this name equal to other?
duke@1: */
duke@1: public boolean equals(Object other) {
duke@1: if (other instanceof Name)
duke@1: return
duke@1: table == ((Name)other).table && index == ((Name)other).index;
duke@1: else return false;
duke@1: }
duke@1:
duke@1: /** Compare this name to other name, yielding -1 if smaller, 0 if equal,
duke@1: * 1 if greater.
duke@1: */
duke@1: public boolean less(Name that) {
duke@1: int i = 0;
duke@1: while (i < this.len && i < that.len) {
duke@1: byte thisb = this.table.names[this.index + i];
duke@1: byte thatb = that.table.names[that.index + i];
duke@1: if (thisb < thatb) return true;
duke@1: else if (thisb > thatb) return false;
duke@1: else i++;
duke@1: }
duke@1: return this.len < that.len;
duke@1: }
duke@1:
duke@1: /** Returns the length of this name.
duke@1: */
duke@1: public int length() {
duke@1: return toString().length();
duke@1: }
duke@1:
duke@1: /** Returns i'th byte of this name.
duke@1: */
duke@1: public byte byteAt(int i) {
duke@1: return table.names[index + i];
duke@1: }
duke@1:
duke@1: /** Returns first occurrence of byte b in this name, len if not found.
duke@1: */
duke@1: public int indexOf(byte b) {
duke@1: byte[] names = table.names;
duke@1: int i = 0;
duke@1: while (i < len && names[index + i] != b) i++;
duke@1: return i;
duke@1: }
duke@1:
duke@1: /** Returns last occurrence of byte b in this name, -1 if not found.
duke@1: */
duke@1: public int lastIndexOf(byte b) {
duke@1: byte[] names = table.names;
duke@1: int i = len - 1;
duke@1: while (i >= 0 && names[index + i] != b) i--;
duke@1: return i;
duke@1: }
duke@1:
duke@1: /** Does this name start with prefix?
duke@1: */
duke@1: public boolean startsWith(Name prefix) {
duke@1: int i = 0;
duke@1: while (i < prefix.len &&
duke@1: i < len &&
duke@1: table.names[index + i] == prefix.table.names[prefix.index + i])
duke@1: i++;
duke@1: return i == prefix.len;
duke@1: }
duke@1:
duke@1: /** Does this name end with suffix?
duke@1: */
duke@1: public boolean endsWith(Name suffix) {
duke@1: int i = len - 1;
duke@1: int j = suffix.len - 1;
duke@1: while (j >= 0 && i >= 0 &&
duke@1: table.names[index + i] == suffix.table.names[suffix.index + j]) {
duke@1: i--; j--;
duke@1: }
duke@1: return j < 0;
duke@1: }
duke@1:
duke@1: /** Returns the sub-name starting at position start, up to and
duke@1: * excluding position end.
duke@1: */
duke@1: public Name subName(int start, int end) {
duke@1: if (end < start) end = start;
duke@1: return fromUtf(table, table.names, index + start, end - start);
duke@1: }
duke@1:
duke@1: /** Replace all `from' bytes in this name with `to' bytes.
duke@1: */
duke@1: public Name replace(byte from, byte to) {
duke@1: byte[] names = table.names;
duke@1: int i = 0;
duke@1: while (i < len) {
duke@1: if (names[index + i] == from) {
duke@1: byte[] bs = new byte[len];
duke@1: System.arraycopy(names, index, bs, 0, i);
duke@1: bs[i] = to;
duke@1: i++;
duke@1: while (i < len) {
duke@1: byte b = names[index + i];
duke@1: bs[i] = b == from ? to : b;
duke@1: i++;
duke@1: }
duke@1: return fromUtf(table, bs, 0, len);
duke@1: }
duke@1: i++;
duke@1: }
duke@1: return this;
duke@1: }
duke@1:
duke@1: /** Return the concatenation of this name and name `n'.
duke@1: */
duke@1: public Name append(Name n) {
duke@1: byte[] bs = new byte[len + n.len];
duke@1: getBytes(bs, 0);
duke@1: n.getBytes(bs, len);
duke@1: return fromUtf(table, bs, 0, bs.length);
duke@1: }
duke@1:
duke@1: /** Return the concatenation of this name, the given ASCII
duke@1: * character, and name `n'.
duke@1: */
duke@1: public Name append(char c, Name n) {
duke@1: byte[] bs = new byte[len + n.len + 1];
duke@1: getBytes(bs, 0);
duke@1: bs[len] = (byte)c;
duke@1: n.getBytes(bs, len+1);
duke@1: return fromUtf(table, bs, 0, bs.length);
duke@1: }
duke@1:
duke@1: /** An arbitrary but consistent complete order among all Names.
duke@1: */
duke@1: public int compareTo(Name other) {
duke@1: return other.index - this.index;
duke@1: }
duke@1:
duke@1: /** Return the concatenation of all names in the array `ns'.
duke@1: */
duke@1: public static Name concat(Table table, Name ns[]) {
duke@1: int len = 0;
duke@1: for (int i = 0; i < ns.length; i++)
duke@1: len = len + ns[i].len;
duke@1: byte[] bs = new byte[len];
duke@1: len = 0;
duke@1: for (int i = 0; i < ns.length; i++) {
duke@1: ns[i].getBytes(bs, len);
duke@1: len = len + ns[i].len;
duke@1: }
duke@1: return fromUtf(table, bs, 0, len);
duke@1: }
duke@1:
duke@1: public char charAt(int index) {
duke@1: return toString().charAt(index);
duke@1: }
duke@1:
duke@1: public CharSequence subSequence(int start, int end) {
duke@1: return toString().subSequence(start, end);
duke@1: }
duke@1:
duke@1: public boolean contentEquals(CharSequence cs) {
duke@1: return this.toString().equals(cs.toString());
duke@1: }
duke@1:
duke@1: public static class Table {
duke@1: // maintain a freelist of recently used name tables for reuse.
duke@1: private static List(t));
duke@1: }
duke@1:
duke@1: public void dispose() {
duke@1: dispose(this);
duke@1: }
duke@1:
duke@1: public static final Context.Key
namesKey =
duke@1: new Context.Key
();
duke@1:
duke@1: public static Table instance(Context context) {
duke@1: Table instance = context.get(namesKey);
duke@1: if (instance == null) {
duke@1: instance = make();
duke@1: context.put(namesKey, instance);
duke@1: }
duke@1: return instance;
duke@1: }
duke@1:
duke@1: /** The hash table for names.
duke@1: */
duke@1: private Name[] hashes;
duke@1:
duke@1: /** The array holding all encountered names.
duke@1: */
duke@1: public byte[] names;
duke@1:
duke@1: /** The mask to be used for hashing
duke@1: */
duke@1: private int hashMask;
duke@1:
duke@1: /** The number of filled bytes in `names'.
duke@1: */
duke@1: private int nc = 0;
duke@1:
duke@1: /** Allocator
duke@1: * @param hashSize the (constant) size to be used for the hash table
duke@1: * needs to be a power of two.
duke@1: * @param nameSize the initial size of the name table.
duke@1: */
duke@1: public Table(int hashSize, int nameSize) {
duke@1: hashMask = hashSize - 1;
duke@1: hashes = new Name[hashSize];
duke@1: names = new byte[nameSize];
duke@1:
duke@1: slash = fromString("/");
duke@1: hyphen = fromString("-");
duke@1: T = fromString("T");
duke@1: slashequals = fromString("/=");
duke@1: deprecated = fromString("deprecated");
duke@1:
duke@1: init = fromString("