mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8200377: String::strip, String::stripLeading, String::stripTrailing
Reviewed-by: sundar, rriggs
This commit is contained in:
parent
ec2d9845e0
commit
7906014509
4 changed files with 300 additions and 1 deletions
|
@ -2602,7 +2602,7 @@ public final class String
|
|||
* Returns a string whose value is this string, with all leading
|
||||
* and trailing space removed, where space is defined
|
||||
* as any character whose codepoint is less than or equal to
|
||||
* {@code '\u005Cu0020'} (the space character).
|
||||
* {@code 'U+0020'} (the space character).
|
||||
* <p>
|
||||
* If this {@code String} object represents an empty character
|
||||
* sequence, or the first and last characters of character sequence
|
||||
|
@ -2636,6 +2636,98 @@ public final class String
|
|||
return ret == null ? this : ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string whose value is this string, with all leading
|
||||
* and trailing {@link Character#isWhitespace(int) white space}
|
||||
* removed.
|
||||
* <p>
|
||||
* If this {@code String} object represents an empty string,
|
||||
* or if all code points in this string are
|
||||
* {@link Character#isWhitespace(int) white space}, then an empty string
|
||||
* is returned.
|
||||
* <p>
|
||||
* Otherwise, returns a substring of this string beginning with the first
|
||||
* code point that is not a {@link Character#isWhitespace(int) white space}
|
||||
* up to and including the last code point that is not a
|
||||
* {@link Character#isWhitespace(int) white space}.
|
||||
* <p>
|
||||
* This method may be used to strip
|
||||
* {@link Character#isWhitespace(int) white space} from
|
||||
* the beginning and end of a string.
|
||||
*
|
||||
* @return a string whose value is this string, with all leading
|
||||
* and trailing white space removed
|
||||
*
|
||||
* @see Character#isWhitespace(int)
|
||||
*
|
||||
* @since 11
|
||||
*/
|
||||
public String strip() {
|
||||
String ret = isLatin1() ? StringLatin1.strip(value)
|
||||
: StringUTF16.strip(value);
|
||||
return ret == null ? this : ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string whose value is this string, with all leading
|
||||
* {@link Character#isWhitespace(int) white space} removed.
|
||||
* <p>
|
||||
* If this {@code String} object represents an empty string,
|
||||
* or if all code points in this string are
|
||||
* {@link Character#isWhitespace(int) white space}, then an empty string
|
||||
* is returned.
|
||||
* <p>
|
||||
* Otherwise, returns a substring of this string beginning with the first
|
||||
* code point that is not a {@link Character#isWhitespace(int) white space}
|
||||
* up to to and including the last code point of this string.
|
||||
* <p>
|
||||
* This method may be used to trim
|
||||
* {@link Character#isWhitespace(int) white space} from
|
||||
* the beginning of a string.
|
||||
*
|
||||
* @return a string whose value is this string, with all leading white
|
||||
* space removed
|
||||
*
|
||||
* @see Character#isWhitespace(int)
|
||||
*
|
||||
* @since 11
|
||||
*/
|
||||
public String stripLeading() {
|
||||
String ret = isLatin1() ? StringLatin1.stripLeading(value)
|
||||
: StringUTF16.stripLeading(value);
|
||||
return ret == null ? this : ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string whose value is this string, with all trailing
|
||||
* {@link Character#isWhitespace(int) white space} removed.
|
||||
* <p>
|
||||
* If this {@code String} object represents an empty string,
|
||||
* or if all characters in this string are
|
||||
* {@link Character#isWhitespace(int) white space}, then an empty string
|
||||
* is returned.
|
||||
* <p>
|
||||
* Otherwise, returns a substring of this string beginning with the first
|
||||
* code point of this string up to and including the last code point
|
||||
* that is not a {@link Character#isWhitespace(int) white space}.
|
||||
* <p>
|
||||
* This method may be used to trim
|
||||
* {@link Character#isWhitespace(int) white space} from
|
||||
* the end of a string.
|
||||
*
|
||||
* @return a string whose value is this string, with all trailing white
|
||||
* space removed
|
||||
*
|
||||
* @see Character#isWhitespace(int)
|
||||
*
|
||||
* @since 11
|
||||
*/
|
||||
public String stripTrailing() {
|
||||
String ret = isLatin1() ? StringLatin1.stripTrailing(value)
|
||||
: StringUTF16.stripTrailing(value);
|
||||
return ret == null ? this : ret;
|
||||
}
|
||||
|
||||
/**
|
||||
* This object (which is already a string!) is itself returned.
|
||||
*
|
||||
|
|
|
@ -538,6 +538,57 @@ final class StringLatin1 {
|
|||
newString(value, st, len - st) : null;
|
||||
}
|
||||
|
||||
public static int indexOfNonWhitespace(byte[] value) {
|
||||
int length = value.length;
|
||||
int left = 0;
|
||||
while (left < length) {
|
||||
char ch = (char)(value[left] & 0xff);
|
||||
if (ch != ' ' && ch != '\t' && !Character.isWhitespace(ch)) {
|
||||
break;
|
||||
}
|
||||
left++;
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
public static int lastIndexOfNonWhitespace(byte[] value) {
|
||||
int length = value.length;
|
||||
int right = length;
|
||||
while (0 < right) {
|
||||
char ch = (char)(value[right - 1] & 0xff);
|
||||
if (ch != ' ' && ch != '\t' && !Character.isWhitespace(ch)) {
|
||||
break;
|
||||
}
|
||||
right--;
|
||||
}
|
||||
return right;
|
||||
}
|
||||
|
||||
public static String strip(byte[] value) {
|
||||
int left = indexOfNonWhitespace(value);
|
||||
if (left == value.length) {
|
||||
return "";
|
||||
}
|
||||
int right = lastIndexOfNonWhitespace(value);
|
||||
return ((left > 0) || (right < value.length)) ? newString(value, left, right - left) : null;
|
||||
}
|
||||
|
||||
public static String stripLeading(byte[] value) {
|
||||
int left = indexOfNonWhitespace(value);
|
||||
if (left == value.length) {
|
||||
return "";
|
||||
}
|
||||
return (left != 0) ? newString(value, left, value.length - left) : null;
|
||||
}
|
||||
|
||||
public static String stripTrailing(byte[] value) {
|
||||
int right = lastIndexOfNonWhitespace(value);
|
||||
if (right == 0) {
|
||||
return "";
|
||||
}
|
||||
return (right != value.length) ? newString(value, 0, right) : null;
|
||||
}
|
||||
|
||||
public static void putChar(byte[] val, int index, int c) {
|
||||
//assert (canEncode(c));
|
||||
val[index] = (byte)(c);
|
||||
|
|
|
@ -856,6 +856,61 @@ final class StringUTF16 {
|
|||
null;
|
||||
}
|
||||
|
||||
|
||||
public static int indexOfNonWhitespace(byte[] value) {
|
||||
int length = value.length >> 1;
|
||||
int left = 0;
|
||||
while (left < length) {
|
||||
int codepoint = codePointAt(value, left, length);
|
||||
if (codepoint != ' ' && codepoint != '\t' && !Character.isWhitespace(codepoint)) {
|
||||
break;
|
||||
}
|
||||
left += Character.charCount(codepoint);
|
||||
}
|
||||
return left;
|
||||
}
|
||||
|
||||
public static int lastIndexOfNonWhitespace(byte[] value) {
|
||||
int length = value.length >> 1;
|
||||
int right = length;
|
||||
while (0 < right) {
|
||||
int codepoint = codePointBefore(value, right);
|
||||
if (codepoint != ' ' && codepoint != '\t' && !Character.isWhitespace(codepoint)) {
|
||||
break;
|
||||
}
|
||||
right -= Character.charCount(codepoint);
|
||||
}
|
||||
return right;
|
||||
}
|
||||
|
||||
public static String strip(byte[] value) {
|
||||
int length = value.length >> 1;
|
||||
int left = indexOfNonWhitespace(value);
|
||||
if (left == length) {
|
||||
return "";
|
||||
}
|
||||
int right = lastIndexOfNonWhitespace(value);
|
||||
return ((left > 0) || (right < length)) ? newString(value, left, right - left) : null;
|
||||
}
|
||||
|
||||
public static String stripLeading(byte[] value) {
|
||||
int length = value.length >> 1;
|
||||
int left = indexOfNonWhitespace(value);
|
||||
if (left == length) {
|
||||
return "";
|
||||
}
|
||||
return (left != 0) ? newString(value, left, length - left) : null;
|
||||
}
|
||||
|
||||
public static String stripTrailing(byte[] value) {
|
||||
int length = value.length >> 1;
|
||||
int right = lastIndexOfNonWhitespace(value);
|
||||
if (right == 0) {
|
||||
return "";
|
||||
}
|
||||
return (right != length) ? newString(value, 0, right) : null;
|
||||
}
|
||||
|
||||
private static void putChars(byte[] val, int index, char[] str, int off, int end) {
|
||||
while (off < end) {
|
||||
putChar(val, index++, str[off++]);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue