看了下firefox的parseInt的源码,确实有负数判断啊
static bool
ParseIntStringHelper(JSContext *cx, const jschar *ws, const jschar *end, int maybeRadix,
bool stripPrefix, jsdouble *dp)
{
JS_ASSERT(maybeRadix == 0 || (2 <= maybeRadix && maybeRadix <= 36));
JS_ASSERT(ws <= end);
const jschar *s = js_SkipWhiteSpace(ws, end);
JS_ASSERT(ws <= s);
JS_ASSERT(s <= end);
/* 15.1.2.2 steps 3-4. 这里就是负数判断,何来的不认一说呢*/
bool negative = (s != end && s[0] == '-');
/* 15.1.2.2 step 5. */
if (s != end && (s[0] == '-' || s[0] == '+'))
s++;
/* 15.1.2.2 step 9. */
int radix = maybeRadix;
if (radix == 0) {
if (end - s >= 2 && s[0] == '0' && (s[1] != 'x' && s[1] != 'X')) {
/*
* Non-standard: ES5 requires that parseInt interpret leading-zero
* strings not starting with "0x" or "0X" as decimal (absent an
* explicitly specified non-zero radix), but we continue to
* interpret such strings as octal, as per ES3 and web practice.
*/
radix = 8;
} else {
radix = 10;
}
}
/* 15.1.2.2 step 10. */
if (stripPrefix) {
if (end - s >= 2 && s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) {
s += 2;
radix = 16;
}
}
/* 15.1.2.2 steps 11-14. */
const jschar *actualEnd;
if (!GetPrefixInteger(cx, s, end, radix, &actualEnd, dp))
return false;
if (s == actualEnd)
*dp = js_NaN;
else if (negative)
*dp = -*dp;
return true;
}
补充下图片