Skip to content

Commit

Permalink
Align isWhitespace with Java 11, code cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
zbynek committed Jul 20, 2024
1 parent dcfbc17 commit 9232405
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 37 deletions.
5 changes: 0 additions & 5 deletions .idea/codeStyles/codeStyleConfig.xml

This file was deleted.

2 changes: 1 addition & 1 deletion user/super/com/google/gwt/emul/java/lang/Character.java
Original file line number Diff line number Diff line change
Expand Up @@ -322,7 +322,7 @@ private static boolean isWhitespace(String ch) {
// the Java definition includes separators.
whitespaceRegex =
new NativeRegExp(
"[\\u1680\\u180E\\u2000-\\u2006\\u2008-\\u200A\\u2028\\u2029\\u205F\\u3000\\uFEFF]"
"[\\u1680\\u2000-\\u2006\\u2008-\\u200A\\u2028\\u2029\\u205F\\u3000]"
+ "|[\\t-\\r ]"
+ "|[\\x1C-\\x1F]");
}
Expand Down
25 changes: 11 additions & 14 deletions user/super/com/google/gwt/emul/java/lang/String.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package java.lang;

import static javaemul.internal.InternalPreconditions.checkArgument;
import static javaemul.internal.InternalPreconditions.checkCriticalStringBounds;
import static javaemul.internal.InternalPreconditions.checkNotNull;
import static javaemul.internal.InternalPreconditions.checkStringBounds;
Expand Down Expand Up @@ -782,13 +783,11 @@ public boolean isBlank() {
}

public Stream<String> lines() {
return StreamSupport.stream(new LinesSpliterator(this), false);
return StreamSupport.stream(new LinesSpliterator(), false);
}

public String repeat(int count) {
if (count < 0) {
throw new IllegalArgumentException("count is negative: " + count);
}
checkArgument(count >= 0, "count is negative: " + count);
return asNativeString().repeat(count);
}

Expand All @@ -812,15 +811,13 @@ private int getTrailingWhitespaceLength() {
return length;
}

private static class LinesSpliterator extends Spliterators.AbstractSpliterator<String> {
private class LinesSpliterator extends Spliterators.AbstractSpliterator<String> {
private int nextIndex = 0;
private String content;
int rPosition = -2;
int nPosition = -2;
private int rPosition = -1;
private int nPosition = -1;

private LinesSpliterator(String content) {
private LinesSpliterator() {
super(Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED);
this.content = content;
}

@Override
Expand All @@ -832,17 +829,17 @@ public boolean tryAdvance(Consumer<? super String> action) {
nPosition = cappedIndexOf('\n');
}
int lineEnd = Math.min(nPosition, rPosition);
action.accept(content.substring(nextIndex, lineEnd));
action.accept(substring(nextIndex, lineEnd));
nextIndex = lineEnd + 1;
if (nPosition == rPosition + 1) {
nextIndex++;
}
return nextIndex < content.length();
return nextIndex < length();
}

private int cappedIndexOf(char c) {
int index = content.indexOf(c);
return index == -1 ? content.length() : index;
int index = indexOf(c);
return index == -1 ? length() : index;
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,8 @@ public void testIsWhitepace() {
char[] nonBreakingSpaceSeparators = {
'\u00A0', // NO-BREAK SPACE.
'\u2007', // FIGURE SPACE.
'\u202F' // NARROW NO-BREAK SPACE.
'\u202F', // NARROW NO-BREAK SPACE.
'\uFFEF' // ZERO WIDTH NO-BREAK SPACE.
};

char[] specialCases = {
Expand All @@ -379,7 +380,8 @@ public void testIsWhitepace() {
'a', // LATIN SMALL LETTER A.
'B', // LATIN CAPITAL LETTER B.
'_', // LOW LINE.
'\u2500' // BOX DRAWINGS LIGHT HORIZONTAL.
'\u2500', // BOX DRAWINGS LIGHT HORIZONTAL.
'\u180E', // MONGOLIAN VOWEL SEPARATOR, was considered whitespace in Java 8.
};

int[] supplementaryCounterExamples = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,7 @@ public void testTrim() {

// JavaScript would trim \u2029 and other unicode whitespace type characters; but Java wont
trimRightAssertEquals("\u2029abc\u00a0","\u2029abc\u00a0");
trimRightAssertEquals("\uffefx\u180e", "\uffefx\u180e ");
}

public void testUpperCase() {
Expand Down
43 changes: 28 additions & 15 deletions user/test/com/google/gwt/emultest/java11/lang/StringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,27 +33,40 @@ public void testIsBlank() {
}

public void testStrip() {
assertEquals("", "".strip());
assertEquals("", " ".strip());
assertEquals("x", " x ".strip());
assertEquals("x", "\u001cx\u001c".strip());
assertEquals("\u00a0x\u00a0", "\u00a0x\u00a0 ".strip());
stripRightAsssertEquals("", "");
stripRightAsssertEquals("", " ");
stripRightAsssertEquals("x", " x ");
stripRightAsssertEquals("x", "\u001cx\u001c");
stripRightAsssertEquals("\u00a0x\u00a0", "\u00a0x\u00a0 ");
stripRightAsssertEquals("\uffefx\u180e", "\uffefx\u180e ");
}

public void testStripLeading() {
assertEquals("", "".stripLeading());
assertEquals("", " ".stripLeading());
assertEquals("x ", " x ".stripLeading());
assertEquals("x\u001c", "\u001cx\u001c".stripLeading());
assertEquals("\u00a0x\u00a0", "\u00a0x\u00a0".stripLeading());
stripRightLeadingAsssertEquals("", "");
stripRightLeadingAsssertEquals("", " ");
stripRightLeadingAsssertEquals("x ", " x ");
stripRightLeadingAsssertEquals("x\u001c", "\u001cx\u001c");
stripRightLeadingAsssertEquals("\u00a0x\u00a0", "\u00a0x\u00a0");
}

public void testStripTrailing() {
assertEquals("", "".stripTrailing());
assertEquals("", " ".stripTrailing());
assertEquals(" x", " x ".stripTrailing());
assertEquals("\u001cx", "\u001cx\u001c".stripTrailing());
assertEquals("\u00a0x\u00a0", "\u00a0x\u00a0 ".stripTrailing());
stripRightTrailingAsssertEquals("", "");
stripRightTrailingAsssertEquals("", " ");
stripRightTrailingAsssertEquals(" x", " x ");
stripRightTrailingAsssertEquals("\u001cx", "\u001cx\u001c");
stripRightTrailingAsssertEquals("\u00a0x\u00a0", "\u00a0x\u00a0 ");
}

private void stripRightAsssertEquals(String expected, String arg) {
assertEquals(expected, arg.strip())
}

private void stripRightLeadingAsssertEquals(String expected, String arg) {
assertEquals(expected, arg.stripLeading())
}

private void stripRightTrailingAsssertEquals(String expected, String arg) {
assertEquals(expected, arg.stripTrailing())
}

public void testRepeat() {
Expand Down

0 comments on commit 9232405

Please sign in to comment.