Skip to content
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ public class JavaResourceBase {
"public interface CharSequence {",
" char charAt(int index);",
" int length();",
" default boolean isEmpty() { return length() == 0; }",
" String toString();",
"}");

Expand Down
97 changes: 97 additions & 0 deletions gwt.iml
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="CheckStyle-IDEA-Module" serialisationVersion="2">
<option name="activeLocationsIds" />
</component>
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/build_tools/doctool/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/dev/codeserver/java" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/dev/core/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/dev/core/super" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/dev/core/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/user/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/user/test" isTestSource="true" />
<sourceFolder url="file://$MODULE_DIR$/samples/dynatable/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/tools/api-checker/src" isTestSource="false" />
<sourceFolder url="file://$MODULE_DIR$/user/super/com/google/gwt/emul" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../tools/lib/htmlunit/htmlunit-4.11.1/htmlunit-4.11.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../tools/lib/htmlunit/htmlunit-4.11.1/htmlunit-core-js-4.11.0-unicode.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../tools/lib/htmlunit/htmlunit-4.11.1/htmlunit-core-js-4.11.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../tools/lib/htmlunit/htmlunit-4.11.1/htmlunit-csp-4.11.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../tools/lib/htmlunit/htmlunit-4.11.1/htmlunit-cssparser-4.11.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../tools/lib/htmlunit/htmlunit-4.11.1/htmlunit-websocket-client-4.11.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../tools/lib/htmlunit/htmlunit-4.11.1/htmlunit-xpath-4.11.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="module-library">
<library>
<CLASSES>
<root url="jar://$MODULE_DIR$/../tools/lib/htmlunit/htmlunit-4.11.1/neko-htmlunit-4.11.0.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
<orderEntry type="library" name="ecj-3.33.0-sources" level="project" />
</component>
</module>
32 changes: 32 additions & 0 deletions user/super/com/google/gwt/emul/java/lang/CharSequence.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,38 @@ public boolean hasNext() {
}, Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.ORDERED, false);
}

default IntStream codePoints() {
return StreamSupport.intStream(() -> {
PrimitiveIterator.OfInt it = new PrimitiveIterator.OfInt() {
int cursor;

@Override
public int nextInt() {
checkElement(hasNext());
int codePoint = CharSequence.this.toString().codePointAt(cursor++);
if (codePoint >= 1 << 16) {
cursor++;
}
return codePoint;
}

@Override
public boolean hasNext() {
return cursor < length();
}
};
return Spliterators.spliterator(it, length(), Spliterator.ORDERED);
}, Spliterator.SIZED | Spliterator.SUBSIZED | Spliterator.ORDERED, false);
}

default boolean isEmpty() {
return length() == 0;
}

static int compare(CharSequence cs1, CharSequence cs2) {
return cs1.toString().compareTo(cs2.toString());
}

// CHECKSTYLE_OFF: Utility methods.
@JsMethod
static boolean $isInstance(HasCharSequenceTypeMarker instance) {
Expand Down
140 changes: 134 additions & 6 deletions user/super/com/google/gwt/emul/java/lang/String.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@
import java.nio.charset.Charset;
import java.nio.charset.UnsupportedCharsetException;
import java.util.Comparator;
import java.util.List;
import java.util.Locale;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.StringJoiner;
import java.util.function.Consumer;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import java.util.stream.StreamSupport;
import javaemul.internal.ArrayHelper;
Expand Down Expand Up @@ -488,6 +491,7 @@ public String intern() {
return checkNotNull(this);
}

@Override
public boolean isEmpty() {
return length() == 0;
}
Expand Down Expand Up @@ -791,6 +795,10 @@ public String repeat(int count) {
return asNativeString().repeat(count);
}

public <R> R transform(Function<? super String,? extends R> f) {
return f.apply(this);
}

private int getLeadingWhitespaceLength() {
int length = length();
for (int i = 0; i < length; i++) {
Expand All @@ -811,6 +819,126 @@ private int getTrailingWhitespaceLength() {
return length;
}

public String indent(int spaces) {
if (spaces == 0) {
return this;
}
Stream<String> indentedLines;
if (spaces > 0) {
String spaceString = " ".repeat(spaces);
indentedLines = lines().map(line -> spaceString + line);
} else {
indentedLines = lines().map(
line -> line.substring(Math.min(-spaces, line.getLeadingWhitespaceLength())));
}
return indentedLines.collect(Collectors.joining("\n", "", "\n"));
}

public String stripIndent() {
if (isEmpty()) {
return "";
}
List<String> lines = lines().collect(Collectors.toList());
int minIndent;
char lastChar = charAt(length() - 1);
String suffix = "";
if (lastChar != '\r' && lastChar != '\n') {
minIndent = Integer.MAX_VALUE;
for (int i = 0; i < lines.size() - 1; i++) {
String line = lines.get(i);
int leadingWhitespace = line.getLeadingWhitespaceLength();
// only update minIndent if not blank
if (leadingWhitespace < line.length()) {
minIndent = Math.min(minIndent, leadingWhitespace);
}
}
// the last line affects minIndent even if blank
minIndent = Math.min(minIndent, lines.get(lines.size() - 1).getLeadingWhitespaceLength());
} else {
suffix = "\n";
minIndent = 0;
}
final int outdent = minIndent;
return lines.stream().map(line -> {
if (line.isBlank()) {
return "";
}
return line.substring(outdent).stripTrailing();
})
.collect(Collectors.joining("\n", "", suffix));
}

public String translateEscapes() {
StringBuilder result = new StringBuilder();
int translated = 0;
while (translated < length()) {
int nextBackslash = indexOf("\\", translated);
if (nextBackslash == -1) {
result.append(substring(translated));
return result.toString();
}
if (nextBackslash == length() - 1) {
throw new IllegalArgumentException();
}
result.append(substring(translated, nextBackslash));
char currentChar = charAt(nextBackslash + 1);
translated = nextBackslash + 2;
switch (currentChar) {
case 'b':
result.append('\b');
break;
case 's':
result.append(' ');
break;
case 't':
result.append('\t');
break;
case 'n':
result.append('\n');
break;
case 'f':
result.append('\f');
break;
case 'r':
result.append('\r');
break;
case '\n':
case '\r':
// discard
break;
case '"':
result.append('"');
break;
case '\'':
result.append('\'');
break;
case '\\':
result.append('\\');
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
int unicode = currentChar - '0';
char nextChar = charAt(translated);
while (nextChar >= '0' && nextChar < '8' && unicode < 32) {
unicode = (unicode << 3) + (nextChar - '0');
translated++;
nextChar = translated < length() ? charAt(translated) : 0;
}
result.append((char) unicode);
break;
default:
throw new IllegalArgumentException();
}
}
return result.toString();
}

private class LinesSpliterator extends Spliterators.AbstractSpliterator<String> {
private int nextIndex = 0;
private int rPosition = -1;
Expand All @@ -826,10 +954,10 @@ public boolean tryAdvance(Consumer<? super String> action) {
return false;
}
if (rPosition < nextIndex) {
rPosition = cappedIndexOf('\r');
rPosition = cappedIndexOf('\r', nextIndex);
}
if (nPosition < nextIndex) {
nPosition = cappedIndexOf('\n');
nPosition = cappedIndexOf('\n', nextIndex);
}
int lineEnd = Math.min(nPosition, rPosition);
action.accept(substring(nextIndex, lineEnd));
Expand All @@ -839,11 +967,11 @@ public boolean tryAdvance(Consumer<? super String> action) {
}
return nextIndex < length();
}
}

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

@JsType(isNative = true, name = "String", namespace = "<window>")
Expand Down
39 changes: 39 additions & 0 deletions user/super/com/google/gwt/emul/java/util/BitSet.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@
import javaemul.internal.ArrayHelper;
import javaemul.internal.LongUtils;

import java.util.function.IntConsumer;

Check warning on line 24 in user/super/com/google/gwt/emul/java/util/BitSet.java

View workflow job for this annotation

GitHub Actions / build (21)

[checkstyle] reported by reviewdog 🐶 Wrong order for 'java.util.function.IntConsumer' import. Raw Output: /home/runner/work/gwt/gwt/gwt/user/super/com/google/gwt/emul/java/util/BitSet.java:24:1: warning: Wrong order for 'java.util.function.IntConsumer' import. (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)

Check warning on line 24 in user/super/com/google/gwt/emul/java/util/BitSet.java

View workflow job for this annotation

GitHub Actions / build (21)

[checkstyle] reported by reviewdog 🐶 Extra separation in import group before 'java.util.function.IntConsumer' Raw Output: /home/runner/work/gwt/gwt/gwt/user/super/com/google/gwt/emul/java/util/BitSet.java:24:1: warning: Extra separation in import group before 'java.util.function.IntConsumer' (com.puppycrawl.tools.checkstyle.checks.imports.ImportOrderCheck)
import java.util.stream.IntStream;
import java.util.stream.StreamSupport;

/**
* This implementation uses a dense array holding bit groups of size 31 to keep track of when bits
* are set to true or false. Using 31 bits keeps our implementation within the range of V8's
Expand All @@ -37,6 +41,36 @@

private final int[] array;

private class BitSetSpliterator implements Spliterator.OfInt {
int nextBitIndex = 0;

@Override
public boolean tryAdvance(IntConsumer action) {
int nextBit = nextSetBit(nextBitIndex);
if (nextBit >= 0) {
nextBitIndex = nextBit + 1;
action.accept(nextBit);
return true;
}
return false;
}

@Override
public Spliterator.OfInt trySplit() {
return null;
}

@Override
public long estimateSize() {
return size();
}

@Override
public int characteristics() {
return Spliterator.SIZED | Spliterator.ORDERED | Spliterator.DISTINCT | Spliterator.SORTED;
}
}

public BitSet() {
array = new int[0];
}
Expand Down Expand Up @@ -604,6 +638,11 @@
return bitIndex(index) + Integer.numberOfTrailingZeros(word);
}

public IntStream stream() {
Spliterator.OfInt spliterator = new BitSetSpliterator();
return StreamSupport.intStream(spliterator, false);
}

public int previousClearBit(int fromIndex) {
if (fromIndex == -1) {
return -1;
Expand Down
Loading