mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 00:04:10 -08:00
classlib: implement CharSequence chars and codePoints
This commit is contained in:
parent
e48dfb27b0
commit
0dd10899a0
|
@ -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();
|
||||
|
||||
|
@ -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();
|
||||
}
|
||||
|
|
|
@ -33,6 +33,7 @@ import org.teavm.classlib.java.util.TLocale;
|
|||
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;
|
||||
|
||||
|
@ -589,10 +590,16 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
|
|||
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";
|
||||
}
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
||||
|
@ -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
|
||||
|
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
|
@ -265,4 +266,22 @@ public class StringBuilderTest {
|
|||
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());
|
||||
}
|
||||
}
|
||||
|
|
|
@ -378,4 +378,16 @@ public class StringTest {
|
|||
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());
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user