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

📄 string.java

📁 linux下建立JAVA虚拟机的源码KAFFE
💻 JAVA
📖 第 1 页 / 共 2 页
字号:
/* * Java core library component. * * Copyright (c) 1997, 1998 *      Transvirtual Technologies, Inc.  All rights reserved. * * See the file "license.terms" for information on usage and redistribution * of this file. */package java.lang;import java.io.ByteArrayOutputStream;import java.io.CharConversionException;import java.io.Serializable;import java.io.UnsupportedEncodingException;import java.nio.charset.Charset;import java.nio.charset.CharsetEncoder;import java.nio.charset.CharsetDecoder;import java.nio.charset.CodingErrorAction;import java.nio.charset.CharacterCodingException;import java.nio.charset.IllegalCharsetNameException;import java.nio.charset.UnsupportedCharsetException;import java.nio.ByteBuffer;import java.nio.CharBuffer;import java.util.Comparator;import java.util.Locale;import java.util.regex.Pattern;public final class String implements Serializable, Comparable, CharSequence {	/**	 * Maximum slop (extra unused chars in the char[]) that	 * will be accepted in a StringBuffer -> String conversion.	 * This helps avoid certain pathological cases where lots of	 * extra buffer space is maintained for short strings that	 * were created from StringBuffer objects.	 */	static final int STRINGBUFFER_SLOP = 32;	// Note: value, offset, and count are not private, because	// StringBuffer uses them for faster access	final char[] value;	final int offset;	final int count;	private boolean interned;	private int hash;	/* This is what Sun's JDK1.1 "serialver java.lang.String" spits out */	private static final long serialVersionUID = -6849794470754667710L;	public static final Comparator CASE_INSENSITIVE_ORDER = new ICComp();	private static class ICComp implements Comparator, Serializable {	  public int compare(Object s1, Object s2) {	    return ((String)s1).compareToIgnoreCase((String)s2);	  }	}public String() {	value = new char[0];	offset = 0;	count = 0;}public String(String other) {	value = other.value;	offset = other.offset;	count = other.count;	hash = other.hash;}public String(StringBuffer sb) {	synchronized (sb) {		if (sb.value.length > sb.count + STRINGBUFFER_SLOP) {			value = new char[sb.count];			offset = 0;			count = sb.count;			sb.getChars(0, count, value, 0);		}		else {			value = sb.value;			offset = 0;			count = sb.count;			sb.shared = true;		}	}}public String(byte[] bytes) {	this(bytes, 0, bytes.length);}/* taken from GNU Classpath */  public String(byte[] data, String encoding)    throws UnsupportedEncodingException  {    this(data, 0, data.length, encoding);  }/** * @deprecated */public String(byte ascii[], int hibyte) {	this(ascii, hibyte, 0, ascii.length);}/* taken from GNU Claspath */  public String(byte[] data, int offset, int count)  {    if (offset < 0 || count < 0 || offset + count > data.length)      throw new StringIndexOutOfBoundsException();    int o, c;    char[] v;    String encoding;    try 	{	  encoding = System.getProperty("file.encoding");	  CharsetDecoder csd = Charset.forName(encoding).newDecoder();	  csd.onMalformedInput(CodingErrorAction.REPLACE);	  csd.onUnmappableCharacter(CodingErrorAction.REPLACE);	  CharBuffer cbuf = csd.decode(ByteBuffer.wrap(data, offset, count));	  if(cbuf.hasArray())	    {              v = cbuf.array();	      o = cbuf.position();	      c = cbuf.remaining();	    } else {	      // Doubt this will happen. But just in case.	      v = new char[cbuf.remaining()];	      cbuf.get(v);	      o = 0;	      c = v.length;	    }	} catch(Exception ex){	    // If anything goes wrong (System property not set,	    // NIO provider not available, etc)	    // Default to the 'safe' encoding ISO8859_1	    v = new char[count];	    o = 0;	    c = count;	    for (int i=0;i<count;i++)	      v[i] = (char)data[offset+i];	}    this.value = v;    this.offset = o;    this.count = c;  }/* taken from GNU Claspath */  public String(byte[] data, int offset, int count, String encoding)    throws UnsupportedEncodingException  {    if (offset < 0 || count < 0 || offset + count > data.length)      throw new StringIndexOutOfBoundsException();    try       {        CharsetDecoder csd = Charset.forName(encoding).newDecoder();	csd.onMalformedInput(CodingErrorAction.REPLACE);	csd.onUnmappableCharacter(CodingErrorAction.REPLACE);	CharBuffer cbuf = csd.decode(ByteBuffer.wrap(data, offset, count)); 	if(cbuf.hasArray()) 	  { 	    value = cbuf.array();	    this.offset = cbuf.position();	    this.count = cbuf.remaining(); 	  } else {	    // Doubt this will happen. But just in case.	    value = new char[cbuf.remaining()];	    cbuf.get(value);	    this.offset = 0;	    this.count = value.length;	  }      } catch(CharacterCodingException e){	  throw new UnsupportedEncodingException("Encoding: "+encoding+						 " not found.");	        } catch(IllegalCharsetNameException e){	  throw new UnsupportedEncodingException("Encoding: "+encoding+						 " not found.");      } catch(UnsupportedCharsetException e){	  throw new UnsupportedEncodingException("Encoding: "+encoding+						 " not found.");      }      }/** * @deprecated */public String( byte ascii[], int hibyte, int offset, int count) {	// Test needed to conform to the spec. - TIM	if (ascii == null) {		throw new NullPointerException();	}	value = new char[count];	this.offset = 0;	this.count = count;	hibyte = (hibyte & 0xFF) << 8;	for (int pos = 0; pos < count; pos++) {		value[pos]=(char)(hibyte | (ascii[pos+offset] & 0xFF));		}}public String( char other[]) {	this( other, 0, other.length);}public String( char other[], int offset, int count) {	if (count < 0)		throw new StringIndexOutOfBoundsException();	value = new char[count];	this.offset = 0;	this.count = count;    	System.arraycopy( other, offset, value, 0, count);}String(int sIdx, int eIdx, char[] val) {	value  = val;	offset = sIdx;	count  = eIdx - sIdx;}public char charAt ( int index ) {	if (( index < 0) || ( index >= count))		throw new StringIndexOutOfBoundsException("index = "+index+", length="+count);	return value[offset+index];}public int compareTo(Object o) {	return compareTo((String)o);}public int compareTo( String s1) {	/* lexicographical comparison, assume they mean English lexiographical, since Character has no ordering */	int minLen = Math.min( count, s1.count);	char c, c1;	for ( int pos=0; pos<minLen; pos++) {		/* Can we guarantee that the Unicode '<' relation means also numerically '<'.. Probably, but just incase */		c = value[offset+pos]; c1 = s1.value[s1.offset+pos];		if ( c != c1) return ( c-c1);	}	/* Both equal up to min length, therefore check lengths for lexiographical ordering */	return ( count - s1.count);}public int compareToIgnoreCase(String that) {	return this.toUpperCase().toLowerCase().compareTo(		that.toUpperCase().toLowerCase());}public String concat(String str) {	if (str.count == 0) {		return (this);	}	char buf[] = new char[count + str.count];	getChars(0, count, buf, 0);	str.getChars(0, str.count, buf, count);	return (new String(0, buf.length, buf));}public static String copyValueOf( char data[]) {	return copyValueOf( data, 0, data.length);}public static String copyValueOf(char data[], int offset, int count) {	if (offset < 0 || count < 0 || offset + count > data.length) {		throw new IndexOutOfBoundsException();	}	char buf[]=new char[count];	if ( count > 0) {		System.arraycopy( data, offset, buf, 0, count);	}	return (new String( 0, count, buf));}public boolean endsWith( String suffix) {	return regionMatches( false, count-suffix.count, suffix, 0, suffix.count);}// This is one of the most frequently called methods; it must be as// efficient as possible.public boolean equals (Object anObject) {	if (anObject == this) {		return (true);	}	if (!(anObject instanceof String)) {		return (false);	}	final String that = (String)anObject;	if (this.count != that.count) {		return (false);	}	int i = this.offset;	int j = that.offset;	final int n = i + this.count;	final char[] this_value = this.value;	final char[] that_value = that.value;	for (; i < n; i++, j++) {		if (this_value[i] != that_value[j] ) {			return (false);		}	}	return (true);}// This also should be as fast as possiblepublic boolean equalsIgnoreCase (String that) {	if (that == null || this.count != that.count) {		return (false);	}	int i = this.offset;	int j = that.offset;	final int n = i + this.count;	final char[] this_value = this.value;	final char[] that_value = that.value;	for (; i < n; i++, j++) {		if (this_value[i] != that_value[j]		   && Character.toUpperCase(this_value[i])		     != Character.toUpperCase(that_value[j])) {			return (false);		}	}	return (true);}    /* Taken from GNU Classpath */  public byte[] getBytes()  {       try 	  {	      return getBytes(System.getProperty("file.encoding"));	  } catch(Exception e) {	      // XXX - Throw an error here? 	      // For now, default to the 'safe' encoding.	      byte[] bytes = new byte[count];	      for(int i=0;i<count;i++)		  bytes[i] = (byte)((value[offset+i] <= 0xFF)?				    value[offset+i]:'?');	      return bytes;      }  }    /* Taken from GNU Classpath */  public byte[] getBytes(String enc) throws UnsupportedEncodingException  {    try       {	CharsetEncoder cse = Charset.forName(enc).newEncoder();	cse.onMalformedInput(CodingErrorAction.REPLACE);	cse.onUnmappableCharacter(CodingErrorAction.REPLACE);	ByteBuffer bbuf = cse.encode(CharBuffer.wrap(value, offset, count));	if(bbuf.hasArray())	  return bbuf.array();		// Doubt this will happen. But just in case.	byte[] bytes = new byte[bbuf.remaining()];	bbuf.get(bytes);	return bytes;      }     catch(IllegalCharsetNameException e)      {	throw new UnsupportedEncodingException("Encoding: " + enc					       + " not found.");      }     catch(UnsupportedCharsetException e)      {	throw new UnsupportedEncodingException("Encoding: " + enc					       + " not found.");      }     catch(CharacterCodingException e)      {	// This shouldn't ever happen.	throw (InternalError) new InternalError().initCause(e);      }	    }/** * @deprecated */public void getBytes( int srcBegin, int srcEnd, byte dst[], int dstBegin) {	if (srcBegin < 0 	    || srcBegin > srcEnd	    ||  dstBegin < 0	    ||  dstBegin + (srcEnd - srcBegin) >  dst.length	    || srcEnd > offset + count) {		throw new IndexOutOfBoundsException("");	}	int len = srcEnd-srcBegin;	for (int pos = 0; pos < len; pos++) {		dst[dstBegin+pos] = (byte)value[offset+srcBegin+pos];	}}public void getChars(int srcBegin, int srcEnd, char dst[], int dstBegin) {	System.arraycopy( value, offset+srcBegin, dst, dstBegin, srcEnd-srcBegin);}public int hashCode() {	if (hash == 0 && count > 0) {		int tempHash = 0;		final int stop = offset + count;		for (int index = offset; index < stop; index++) {			tempHash = (31 * tempHash) + value[index];		}		hash = tempHash;	// race condition here is ok	}	return hash;}public int indexOf( String str) {	return indexOf( str, 0);}native public int indexOf( String str, int sIdx);public int indexOf( int ch) {	return indexOf( ch, 0);}public int indexOf( int ch, int sIdx) {	char c = (char)ch;	if (sIdx < 0) { // calling indexOf with sIdx < 0 is apparently okay		sIdx = 0;	}	for (int pos=sIdx; pos<count; pos++) {		if ( value[offset+pos] == c )			return pos;	}	return -1;}public int lastIndexOf( String str) {	return lastIndexOf( str, count);}public int lastIndexOf( String str, int eIdx) {	int ic = str.offset+str.count-1;	int it = offset+eIdx+str.count-1;	int ma = 0;	if (it >= offset+count) {       // clip index		it = offset+count-1;	}	if (str.count == 0) {		return (eIdx < 0) ? -1 : (eIdx < count) ? eIdx : count;	}	for ( ; it>=offset; it--) {		if ( value[it] == str.value[ic] ) {			ic--;			if ( ++ma == str.count) {				return (it-offset);			}		}		else if (ma > 0) {			it++;			ma = 0;			ic = str.offset+str.count-1;		}	}	return -1;}public int lastIndexOf(int ch) {	return lastIndexOf( ch, count-1);}public int lastIndexOf(int ch, int eIdx) {	final char c = (char)ch;	/* If the character is out of range we'll never find it */	if ((int)c != ch) {		return (-1);	}	/* Clip the index to be within the valid range (if non-empty) */	if (eIdx >= count) {		eIdx = count - 1;	}	if (eIdx < 0) {		return(-1);	}	/* Search for character */	for (int pos = eIdx; pos >= 0; pos--) {		if ( value[offset+pos] == c) {			return (pos);		}	}	return (-1);}public int length() {	return count;}public boolean regionMatches(boolean ignoreCase, int thisOffset,		String that, int thatOffset, int len) {	// Check bounds	if ((thisOffset < 0 || thisOffset + len > this.count)	    || (thatOffset < 0 || thatOffset + len > that.count)) {		return false;	}	int thisPos = this.offset + thisOffset;	int thatPos = that.offset + thatOffset;

⌨️ 快捷键说明

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