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/06 22:21:51 UTC

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

Author: prestonc
Date: Mon Aug  6 20:21:51 2012
New Revision: 1369976

URL: http://svn.apache.org/viewvc?rev=1369976&view=rev
Log:
The string conversion functions now consider character order in addition to the type of character. For example: A negative sign must be at the beginning of the value.

Modified:
    incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToDoubleOperation.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=1369976&r1=1369975&r2=1369976&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 Mon Aug  6 20:21:51 2012
@@ -58,81 +58,90 @@ public class CastToDoubleOperation exten
         charIterator.reset();
         byte decimalPlace = 0;
         long value = 0;
+        double valueDouble;
         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);
-                if (pastDecimal) {
-                    decimalPlace--;
-                }
-            } else if (c == Character.valueOf('-')) {
-                negativeValue = true;
-            } else if (c == Character.valueOf('E') || c == Character.valueOf('e')) {
-                break;
-            } 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') || 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('-')) {
-                    offsetNegative = true;
-                } else {
-                    throw new SystemException(ErrorCode.FORG0001);
-                }
-            }
-            if (offsetNegative) {
-                moveOffset *= -1;
-            }
-            decimalPlace += moveOffset;
+
+        // Check sign.
+        c = charIterator.next();
+        if (c == Character.valueOf('-')) {
+            negativeValue = true;
+            c = charIterator.next();
         }
-        double valueDouble;
+        // Check the special cases.
         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;
-                }
+                valueDouble = (negativeValue ? Double.NEGATIVE_INFINITY : 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;
+            // Read in the number.
+            do {
+                if (Character.isDigit(c)) {
+                    if (value > Long.MAX_VALUE / 10) {
+                        throw new SystemException(ErrorCode.FOCA0006);
+                    }
+                    value = value * 10 + Character.getNumericValue(c);
+                    if (pastDecimal) {
+                        decimalPlace--;
+                    }
+                } else if (c == Character.valueOf('.') && pastDecimal == false) {
+                    pastDecimal = true;
+                } else if (c == Character.valueOf('E') || c == Character.valueOf('e')) {
+                    break;
+                } else {
+                    throw new SystemException(ErrorCode.FORG0001);
+                }
+            } while ((c = charIterator.next()) != ICharacterIterator.EOS_CHAR);
+
+            // Parse the exponent.
+            if (c == Character.valueOf('E') || c == Character.valueOf('e')) {
+                int moveOffset = 0;
+                boolean negativeOffset = false;
+                // Check for the negative sign.
+                c = charIterator.next();
+                if (c == Character.valueOf('-')) {
+                    negativeOffset = true;
+                    c = charIterator.next();
+                }
+                // Process the numeric value.
+                do {
+                    if (Character.isDigit(c)) {
+                        moveOffset = moveOffset * 10 + Character.getNumericValue(c);
+                    } else {
+                        throw new SystemException(ErrorCode.FORG0001);
+                    }
+                } while ((c = charIterator.next()) != ICharacterIterator.EOS_CHAR);
+                decimalPlace += (negativeOffset ? -moveOffset : moveOffset);
+                if (decimalPlace > 324 || decimalPlace < -324) {
+                    throw new SystemException(ErrorCode.FOCA0006);
+                }
             }
-            valueDouble = value;
-            while (decimalPlace != 0) {
+
+            // TODO Verify the long value and exponent are combined to give the correct double.
+            valueDouble = (double) value;
+            while (decimalPlace != 0 && valueDouble != 0) {
                 if (decimalPlace > 0) {
                     --decimalPlace;
                     valueDouble *= 10;
-                }
-                else {
+                } else {
                     ++decimalPlace;
                     valueDouble /= 10;
                 }
             }
-        }
 
+        }
         dOut.write(ValueTag.XS_DOUBLE_TAG);
-        dOut.writeDouble(valueDouble);
+        dOut.writeDouble((negativeValue ? -valueDouble : valueDouble));
     }
 
     @Override