0N/A/*
553N/A * Copyright (c) 1999, 2008, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
553N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
553N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
553N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.javac.util;
0N/A
112N/A/** An abstraction for internal compiler strings. They are stored in
112N/A * Utf8 format. Names are stored in a Name.Table, and are unique within
112N/A * that table.
0N/A *
580N/A * <p><b>This is NOT part of any supported API.
580N/A * If you write code that depends on this, you do so at your own risk.
0N/A * This code and its internal interfaces are subject to change or
0N/A * deletion without notice.</b>
0N/A */
112N/Apublic abstract class Name implements javax.lang.model.element.Name {
0N/A
112N/A public final Table table;
0N/A
112N/A protected Name(Table table) {
112N/A this.table = table;
0N/A }
0N/A
112N/A /**
112N/A * @inheritDoc
0N/A */
112N/A public boolean contentEquals(CharSequence cs) {
112N/A return toString().equals(cs.toString());
0N/A }
0N/A
112N/A /**
112N/A * @inheritDoc
0N/A */
0N/A public int length() {
0N/A return toString().length();
0N/A }
0N/A
112N/A /**
112N/A * @inheritDoc
0N/A */
112N/A public char charAt(int index) {
112N/A return toString().charAt(index);
112N/A }
112N/A
112N/A /**
112N/A * @inheritDoc
112N/A */
112N/A public CharSequence subSequence(int start, int end) {
112N/A return toString().subSequence(start, end);
0N/A }
0N/A
112N/A /** Return the concatenation of this name and name `n'.
112N/A */
112N/A public Name append(Name n) {
112N/A int len = getByteLength();
112N/A byte[] bs = new byte[len + n.getByteLength()];
112N/A getBytes(bs, 0);
112N/A n.getBytes(bs, len);
112N/A return table.fromUtf(bs, 0, bs.length);
112N/A }
112N/A
112N/A /** Return the concatenation of this name, the given ASCII
112N/A * character, and name `n'.
0N/A */
112N/A public Name append(char c, Name n) {
112N/A int len = getByteLength();
112N/A byte[] bs = new byte[len + 1 + n.getByteLength()];
112N/A getBytes(bs, 0);
112N/A bs[len] = (byte) c;
112N/A n.getBytes(bs, len+1);
112N/A return table.fromUtf(bs, 0, bs.length);
112N/A }
112N/A
112N/A /** An arbitrary but consistent complete order among all Names.
112N/A */
112N/A public int compareTo(Name other) {
112N/A return other.getIndex() - this.getIndex();
112N/A }
112N/A
112N/A /** Return true if this is the empty name.
112N/A */
112N/A public boolean isEmpty() {
112N/A return getByteLength() == 0;
0N/A }
0N/A
0N/A /** Returns last occurrence of byte b in this name, -1 if not found.
0N/A */
0N/A public int lastIndexOf(byte b) {
112N/A byte[] bytes = getByteArray();
112N/A int offset = getByteOffset();
112N/A int i = getByteLength() - 1;
112N/A while (i >= 0 && bytes[offset + i] != b) i--;
0N/A return i;
0N/A }
0N/A
0N/A /** Does this name start with prefix?
0N/A */
0N/A public boolean startsWith(Name prefix) {
112N/A byte[] thisBytes = this.getByteArray();
112N/A int thisOffset = this.getByteOffset();
112N/A int thisLength = this.getByteLength();
112N/A byte[] prefixBytes = prefix.getByteArray();
112N/A int prefixOffset = prefix.getByteOffset();
112N/A int prefixLength = prefix.getByteLength();
0N/A
112N/A int i = 0;
112N/A while (i < prefixLength &&
112N/A i < thisLength &&
112N/A thisBytes[thisOffset + i] == prefixBytes[prefixOffset + i])
112N/A i++;
112N/A return i == prefixLength;
0N/A }
0N/A
0N/A /** Returns the sub-name starting at position start, up to and
0N/A * excluding position end.
0N/A */
0N/A public Name subName(int start, int end) {
0N/A if (end < start) end = start;
112N/A return table.fromUtf(getByteArray(), getByteOffset() + start, end - start);
0N/A }
0N/A
112N/A /** Return the string representation of this name.
0N/A */
112N/A public String toString() {
112N/A return Convert.utf2string(getByteArray(), getByteOffset(), getByteLength());
0N/A }
0N/A
112N/A /** Return the Utf8 representation of this name.
0N/A */
112N/A public byte[] toUtf() {
112N/A byte[] bs = new byte[getByteLength()];
0N/A getBytes(bs, 0);
112N/A return bs;
0N/A }
0N/A
112N/A /* Get a "reasonably small" value that uniquely identifies this name
112N/A * within its name table.
112N/A */
112N/A public abstract int getIndex();
112N/A
112N/A /** Get the length (in bytes) of this name.
0N/A */
112N/A public abstract int getByteLength();
0N/A
112N/A /** Returns i'th byte of this name.
112N/A */
112N/A public abstract byte getByteAt(int i);
0N/A
112N/A /** Copy all bytes of this name to buffer cs, starting at start.
112N/A */
112N/A public void getBytes(byte cs[], int start) {
112N/A System.arraycopy(getByteArray(), getByteOffset(), cs, start, getByteLength());
0N/A }
0N/A
112N/A /** Get the underlying byte array for this name. The contents of the
112N/A * array must not be modified.
112N/A */
112N/A public abstract byte[] getByteArray();
0N/A
112N/A /** Get the start offset of this name within its byte array.
112N/A */
112N/A public abstract int getByteOffset();
0N/A
112N/A /** An abstraction for the hash table used to create unique Name instances.
112N/A */
112N/A public static abstract class Table {
112N/A /** Standard name table.
112N/A */
112N/A public final Names names;
0N/A
112N/A Table(Names names) {
112N/A this.names = names;
0N/A }
0N/A
112N/A /** Get the name from the characters in cs[start..start+len-1].
0N/A */
112N/A public abstract Name fromChars(char[] cs, int start, int len);
0N/A
112N/A /** Get the name for the characters in string s.
112N/A */
112N/A public Name fromString(String s) {
112N/A char[] cs = s.toCharArray();
112N/A return fromChars(cs, 0, cs.length);
0N/A }
0N/A
112N/A /** Get the name for the bytes in array cs.
112N/A * Assume that bytes are in utf8 format.
112N/A */
112N/A public Name fromUtf(byte[] cs) {
112N/A return fromUtf(cs, 0, cs.length);
0N/A }
0N/A
112N/A /** get the name for the bytes in cs[start..start+len-1].
0N/A * Assume that bytes are in utf8 format.
0N/A */
112N/A public abstract Name fromUtf(byte[] cs, int start, int len);
0N/A
112N/A /** Release any resources used by this table.
112N/A */
112N/A public abstract void dispose();
112N/A
112N/A /** The hashcode of a name.
0N/A */
112N/A protected static int hashValue(byte bytes[], int offset, int length) {
112N/A int h = 0;
112N/A int off = offset;
0N/A
112N/A for (int i = 0; i < length; i++) {
112N/A h = (h << 5) - h + bytes[off++];
112N/A }
112N/A return h;
0N/A }
0N/A
112N/A /** Compare two subarrays
112N/A */
112N/A protected static boolean equals(byte[] bytes1, int offset1,
112N/A byte[] bytes2, int offset2, int length) {
112N/A int i = 0;
112N/A while (i < length && bytes1[offset1 + i] == bytes2[offset2 + i]) {
112N/A i++;
112N/A }
112N/A return i == length;
112N/A }
0N/A }
0N/A}