CraftTweaker

CraftTweaker

151M Downloads

make ZenCode String can use all methods of java.lang.String

friendlyhj opened this issue ยท 3 comments

commented

Feature request name
make ZenCode String can use all methods of java.lang.String, like 1.12

Feature request reason
In 1.12, we can use all Java methods that do not use char. However, in 1.16, only a few methods are available. For example, toUpperCase is available, but substring is not.

Feature request dependencies
nope

Game Version
1.16

commented

In the current ZenScript, all the methods you mentioned (that exist) work besides for removeDiacritics, which I feel should be removed since there isn't a standard on what counts as a diacritic or not (see the notes here https://stackoverflow.com/a/27789934).

You can keep track of what string methods are available and will be available in the future at this issue ZenCodeLang/StdLibs#3

commented
val test as string = "test";

// Methods builtin ZenCode
// See https://github.com/ZenCodeLang/ZenCode/blob/2f30930188b291c5315621c56228258d19bfe484/CodeModel/src/main/java/org/openzen/zenscript/codemodel/type/member/TypeMemberBuilder.java#L104-L125
print(test[1]); // ok
print(test[1 .. 3]); // ok
print((test + "   ").trim()); // ok
print(test.toUpperCase()); //ok
print("TEST".toLowerCase()); // ok
// print(test.removeDiacritics()); // java.lang.UnsupportedOperationException: Not yet supported!

// Methods in stdlibs
// See https://github.com/ZenCodeLang/StdLibs/blob/master/src/main/zencode/stdlib/src/Strings.zs
// print("es" in test); // bytecode error
// print(test.indexOf('s')); // bytecode error
// print(test.indexOf('t', 2)); // bytecode error
// print(test.indexOf("st")); // bytecode error
// print(test.lastIndexOf("t")); // bytecode error
// print(test.lastIndexOf("t", 2)); // bytecode error
// print(test.lastIndexOf("st", 2)); // bytecode error
// print(test.split("e")[1]); // bytecode error
// trim also exists in stdlib
print(test.startsWith("a")); // ok
print(test.endsWith("st")); // ok
// print(test.lpad(8, 'a')); // bytecode error
// print(test.rpad(8, 'b')); // bytecode error

// Non-existent methods

// codePointAt codePointBefore codePointCount offsetByCodePoints equalsIgnoreCase
// compareToIgnoreCase regionMatches regionMatches substring concat replace matches
// replaceFirst replaceAll

I don't think a simple expansion can solve these...

commented

I'm not sure if that should be added here or rather in https://github.com/ZenCodeLang/ZenCode.
In the meantime it can be added to CrT, as long as it's only expansion methods we can then remove them once they are in ZenCode later on.

Since I can't really work much on CrT code and you already have cloned CrT, can you create a PR for the methods?

You can add them here, or create a new Class in the same package:
https://github.com/CraftTweaker/CraftTweaker/blob/1.16/src/main/java/com/blamejared/crafttweaker/api/zencode/expands/ExpandString.java