Skip to content

Commit

Permalink
Add tests, reduce indexOf lookups
Browse files Browse the repository at this point in the history
  • Loading branch information
zbynek committed Jul 8, 2024
1 parent e01ccee commit e9a0c86
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 59 deletions.
5 changes: 5 additions & 0 deletions .idea/codeStyles/codeStyleConfig.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

31 changes: 21 additions & 10 deletions user/super/com/google/gwt/emul/java/lang/String.java
Original file line number Diff line number Diff line change
Expand Up @@ -813,8 +813,10 @@ private int getTrailingWhitespaceLength() {
}

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

private LinesSpliterator(String content) {
super(Long.MAX_VALUE, Spliterator.IMMUTABLE | Spliterator.ORDERED);
Expand All @@ -823,17 +825,26 @@ private LinesSpliterator(String content) {

@Override
public boolean tryAdvance(Consumer<? super String> action) {
int rPosition = content.indexOf('\r', processed);
int nPosition = content.indexOf('\n', processed);
int lineEnd = nPosition == -1 ? rPosition : Math.min(nPosition, rPosition);
action.accept(lineEnd == -1 ? content.substring(processed)
: content.substring(processed, lineEnd));
processed = lineEnd + 1;
if (nPosition == rPosition + 1 && rPosition != -1) {
processed++;
if (rPosition < nextIndex) {
rPosition = cappedIndexOf('\r');
}
return lineEnd != -1 && processed < content.length();
if (nPosition < nextIndex) {
nPosition = cappedIndexOf('\n');
}
int lineEnd = Math.min(nPosition, rPosition);
action.accept(content.substring(nextIndex, lineEnd));
nextIndex = lineEnd + 1;
if (nPosition == rPosition + 1) {
nextIndex++;
}
return nextIndex < content.length();
}

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

}

@JsType(isNative = true, name = "String", namespace = "<window>")
Expand Down
106 changes: 57 additions & 49 deletions user/test/com/google/gwt/emultest/java11/lang/StringTest.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright 2023 Google Inc.
* Copyright 2024 GWT Project Authors
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may not
* use this file except in compliance with the License. You may obtain a copy of
Expand All @@ -24,58 +24,66 @@
* Tests for java.lang.String Java 11 API emulation.
*/
public class StringTest extends EmulTestBase {
public void testIsBlank() {
assertTrue("".isBlank());
assertTrue(" ".isBlank());
assertFalse("x ".isBlank());
assertTrue("\u001c".isBlank());
assertFalse("\u00a0".isBlank());
}
public void testIsBlank() {
assertTrue("".isBlank());
assertTrue(" ".isBlank());
assertFalse("x ".isBlank());
assertTrue("\u001c".isBlank());
assertFalse("\u00a0".isBlank());
}

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

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

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

public void testRepeat() {
assertEquals("", "foo".repeat(0));
assertEquals("foo", "foo".repeat(1));
assertEquals("foofoofoo", "foo".repeat(3));
try {
String noFoo = "foo".repeat(-1);
throw new Error("Should fail with negative arg");
} catch (IllegalArgumentException ex) {
assertEquals("count is negative: -1", ex.getMessage());
}
public void testRepeat() {
assertEquals("", "foo".repeat(0));
assertEquals("foo", "foo".repeat(1));
assertEquals("foofoofoo", "foo".repeat(3));
try {
String noFoo = "foo".repeat(-1);
throw new Error("Should fail with negative arg");
} catch (IllegalArgumentException ex) {
assertEquals("count is negative: -1", ex.getMessage());
}
}

public void testLines() {
assertEquals(Arrays.asList("a", "b", "c", "d"),
"a\rb\nc\r\nd".lines().collect(Collectors.toList()));
assertEquals(Arrays.asList("a"),
"a\n".lines().collect(Collectors.toList()));
assertEquals(Arrays.asList(),
"".lines().collect(Collectors.toList()));
assertEquals(Arrays.asList(""),
"\n".lines().collect(Collectors.toList()));
}
public void testLines() {
assertEquals(Arrays.asList("a", "b", "c", "d"),
"a\rb\nc\r\nd".lines().collect(Collectors.toList()));
assertEquals(Arrays.asList("a"),
"a\n".lines().collect(Collectors.toList()));
assertEquals(Arrays.asList("a"),
"a\r\n".lines().collect(Collectors.toList()));
assertEquals(Arrays.asList(),
"".lines().collect(Collectors.toList()));
assertEquals(Arrays.asList(""),
"\n".lines().collect(Collectors.toList()));
assertEquals(Arrays.asList(""),
"\r\n".lines().collect(Collectors.toList()));
assertEquals(Arrays.asList("", ""),
"\n\r\n".lines().collect(Collectors.toList()));
assertEquals(Arrays.asList("", "", "c"),
"\n\r\nc".lines().collect(Collectors.toList()));
}
}

0 comments on commit e9a0c86

Please sign in to comment.