Skip to content

Commit

Permalink
classlib: implement CharSequence chars and codePoints
Browse files Browse the repository at this point in the history
  • Loading branch information
konsoletyper committed Apr 29, 2024
1 parent e48dfb2 commit 0dd1089
Show file tree
Hide file tree
Showing 8 changed files with 219 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
package org.teavm.classlib.java.lang;

import org.teavm.classlib.java.util.stream.TIntStream;
import org.teavm.classlib.java.util.stream.intimpl.TCharSequenceCharsStream;
import org.teavm.classlib.java.util.stream.intimpl.TCharSequenceCodePointsStream;

public interface TCharSequence {
int length();

Expand All @@ -24,6 +28,14 @@ public interface TCharSequence {

TCharSequence subSequence(int start, int end);

default TIntStream chars() {
return new TCharSequenceCharsStream(this);
}

default TIntStream codePoints() {
return new TCharSequenceCodePointsStream(this);
}

@Override
String toString();
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
import org.teavm.classlib.java.util.regex.TPattern;
import org.teavm.classlib.java.util.stream.TIntStream;
import org.teavm.classlib.java.util.stream.intimpl.TStringCharsStream;
import org.teavm.classlib.java.util.stream.intimpl.TStringCodePointsStream;
import org.teavm.dependency.PluggableDependency;
import org.teavm.interop.NoSideEffects;

Expand Down Expand Up @@ -589,10 +590,16 @@ public char[] toCharArray() {
return array;
}

@Override
public TIntStream chars() {
return new TStringCharsStream(this);
}

@Override
public TIntStream codePoints() {
return new TStringCodePointsStream(this);
}

public static String valueOf(Object obj) {
return obj != null ? obj.toString() : "null";
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright 2023 JFronny.
*
* 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 the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.util.stream.intimpl;

import java.util.function.IntPredicate;
import org.teavm.classlib.java.lang.TCharSequence;

public class TCharSequenceCharsStream extends TSimpleIntStreamImpl {
private final TCharSequence csq;
private int index;

public TCharSequenceCharsStream(TCharSequence csq) {
this.csq = csq;
}

@Override
public boolean next(IntPredicate consumer) {
while (index < csq.length()) {
if (!consumer.test(csq.charAt(index++))) {
break;
}
}
return index < csq.length();
}

@Override
protected int estimateSize() {
return csq.length();
}

@Override
public long count() {
return csq.length();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2024 Alexey Andreev.
*
* 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 the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.util.stream.intimpl;

import java.util.function.IntPredicate;
import org.teavm.classlib.java.lang.TCharSequence;
import org.teavm.classlib.java.lang.TCharacter;

public class TCharSequenceCodePointsStream extends TSimpleIntStreamImpl {
private final TCharSequence csq;
private int index;

public TCharSequenceCodePointsStream(TCharSequence csq) {
this.csq = csq;
}

@Override
public boolean next(IntPredicate consumer) {
while (index < csq.length()) {
var hi = csq.charAt(index++);
if (TCharacter.isHighSurrogate(hi) && index < csq.length()) {
var lo = csq.charAt(index);
if (TCharacter.isLowSurrogate(lo)) {
++index;
if (!consumer.test(TCharacter.toCodePoint(hi, lo))) {
break;
}
continue;
}
}
if (!consumer.test(hi)) {
break;
}
}
return index < csq.length();
}

@Override
protected int estimateSize() {
return csq.length();
}

@Override
public long count() {
return csq.length();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.teavm.classlib.java.util.stream.intimpl;

import java.util.Objects;
import java.util.function.IntPredicate;
import org.teavm.classlib.java.lang.TString;

Expand All @@ -24,7 +23,7 @@ public class TStringCharsStream extends TSimpleIntStreamImpl {
private int index;

public TStringCharsStream(TString string) {
this.string = Objects.requireNonNull(string);
this.string = string;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
/*
* Copyright 2024 Alexey Andreev.
*
* 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 the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.teavm.classlib.java.util.stream.intimpl;

import java.util.function.IntPredicate;
import org.teavm.classlib.java.lang.TCharacter;
import org.teavm.classlib.java.lang.TString;

public class TStringCodePointsStream extends TSimpleIntStreamImpl {
private final TString string;
private int index;

public TStringCodePointsStream(TString string) {
this.string = string;
}

@Override
public boolean next(IntPredicate consumer) {
while (index < string.length()) {
var hi = string.charAt(index++);
if (TCharacter.isHighSurrogate(hi) && index < string.length()) {
var lo = string.charAt(index);
if (TCharacter.isLowSurrogate(lo)) {
++index;
if (!consumer.test(TCharacter.toCodePoint(hi, lo))) {
break;
}
continue;
}
}
if (!consumer.test(hi)) {
break;
}
}
return index < string.length();
}

@Override
protected int estimateSize() {
return string.length();
}

@Override
public long count() {
return string.length();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package org.teavm.classlib.java.lang;

import static org.junit.Assert.assertArrayEquals;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.fail;
import org.junit.Test;
Expand Down Expand Up @@ -265,4 +266,22 @@ public void replace() {
sb.replace(1, 1, "bc");
assertEquals("abcgh", sb.toString());
}

@Test
public void charStream() {
assertEquals(0, new StringBuilder().chars().toArray().length);
assertArrayEquals(new int[] {'A', 'B', 'C', '1', '2', '3'}, new StringBuilder("ABC123").chars().toArray());
}

@Test
public void codePointsStream() {
assertEquals(0, "".codePoints().toArray().length);
assertArrayEquals(new int[] {'A', 'B', 'C', '1', '2', '3'}, new StringBuilder("ABC123").chars().toArray());

assertArrayEquals(new int[] { 969356 }, new StringBuilder().append(new char[] { (char) 56178, (char) 56972 })
.codePoints().toArray());
assertArrayEquals(new int[] { 56972, 56178 }, new StringBuilder().append(
new char[] { (char) 56972, (char) 56178 }).codePoints().toArray());
assertArrayEquals(new int[] { 56178 }, new StringBuilder().append((char) 56178).codePoints().toArray());
}
}
12 changes: 12 additions & 0 deletions tests/src/test/java/org/teavm/classlib/java/lang/StringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -378,4 +378,16 @@ public void testChars() {
assertEquals(0, "".chars().toArray().length);
assertArrayEquals(new int[] {'A', 'B', 'C', '1', '2', '3'}, "ABC123".chars().toArray());
}

@Test
public void codePointsStream() {
assertEquals(0, "".codePoints().toArray().length);
assertArrayEquals(new int[] {'A', 'B', 'C', '1', '2', '3'}, "ABC123".chars().toArray());

assertArrayEquals(new int[] { 969356 }, new String(new char[] { (char) 56178, (char) 56972 })
.codePoints().toArray());
assertArrayEquals(new int[] { 56972, 56178 }, new String(new char[] { (char) 56972, (char) 56178 })
.codePoints().toArray());
assertArrayEquals(new int[] { 56178 }, String.valueOf((char) 56178).codePoints().toArray());
}
}

0 comments on commit 0dd1089

Please sign in to comment.