string.java
来自「纯java操作系统jnode,安装简单和操作简单的个人使用的Java操作系统」· Java 代码 · 共 1,585 行 · 第 1/4 页
JAVA
1,585 行
// Now we perform the conversion. Fortunately, there are no multi-character
// lowercase expansions in Unicode 3.0.0.
char[] newStr = (char[]) value.clone();
do {
char ch = value[x];
// Hardcoded special case.
newStr[x++] = (turkish && ch == '\u0049') ? '\u0131' : Character.toLowerCase(ch);
} while (--i >= 0);
// Package constructor avoids an array copy.
return new String(newStr, offset, count, true);
}
/**
* Lowercases this String. This uses Unicode's special case mappings, as
* applied to the platform's default Locale, so the resulting string may
* be a different length.
*
* @return new lowercased String, or this if no characters were lowercased
* @see #toLowerCase(Locale)
* @see #toUpperCase()
*/
public String toLowerCase() {
return toLowerCase(Locale.getDefault());
}
/**
* Uppercases this String according to a particular locale. This uses
* Unicode's special case mappings, as applied to the given Locale, so the
* resulting string may be a different length.
*
* @param loc locale to use
* @return new uppercased String, or this if no characters were uppercased
* @throws NullPointerException if loc is null
* @see #toLowerCase(Locale)
* @since 1.1
*/
public String toUpperCase(Locale loc) {
// First, see how many characters we have to grow by, as well as if the
// current string is already upper case.
boolean turkish = "tr".equals(loc.getLanguage());
int expand = 0;
boolean unchanged = true;
int i = count;
int x = i + offset;
while (--i >= 0) {
char ch = value[--x];
expand += upperCaseExpansion(ch);
unchanged = (unchanged && expand == 0 && !(turkish && ch == '\u0069') && ch == Character.toUpperCase(ch));
}
if (unchanged)
return this;
// Now we perform the conversion.
i = count;
if (expand == 0) {
char[] newStr = (char[]) value.clone();
while (--i >= 0) {
char ch = value[x];
// Hardcoded special case.
newStr[x++] = (turkish && ch == '\u0069') ? '\u0130' : Character.toUpperCase(ch);
}
// Package constructor avoids an array copy.
return new String(newStr, offset, count, true);
}
// Expansion is necessary.
char[] newStr = new char[count + expand];
char[] upperExpand = getUpperExpand();
int j = 0;
while (--i >= 0) {
char ch = value[x++];
// Hardcoded special case.
if (turkish && ch == '\u0069') {
newStr[j++] = '\u0130';
continue;
}
expand = upperCaseExpansion(ch);
if (expand > 0) {
int index = upperCaseIndex(ch);
while (expand-- >= 0)
newStr[j++] = upperExpand[index++];
} else
newStr[j++] = Character.toUpperCase(ch);
}
// Package constructor avoids an array copy.
return new String(newStr, 0, newStr.length, true);
}
/**
* Uppercases this String. This uses Unicode's special case mappings, as
* applied to the platform's default Locale, so the resulting string may
* be a different length.
*
* @return new uppercased String, or this if no characters were uppercased
* @see #toUpperCase(Locale)
* @see #toLowerCase()
*/
public String toUpperCase() {
return toUpperCase(Locale.getDefault());
}
/**
* Trims all characters less than or equal to <code>'\u0020'</code>
* (<code>' '</code>) from the beginning and end of this String. This
* includes many, but not all, ASCII control characters, and all
* {@link Character#whitespace(char)}.
*
* @return new trimmed String, or this if nothing trimmed
*/
public String trim() {
int limit = count + offset;
if (count == 0 || (value[offset] > '\u0020' && value[limit - 1] > '\u0020'))
return this;
int begin = offset;
do if (begin == limit)
return "";
while (value[begin++] <= '\u0020');
int end = limit;
while (value[--end] <= '\u0020');
return substring(begin - offset - 1, end - offset + 1);
}
/**
* Returns this, as it is already a String!
*
* @return this
*/
public String toString() {
return this;
}
/**
* Copies the contents of this String into a character array. Subsequent
* changes to the array do not affect the String.
*
* @return character array copying the String
*/
public char[] toCharArray() {
// XXX ORP 1.0.9 crashes on (char[]) clone() during bootstrap, so we
// omit this optimization for now.
// if (count == value.length)
// return (char[]) value.clone();
char[] copy = new char[count];
System.arraycopy(value, offset, copy, 0, count);
return copy;
}
/**
* Returns a String representation of an Object. This is "null" if the
* object is null, otherwise it is <code>obj.toString()</code> (which
* can be null).
*
* @param obj the Object
* @return the string conversion of obj
*/
public static String valueOf(Object obj) {
return obj == null ? "null" : obj.toString();
}
/**
* Returns a String representation of a character array. Subsequent
* changes to the array do not affect the String.
*
* @param data the character array
* @return a String containing the same character sequence as data
* @throws NullPointerException if data is null
* @see #valueOf(char[], int, int)
* @see #String(char[])
*/
public static String valueOf(char[] data) {
return new String(data, 0, data.length, false);
}
/**
* Returns a String representing the character sequence of the char array,
* starting at the specified offset, and copying chars up to the specified
* count. Subsequent changes to the array do not affect the String.
*
* @param data character array
* @param offset position (base 0) to start copying out of data
* @param count the number of characters from data to copy
* @return String containing the chars from data[offset..offset+count]
* @throws NullPointerException if data is null
* @throws IndexOutOfBoundsException if (offset < 0 || count < 0
* || offset + count > data.length)
* (while unspecified, this is a StringIndexOutOfBoundsException)
* @see #String(char[], int, int)
*/
public static String valueOf(char[] data, int offset, int count) {
return new String(data, offset, count, false);
}
/**
* Returns a String representing the character sequence of the char array,
* starting at the specified offset, and copying chars up to the specified
* count. Subsequent changes to the array do not affect the String.
*
* @param data character array
* @param offset position (base 0) to start copying out of data
* @param count the number of characters from data to copy
* @return String containing the chars from data[offset..offset+count]
* @throws NullPointerException if data is null
* @throws IndexOutOfBoundsException if (offset < 0 || count < 0
* || offset + count > data.length)
* (while unspecified, this is a StringIndexOutOfBoundsException)
* @see #String(char[], int, int)
*/
public static String copyValueOf(char[] data, int offset, int count) {
return new String(data, offset, count, false);
}
/**
* Returns a String representation of a character array. Subsequent
* changes to the array do not affect the String.
*
* @param data the character array
* @return a String containing the same character sequence as data
* @throws NullPointerException if data is null
* @see #copyValueOf(char[], int, int)
* @see #String(char[])
*/
public static String copyValueOf(char[] data) {
return new String(data, 0, data.length, false);
}
/**
* Returns a String representing a boolean.
*
* @param b the boolean
* @return "true" if b is true, else "false"
*/
public static String valueOf(boolean b) {
return b ? "true" : "false";
}
/**
* Returns a String representing a character.
*
* @param c the character
* @return String containing the single character c
*/
public static String valueOf(char c) {
// Package constructor avoids an array copy.
return new String(new char[] { c }, 0, 1, true);
}
/**
* Returns a String representing an integer.
*
* @param i the integer
* @return String containing the integer in base 10
* @see Integer#toString(int)
*/
public static String valueOf(int i) {
// See Integer to understand why we call the two-arg variant.
return Integer.toString(i, 10);
}
/**
* Returns a String representing a long.
*
* @param l the long
* @return String containing the long in base 10
* @see Long#toString(long)
*/
public static String valueOf(long l) {
return Long.toString(l);
}
/**
* Returns a String representing a float.
*
* @param f the float
* @return String containing the float
* @see Float#toString(float)
*/
public static String valueOf(float f) {
return Float.toString(f);
}
/**
* Returns a String representing a double.
*
* @param d the double
* @return String containing the double
* @see Double#toString(double)
*/
public static String valueOf(double d) {
return Double.toString(d);
}
/**
* Fetches this String from the intern hashtable. If two Strings are
* considered equal, by the equals() method, then intern() will return the
* same String instance. ie. if (s1.equals(s2)) then
* (s1.intern() == s2.intern()). All string literals and string-valued
* constant expressions are already interned.
*
* @return the interned String
*/
public String intern() {
if (internTable == null) {
synchronized (getClass()) {
if (internTable == null) {
internTable = new WeakHashMap();
}
}
}
synchronized (internTable) {
WeakReference ref = (WeakReference) internTable.get(this);
if (ref != null) {
String s = (String) ref.get();
// If s is null, then no strong references exist to the String;
// the weak hash map will soon delete the key.
if (s != null)
return s;
}
internTable.put(this, new WeakReference(this));
}
return this;
}
/**
* Helper function used to detect which characters have a multi-character
* uppercase expansion. Note that this is only used in locations which
* track one-to-many capitalization (java.lang.Character does not do this).
* As of Unicode 3.0.0, the result is limited in the range 0 to 2, as the
* longest uppercase expansion is three characters (a growth of 2 from the
* lowercase character).
*
* @param ch the char to check
* @return the number of characters to add when converting to uppercase
* @see CharData#DIRECTION
* @see CharData#UPPER_SPECIAL
* @see #toUpperCase(Locale)
*/
private static int upperCaseExpansion(char ch) {
return Character.direction[Character.readChar(ch) >> 7] & 3;
}
/**
* Helper function used to locate the offset in upperExpand given a
* character with a multi-character expansion. The binary search is
* optimized under the assumption that this method will only be called on
* characters which exist in upperSpecial.
*
* @param ch the char to check
* @return the index where its expansion begins
* @see CharData#UPPER_SPECIAL
* @see CharData#UPPER_EXPAND
* @see #toUpperCase(Locale)
*/
private static int upperCaseIndex(char ch) {
// Simple binary search for the correct character.
int low = 0;
char[] upperSpecial = getUpperSpecial();
int hi = upperSpecial.length - 2;
int mid = ((low + hi) >> 2) << 1;
char c = upperSpecial[mid];
while (ch != c) {
if (ch < c)
hi = mid - 2;
else
low = mid + 2;
mid = ((low + hi) >> 2) << 1;
c = upperSpecial[mid];
}
return upperSpecial[mid + 1];
}
/**
* Returns the value array of the given string if it is zero based or a
* copy of it that is zero based (stripping offset and making length equal
* to count). Used for accessing the char[]s of gnu.java.lang.CharData.
* Package private for use in Character.
*/
static char[] zeroBasedStringValue(String s) {
char[] value;
if (s.offset == 0 && s.count == s.value.length)
value = s.value;
else {
int count = s.count;
value = new char[count];
System.arraycopy(s.value, s.offset, value, 0, count);
}
return value;
}
} // class String
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?