mirror of
https://github.com/Eaglercraft-TeaVM-Fork/eagler-teavm.git
synced 2024-12-22 08:14:09 -08:00
classlib: add stripLeading and stripTrailing methods to String class (#707)
The stripLeading and stripTrailing methods were added in Java 11 to the String class. Based on the implementation of the existing strip method in TeaVM this commit adds the stripLeading() and stripTrailing() methods (and tests) to the String class in the class library.
This commit is contained in:
parent
db69f8ec58
commit
7104edb592
|
@ -487,6 +487,22 @@ public class TString extends TObject implements TSerializable, TComparable<TStri
|
||||||
return substring(lower, upper + 1);
|
return substring(lower, upper + 1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public TString stripLeading() {
|
||||||
|
var lower = 0;
|
||||||
|
while (lower < length() && Character.isWhitespace(charAt(lower))) {
|
||||||
|
++lower;
|
||||||
|
}
|
||||||
|
return substring(lower, length());
|
||||||
|
}
|
||||||
|
|
||||||
|
public TString stripTrailing() {
|
||||||
|
var upper = length() - 1;
|
||||||
|
while (0 <= upper && Character.isWhitespace(charAt(upper))) {
|
||||||
|
--upper;
|
||||||
|
}
|
||||||
|
return substring(0, upper + 1);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public String toString() {
|
public String toString() {
|
||||||
return (String) (Object) this;
|
return (String) (Object) this;
|
||||||
|
|
|
@ -209,6 +209,26 @@ public class StringTest {
|
||||||
assertEquals("ab", "\t\n \u2008ab\r\f".strip());
|
assertEquals("ab", "\t\n \u2008ab\r\f".strip());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void stripLeadingWorks() {
|
||||||
|
assertEquals("ab ", " ab ".stripLeading());
|
||||||
|
assertEquals("ab ", "ab ".stripLeading());
|
||||||
|
assertEquals("ab", "ab".stripLeading());
|
||||||
|
assertEquals("a b", "a b".stripLeading());
|
||||||
|
assertEquals("", " \t".stripLeading());
|
||||||
|
assertEquals("ab", "\t\n \u2008\r\fab".stripLeading());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void stripTrailingWorks() {
|
||||||
|
assertEquals(" ab", " ab ".stripTrailing());
|
||||||
|
assertEquals(" ab", " ab".stripTrailing());
|
||||||
|
assertEquals("ab", "ab".stripTrailing());
|
||||||
|
assertEquals("a b", "a b".stripTrailing());
|
||||||
|
assertEquals("", " \t".stripTrailing());
|
||||||
|
assertEquals("ab", "ab\t\n \u2008\r\f".stripTrailing());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void convertedToCharArray() {
|
public void convertedToCharArray() {
|
||||||
char[] array = "123".toCharArray();
|
char[] array = "123".toCharArray();
|
||||||
|
|
Loading…
Reference in New Issue
Block a user