Skip to content

Commit

Permalink
Add underscore formatting in numbers (#7030)
Browse files Browse the repository at this point in the history
* init commit innit

* update test name, var name

* moved file

* add double test

* update invalid tests to test more underscores

* remove multiple _

* Update src/test/skript/tests/misc/underscores in numbers.sk

Co-authored-by: Asleepp <[email protected]>

* forgor

* fixed error

---------

Co-authored-by: Asleepp <[email protected]>
Co-authored-by: sovdee <[email protected]>
  • Loading branch information
3 people authored Oct 13, 2024
1 parent 7cf5d79 commit 97055a2
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 7 deletions.
20 changes: 13 additions & 7 deletions src/main/java/ch/njol/skript/classes/data/JavaClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,8 @@
public class JavaClasses {

public static final int VARIABLENAME_NUMBERACCURACY = 8;
public static final Pattern INTEGER_PATTERN = Pattern.compile("-?[0-9]+");
public static final Pattern NUMBER_PATTERN = Pattern.compile("-?[0-9]+(?>\\.[0-9]+)?%?");
public static final Pattern INTEGER_PATTERN = Pattern.compile("-?\\d+(_\\d+)*");
public static final Pattern NUMBER_PATTERN = Pattern.compile("-?\\d+(_\\d+)*(?>\\.\\d+(_\\d+)*)?%?");

static {
Classes.registerClass(new ClassInfo<>(Object.class, "object")
Expand Down Expand Up @@ -72,10 +72,12 @@ public Number parse(String s, ParseContext context) {
return null;
if (INTEGER_PATTERN.matcher(s).matches()) {
try {
return Long.valueOf(s);
return Long.valueOf(s.replace("_", ""));
} catch (NumberFormatException ignored) { }
}
try {
s = s.replace("_", "");

Double d = s.endsWith("%") ? Double.parseDouble(s.substring(0, s.length() - 1)) / 100 : Double.parseDouble(s);
if (d.isNaN() || d.isInfinite())
return null;
Expand Down Expand Up @@ -141,7 +143,7 @@ public Long parse(String s, ParseContext context) {
if (!INTEGER_PATTERN.matcher(s).matches())
return null;
try {
return Long.valueOf(s);
return Long.valueOf(s.replace("_", ""));
} catch (NumberFormatException e) {
return null;
}
Expand Down Expand Up @@ -198,7 +200,7 @@ public Integer parse(String s, ParseContext context) {
if (!INTEGER_PATTERN.matcher(s).matches())
return null;
try {
return Integer.valueOf(s);
return Integer.valueOf(s.replace("_", ""));
} catch (NumberFormatException e) {
return null;
}
Expand Down Expand Up @@ -257,6 +259,8 @@ public Double parse(String s, ParseContext context) {
if (!NUMBER_PATTERN.matcher(s).matches())
return null;
try {
s = s.replace("_", "");

Double d = s.endsWith("%") ? Double.parseDouble(s.substring(0, s.length() - 1)) / 100 : Double.parseDouble(s);
if (d.isNaN() || d.isInfinite())
return null;
Expand Down Expand Up @@ -317,6 +321,8 @@ public Float parse(String s, ParseContext context) {
if (!NUMBER_PATTERN.matcher(s).matches())
return null;
try {
s = s.replace("_", "");

Float f = s.endsWith("%") ? Float.parseFloat(s.substring(0, s.length() - 1)) / 100 : Float.parseFloat(s);
if (f.isNaN() || f.isInfinite()) {
return null;
Expand Down Expand Up @@ -443,7 +449,7 @@ public Short parse(String s, ParseContext context) {
if (!INTEGER_PATTERN.matcher(s).matches())
return null;
try {
return Short.valueOf(s);
return Short.valueOf(s.replace("_", ""));
} catch (NumberFormatException e) {
return null;
}
Expand Down Expand Up @@ -500,7 +506,7 @@ public Byte parse(String s, ParseContext context) {
if (!INTEGER_PATTERN.matcher(s).matches())
return null;
try {
return Byte.valueOf(s);
return Byte.valueOf(s.replace("_", ""));
} catch (NumberFormatException e) {
return null;
}
Expand Down
44 changes: 44 additions & 0 deletions src/test/skript/tests/misc/underscores in numbers.sk
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
test "underscores in numbers":
assert 1 is a number with "1 is not a number"
assert -1 is a number with "-1 is not a number"
assert 1.236 is a number with "1.236 is not a number"
assert -1.236 is a number with "1.236 is not a number"
assert 1234567890 is a number with "1234567890 is not a number"
assert -1234567890 is a number with "-1234567890 is not a number"

assert 1_23_456 is 123456 with "1_23_456 is not 123456"
assert -1_23_456 is -123456 with "-1_23_456 is not -123456"
assert 1_23_4 is 1234 with "1_23_4 is not 1234"
assert -1_23_4 is -1234 with "-1_23_4 is not -1234"

assert 1_2.3_456 is 12.3456 with "1_2.3_456 is not 12.3456"
assert -1_2.3_456 is -12.3456 with "-1_2.3_456 is not -12.3456"
assert 1_2.3_4 is 12.34 with "1_2.3_4 is not 12.34"
assert -1_2.3_4 is -12.34 with "-1_2.3_4 is not -12.34"

assert 1_23_456_65665_7354 is 123456656657354 with "1_23_456_65665_7354 is not 123456656657354"
assert -1_23_456_65665_7354 is -123456656657354 with "-1_23_456_65665_7354 is not -123456656657354"

assert 1_23_4% is 1234/100 with "1_23_4%% is not 1234/100"
assert -1_23_4% is -1234/100 with "-1_23_4%% is not -1234/100"
assert 1_2.3_4% is 12.34/100 with "1_2.3_4%% is not 12.34/100"
assert -1_2.3_4% is -12.34/100 with "-1_2.3_4%% is not -12.34/100"

assert ("1__00" parsed as number) is not a number with "1__00 is a number"
assert ("1.2__3_6" parsed as number) is not a number with "1.2__3_6 is a number"
assert ("1.2__3_6%%" parsed as number) is not a number with "1.2__3_6%% is a number"

assert ("_1" parsed as number) is not a number with "_1 is a number"
assert ("_1.2_3_6" parsed as number) is not a number with "_1.2_3_6 is a number"
assert ("___1" parsed as number) is not a number with "___1 is a number"
assert ("___1.2_3_6" parsed as number) is not a number with "___1.2_3_6 is a number"

assert ("1_" parsed as number) is not a number with "1_ is a number"
assert ("1.2_3_6_" parsed as number) is not a number with "1.2_3_6_ is a number"
assert ("1___" parsed as number) is not a number with "1___ is a number"
assert ("1.2_3_6___" parsed as number) is not a number with "1.2_3_6___ is a number"

assert ("_1_" parsed as number) is not a number with "_1_ is a number"
assert ("_1.2_3_6_" parsed as number) is not a number with "_1.2_3_6_ is a number"
assert ("___1___" parsed as number) is not a number with "___1___ is a number"
assert ("___1.2_3_6___" parsed as number) is not a number with "___1.2_3_6___ is a number"

0 comments on commit 97055a2

Please sign in to comment.