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 143 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
154 bool valid = false; | 154 bool valid = false; |
155 double value = string.toDouble(&valid); | 155 double value = string.toDouble(&valid); |
156 return checkDoubleValue(value, valid, fallbackValue); | 156 return checkDoubleValue(value, valid, fallbackValue); |
157 } | 157 } |
158 | 158 |
159 template <typename CharacterType> | 159 template <typename CharacterType> |
160 static bool parseHTMLIntegerInternal(const CharacterType* position, | 160 static bool parseHTMLIntegerInternal(const CharacterType* position, |
161 const CharacterType* end, | 161 const CharacterType* end, |
162 int& value) { | 162 int& value) { |
163 // Step 3 | 163 // Step 3 |
164 // We do not do this step and do not have a local sign | 164 bool isNegative = false; |
165 // variable since due to a bug in charactersToIntStrict | |
166 // we have to add the sign to the digits string. | |
167 | 165 |
168 // Step 4 | 166 // Step 4 |
169 while (position < end) { | 167 while (position < end) { |
170 if (!isHTMLSpace<CharacterType>(*position)) | 168 if (!isHTMLSpace<CharacterType>(*position)) |
171 break; | 169 break; |
172 ++position; | 170 ++position; |
173 } | 171 } |
174 | 172 |
175 // Step 5 | 173 // Step 5 |
176 if (position == end) | 174 if (position == end) |
177 return false; | 175 return false; |
178 ASSERT(position < end); | 176 ASSERT(position < end); |
179 | 177 |
180 // Step 6 | 178 // Step 6 |
181 StringBuilder digits; | |
182 if (*position == '-') { | 179 if (*position == '-') { |
183 digits.append('-'); | 180 isNegative = true; |
184 ++position; | 181 ++position; |
185 } else if (*position == '+') | 182 } else if (*position == '+') |
186 ++position; | 183 ++position; |
187 if (position == end) | 184 if (position == end) |
188 return false; | 185 return false; |
189 ASSERT(position < end); | 186 ASSERT(position < end); |
190 | 187 |
191 // Step 7 | 188 // Step 7 |
192 if (!isASCIIDigit(*position)) | 189 if (!isASCIIDigit(*position)) |
193 return false; | 190 return false; |
194 | 191 |
195 // Step 8 | 192 // Step 8 |
196 while (position < end) { | 193 static const int intMax = std::numeric_limits<int>::max(); |
197 if (!isASCIIDigit(*position)) | 194 const int base = 10; |
198 break; | 195 const int maxMultiplier = intMax / base; |
199 digits.append(*position++); | |
200 } | |
201 | 196 |
| 197 unsigned temp = 0; |
| 198 do { |
| 199 int digitValue = *position - '0'; |
| 200 if (temp > maxMultiplier || |
| 201 (temp == maxMultiplier && digitValue > (intMax % base) + isNegative)) |
| 202 return false; |
| 203 temp = temp * base + digitValue; |
| 204 ++position; |
| 205 } while (position < end && isASCIIDigit(*position)); |
202 // Step 9 | 206 // Step 9 |
203 bool ok; | 207 value = isNegative ? (0 - temp) : temp; |
204 if (digits.is8Bit()) | 208 return true; |
205 value = charactersToIntStrict(digits.characters8(), digits.length(), &ok); | |
206 else | |
207 value = charactersToIntStrict(digits.characters16(), digits.length(), &ok); | |
208 return ok; | |
209 } | 209 } |
210 | 210 |
211 // http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-integers | 211 // http://www.whatwg.org/specs/web-apps/current-work/#rules-for-parsing-integers |
212 bool parseHTMLInteger(const String& input, int& value) { | 212 bool parseHTMLInteger(const String& input, int& value) { |
213 // Step 1 | 213 // Step 1 |
214 // Step 2 | 214 // Step 2 |
215 unsigned length = input.length(); | 215 unsigned length = input.length(); |
216 if (!length || input.is8Bit()) { | 216 if (!length || input.is8Bit()) { |
217 const LChar* start = input.characters8(); | 217 const LChar* start = input.characters8(); |
218 return parseHTMLIntegerInternal(start, start + length, value); | 218 return parseHTMLIntegerInternal(start, start + length, value); |
(...skipping 271 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
490 string = StringImpl::create8BitIfPossible(characters, size); | 490 string = StringImpl::create8BitIfPossible(characters, size); |
491 else if (width == Force8Bit) | 491 else if (width == Force8Bit) |
492 string = String::make8BitFrom16BitSource(characters, size); | 492 string = String::make8BitFrom16BitSource(characters, size); |
493 else | 493 else |
494 string = String(characters, size); | 494 string = String(characters, size); |
495 | 495 |
496 return string; | 496 return string; |
497 } | 497 } |
498 | 498 |
499 } // namespace blink | 499 } // namespace blink |
OLD | NEW |