/** * Converts a string to an integer. * @param string A string to convert into a number. * @param radix A value between 2 and 36 that specifies the base of the number in `string`. * If this argument is not supplied, strings with a prefix of '0x' are considered hexadecimal. * All other strings are considered decimal. */ declarefunctionparseInt(string: string, radix?: number): number;
/** * Converts a string to a floating-point number. * @param string A string that contains a floating-point number. */ declarefunctionparseFloat(string: string): number;
// 入口,方法定义 ALWAYS_INLINE staticdoubleparseInt(StringView s, int radix) { if (s.is8Bit()) return parseInt(s, s.characters8(), radix); return parseInt(s, s.characters16(), radix); }
// ES5.1 15.1.2.2 template <typename CharType> ALWAYS_INLINE staticdoubleparseInt(StringView s, const CharType* data, int radix) { // 1. Let inputString be ToString(string). // 2. Let S be a newly created substring of inputString consisting of the first character that is not a // StrWhiteSpaceChar and all characters following that character. (In other words, remove leading white // space.) If inputString does not contain any such characters, let S be the empty string. int length = s.length(); int p = 0; while (p < length && isStrWhiteSpace(data[p])) ++p;
// 3. Let sign be 1. // 4. If S is not empty and the first character of S is a minus sign -, let sign be -1. // 5. If S is not empty and the first character of S is a plus sign + or a minus sign -, then remove the first character from S. double sign = 1; if (p < length) { if (data[p] == '+') ++p; elseif (data[p] == '-') { sign = -1; ++p; } }
// 6. Let R = ToInt32(radix). // 7. Let stripPrefix be true. // 8. If R != 0,then // b. If R != 16, let stripPrefix be false. // 9. Else, R == 0 // a. LetR = 10. // 10. If stripPrefix is true, then // a. If the length of S is at least 2 and the first two characters of S are either ―0x or ―0X, // then remove the first two characters from S and let R = 16. // 11. If S contains any character that is not a radix-R digit, then let Z be the substring of S // consisting of all characters before the first such character; otherwise, let Z be S. if ((radix == 0 || radix == 16) && length - p >= 2 && data[p] == '0' && (data[p + 1] == 'x' || data[p + 1] == 'X')) { radix = 16; p += 2; } elseif (radix == 0) radix = 10;
// 8.a If R < 2 or R > 36, then return NaN. if (radix < 2 || radix > 36) return PNaN;
// 13. Let mathInt be the mathematical integer value that is represented by Z in radix-R notation, using the letters // A-Z and a-z for digits with values 10 through 35. (However, if R is 10 and Z contains more than 20 significant // digits, every significant digit after the 20th may be replaced by a 0 digit, at the option of the implementation; // and if R is not 2, 4, 8, 10, 16, or 32, then mathInt may be an implementation-dependent approximation to the // mathematical integer value that is represented by Z in radix-R notation.) // 14. Let number be the Number value for mathInt. int firstDigitPosition = p; bool sawDigit = false; double number = 0; while (p < length) { int digit = parseDigit(data[p], radix); if (digit == -1) break; sawDigit = true; number *= radix; number += digit; ++p; }
// 12. If Z is empty, return NaN. if (!sawDigit) return PNaN;
// Alternate code path for certain large numbers. if (number >= mantissaOverflowLowerBound) { if (radix == 10) { size_t parsedLength; number = parseDouble(s.substring(firstDigitPosition, p - firstDigitPosition), parsedLength); } elseif (radix == 2 || radix == 4 || radix == 8 || radix == 16 || radix == 32) number = parseIntOverflow(s.substring(firstDigitPosition, p - firstDigitPosition), radix); }
PASS parseInt('123') is 123 PASS parseInt('123x4') is 123 PASS parseInt('-123') is -123 PASS parseInt('0x123') is 0x123 PASS parseInt('0x123x4') is 0x123 PASS parseInt('-0x123x4') is -0x123 PASS parseInt('-') is Number.NaN PASS parseInt('0x') is Number.NaN PASS parseInt('-0x') is Number.NaN PASS parseInt('123', undefined) is 123 PASS parseInt('123', null) is 123 PASS parseInt('123', 0) is 123 PASS parseInt('123', 10) is 123 PASS parseInt('123', 16) is 0x123 PASS parseInt('0x123', undefined) is 0x123 PASS parseInt('0x123', null) is 0x123 PASS parseInt('0x123', 0) is 0x123 PASS parseInt('0x123', 10) is 0 PASS parseInt('0x123', 16) is 0x123 PASS parseInt(Math.pow(10, 20)) is 100000000000000000000 PASS parseInt(Math.pow(10, 21)) is 1 PASS parseInt(Math.pow(10, -6)) is 0 PASS parseInt(Math.pow(10, -7)) is 1 PASS parseInt(-Math.pow(10, 20)) is -100000000000000000000 PASS parseInt(-Math.pow(10, 21)) is -1 PASS parseInt(-Math.pow(10, -6)) is -0 PASS parseInt(-Math.pow(10, -7)) is -1 PASS parseInt('0') is 0 PASS parseInt('-0') is -0 PASS parseInt(0) is 0 PASS parseInt(-0) is 0 PASS parseInt(2147483647) is 2147483647 PASS parseInt(2147483648) is 2147483648 PASS parseInt('2147483647') is 2147483647 PASS parseInt('2147483648') is 2147483648 PASS state = null; try { parseInt('123', throwingRadix); } catch (e) {} state; is "throwingRadix" PASS state = null; try { parseInt(throwingString, throwingRadix); } catch (e) {} state; is "throwingString"
PASS parseFloat() is NaN PASS parseFloat('') is NaN PASS parseFloat(' ') is NaN PASS parseFloat(' 0') is 0 PASS parseFloat('0 ') is 0 PASS parseFloat('x0') is NaN PASS parseFloat('0x') is 0 PASS parseFloat(' 1') is 1 PASS parseFloat('1 ') is 1 PASS parseFloat('x1') is NaN PASS parseFloat('1x') is 1 PASS parseFloat(' 2.3') is 2.3 PASS parseFloat('2.3 ') is 2.3 PASS parseFloat('x2.3') is NaN PASS parseFloat('2.3x') is 2.3 PASS parseFloat('0x2') is 0 PASS parseFloat('1' + nonASCIINonSpaceCharacter) is 1 PASS parseFloat(nonASCIINonSpaceCharacter + '1') is NaN PASS parseFloat('1' + illegalUTF16Sequence) is 1 PASS parseFloat(illegalUTF16Sequence + '1') is NaN PASS parseFloat(tab + '1') is 1 PASS parseFloat(nbsp + '1') is 1 PASS parseFloat(ff + '1') is 1 PASS parseFloat(vt + '1') is 1 PASS parseFloat(cr + '1') is 1 PASS parseFloat(lf + '1') is 1 PASS parseFloat(ls + '1') is 1 PASS parseFloat(ps + '1') is 1 PASS parseFloat(oghamSpaceMark + '1') is 1 PASS parseFloat(mongolianVowelSeparator + '1') is NaN PASS parseFloat(enQuad + '1') is 1 PASS parseFloat(emQuad + '1') is 1 PASS parseFloat(enSpace + '1') is 1 PASS parseFloat(emSpace + '1') is 1 PASS parseFloat(threePerEmSpace + '1') is 1 PASS parseFloat(fourPerEmSpace + '1') is 1 PASS parseFloat(sixPerEmSpace + '1') is 1 PASS parseFloat(figureSpace + '1') is 1 PASS parseFloat(punctuationSpace + '1') is 1 PASS parseFloat(thinSpace + '1') is 1 PASS parseFloat(hairSpace + '1') is 1 PASS parseFloat(narrowNoBreakSpace + '1') is 1 PASS parseFloat(mediumMathematicalSpace + '1') is 1 PASS parseFloat(ideographicSpace + '1') is 1
Author
My name is Micheal Wayne and this is my blog.
I am a front-end software engineer.
Contact: michealwayne@163.com