OLD | NEW |
1 // Copyright 2006-2008 the V8 project authors. All rights reserved. | 1 // Copyright 2006-2008 the V8 project authors. All rights reserved. |
2 // Redistribution and use in source and binary forms, with or without | 2 // Redistribution and use in source and binary forms, with or without |
3 // modification, are permitted provided that the following conditions are | 3 // modification, are permitted provided that the following conditions are |
4 // met: | 4 // met: |
5 // | 5 // |
6 // * Redistributions of source code must retain the above copyright | 6 // * Redistributions of source code must retain the above copyright |
7 // notice, this list of conditions and the following disclaimer. | 7 // notice, this list of conditions and the following disclaimer. |
8 // * Redistributions in binary form must reproduce the above | 8 // * Redistributions in binary form must reproduce the above |
9 // copyright notice, this list of conditions and the following | 9 // copyright notice, this list of conditions and the following |
10 // disclaimer in the documentation and/or other materials provided | 10 // disclaimer in the documentation and/or other materials provided |
(...skipping 251 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
262 int flags, | 262 int flags, |
263 double empty_string_val) { | 263 double empty_string_val) { |
264 double result = 0.0; | 264 double result = 0.0; |
265 int index = 0; | 265 int index = 0; |
266 | 266 |
267 int len = GetLength(str); | 267 int len = GetLength(str); |
268 | 268 |
269 // Skip leading spaces. | 269 // Skip leading spaces. |
270 while ((index < len) && IsSpace(str, index)) index++; | 270 while ((index < len) && IsSpace(str, index)) index++; |
271 | 271 |
272 // Compute sign of result. | 272 // Is the string empty? |
| 273 if (index >= len) return empty_string_val; |
| 274 |
| 275 // Get the first character. |
| 276 uint16_t fst = GetChar(str, index); |
| 277 |
| 278 // Numbers can only start with '-', '+', '.', 'I' (Infinity), or a digit. |
| 279 if (fst != '-' && fst != '+' && fst != '.' && fst != 'I' && |
| 280 (fst > '9' || fst < '0')) { |
| 281 return JUNK_STRING_VALUE; |
| 282 } |
| 283 |
| 284 // Compute sign of result based on first character. |
273 int sign = 1; | 285 int sign = 1; |
274 if (index < len && GetChar(str, index) == '-') { | 286 if (fst == '-') { |
275 sign = -1; | 287 sign = -1; |
276 index++; | 288 index++; |
277 // String only containing a '-' are junk chars. | 289 // String only containing a '-' are junk chars. |
278 if (index == len) return JUNK_STRING_VALUE; | 290 if (index == len) return JUNK_STRING_VALUE; |
279 } | 291 } |
280 | 292 |
281 // string is empty? | |
282 if (index >= len) return empty_string_val; | |
283 | |
284 // do we have a hex number? | 293 // do we have a hex number? |
285 // (since the string is 0-terminated, it's ok to look one char beyond the end) | 294 // (since the string is 0-terminated, it's ok to look one char beyond the end) |
286 if ((flags & ALLOW_HEX) != 0 && | 295 if ((flags & ALLOW_HEX) != 0 && |
287 (index + 1) < len && | 296 (index + 1) < len && |
288 GetChar(str, index) == '0' && | 297 GetChar(str, index) == '0' && |
289 (GetChar(str, index + 1) == 'x' || GetChar(str, index + 1) == 'X')) { | 298 (GetChar(str, index + 1) == 'x' || GetChar(str, index + 1) == 'X')) { |
290 index += 2; | 299 index += 2; |
291 index = StringToInt(str, index, 16, &result); | 300 index = StringToInt(str, index, 16, &result); |
292 } else if ((flags & ALLOW_OCTALS) != 0 && ShouldParseOctal(str, index)) { | 301 } else if ((flags & ALLOW_OCTALS) != 0 && ShouldParseOctal(str, index)) { |
293 // NOTE: We optimistically try to parse the number as an octal (if | 302 // NOTE: We optimistically try to parse the number as an octal (if |
(...skipping 390 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
684 // Allocate result and fill in the parts. | 693 // Allocate result and fill in the parts. |
685 StringBuilder builder(result_size + 1); | 694 StringBuilder builder(result_size + 1); |
686 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); | 695 builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); |
687 if (decimal_pos > 0) builder.AddCharacter('.'); | 696 if (decimal_pos > 0) builder.AddCharacter('.'); |
688 builder.AddSubstring(decimal_buffer, decimal_pos); | 697 builder.AddSubstring(decimal_buffer, decimal_pos); |
689 return builder.Finalize(); | 698 return builder.Finalize(); |
690 } | 699 } |
691 | 700 |
692 | 701 |
693 } } // namespace v8::internal | 702 } } // namespace v8::internal |
OLD | NEW |