⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 string.java

📁 一个开源的JAVA虚拟机
💻 JAVA
字号:
/*    libaegisvm - The Aegis Virtual Machine for executing Java bytecode    Modifications by Philip W. L. Fong    Copyright (C) 2001-2002    This library is free software; you can redistribute it and/or    modify it under the terms of the GNU Lesser General Public    License as published by the Free Software Foundation; either    version 2.1 of the License, or (at your option) any later version.    This library is distributed in the hope that it will be useful,    but WITHOUT ANY WARRANTY; without even the implied warranty of    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU    Lesser General Public License for more details.    You should have received a copy of the GNU Lesser General Public    License along with this library; if not, write to the Free Software    Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/// (C) 1996 Glynn Clements <glynn@sensei.co.uk> - Freely Redistributablepackage java.lang;import java.io.*;import java.util.*;/** * \todo Optimization: substring should share char array with original. */public final class String implements Serializable, Comparable {    static final class StringHashTable {	String[] table;	int size;	StringHashTable() {	    table = new String[16];	    size = 0;	}	void expand() {	    int oldCapacity = table.length;	    int newCapacity;	    String[] newTable;	    int mask;	    if (oldCapacity == (1 << 30))		throw new aegis.ImplementationLimitError();	    newCapacity = oldCapacity << 1;	    newTable = new String[newCapacity];	    mask = newCapacity - 1;	    for (int i = 0; i < oldCapacity; i++) {		if (table[i] != null) {		    int index = table[i].hashCode() & mask;		    while (newTable[index] != null)			index = (index + 1) & mask;		    newTable[index] = table[i];		}	    }	    table = newTable;	}	String put(String s) {	    int capacity = table.length;	    int mask, index;	    if (size + 1 > capacity / 2)		expand();	    mask = capacity - 1;	    index = s.hashCode() & mask;	    while (table[index] != null) {		if (s.compareTo(table[index]) == 0)		    return table[index];		index = (index + 1) & mask;	    }	    table[index] = s;	    ++size;	    return s;	}    }    private static StringHashTable stringPool = new StringHashTable();    static final class IgnoreCaseStringComparator implements Comparator {	public IgnoreCaseStringComparator() { }	public int compare(Object o1, Object o2) {	    return ((String) o1).compareToIgnoreCase((String) o2);	}	public boolean equals(Object obj) {	    return obj instanceof IgnoreCaseStringComparator;	}    }    public static final Comparator CASE_INSENSITIVE_ORDER =	new IgnoreCaseStringComparator();    private static char[] emptyValue = new char[0];    /* The Aegis VM depends on the layout of a String object.  In       particular, the function ae_gc_new_string() assumes that       String has exactly one non-static field. */    char[] value;    private String(char[] newValue, char[] dummy) {	value = newValue;    }    public String() {	value = emptyValue;    }    public String(String newValue) {	value = newValue.value;    }    public String(char[] newValue) {	this(newValue, 0, newValue.length);    }    public String(char[] newValue, int offset, int count) {	value = new char[count];	for (int i = 0, j = offset; i < count; i++, j++)	    value[i] = newValue[j];    }    private static StringBuffer readerToStringBuffer(Reader in) {	StringBuffer buf = new StringBuffer();	try {	    int ch;	    while ((ch = in.read()) >= 0)		buf.append((char) ch);	    in.close();	} catch (IOException e) {	}	return buf;    }	    private String(InputStreamReader in) {	this(readerToStringBuffer(in));    }    public String(byte[] bytes, int offset, int length, String enc)	throws UnsupportedEncodingException {	this(new InputStreamReader(new ByteArrayInputStream(bytes,							    offset,							    length),				   enc));    }    public String(byte[] bytes, String enc)	throws UnsupportedEncodingException {	this(bytes, 0, bytes.length, enc);    }    public String(byte[] bytes, int offset, int length) {	this(new InputStreamReader(new ByteArrayInputStream(bytes,							    offset,							    length)));    }    public String(byte[] bytes) {	this(bytes, 0, bytes.length);    }    public String(StringBuffer buffer) {	int length = buffer.length;	value = new char[length];	for (int i = 0; i < length; i++)	    value[i] = buffer.data[i];    }    public int length() {	return value.length;    }    public char charAt(int index) {	if (index < 0 || index >= value.length)	    throw new StringIndexOutOfBoundsException();	return value[index];    }    public void getChars(int srcBegin, int srcEnd, char[] dst, int dstBegin) {	if (srcBegin < 0 ||	    srcBegin > srcEnd ||	    srcEnd > value.length ||	    dstBegin < 0 ||	    dstBegin > dst.length ||	    srcEnd - srcBegin > dst.length - dstBegin)	    throw new StringIndexOutOfBoundsException();	for (int i = srcBegin, j = dstBegin; i < srcEnd; i++, j++)	    dst[j] = value[i];    }    public byte[] getBytes(String enc) {	byte[] array = null;	try {	    ByteArrayOutputStream out = new ByteArrayOutputStream();	    Writer writer = new OutputStreamWriter(out, enc);	    writer.write(value, 0, value.length);	    writer.close();	    array = out.toByteArray();	    out.close();	} catch (IOException e) {	}	return array;    }    public byte[] getBytes() {	byte[] array = null;	try {	    ByteArrayOutputStream out = new ByteArrayOutputStream();	    Writer writer = new OutputStreamWriter(out);	    writer.write(value, 0, value.length);	    writer.close();	    array = out.toByteArray();	    out.close();	} catch (IOException e) {	}	return array;    }    public boolean equals(Object obj) {	if (! (obj instanceof String))	    return false;	String str = (String) obj;	if (str.value.length != this.value.length)	    return false;	for (int i = 0; i < value.length; i++)	    if (str.value[i] != this.value[i])		return false;	return true;    }    public boolean equalsIgnoreCase(String str) {	if (str == null ||	    str.value.length != value.length)	    return false;	for (int i = 0; i < value.length; i++) {	    char c1 = str.value[i];	    char c2 = value[i];	    if (c1 != c2 &&		Character.toUpperCase(c1) != Character.toUpperCase(c2) &&		Character.toLowerCase(c1) != Character.toLowerCase(c2))		return false;	}	return true;    }    public int compareTo(String str) {	int len1 = value.length;	int len2 = str.value.length;	int n = ((len1 < len2) ? len1 : len2);	int i;		for (i = 0; i < n; i++)	    if (value[i] != str.value[i])		break;	if (i < n)	    return value[i] - str.value[i];	else	    return len1 - len2;    }    public int compareTo(Object o) {	return compareTo((String) o);    }    public int compareToIgnoreCase(String str) {	String istr1 = this.toUpperCase().toLowerCase();	String istr2 = str.toUpperCase().toLowerCase();	return istr1.compareTo(istr2);    }    public boolean regionMatches(int toffset,				 String other,				 int ooffset,				 int len) {	return regionMatches(false, toffset, other, ooffset, len);    }    public boolean regionMatches(boolean ignoreCase,				 int toffset,				 String str,				 int ooffset,				 int len) {	if (toffset < 0 ||	    toffset > value.length ||	    ooffset < 0 ||	    ooffset > str.value.length ||	    len > value.length - toffset||	    len > str.value.length - ooffset)	    return false;	for (int i = toffset, j = ooffset; i < toffset + len; i++, j++)	    if (ignoreCase) {		if (Character.toLowerCase(value[i]) !=		    Character.toLowerCase(str.value[j]) &&		    Character.toUpperCase(value[i]) !=		    Character.toUpperCase(str.value[j]))		    return false;	    } else {		if (value[i] != str.value[j])		    return false;	    }	return true;    }    public boolean startsWith(String prefix, int toffset) {	return regionMatches(toffset, prefix, 0, prefix.value.length);    }    public boolean startsWith(String prefix) {	return regionMatches(0, prefix, 0, prefix.value.length);    }    public boolean endsWith(String suffix) {	return regionMatches(value.length - suffix.value.length, suffix, 0,			     suffix.value.length);    }    public int hashCode() {	int hash = 0;	int length = value.length;	for (int i = 0; i < length; i++)	    hash = hash * 31 + value[i];	return hash;    }    public int indexOf(int ch) {	return indexOf(ch, 0);    }    public int indexOf(int ch, int fromIndex) {	for (int i = fromIndex; i < value.length; i++)	    if (value[i] == ch)		return i;	return -1;    }    public int lastIndexOf(int ch, int fromIndex) {	for (int i = fromIndex; i >= 0; i--)	    if (value[i] == ch)		return i;	return -1;    }    public int lastIndexOf(int ch) {	return lastIndexOf(ch, value.length - 1);    }    public int indexOf(String str) {	return indexOf(str, 0);    }    public int indexOf(String str, int fromIndex) {	int n = str.value.length;	for (int i = fromIndex; i < value.length - n; i++)	    if (startsWith(str, i))		return i;	return -1;    }    public int lastIndexOf(String str) {	return lastIndexOf(str, value.length - 1);    }    public int lastIndexOf(String str, int fromIndex) {	for (int i = fromIndex; i >= 0; i--)	    if (startsWith(str, i))		return i;	return -1;    }    public String substring(int beginIndex) {	return substring(beginIndex, value.length);    }    public String substring(int beginIndex, int endIndex) {	if (beginIndex < 0 || endIndex > value.length || beginIndex > endIndex)	    throw new StringIndexOutOfBoundsException();	return new String(value, beginIndex, endIndex - beginIndex);    }    public String concat(String str) {	int n = str.value.length;	if (n == 0)	    return this;	int m = value.length;	int i, j;	char[] buff = new char[m + n];	for (i = 0; i < m; i++)	    buff[i] = value[i];	for (j = 0; j < n; i++, j++)	    buff[i] = str.value[j];	return new String(buff, buff);    }    public String replace(char oldChar, char newChar) {	char[] buff = new char[value.length];	boolean found = false;	for (int i = 0; i < value.length; i++) {	    char c = value[i];	    if (c == oldChar) {		found = true;		buff[i] = newChar;	    } else {		buff[i] = c;	    }	}	return found ? new String(buff, buff) : this;    }    /*     * This function is structured so that case conversion still     * works even when the Locale class is in the process of being     * initialized.     */    public String toLowerCase() {	Locale locale = Locale.getDefault();	if (locale != null)	    return toLowerCase(locale);	else	    return toLowerCase(System.getProperty("user.language"));    }    public String toLowerCase(Locale locale) {	return toLowerCase(locale.getLanguage());    }    /*     * fix me: If string is already in upper case, this function should     *         not create a new copy.     */    private String toLowerCase(String language) {	boolean isTurkish = language.equals("tr");	char[] buf = new char[value.length];	if (isTurkish) {	    for (int i = 0; i < value.length; i++)		if (value[i] == '\u0130')		    buf[i] = '\u0069';		else if (value[i] == '\u0049')		    buf[i] = '\u0131';		else		    buf[i] = Character.toLowerCase(value[i]);	} else {	    for (int i = 0; i < value.length; i++)		buf[i] = Character.toLowerCase(value[i]);	}	return new String(buf, buf);    }    /*     * This function is structured so that case conversion still     * works even when the Locale class is in the process of being     * initialized.     */    public String toUpperCase() {	Locale locale = Locale.getDefault();	if (locale != null)	    return toUpperCase(locale);	else	    return toUpperCase(System.getProperty("user.language"));    }    public String toUpperCase(Locale locale) {	return toUpperCase(locale.getLanguage());    }    /*     * fix me: If string is already in upper case, this function should     *         not create a new copy.     */    private String toUpperCase(String language) {	boolean isTurkish = language.equals("tr");	StringBuffer buf = new StringBuffer();	if (isTurkish) {	    for (int i = 0; i < value.length; i++)		if (value[i] == '\u0069') {		    buf.append('\u0130');		} else if (value[i] == '\u0131') {		    buf.append('\u0049');		} else if (value[i] == '\u00df') {		    buf.append('\u0053');		    buf.append('\u0053');		} else {		    buf.append(Character.toUpperCase(value[i]));		}	} else {	    for (int i = 0; i < value.length; i++)		if (value[i] == '\u00df') {		    buf.append('\u0053');		    buf.append('\u0053');		} else {		    buf.append(Character.toUpperCase(value[i]));		}	}	return new String(buf);    }    public String trim() {	int i, j;	for (i = 0; i < value.length; i++)	    if (value[i] > '\u0020')		break;	for (j = value.length; j > i; j--)	    if (value[j - 1] > '\u0020')		break;	int n = j - i;	char[] buf = new char[n];	for (int k = 0; k < n; k++)	    buf[k] = value[i + k];	return new String(buf, buf);    }    public String toString() {	return this;    }    public char[] toCharArray() {	char[] result = new char[value.length];	for (int i = 0; i < value.length; i++)	    result[i] = value[i];	return result;    }    public static String valueOf(Object obj) {	return (obj == null) ? "null" : obj.toString();    }    public static String valueOf(char[] data) {	return new String(data);    }    public static String valueOf(char[] data, int offset, int count) {	return new String(data, offset, count);    }    public static String copyValueOf(char[] data, int offset, int count) {	return new String(data, offset, count);    }    public static String copyValueOf(char[] data) {	return new String(data);    }    public static String valueOf(boolean b) {	return b ? "true" : "false";    }    public static String valueOf(char c) {	char[] buf = new char[1];	buf[0] = c;	return new String(buf, buf);    }    public static String valueOf(int i) {	return Integer.toString(i);    }    public static String valueOf(long l) {	return Long.toString(l);    }    public static String valueOf(float f) {	return Float.toString(f);    }    public static String valueOf(double d) {	return Double.toString(d);    }    public String intern() {	return stringPool.put(this);    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -