Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rework number parser and angle support #7139

Merged
merged 40 commits into from
Dec 15, 2024
Merged
Changes from 1 commit
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
1001e4f
init commit
Efnilite Oct 9, 2024
f461b24
oops
Efnilite Oct 9, 2024
b3849ce
Merge branch 'dev/feature' into angle
Efnilite Oct 9, 2024
9998616
removed unnecessary checks
Efnilite Oct 9, 2024
efbb1af
Merge remote-tracking branch 'origin/angle' into angle
Efnilite Oct 9, 2024
52d028a
update other syntaxes
Efnilite Oct 9, 2024
b4a1f37
move to parser
Efnilite Oct 9, 2024
cef8b87
oops
Efnilite Oct 9, 2024
c5d5c25
big work
Efnilite Oct 10, 2024
b378a30
forgot !
Efnilite Oct 10, 2024
036adf3
fix doubleValue call
Efnilite Oct 10, 2024
491c7cf
fix parenthesis
Efnilite Oct 11, 2024
fcd2c60
add expression
Efnilite Oct 11, 2024
46b507e
update tests
Efnilite Oct 12, 2024
8d552b9
requested changes
Efnilite Oct 12, 2024
27d0c78
oops
Efnilite Oct 12, 2024
6ee87f1
remove unnecessary converter application
Efnilite Oct 12, 2024
79ce823
remove converter
Efnilite Oct 12, 2024
87b9e84
le fix
Efnilite Oct 13, 2024
a0212de
Merge branch 'dev/feature' into angle
Efnilite Oct 13, 2024
18bf9c7
fix merge w/ display entities
Efnilite Oct 13, 2024
ad27be8
most "regular" regex
Efnilite Oct 13, 2024
cfd09ce
Merge branch 'dev/feature' into angle
sovdeeth Oct 15, 2024
38543ca
fix tests
Efnilite Oct 17, 2024
983e376
fix _ support
Efnilite Oct 17, 2024
a6b27bc
fix rotate
Efnilite Oct 17, 2024
e392b57
Merge branch 'dev/feature' into angle
Efnilite Oct 26, 2024
8d0e8c5
Merge branch 'dev/feature' into angle
Efnilite Oct 28, 2024
686ddc3
minor changes
Efnilite Oct 28, 2024
b5168cc
remove double return
Efnilite Oct 28, 2024
6707d0d
fix ExprRotate
Efnilite Oct 28, 2024
f9afddb
Merge branch 'dev/feature' into angle
Efnilite Nov 1, 2024
e4cd70b
Merge branch 'dev/feature' into angle
Efnilite Nov 5, 2024
d8daf93
Merge branch 'dev/feature' into angle
sovdeeth Nov 8, 2024
656e4ad
Merge branch 'dev/feature' into angle
Efnilite Nov 10, 2024
a9402b5
Merge branch 'dev/feature' into angle
Efnilite Nov 14, 2024
dacf629
oopsie
Efnilite Nov 20, 2024
ae2765a
Merge branch 'dev/feature' into angle
Moderocky Nov 30, 2024
28742ef
Merge branch 'dev/feature' into angle
Efnilite Dec 6, 2024
abd2873
Merge branch 'dev/feature' into angle
sovdeeth Dec 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
remove unnecessary converter application
  • Loading branch information
Efnilite committed Oct 12, 2024
commit 6ee87f16637db089711b155cc576e5033b17a1f3
53 changes: 34 additions & 19 deletions src/main/java/ch/njol/skript/classes/data/JavaClasses.java
Original file line number Diff line number Diff line change
Expand Up @@ -276,20 +276,20 @@ public boolean mustSyncDeserialization() {
* <p>
* Applies {@code stringToNumber} for parsing the number, then tries to
* convert radians to degrees if the string contains a radian group.
* If the string could be parsed, applies {@code converter} to convert
* the number to the desired type.
* If the string could be parsed and converted to radians,
* applies {@code fromDouble} to convert the returned {@link Double} to the desired type.
* </p>
*
* @param string The string with the possible number.
* @param stringToNumber The function to parse the number, e.g. {@link Integer#parseInt(String)}.
* @param converter The function to convert the number to the desired type, e.g. {@link Number#intValue()}.
* @param fromDouble The function to convert a {@link Double} to the desired type, e.g. {@link Double#intValue()}.
* @return The parsed string, or null if the string could not be parsed.
*/
@Contract(pure = true)
private static <T extends Number> @Nullable T convertIntegerFormatted(
String string,
Function<String, Number> stringToNumber,
Function<Number, T> converter
Function<String, T> stringToNumber,
Function<Double, T> fromDouble
) {
Matcher matcher = INTEGER_PATTERN.matcher(string);

Expand All @@ -299,12 +299,12 @@ public boolean mustSyncDeserialization() {
String number = matcher.group("num");
if (matcher.group("rad") != null) {
try {
return converter.apply(Math.toDegrees(stringToNumber.apply(number).doubleValue()));
return fromDouble.apply(Math.toDegrees(stringToNumber.apply(number).doubleValue()));
} catch (NumberFormatException ignored) {
}
} else {
try {
return converter.apply(stringToNumber.apply(number));
return stringToNumber.apply(number);
} catch (NumberFormatException ignored) {
}
}
Expand All @@ -313,24 +313,25 @@ public boolean mustSyncDeserialization() {
}

/**
* Converts a string to a number formatted as an decimal.
* Converts a string to a number formatted as a decimal.
* <p>
* Applies {@code stringToNumber} for parsing the number.
* If the number is a percentage, it gets parsed as a double between 0-1.
* If the number is a percentage, it gets parsed using the double value divided by 100, and {@code fromDouble}.
* Then tries to convert radians to degrees if the string contains a radian group.
* If the string could be parsed, applies {@code converter} to convert the number to the desired type.
* If the string could be parsed and converted to radians,
* applies {@code fromDouble} to convert the returned {@link Double} to the desired type.
* </p>
*
* @param string The string with the possible number.
* @param stringToNumber The function to parse the number, e.g. {@link Integer#parseInt(String)}.
* @param converter The function to convert the number to the desired type, e.g. {@link Number#intValue()}.
* @param fromDouble The function to convert a {@link Double} to the desired type, e.g. {@link Double#intValue()}.
* @return The parsed string, or null if the string could not be parsed.
*/
@Contract(pure = true)
private static <T extends Number> @Nullable T convertDecimalFormatted(
String string,
Function<String, T> stringToNumber,
Function<Double, T> converter
Function<Double, T> fromDouble
) {
Matcher matcher = DECIMAL_PATTERN.matcher(string);

Expand All @@ -343,14 +344,14 @@ public boolean mustSyncDeserialization() {
T result;
if (number.endsWith("%")) {
T extracted = stringToNumber.apply(number.substring(0, number.length() - 1));
result = converter.apply(extracted.doubleValue() / 100.0);
result = fromDouble.apply(extracted.doubleValue() / 100.0);
} else {
result = stringToNumber.apply(number);
}

if (matcher.group("rad") != null) {
try {
return converter.apply(Math.toDegrees(result.doubleValue()));
return fromDouble.apply(Math.toDegrees(result.doubleValue()));
} catch (NumberFormatException ignored) {
}
}
Expand All @@ -369,7 +370,7 @@ private static class NumberParser extends Parser<Number> {
if (!numberMatcher.matches())
return null;

Integer integerAttempt = convertIntegerFormatted(string, Integer::parseInt, Number::intValue);
Integer integerAttempt = convertIntegerFormatted(string, Integer::parseInt, Double::intValue);
if (integerAttempt != null)
return integerAttempt;

Expand All @@ -386,6 +387,7 @@ public String toString(Number number, int flags) {
public String toVariableNameString(Number number) {
return StringUtils.toString(number.doubleValue(), VARIABLENAME_NUMBERACCURACY);
}

}

private static class NumberSerializer extends Serializer<Number> {
Expand Down Expand Up @@ -422,13 +424,14 @@ public void deserialize(Number number, Fields fields) {
public boolean mustSyncDeserialization() {
return false;
}

}

private static class LongParser extends Parser<Long> {

@Override
public @Nullable Long parse(String string, ParseContext context) {
return convertIntegerFormatted(string, Long::parseLong, Number::longValue);
return convertIntegerFormatted(string, Long::parseLong, Double::longValue);
}

@Override
Expand All @@ -440,6 +443,7 @@ public String toString(Long l, int flags) {
public String toVariableNameString(Long l) {
return l.toString();
}

}

private static class LongSerializer extends Serializer<Long> {
Expand Down Expand Up @@ -472,13 +476,14 @@ public void deserialize(Long l, Fields fields) {
public boolean mustSyncDeserialization() {
return false;
}

}

private static class IntegerParser extends Parser<Integer> {

@Override
public @Nullable Integer parse(String string, ParseContext context) {
return convertIntegerFormatted(string, Integer::parseInt, Number::intValue);
return convertIntegerFormatted(string, Integer::parseInt, Double::intValue);
}

@Override
Expand All @@ -490,6 +495,7 @@ public String toString(Integer i, int flags) {
public String toVariableNameString(Integer i) {
return i.toString();
}

}

private static class IntegerSerializer extends Serializer<Integer> {
Expand Down Expand Up @@ -522,6 +528,7 @@ public void deserialize(Integer i, Fields fields) {
public boolean mustSyncDeserialization() {
return false;
}

}

private static class DoubleParser extends Parser<Double> {
Expand All @@ -542,6 +549,7 @@ public String toString(Double d, int flags) {
public String toVariableNameString(Double d) {
return StringUtils.toString(d, VARIABLENAME_NUMBERACCURACY);
}

}

private static class DoubleSerializer extends Serializer<Double> {
Expand Down Expand Up @@ -574,6 +582,7 @@ public void deserialize(Double d, Fields fields) {
public boolean mustSyncDeserialization() {
return false;
}

}

private static class FloatParser extends Parser<Float> {
Expand All @@ -594,6 +603,7 @@ public String toString(Float f, int flags) {
public String toVariableNameString(Float f) {
return StringUtils.toString(f.doubleValue(), VARIABLENAME_NUMBERACCURACY);
}

}

private static class FloatSerializer extends Serializer<Float> {
Expand Down Expand Up @@ -626,13 +636,14 @@ public void deserialize(Float f, Fields fields) {
public boolean mustSyncDeserialization() {
return false;
}

}

private static class ShortParser extends Parser<Short> {

@Override
public @Nullable Short parse(String string, ParseContext context) {
return convertIntegerFormatted(string, Short::parseShort, Number::shortValue);
return convertIntegerFormatted(string, Short::parseShort, Double::shortValue);
}

@Override
Expand All @@ -644,6 +655,7 @@ public String toString(Short s, int flags) {
public String toVariableNameString(Short s) {
return s.toString();
}

}

private static class ShortSerializer extends Serializer<Short> {
Expand Down Expand Up @@ -676,13 +688,14 @@ public void deserialize(Short s, Fields fields) {
public boolean mustSyncDeserialization() {
return false;
}

}

private static class ByteParser extends Parser<Byte> {

@Override
public @Nullable Byte parse(String string, ParseContext context) {
return convertIntegerFormatted(string, Byte::parseByte, Number::byteValue);
return convertIntegerFormatted(string, Byte::parseByte, Double::byteValue);
}

@Override
Expand All @@ -694,6 +707,7 @@ public String toString(Byte b, int flags) {
public String toVariableNameString(Byte b) {
return b.toString();
}

}

private static class ByteSerializer extends Serializer<Byte> {
Expand Down Expand Up @@ -726,6 +740,7 @@ public void deserialize(Byte b, Fields fields) {
public boolean mustSyncDeserialization() {
return false;
}

}

}