Skip to content

Commit

Permalink
LDEV-2793 -fix toInteger method
Browse files Browse the repository at this point in the history
  • Loading branch information
michaeloffner committed Dec 10, 2024
1 parent ed47cae commit 8673611
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 32 deletions.
60 changes: 32 additions & 28 deletions core/src/main/java/lucee/runtime/op/Caster.java
Original file line number Diff line number Diff line change
Expand Up @@ -806,30 +806,6 @@ public static int toIntValue(Object o) throws PageException {
throw new CasterException(o, "number");
}

/**
* cast an Object to an int value (primitive value type)
*
* @param o Object to cast
* @param defaultValue
* @return casted int value
*/
public static int toIntValue(Object o, int defaultValue) {

if (o instanceof Number) return ((Number) o).intValue();
else if (o instanceof Boolean) return ((Boolean) o).booleanValue() ? 1 : 0;
else if (o instanceof CharSequence) return toIntValue(o.toString().trim(), defaultValue);
// else if(o instanceof Clob) return toIntValue(toString(o));
else if (o instanceof Character) return (((Character) o).charValue());
else if (o instanceof Castable) {
return (int) ((Castable) o).castToDoubleValue(defaultValue);

}
else if (o instanceof Date) return (int) new DateTimeImpl((Date) o).castToDoubleValue();
else if (o instanceof ObjectWrap) return toIntValue(((ObjectWrap) o).getEmbededObject(Integer.valueOf(defaultValue)), defaultValue);

return defaultValue;
}

public static int toIntValue(Integer i, int defaultValue) {
if (i == null) return defaultValue;
return i.intValue();
Expand Down Expand Up @@ -4843,6 +4819,12 @@ public static Integer toInteger(String str) throws PageException {
return Integer.valueOf(toIntValue(str));
}

public static Integer toInteger(String str, Integer defaultValue) {
Double d = toDouble(str, null);
if (d == null) return defaultValue;
return Integer.valueOf(d.intValue());
}

// used in bytecode genrator
public static Integer toInteger(int i) {
return Integer.valueOf(i);
Expand All @@ -4856,10 +4838,32 @@ public static Integer toInteger(int i) {
* @return Integer from Object
*/
public static Integer toInteger(Object o, Integer defaultValue) {
if (defaultValue != null) return Integer.valueOf(toIntValue(o, defaultValue.intValue()));
int res = toIntValue(o, Integer.MIN_VALUE);
if (res == Integer.MIN_VALUE) return defaultValue;
return Integer.valueOf(res);

if (o instanceof Number) return Integer.valueOf(((Number) o).intValue());
else if (o instanceof Boolean) return Integer.valueOf(((Boolean) o).booleanValue() ? 1 : 0);
else if (o instanceof CharSequence) return toInteger(o.toString().trim(), defaultValue);
// else if(o instanceof Clob) return toIntValue(toString(o));
else if (o instanceof Character) return Integer.valueOf(((Character) o).charValue());
else if (o instanceof Castable) {
return Integer.valueOf((int) ((Castable) o).castToDoubleValue(defaultValue));

}
else if (o instanceof Date) return Integer.valueOf((int) new DateTimeImpl((Date) o).castToDoubleValue());
else if (o instanceof ObjectWrap) return toInteger(((ObjectWrap) o).getEmbededObject(Integer.valueOf(defaultValue)), defaultValue);
return defaultValue;
}

/**
* cast an Object to an int value (primitive value type)
*
* @param o Object to cast
* @param defaultValue
* @return casted int value
*/
public static int toIntValue(Object o, int defaultValue) {
Integer i = toInteger(o, null);
if (i == null) return defaultValue;
return i.intValue();
}

/**
Expand Down
2 changes: 1 addition & 1 deletion loader/build.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<project default="core" basedir="." name="Lucee"
xmlns:resolver="antlib:org.apache.maven.resolver.ant">

<property name="version" value="6.2.0.196-SNAPSHOT"/>
<property name="version" value="6.2.0.197-SNAPSHOT"/>

<taskdef uri="antlib:org.apache.maven.resolver.ant" resource="org/apache/maven/resolver/ant/antlib.xml">
<classpath>
Expand Down
2 changes: 1 addition & 1 deletion loader/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<groupId>org.lucee</groupId>
<artifactId>lucee</artifactId>
<version>6.2.0.196-SNAPSHOT</version>
<version>6.2.0.197-SNAPSHOT</version>
<packaging>jar</packaging>

<name>Lucee Loader Build</name>
Expand Down
3 changes: 1 addition & 2 deletions test/tickets/LDEV2793.cfc
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ component extends="org.lucee.cfml.test.LuceeTestCase" labels="java" {

});

it( title='test parseDateTime (preciseMath=false)', skip=true, body=function() {
it( title='test parseDateTime (preciseMath=false)', body=function() {
application action="update" preciseMath=false;
var projects = [
{
Expand Down Expand Up @@ -73,5 +73,4 @@ component extends="org.lucee.cfml.test.LuceeTestCase" labels="java" {
});
});
}

}

0 comments on commit 8673611

Please sign in to comment.