Skip to content

Commit

Permalink
use more fast parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
zspitzer committed Oct 31, 2024
1 parent 276b12a commit a4ee913
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
13 changes: 7 additions & 6 deletions core/src/main/java/lucee/runtime/op/Caster.java
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ private Caster() {
}
// static Map calendarsMap=new ReferenceMap(ReferenceMap.SOFT,ReferenceMap.SOFT);

private static final boolean USE_FAST_PARSER = true;
private static final int NUMBERS_MIN = 0;
private static final int NUMBERS_MAX = 999;
private static final String[] NUMBERS = { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23",
Expand Down Expand Up @@ -564,7 +565,7 @@ public static double toDoubleValue(String str, boolean alsoFromDate) throws Cast
else if (curr > '9') {
if (curr == 'e' || curr == 'E') {
try {
return Double.parseDouble(str);
return NumberInput.parseDouble(str, USE_FAST_PARSER);
}
catch (NumberFormatException e) {
if (!alsoFromDate) throw new CasterException("cannot cast [" + str + "] string to a number value");
Expand All @@ -584,7 +585,7 @@ else if (curr > '9') {
rtn += toDigit(curr);
if (hasDot) {
deep *= 10;
if (deep > 1000000000000000000000D) return Double.parseDouble(str); // patch for LDEV-2654
if (deep > 1000000000000000000000D) return NumberInput.parseDouble(str, USE_FAST_PARSER); // patch for LDEV-2654
}

}
Expand Down Expand Up @@ -703,7 +704,7 @@ else if (curr == '-') {
else if (curr > '9') {
if (curr == 'e' || curr == 'E') {
try {
return Double.parseDouble(str);
return NumberInput.parseDouble(str, USE_FAST_PARSER);
}
catch (NumberFormatException e) {
if (!alsoFromDate) return defaultValue;
Expand Down Expand Up @@ -1580,7 +1581,7 @@ else if (o instanceof Number) {
else if (o instanceof CharSequence) {
String str = o.toString();
try {
return Long.parseLong(str);
return NumberInput.parseLong(str);
}
catch (NumberFormatException nfe) {
return (long) toDoubleValue(str);
Expand All @@ -1603,7 +1604,7 @@ else if (o instanceof CharSequence) {
public static long toLongValue(String str) throws PageException {
BigInteger bi = null;
try {
bi = new BigInteger(str);
bi = NumberInput.parseBigInteger(str, USE_FAST_PARSER);

}
catch (Throwable t) {
Expand Down Expand Up @@ -1631,7 +1632,7 @@ public static Number toNumber(String str, Number defaultValue) {
return Caster.toBigDecimal(str);
}
// integer
BigInteger bi = new BigInteger(str);
BigInteger bi = NumberInput.parseBigInteger(str, USE_FAST_PARSER);
int l = bi.bitLength();
if (l < 32) return Integer.valueOf(bi.intValue());
if (l < 64) return Long.valueOf(bi.longValue());
Expand Down
5 changes: 4 additions & 1 deletion core/src/main/java/lucee/runtime/op/Decision.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,12 +75,15 @@
import lucee.runtime.type.UDF;
import lucee.runtime.type.dt.DateTime;

import com.fasterxml.jackson.core.io.NumberInput;

/**
* Object to test if an Object is a specific type
*/
public final class Decision {

private static final String STRING_DEFAULT_VALUE = "this is a unique string";
private static final boolean USE_FAST_PARSER = true;

private static Pattern ssnPattern;
private static Pattern phonePattern;
Expand Down Expand Up @@ -210,7 +213,7 @@ else if (curr > '9') {
}
if (hasExp) {
try {
if (Double.isInfinite(Double.parseDouble(str))) return false;
if (Double.isInfinite(NumberInput.parseDouble(str, USE_FAST_PARSER))) return false;

return true;
}
Expand Down

0 comments on commit a4ee913

Please sign in to comment.