jkesselm 01/10/17 07:27:53
Modified: java/src/org/apache/xpath/objects XStringForFSB.java
Log:
Avoid overgenerality of isDigits
Combine most character tests into a single switch for performance
Reject values with embedded (as opposed to leading/trailing) whitespace.
Note that this is "more correct:" than the old solution of calling Java's
toDouble, since (I think) XSLT doesn't officially accept leading + or
scientific notation. If folks want to accept those notations, an
extension function specifically for that purpose seems the
simplest/safest/most-portable solution.
Revision Changes Path
1.6 +26 -13
xml-xalan/java/src/org/apache/xpath/objects/XStringForFSB.java
Index: XStringForFSB.java
===================================================================
RCS file:
/home/cvs/xml-xalan/java/src/org/apache/xpath/objects/XStringForFSB.java,v
retrieving revision 1.5
retrieving revision 1.6
diff -u -r1.5 -r1.6
--- XStringForFSB.java 2001/10/15 15:48:23 1.5
+++ XStringForFSB.java 2001/10/17 14:27:53 1.6
@@ -984,11 +984,12 @@
long longResult=0;
boolean isNegative=false;
+ boolean trailingSpace=false;
int[] digitsFound={0,0}; // intpart,fracpart
int digitType=0; // Index to which kind of digit we're accumulating
double doubleResult;
- // Scan past whitespace characters
+ // Scan past leading whitespace characters
while(start< end &&
XMLCharacterRecognizer.isWhiteSpace( fsb.charAt(start) )
)
@@ -1005,26 +1006,38 @@
{
char c = fsb.charAt(i);
- if (c == '.')
+ if(XMLCharacterRecognizer.isWhiteSpace(c))
{
+ trailngSpace=true;
+ break; // Trailing whitespace is ignored
+ }
+ else if(trailingSpace)
+ return Double.NaN; // Nonspace after space is poorly formed
+
+ switch(c)
+ {
+ case '.':
if(digitType==0)
digitType=1;
else
return Double.NaN; // Second period is error
- }
-
- // Should embedded whitespace _REALLY_ be ignored???
- else if(XMLCharacterRecognizer.isWhiteSpace(c))
- break; // Whitespace is ignored
-
- else if (Character.isDigit(c))
- {
+ break;
+
+ case '0': // NOT Unicode isDigit(); ASCII digits
_only_
+ case '1':
+ case '2':
+ case '3':
+ case '4':
+ case '5':
+ case '6':
+ case '7':
+ case '8':
+ case '9':
longResult = longResult * 10 + (c - '0'); // Accumulate as int
++digitsFound[digitType]; // Remember scaling
- }
+ break;
- else
- {
+ default:
return Double.NaN; // Nonnumeric is error
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]