OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2010 Apple Inc. All rights reserved. | 2 * Copyright (C) 2010 Apple Inc. All rights reserved. |
3 * | 3 * |
4 * Redistribution and use in source and binary forms, with or without | 4 * Redistribution and use in source and binary forms, with or without |
5 * modification, are permitted provided that the following conditions | 5 * modification, are permitted provided that the following conditions |
6 * are met: | 6 * are met: |
7 * 1. Redistributions of source code must retain the above copyright | 7 * 1. Redistributions of source code must retain the above copyright |
8 * notice, this list of conditions and the following disclaimer. | 8 * notice, this list of conditions and the following disclaimer. |
9 * 2. Redistributions in binary form must reproduce the above copyright | 9 * 2. Redistributions in binary form must reproduce the above copyright |
10 * notice, this list of conditions and the following disclaimer in the | 10 * notice, this list of conditions and the following disclaimer in the |
(...skipping 77 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
88 | 88 |
89 String serializeForNumberType(double number) | 89 String serializeForNumberType(double number) |
90 { | 90 { |
91 // According to HTML5, "the best representation of the number n as a floatin
g | 91 // According to HTML5, "the best representation of the number n as a floatin
g |
92 // point number" is a string produced by applying ToString() to n. | 92 // point number" is a string produced by applying ToString() to n. |
93 return String::numberToStringECMAScript(number); | 93 return String::numberToStringECMAScript(number); |
94 } | 94 } |
95 | 95 |
96 Decimal parseToDecimalForNumberType(const String& string, const Decimal& fallbac
kValue) | 96 Decimal parseToDecimalForNumberType(const String& string, const Decimal& fallbac
kValue) |
97 { | 97 { |
98 // See HTML5 2.5.4.3 `Real numbers.' and parseToDoubleForNumberType | 98 // http://www.whatwg.org/specs/web-apps/current-work/#floating-point-numbers
and parseToDoubleForNumberType |
99 | |
100 // String::toDouble() accepts leading + and whitespace characters, which are
not valid here. | 99 // String::toDouble() accepts leading + and whitespace characters, which are
not valid here. |
101 const UChar firstCharacter = string[0]; | 100 const UChar firstCharacter = string[0]; |
102 if (firstCharacter != '-' && firstCharacter != '.' && !isASCIIDigit(firstCha
racter)) | 101 if (firstCharacter != '-' && firstCharacter != '.' && !isASCIIDigit(firstCha
racter)) |
103 return fallbackValue; | 102 return fallbackValue; |
104 | 103 |
105 const Decimal value = Decimal::fromString(string); | 104 const Decimal value = Decimal::fromString(string); |
106 if (!value.isFinite()) | 105 if (!value.isFinite()) |
107 return fallbackValue; | 106 return fallbackValue; |
108 | 107 |
109 // Numbers are considered finite IEEE 754 single-precision floating point va
lues. | 108 // Numbers are considered finite IEEE 754 Double-precision floating point va
lues. |
110 // See HTML5 2.5.4.3 `Real numbers.' | 109 const Decimal doubleMax = Decimal::fromDouble(std::numeric_limits<double>::m
ax()); |
111 // FIXME: We should use numeric_limits<double>::max for number input type. | 110 if (value < -doubleMax || value > doubleMax) |
112 const Decimal floatMax = Decimal::fromDouble(std::numeric_limits<float>::max
()); | |
113 if (value < -floatMax || value > floatMax) | |
114 return fallbackValue; | 111 return fallbackValue; |
115 | 112 |
116 // We return +0 for -0 case. | 113 // We return +0 for -0 case. |
117 return value.isZero() ? Decimal(0) : value; | 114 return value.isZero() ? Decimal(0) : value; |
118 } | 115 } |
119 | 116 |
120 double parseToDoubleForNumberType(const String& string, double fallbackValue) | 117 double parseToDoubleForNumberType(const String& string, double fallbackValue) |
121 { | 118 { |
122 // See HTML5 2.5.4.3 `Real numbers.' | 119 // http://www.whatwg.org/specs/web-apps/current-work/#floating-point-numbers |
123 | |
124 // String::toDouble() accepts leading + and whitespace characters, which are
not valid here. | 120 // String::toDouble() accepts leading + and whitespace characters, which are
not valid here. |
125 UChar firstCharacter = string[0]; | 121 UChar firstCharacter = string[0]; |
126 if (firstCharacter != '-' && firstCharacter != '.' && !isASCIIDigit(firstCha
racter)) | 122 if (firstCharacter != '-' && firstCharacter != '.' && !isASCIIDigit(firstCha
racter)) |
127 return fallbackValue; | 123 return fallbackValue; |
128 | 124 |
129 bool valid = false; | 125 bool valid = false; |
130 double value = string.toDouble(&valid); | 126 double value = string.toDouble(&valid); |
131 if (!valid) | 127 if (!valid) |
132 return fallbackValue; | 128 return fallbackValue; |
133 | 129 |
134 // NaN and infinity are considered valid by String::toDouble, but not valid
here. | 130 // NaN and infinity are considered valid by String::toDouble, but not valid
here. |
135 if (!std::isfinite(value)) | 131 if (!std::isfinite(value)) |
136 return fallbackValue; | 132 return fallbackValue; |
137 | 133 |
138 // Numbers are considered finite IEEE 754 single-precision floating point va
lues. | 134 // Numbers are considered finite IEEE 754 Double-precision floating point va
lues. |
139 // See HTML5 2.5.4.3 `Real numbers.' | 135 if (-std::numeric_limits<double>::max() > value || value > std::numeric_limi
ts<double>::max()) |
140 if (-std::numeric_limits<float>::max() > value || value > std::numeric_limit
s<float>::max()) | |
141 return fallbackValue; | 136 return fallbackValue; |
142 | 137 |
143 // The following expression converts -0 to +0. | 138 // The following expression converts -0 to +0. |
144 return value ? value : 0; | 139 return value ? value : 0; |
145 } | 140 } |
146 | 141 |
147 template <typename CharacterType> | 142 template <typename CharacterType> |
148 static bool parseHTMLIntegerInternal(const CharacterType* position, const Charac
terType* end, int& value) | 143 static bool parseHTMLIntegerInternal(const CharacterType* position, const Charac
terType* end, int& value) |
149 { | 144 { |
150 // Step 3 | 145 // Step 3 |
(...skipping 239 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
390 // It's possible to have hash collisions between arbitrary strings and | 385 // It's possible to have hash collisions between arbitrary strings and |
391 // known identifiers (e.g. "bvvfg" collides with "script"). | 386 // known identifiers (e.g. "bvvfg" collides with "script"). |
392 // However ASSERTs in StringImpl::createStatic guard against there ever bein
g collisions | 387 // However ASSERTs in StringImpl::createStatic guard against there ever bein
g collisions |
393 // between static strings. | 388 // between static strings. |
394 if (!equal(it->value, characters, length)) | 389 if (!equal(it->value, characters, length)) |
395 return 0; | 390 return 0; |
396 return it->value; | 391 return it->value; |
397 } | 392 } |
398 | 393 |
399 } | 394 } |
OLD | NEW |