mirror of
https://github.com/openjdk/jdk.git
synced 2025-08-27 14:54:52 +02:00
8223775: String::stripIndent (Preview)
Reviewed-by: abuckley, vromero, jlahoda, bchristi, rriggs, smarks
This commit is contained in:
parent
618afc3fa2
commit
0777a86d68
4 changed files with 185 additions and 148 deletions
|
@ -1119,76 +1119,10 @@ final class StringUTF16 {
|
|||
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, 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);
|
||||
}
|
||||
static Stream<String> lines(byte[] value) {
|
||||
return StreamSupport.stream(LinesSpliterator.spliterator(value), 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