Chromium Code Reviews| 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 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 101 const UChar firstCharacter = string[0]; | 101 const UChar firstCharacter = string[0]; |
| 102 if (firstCharacter != '-' && firstCharacter != '.' && !isASCIIDigit(firstCha racter)) | 102 if (firstCharacter != '-' && firstCharacter != '.' && !isASCIIDigit(firstCha racter)) |
| 103 return fallbackValue; | 103 return fallbackValue; |
| 104 | 104 |
| 105 const Decimal value = Decimal::fromString(string); | 105 const Decimal value = Decimal::fromString(string); |
| 106 if (!value.isFinite()) | 106 if (!value.isFinite()) |
| 107 return fallbackValue; | 107 return fallbackValue; |
| 108 | 108 |
| 109 // Numbers are considered finite IEEE 754 single-precision floating point va lues. | 109 // Numbers are considered finite IEEE 754 single-precision floating point va lues. |
| 110 // See HTML5 2.5.4.3 `Real numbers.' | 110 // See HTML5 2.5.4.3 `Real numbers.' |
| 111 // FIXME: We should use numeric_limits<double>::max for number input type. | 111 // FIXME: We should use numeric_limits<double>::max for number input type. |
|
Inactive
2014/02/19 14:23:18
Please remove FIXME comment.
Habib Virji
2014/02/19 15:19:45
Done.
| |
| 112 const Decimal floatMax = Decimal::fromDouble(std::numeric_limits<float>::max ()); | 112 const Decimal doubleMax = Decimal::fromDouble(std::numeric_limits<double>::m ax()); |
| 113 if (value < -floatMax || value > floatMax) | 113 if (value < -doubleMax || value > doubleMax) |
| 114 return fallbackValue; | 114 return fallbackValue; |
| 115 | 115 |
| 116 // We return +0 for -0 case. | 116 // We return +0 for -0 case. |
| 117 return value.isZero() ? Decimal(0) : value; | 117 return value.isZero() ? Decimal(0) : value; |
| 118 } | 118 } |
| 119 | 119 |
| 120 double parseToDoubleForNumberType(const String& string, double fallbackValue) | 120 double parseToDoubleForNumberType(const String& string, double fallbackValue) |
| 121 { | 121 { |
| 122 // See HTML5 2.5.4.3 `Real numbers.' | 122 // See HTML5 2.5.4.3 `Real numbers.' |
| 123 | 123 |
| 124 // String::toDouble() accepts leading + and whitespace characters, which are not valid here. | 124 // String::toDouble() accepts leading + and whitespace characters, which are not valid here. |
| 125 UChar firstCharacter = string[0]; | 125 UChar firstCharacter = string[0]; |
| 126 if (firstCharacter != '-' && firstCharacter != '.' && !isASCIIDigit(firstCha racter)) | 126 if (firstCharacter != '-' && firstCharacter != '.' && !isASCIIDigit(firstCha racter)) |
| 127 return fallbackValue; | 127 return fallbackValue; |
| 128 | 128 |
| 129 bool valid = false; | 129 bool valid = false; |
| 130 double value = string.toDouble(&valid); | 130 double value = string.toDouble(&valid); |
| 131 if (!valid) | 131 if (!valid) |
| 132 return fallbackValue; | 132 return fallbackValue; |
| 133 | 133 |
| 134 // NaN and infinity are considered valid by String::toDouble, but not valid here. | 134 // NaN and infinity are considered valid by String::toDouble, but not valid here. |
| 135 if (!std::isfinite(value)) | 135 if (!std::isfinite(value)) |
| 136 return fallbackValue; | 136 return fallbackValue; |
| 137 | 137 |
| 138 // Numbers are considered finite IEEE 754 single-precision floating point va lues. | 138 // Numbers are considered finite IEEE 754 single-precision floating point va lues. |
| 139 // See HTML5 2.5.4.3 `Real numbers.' | 139 // See HTML5 2.5.4.3 `Real numbers.' |
| 140 if (-std::numeric_limits<float>::max() > value || value > std::numeric_limit s<float>::max()) | 140 if (-std::numeric_limits<double>::max() > value || value > std::numeric_limi ts<double>::max()) |
| 141 return fallbackValue; | 141 return fallbackValue; |
| 142 | 142 |
| 143 // The following expression converts -0 to +0. | 143 // The following expression converts -0 to +0. |
| 144 return value ? value : 0; | 144 return value ? value : 0; |
| 145 } | 145 } |
| 146 | 146 |
| 147 template <typename CharacterType> | 147 template <typename CharacterType> |
| 148 static bool parseHTMLIntegerInternal(const CharacterType* position, const Charac terType* end, int& value) | 148 static bool parseHTMLIntegerInternal(const CharacterType* position, const Charac terType* end, int& value) |
| 149 { | 149 { |
| 150 // Step 3 | 150 // 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 | 390 // It's possible to have hash collisions between arbitrary strings and |
| 391 // known identifiers (e.g. "bvvfg" collides with "script"). | 391 // known identifiers (e.g. "bvvfg" collides with "script"). |
| 392 // However ASSERTs in StringImpl::createStatic guard against there ever bein g collisions | 392 // However ASSERTs in StringImpl::createStatic guard against there ever bein g collisions |
| 393 // between static strings. | 393 // between static strings. |
| 394 if (!equal(it->value, characters, length)) | 394 if (!equal(it->value, characters, length)) |
| 395 return 0; | 395 return 0; |
| 396 return it->value; | 396 return it->value; |
| 397 } | 397 } |
| 398 | 398 |
| 399 } | 399 } |
| OLD | NEW |