follow-up on my incorrect number parsing patch
Darin Adler
darin at apple.com
Thu Dec 11 05:56:36 CET 2003
Here's how I fixed the incorrect number parsing patch I sent a while
back. Maybe there's an even more elegant solution to this problem. I
suspect that tolerating empty strings in all the other numeric
conversion calls could be trouble, so over time we may find more places
where we need to be intolerant, but this restricts the change to the
number parsing in the regular expression replace function.
-------------- next part --------------
Index: ChangeLog
===================================================================
RCS file: /local/home/cvs/Labyrinth/JavaScriptCore/ChangeLog,v
retrieving revision 1.411
diff -p -u -u -p -r1.411 ChangeLog
--- ChangeLog 2003/12/11 02:20:43 1.411
+++ ChangeLog 2003/12/11 04:55:02
@@ -1,3 +1,20 @@
+2003-12-10 Darin Adler <darin at apple.com>
+
+ Reviewed by Maciej.
+
+ - fixed regression in JavaScript tests reported by the KDE guys
+ - fixed 3506345: REGRESSION (115-116): VIP: chordfind.com no longer displays chords
+
+ * kjs/ustring.h: Add tolerateEmptyString parameter to toDouble and toULong.
+ * kjs/ustring.cpp:
+ (KJS::UString::toDouble): Separate the "tolerant" parameter into two separate ones:
+ tolerateTrailingJunk and tolerateEmptyString. Add new overloads; better for code size
+ and binary compatibility than default parameter values.
+ (KJS::UString::toULong): Pass tolerateEmptyString down to toDouble. Add new overload.
+
+ * kjs/string_object.cpp: (StringProtoFuncImp::call): Pass false for the new
+ "tolerate empty string" parameter.
+
2003-12-10 Richard Williamson <rjw at apple.com>
Added code to manage reference counting of JavaScript
Index: kjs/string_object.cpp
===================================================================
RCS file: /local/home/cvs/Labyrinth/JavaScriptCore/kjs/string_object.cpp,v
retrieving revision 1.19
diff -p -u -u -p -r1.19 kjs/string_object.cpp
--- kjs/string_object.cpp 2003/11/21 08:54:42 1.19
+++ kjs/string_object.cpp 2003/12/11 04:55:02
@@ -338,7 +338,7 @@ Value StringProtoFuncImp::call(ExecState
continue;
}
// Assume number part is one char exactly
- unsigned long pos = rstr.substr(i+1,1).toULong(&ok);
+ unsigned long pos = rstr.substr(i+1,1).toULong(&ok, false /* tolerate empty string */);
if (ok && pos <= (unsigned)reg->subPatterns()) {
rstr = rstr.substr(0,i)
+ u.substr((*ovector)[2*pos],
Index: kjs/ustring.cpp
===================================================================
RCS file: /local/home/cvs/Labyrinth/JavaScriptCore/kjs/ustring.cpp,v
retrieving revision 1.37
diff -p -u -u -p -r1.37 kjs/ustring.cpp
--- kjs/ustring.cpp 2003/12/02 16:04:55 1.37
+++ kjs/ustring.cpp 2003/12/11 04:55:03
@@ -617,10 +617,12 @@ UCharReference UString::operator[](int p
return UCharReference(this, pos);
}
-double UString::toDouble( bool tolerant ) const
+double UString::toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const
{
double d;
+ // FIXME: If tolerateTrailingJunk is true, then we want to tolerate non-8-bit junk
+ // after the number, so is8Bit is too strict a check.
if (!is8Bit())
return NaN;
@@ -632,7 +634,7 @@ double UString::toDouble( bool tolerant
// empty string ?
if (*c == '\0')
- return tolerant ? 0.0 : NaN;
+ return tolerateEmptyString ? 0.0 : NaN;
// hex number ?
if (*c == '0' && (*(c+1) == 'x' || *(c+1) == 'X')) {
@@ -672,15 +674,25 @@ double UString::toDouble( bool tolerant
while (isspace(*c))
c++;
// don't allow anything after - unless tolerant=true
- if ( !tolerant && *c != '\0')
+ if (!tolerateTrailingJunk && *c != '\0')
d = NaN;
return d;
}
-unsigned long UString::toULong(bool *ok) const
+double UString::toDouble(bool tolerateTrailingJunk) const
+{
+ return toDouble(tolerateTrailingJunk, true);
+}
+
+double UString::toDouble() const
{
- double d = toDouble();
+ return toDouble(false, true);
+}
+
+unsigned long UString::toULong(bool *ok, bool tolerateEmptyString) const
+{
+ double d = toDouble(false, tolerateEmptyString);
bool b = true;
if (isNaN(d) || d != static_cast<unsigned long>(d)) {
@@ -692,6 +704,11 @@ unsigned long UString::toULong(bool *ok)
*ok = b;
return static_cast<unsigned long>(d);
+}
+
+unsigned long UString::toULong(bool *ok) const
+{
+ return toULong(ok, true);
}
uint32_t UString::toUInt32(bool *ok) const
Index: kjs/ustring.h
===================================================================
RCS file: /local/home/cvs/Labyrinth/JavaScriptCore/kjs/ustring.h,v
retrieving revision 1.28
diff -p -u -u -p -r1.28 kjs/ustring.h
--- kjs/ustring.h 2003/08/18 18:51:25 1.28
+++ kjs/ustring.h 2003/12/11 04:55:03
@@ -391,13 +391,18 @@ namespace KJS {
* the algorithm will recognize hexadecimal representations (as
* indicated by a 0x or 0X prefix) and +/- Infinity.
* Returns NaN if the conversion failed.
- * @param tolerant if true, toDouble can tolerate garbage after the number.
+ * @param tolerateTrailingJunk if true, toDouble can tolerate garbage after the number.
+ * @param tolerateEmptyString if false, toDouble will turn an empty string into NaN rather than 0.
*/
- double toDouble(bool tolerant=false) const;
+ double toDouble(bool tolerateTrailingJunk, bool tolerateEmptyString) const;
+ double toDouble(bool tolerateTrailingJunk) const;
+ double toDouble() const;
/**
* Attempts an conversion to an unsigned long integer. ok will be set
* according to the success.
+ * @param tolerateEmptyString if false, toULong will return false for *ok for an empty string.
*/
+ unsigned long toULong(bool *ok, bool tolerateEmptyString) const;
unsigned long toULong(bool *ok = 0) const;
uint32_t toUInt32(bool *ok = 0) const;
-------------- next part --------------
-- Darin
More information about the Khtml-devel
mailing list