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:26 UTC
svn commit: r1369974 - in
/incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast:
CastToDecimalOperation.java CastToFloatOperation.java
CastToIntegerOperation.java
Author: prestonc
Date: Mon Aug 6 20:21:26 2012
New Revision: 1369974
URL: http://svn.apache.org/viewvc?rev=1369974&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/CastToDecimalOperation.java
incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToFloatOperation.java
incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToIntegerOperation.java
Modified: incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToDecimalOperation.java
URL: http://svn.apache.org/viewvc/incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToDecimalOperation.java?rev=1369974&r1=1369973&r2=1369974&view=diff
==============================================================================
--- incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToDecimalOperation.java (original)
+++ incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToDecimalOperation.java Mon Aug 6 20:21:26 2012
@@ -117,7 +117,7 @@ public class CastToDecimalOperation exte
// Normalize the value and take off trailing zeros.
while (value != 0 && value % 10 == 0) {
- value -= 10;
+ value /= 10;
--decimalPlace;
}
dOut.write(ValueTag.XS_DECIMAL_TAG);
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=1369974&r1=1369973&r2=1369974&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 Mon Aug 6 20:21:26 2012
@@ -58,81 +58,83 @@ public class CastToFloatOperation extend
charIterator.reset();
byte decimalPlace = 0;
long value = 0;
+ float valueFloat;
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();
}
- float valueFloat;
+ // 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) {
- valueFloat = Float.NEGATIVE_INFINITY;
- } else {
- valueFloat = Float.POSITIVE_INFINITY;
- }
+ valueFloat = (negativeValue ? Float.NEGATIVE_INFINITY : 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;
+ // Read in the number.
+ do {
+ if (Character.isDigit(c)) {
+ 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);
}
- valueFloat = value;
- while (decimalPlace != 0) {
+
+ valueFloat = (float) value;
+ while (decimalPlace != 0 && valueFloat != 0) {
if (decimalPlace > 0) {
--decimalPlace;
valueFloat *= 10;
} else {
++decimalPlace;
- valueFloat *= 0.1;
+ valueFloat /= 10;
}
}
}
dOut.write(ValueTag.XS_FLOAT_TAG);
- dOut.writeFloat(valueFloat);
+ dOut.writeFloat((negativeValue ? -valueFloat : valueFloat));
}
@Override
Modified: incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToIntegerOperation.java
URL: http://svn.apache.org/viewvc/incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToIntegerOperation.java?rev=1369974&r1=1369973&r2=1369974&view=diff
==============================================================================
--- incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToIntegerOperation.java (original)
+++ incubator/vxquery/trunk/vxquery/vxquery-core/src/main/java/org/apache/vxquery/runtime/functions/cast/CastToIntegerOperation.java Mon Aug 6 20:21:26 2012
@@ -69,21 +69,25 @@ public class CastToIntegerOperation exte
long value = 0;
int c = 0;
boolean negative = false;
- while ((c = charIterator.next()) != ICharacterIterator.EOS_CHAR) {
+
+ // Check the first character.
+ c = charIterator.next();
+ if (c == Character.valueOf('-')) {
+ negative = true;
+ c = charIterator.next();
+ }
+
+ // Read the numeric value.
+ do {
if (Character.isDigit(c)) {
value = value * 10 + Character.getNumericValue(c);
- } else if (c == Character.valueOf('-')) {
- negative = true;
} else {
throw new SystemException(ErrorCode.FORG0001);
}
- }
- if (negative) {
- value *= -1;
- }
+ } while ((c = charIterator.next()) != ICharacterIterator.EOS_CHAR);
dOut.write(ValueTag.XS_INTEGER_TAG);
- dOut.writeLong(value);
+ dOut.writeLong((negative ? -value : value));
}
@Override