string.java
来自「kaffe Java 解释器语言,源码,Java的子集系统,开放源代码」· Java 代码 · 共 780 行 · 第 1/2 页
JAVA
780 行
/* * 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.Serializable;import java.io.UnsupportedEncodingException;import java.util.Comparator;import java.util.Locale;import java.util.regex.Pattern;import java.util.regex.PatternSyntaxException;import kaffe.io.ByteToCharConverter;import kaffe.io.CharToByteConverter;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(decodeBytes(bytes, 0, bytes.length, ByteToCharConverter.getDefault()));}public String(byte[] bytes, String enc) throws UnsupportedEncodingException { this(decodeBytes(bytes, 0, bytes.length, ByteToCharConverter.getConverter(enc)));}/** * @deprecated */public String(byte ascii[], int hibyte) { this(ascii, hibyte, 0, ascii.length);}public String(byte[] bytes, int offset, int length) { this(decodeBytes(bytes, offset, length, ByteToCharConverter.getDefault()));}public String(byte[] bytes, int offset, int length, String enc) throws UnsupportedEncodingException { this(decodeBytes(bytes, offset, length, ByteToCharConverter.getConverter(enc)));}/** * @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);}public byte[] getBytes() { return ( getBytes( CharToByteConverter.getDefault()));}private byte[] getBytes( CharToByteConverter encoding) { ByteArrayOutputStream out = new ByteArrayOutputStream( value.length); byte[] buf = new byte[value.length*7]; int buflen = encoding.convert( value, offset, count, buf, 0, buf.length); while (buflen > 0) { out.write(buf, 0, buflen); buflen = encoding.flush(buf, 0, buf.length); } return (out.toByteArray());}public byte[] getBytes( String enc) throws UnsupportedEncodingException{ return ( getBytes( CharToByteConverter.getConverter(enc)));}/** * @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;}private static StringBuffer decodeBytes(byte[] bytes, int offset, int len, ByteToCharConverter encoding) { StringBuffer sbuf = new StringBuffer(len); char[] out = new char[512]; int outlen = encoding.convert(bytes, offset, len, out, 0, out.length); while (outlen > 0) { sbuf.append(out, 0, outlen); outlen = encoding.flush(out, 0, out.length); } return sbuf;}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) {
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?