mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 06:45:07 +02:00
8200434
: String::align, String::indent
Reviewed-by: abuckley, smarks, sherman, rriggs, jrose, sundar, igerasim, briangoetz, darcy, jjg
This commit is contained in:
parent
2065ebd890
commit
12dad310bb
4 changed files with 541 additions and 34 deletions
|
@ -859,7 +859,6 @@ final class StringUTF16 {
|
|||
null;
|
||||
}
|
||||
|
||||
|
||||
public static int indexOfNonWhitespace(byte[] value) {
|
||||
int length = value.length >> 1;
|
||||
int left = 0;
|
||||
|
@ -874,7 +873,7 @@ final class StringUTF16 {
|
|||
}
|
||||
|
||||
public static int lastIndexOfNonWhitespace(byte[] value) {
|
||||
int length = value.length >> 1;
|
||||
int length = value.length >>> 1;
|
||||
int right = length;
|
||||
while (0 < right) {
|
||||
int codepoint = codePointBefore(value, right);
|
||||
|
@ -887,17 +886,18 @@ final class StringUTF16 {
|
|||
}
|
||||
|
||||
public static String strip(byte[] value) {
|
||||
int length = value.length >> 1;
|
||||
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;
|
||||
boolean ifChanged = (left > 0) || (right < length);
|
||||
return ifChanged ? newString(value, left, right - left) : null;
|
||||
}
|
||||
|
||||
public static String stripLeading(byte[] value) {
|
||||
int length = value.length >> 1;
|
||||
int length = value.length >>> 1;
|
||||
int left = indexOfNonWhitespace(value);
|
||||
if (left == length) {
|
||||
return "";
|
||||
|
@ -906,7 +906,7 @@ final class StringUTF16 {
|
|||
}
|
||||
|
||||
public static String stripTrailing(byte[] value) {
|
||||
int length = value.length >> 1;
|
||||
int length = value.length >>> 1;
|
||||
int right = lastIndexOfNonWhitespace(value);
|
||||
if (right == 0) {
|
||||
return "";
|
||||
|
@ -919,11 +919,7 @@ final class StringUTF16 {
|
|||
private int index; // current index, modified on advance/split
|
||||
private final int fence; // one past last index
|
||||
|
||||
LinesSpliterator(byte[] value) {
|
||||
this(value, 0, value.length >>> 1);
|
||||
}
|
||||
|
||||
LinesSpliterator(byte[] value, int start, int length) {
|
||||
private LinesSpliterator(byte[] value, int start, int length) {
|
||||
this.value = value;
|
||||
this.index = start;
|
||||
this.fence = start + length;
|
||||
|
@ -1002,10 +998,80 @@ final class StringUTF16 {
|
|||
public int characteristics() {
|
||||
return Spliterator.ORDERED | Spliterator.IMMUTABLE | Spliterator.NONNULL;
|
||||
}
|
||||
|
||||
static LinesSpliterator spliterator(byte[] value) {
|
||||
return new LinesSpliterator(value, 0, value.length >>> 1);
|
||||
}
|
||||
|
||||
static LinesSpliterator spliterator(byte[] value, int leading, int trailing) {
|
||||
int length = value.length >>> 1;
|
||||
int left = 0;
|
||||
int index;
|
||||
for (int l = 0; l < leading; l++) {
|
||||
index = skipBlankForward(value, left, length);
|
||||
if (index == left) {
|
||||
break;
|
||||
}
|
||||
left = index;
|
||||
}
|
||||
int right = length;
|
||||
for (int t = 0; t < trailing; t++) {
|
||||
index = skipBlankBackward(value, left, right);
|
||||
if (index == right) {
|
||||
break;
|
||||
}
|
||||
right = index;
|
||||
}
|
||||
return new LinesSpliterator(value, left, right - left);
|
||||
}
|
||||
|
||||
private static int skipBlankForward(byte[] value, int start, int length) {
|
||||
int index = start;
|
||||
while (index < length) {
|
||||
char ch = getChar(value, index++);
|
||||
if (ch == '\n') {
|
||||
return index;
|
||||
}
|
||||
if (ch == '\r') {
|
||||
if (index < length && getChar(value, index) == '\n') {
|
||||
return index + 1;
|
||||
}
|
||||
return index;
|
||||
}
|
||||
if (ch != ' ' && ch != '\t' && !Character.isWhitespace(ch)) {
|
||||
return start;
|
||||
}
|
||||
}
|
||||
return length;
|
||||
}
|
||||
|
||||
private static int skipBlankBackward(byte[] value, int start, int fence) {
|
||||
int index = fence;
|
||||
if (start < index && getChar(value, index - 1) == '\n') {
|
||||
index--;
|
||||
}
|
||||
if (start < index && getChar(value, index - 1) == '\r') {
|
||||
index--;
|
||||
}
|
||||
while (start < index) {
|
||||
char ch = getChar(value, --index);
|
||||
if (ch == '\r' || ch == '\n') {
|
||||
return index + 1;
|
||||
}
|
||||
if (ch != ' ' && ch != '\t' && !Character.isWhitespace(ch)) {
|
||||
return fence;
|
||||
}
|
||||
}
|
||||
return start;
|
||||
}
|
||||
}
|
||||
|
||||
static Stream<String> lines(byte[] value) {
|
||||
return StreamSupport.stream(new LinesSpliterator(value), false);
|
||||
static Stream<String> lines(byte[] value, int leading, int trailing) {
|
||||
if (leading == 0 && trailing == 0) {
|
||||
return StreamSupport.stream(LinesSpliterator.spliterator(value), false);
|
||||
} else {
|
||||
return StreamSupport.stream(LinesSpliterator.spliterator(value, leading, trailing), false);
|
||||
}
|
||||
}
|
||||
|
||||
private static void putChars(byte[] val, int index, char[] str, int off, int end) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue