You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@vxquery.apache.org by pr...@apache.org on 2012/08/01 23:59:07 UTC

svn commit: r1368292 - in /incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast: CastToDoubleOperation.java CastToFloatOperation.java

Author: prestonc
Date: Wed Aug  1 21:59:06 2012
New Revision: 1368292

URL: http://svn.apache.org/viewvc?rev=1368292&view=rev
Log:
Added support for INF, -INF and NaN values in the convertString.

Modified:
    incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToDoubleOperation.java
    incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToFloatOperation.java

Modified: incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToDoubleOperation.java
URL: http://svn.apache.org/viewvc/incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToDoubleOperation.java?rev=1368292&r1=1368291&r2=1368292&view=diff
==============================================================================
--- incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToDoubleOperation.java (original)
+++ incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToDoubleOperation.java Wed Aug  1 21:59:06 2012
@@ -5,6 +5,7 @@ import java.io.IOException;
 
 import org.apache.vxquery.datamodel.accessors.atomic.XSDecimalPointable;
 import org.apache.vxquery.datamodel.values.ValueTag;
+import org.apache.vxquery.exceptions.ErrorCode;
 import org.apache.vxquery.exceptions.SystemException;
 import org.apache.vxquery.runtime.functions.strings.ICharacterIterator;
 import org.apache.vxquery.runtime.functions.strings.UTF8StringCharacterIterator;
@@ -58,7 +59,9 @@ public class CastToDoubleOperation exten
         byte decimalPlace = 0;
         long value = 0;
         boolean pastDecimal = false, negativeValue = false;
-        int c = 0;
+        int c = ICharacterIterator.EOS_CHAR;
+        int c2 = ICharacterIterator.EOS_CHAR;
+        int c3 = ICharacterIterator.EOS_CHAR;
         while ((c = charIterator.next()) != ICharacterIterator.EOS_CHAR) {
             if (Character.isDigit(c)) {
                 value = value * 10 + Character.getNumericValue(c);
@@ -67,29 +70,66 @@ public class CastToDoubleOperation exten
                 }
             } else if (c == Character.valueOf('-')) {
                 negativeValue = true;
-            } else if (c == Character.valueOf('E')) {
+            } else if (c == Character.valueOf('E') || c == Character.valueOf('e')) {
                 break;
-            } else {
+            } else if (c == Character.valueOf('.')) {
                 pastDecimal = true;
+            } else if (c == Character.valueOf('I') || c == Character.valueOf('N') && value == 0) {
+                break;
+            } else {
+                throw new SystemException(ErrorCode.FORG0001);
             }
         }
-        if (c == Character.valueOf('E')) {
+        if (c == Character.valueOf('E') || c == Character.valueOf('e')) {
             int moveOffset = 0;
+            boolean offsetNegative = false;
             while ((c = charIterator.next()) != ICharacterIterator.EOS_CHAR) {
                 if (Character.isDigit(c)) {
                     moveOffset = moveOffset * 10 + Character.getNumericValue(c);
                 } else if (c == Character.valueOf('-')) {
-                    moveOffset = moveOffset * -1;
+                    offsetNegative = true;
                 } else {
-                    break;
+                    throw new SystemException(ErrorCode.FORG0001);
                 }
             }
+            if (offsetNegative) {
+                moveOffset *= -1;
+            }
             decimalPlace += moveOffset;
         }
-        if (negativeValue) {
-            value *= -1;
+        double valueDouble;
+        if (c == Character.valueOf('I') || c == Character.valueOf('N')) {
+            c2 = charIterator.next();
+            c3 = charIterator.next();
+            if (charIterator.next() != ICharacterIterator.EOS_CHAR) {
+                throw new SystemException(ErrorCode.FORG0001);
+            } else if (c == Character.valueOf('I') && c2 == Character.valueOf('N') && c3 == Character.valueOf('F')) {
+                if (negativeValue) {
+                    valueDouble = Double.NEGATIVE_INFINITY;
+                } else {
+                    valueDouble = Double.POSITIVE_INFINITY;
+                }
+            } else if (c == Character.valueOf('N') && c2 == Character.valueOf('a') && c3 == Character.valueOf('N')) {
+                valueDouble = Double.NaN;
+            } else {
+                throw new SystemException(ErrorCode.FORG0001);
+            }
+        } else {
+            if (negativeValue) {
+                value *= -1;
+            }
+            valueDouble = value;
+            while (decimalPlace != 0) {
+                if (decimalPlace > 0) {
+                    --decimalPlace;
+                    valueDouble *= 10;
+                }
+                else {
+                    ++decimalPlace;
+                    valueDouble /= 10;
+                }
+            }
         }
-        double valueDouble = value * Math.pow(10, decimalPlace);
 
         dOut.write(ValueTag.XS_DOUBLE_TAG);
         dOut.writeDouble(valueDouble);

Modified: incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToFloatOperation.java
URL: http://svn.apache.org/viewvc/incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToFloatOperation.java?rev=1368292&r1=1368291&r2=1368292&view=diff
==============================================================================
--- incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToFloatOperation.java (original)
+++ incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToFloatOperation.java Wed Aug  1 21:59:06 2012
@@ -5,6 +5,7 @@ import java.io.IOException;
 
 import org.apache.vxquery.datamodel.accessors.atomic.XSDecimalPointable;
 import org.apache.vxquery.datamodel.values.ValueTag;
+import org.apache.vxquery.exceptions.ErrorCode;
 import org.apache.vxquery.exceptions.SystemException;
 import org.apache.vxquery.runtime.functions.strings.ICharacterIterator;
 import org.apache.vxquery.runtime.functions.strings.UTF8StringCharacterIterator;
@@ -59,6 +60,9 @@ public class CastToFloatOperation extend
         long value = 0;
         boolean pastDecimal = false, negativeValue = false;
         int c = ICharacterIterator.EOS_CHAR;
+        int c2 = ICharacterIterator.EOS_CHAR;
+        int c3 = ICharacterIterator.EOS_CHAR;
+
         while ((c = charIterator.next()) != ICharacterIterator.EOS_CHAR) {
             if (Character.isDigit(c)) {
                 value = value * 10 + Character.getNumericValue(c);
@@ -67,29 +71,56 @@ public class CastToFloatOperation extend
                 }
             } else if (c == Character.valueOf('-')) {
                 negativeValue = true;
-            } else if (c == Character.valueOf('E')) {
+            } else if (c == Character.valueOf('E') || c == Character.valueOf('e')) {
                 break;
-            } else {
+            } else if (c == Character.valueOf('.')) {
                 pastDecimal = true;
+            } else if (c == Character.valueOf('I') || c == Character.valueOf('N') && value == 0) {
+                break;
+            } else {
+                throw new SystemException(ErrorCode.FORG0001);
             }
         }
-        if (c == Character.valueOf('E')) {
+        if (c == Character.valueOf('E') || c == Character.valueOf('e')) {
             int moveOffset = 0;
+            boolean offsetNegative = false;
             while ((c = charIterator.next()) != ICharacterIterator.EOS_CHAR) {
                 if (Character.isDigit(c)) {
                     moveOffset = moveOffset * 10 + Character.getNumericValue(c);
                 } else if (c == Character.valueOf('-')) {
-                    moveOffset = moveOffset * -1;
+                    offsetNegative = true;
                 } else {
-                    break;
+                    throw new SystemException(ErrorCode.FORG0001);
                 }
             }
+            if (offsetNegative) {
+                moveOffset *= -1;
+            }
             decimalPlace += moveOffset;
         }
-        if (negativeValue) {
-            value *= -1;
+        float valueFloat;
+        if (c == Character.valueOf('I') || c == Character.valueOf('N')) {
+            c2 = charIterator.next();
+            c3 = charIterator.next();
+            if (charIterator.next() != ICharacterIterator.EOS_CHAR) {
+                throw new SystemException(ErrorCode.FORG0001);
+            } else if (c == Character.valueOf('I') && c2 == Character.valueOf('N') && c3 == Character.valueOf('F')) {
+                if (negativeValue) {
+                    valueFloat = Float.NEGATIVE_INFINITY;
+                } else {
+                    valueFloat = Float.POSITIVE_INFINITY;
+                }
+            } else if (c == Character.valueOf('N') && c2 == Character.valueOf('a') && c3 == Character.valueOf('N')) {
+                valueFloat = Float.NaN;
+            } else {
+                throw new SystemException(ErrorCode.FORG0001);
+            }
+        } else {
+            if (negativeValue) {
+                value *= -1;
+            }
+            valueFloat = (float) (value * Math.pow(10, decimalPlace));
         }
-        float valueFloat = (float) (value * Math.pow(10, decimalPlace));
 
         dOut.write(ValueTag.XS_FLOAT_TAG);
         dOut.writeFloat(valueFloat);