duke@1: /* ohair@554: * Copyright (c) 1999, 2008, Oracle and/or its affiliates. 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 ohair@554: * published by the Free Software Foundation. Oracle designates this duke@1: * particular file as subject to the "Classpath" exception as provided ohair@554: * by Oracle 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: * ohair@554: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@554: * or visit www.oracle.com if you need additional information or have any ohair@554: * questions. duke@1: */ duke@1: duke@1: package com.sun.tools.javac.util; duke@1: jjg@113: /** An abstraction for internal compiler strings. They are stored in jjg@113: * Utf8 format. Names are stored in a Name.Table, and are unique within jjg@113: * that table. duke@1: * jjg@581: *

This is NOT part of any supported API. jjg@581: * If 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: */ jjg@113: public abstract class Name implements javax.lang.model.element.Name { duke@1: jjg@113: public final Table table; duke@1: jjg@113: protected Name(Table table) { jjg@113: this.table = table; duke@1: } duke@1: jjg@113: /** jjg@113: * @inheritDoc duke@1: */ jjg@113: public boolean contentEquals(CharSequence cs) { jjg@113: return toString().equals(cs.toString()); duke@1: } duke@1: jjg@113: /** jjg@113: * @inheritDoc duke@1: */ duke@1: public int length() { duke@1: return toString().length(); duke@1: } duke@1: jjg@113: /** jjg@113: * @inheritDoc duke@1: */ jjg@113: public char charAt(int index) { jjg@113: return toString().charAt(index); duke@1: } duke@1: jjg@113: /** jjg@113: * @inheritDoc duke@1: */ jjg@113: public CharSequence subSequence(int start, int end) { jjg@113: return toString().subSequence(start, end); jjg@113: } jjg@113: jjg@113: /** Return the concatenation of this name and name `n'. jjg@113: */ jjg@113: public Name append(Name n) { jjg@113: int len = getByteLength(); jjg@113: byte[] bs = new byte[len + n.getByteLength()]; jjg@113: getBytes(bs, 0); jjg@113: n.getBytes(bs, len); jjg@113: return table.fromUtf(bs, 0, bs.length); jjg@113: } jjg@113: jjg@113: /** Return the concatenation of this name, the given ASCII jjg@113: * character, and name `n'. jjg@113: */ jjg@113: public Name append(char c, Name n) { jjg@113: int len = getByteLength(); jjg@113: byte[] bs = new byte[len + 1 + n.getByteLength()]; jjg@113: getBytes(bs, 0); jjg@113: bs[len] = (byte) c; jjg@113: n.getBytes(bs, len+1); jjg@113: return table.fromUtf(bs, 0, bs.length); jjg@113: } jjg@113: jjg@113: /** An arbitrary but consistent complete order among all Names. jjg@113: */ jjg@113: public int compareTo(Name other) { jjg@113: return other.getIndex() - this.getIndex(); jjg@113: } jjg@113: jjg@113: /** Return true if this is the empty name. jjg@113: */ jjg@113: public boolean isEmpty() { jjg@113: return getByteLength() == 0; 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) { jjg@113: byte[] bytes = getByteArray(); jjg@113: int offset = getByteOffset(); jjg@113: int i = getByteLength() - 1; jjg@113: while (i >= 0 && bytes[offset + 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) { jjg@113: byte[] thisBytes = this.getByteArray(); jjg@113: int thisOffset = this.getByteOffset(); jjg@113: int thisLength = this.getByteLength(); jjg@113: byte[] prefixBytes = prefix.getByteArray(); jjg@113: int prefixOffset = prefix.getByteOffset(); jjg@113: int prefixLength = prefix.getByteLength(); jjg@113: duke@1: int i = 0; jjg@113: while (i < prefixLength && jjg@113: i < thisLength && jjg@113: thisBytes[thisOffset + i] == prefixBytes[prefixOffset + i]) duke@1: i++; jjg@113: return i == prefixLength; 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; jjg@113: return table.fromUtf(getByteArray(), getByteOffset() + start, end - start); duke@1: } duke@1: jjg@113: /** Return the string representation of this name. duke@1: */ jjg@113: public String toString() { jjg@113: return Convert.utf2string(getByteArray(), getByteOffset(), getByteLength()); duke@1: } duke@1: jjg@113: /** Return the Utf8 representation of this name. duke@1: */ jjg@113: public byte[] toUtf() { jjg@113: byte[] bs = new byte[getByteLength()]; duke@1: getBytes(bs, 0); jjg@113: return bs; duke@1: } duke@1: jjg@113: /* Get a "reasonably small" value that uniquely identifies this name jjg@113: * within its name table. duke@1: */ jjg@113: public abstract int getIndex(); jjg@113: jjg@113: /** Get the length (in bytes) of this name. jjg@113: */ jjg@113: public abstract int getByteLength(); jjg@113: jjg@113: /** Returns i'th byte of this name. jjg@113: */ jjg@113: public abstract byte getByteAt(int i); jjg@113: jjg@113: /** Copy all bytes of this name to buffer cs, starting at start. jjg@113: */ jjg@113: public void getBytes(byte cs[], int start) { jjg@113: System.arraycopy(getByteArray(), getByteOffset(), cs, start, getByteLength()); duke@1: } duke@1: jjg@113: /** Get the underlying byte array for this name. The contents of the jjg@113: * array must not be modified. duke@1: */ jjg@113: public abstract byte[] getByteArray(); duke@1: jjg@113: /** Get the start offset of this name within its byte array. duke@1: */ jjg@113: public abstract int getByteOffset(); duke@1: jjg@113: /** An abstraction for the hash table used to create unique Name instances. jjg@113: */ jjg@113: public static abstract class Table { jjg@113: /** Standard name table. jjg@113: */ jjg@113: public final Names names; duke@1: jjg@113: Table(Names names) { jjg@113: this.names = names; duke@1: } duke@1: jjg@113: /** Get the name from the characters in cs[start..start+len-1]. jjg@113: */ jjg@113: public abstract Name fromChars(char[] cs, int start, int len); jjg@113: jjg@113: /** Get the name for the characters in string s. jjg@113: */ jjg@113: public Name fromString(String s) { jjg@113: char[] cs = s.toCharArray(); jjg@113: return fromChars(cs, 0, cs.length); duke@1: } duke@1: jjg@113: /** Get the name for the bytes in array cs. jjg@113: * Assume that bytes are in utf8 format. jjg@113: */ jjg@113: public Name fromUtf(byte[] cs) { jjg@113: return fromUtf(cs, 0, cs.length); duke@1: } duke@1: jjg@113: /** get the name for the bytes in cs[start..start+len-1]. jjg@113: * Assume that bytes are in utf8 format. jjg@113: */ jjg@113: public abstract Name fromUtf(byte[] cs, int start, int len); duke@1: jjg@113: /** Release any resources used by this table. jjg@113: */ jjg@113: public abstract void dispose(); jjg@113: jjg@113: /** The hashcode of a name. jjg@113: */ jjg@113: protected static int hashValue(byte bytes[], int offset, int length) { jjg@113: int h = 0; jjg@113: int off = offset; jjg@113: jjg@113: for (int i = 0; i < length; i++) { jjg@113: h = (h << 5) - h + bytes[off++]; duke@1: } jjg@113: return h; duke@1: } duke@1: jjg@113: /** Compare two subarrays duke@1: */ jjg@113: protected static boolean equals(byte[] bytes1, int offset1, jjg@113: byte[] bytes2, int offset2, int length) { jjg@113: int i = 0; jjg@113: while (i < length && bytes1[offset1 + i] == bytes2[offset2 + i]) { jjg@113: i++; jjg@113: } jjg@113: return i == length; duke@1: } duke@1: } duke@1: }