Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1 | // Copyright 2006-2008 the V8 project authors. All rights reserved. |
| 2 | // Redistribution and use in source and binary forms, with or without |
| 3 | // modification, are permitted provided that the following conditions are |
| 4 | // met: |
| 5 | // |
| 6 | // * Redistributions of source code must retain the above copyright |
| 7 | // notice, this list of conditions and the following disclaimer. |
| 8 | // * Redistributions in binary form must reproduce the above |
| 9 | // copyright notice, this list of conditions and the following |
| 10 | // disclaimer in the documentation and/or other materials provided |
| 11 | // with the distribution. |
| 12 | // * Neither the name of Google Inc. nor the names of its |
| 13 | // contributors may be used to endorse or promote products derived |
| 14 | // from this software without specific prior written permission. |
| 15 | // |
| 16 | // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 17 | // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 18 | // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 19 | // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 20 | // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 21 | // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 22 | // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 23 | // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 24 | // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 25 | // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 26 | // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 27 | |
| 28 | #include <stdarg.h> |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 29 | #include <limits.h> |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 30 | |
| 31 | #include "v8.h" |
| 32 | |
| 33 | #include "conversions-inl.h" |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 34 | #include "dtoa.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 35 | #include "factory.h" |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 36 | #include "scanner-base.h" |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 37 | #include "strtod.h" |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 38 | |
| 39 | namespace v8 { |
| 40 | namespace internal { |
| 41 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 42 | namespace { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 43 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 44 | // C++-style iterator adaptor for StringInputBuffer |
| 45 | // (unlike C++ iterators the end-marker has different type). |
| 46 | class StringInputBufferIterator { |
| 47 | public: |
| 48 | class EndMarker {}; |
| 49 | |
| 50 | explicit StringInputBufferIterator(StringInputBuffer* buffer); |
| 51 | |
| 52 | int operator*() const; |
| 53 | void operator++(); |
| 54 | bool operator==(EndMarker const&) const { return end_; } |
| 55 | bool operator!=(EndMarker const& m) const { return !end_; } |
| 56 | |
| 57 | private: |
| 58 | StringInputBuffer* const buffer_; |
| 59 | int current_; |
| 60 | bool end_; |
| 61 | }; |
| 62 | |
| 63 | |
| 64 | StringInputBufferIterator::StringInputBufferIterator( |
| 65 | StringInputBuffer* buffer) : buffer_(buffer) { |
| 66 | ++(*this); |
| 67 | } |
| 68 | |
| 69 | int StringInputBufferIterator::operator*() const { |
| 70 | return current_; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 74 | void StringInputBufferIterator::operator++() { |
| 75 | end_ = !buffer_->has_more(); |
| 76 | if (!end_) { |
| 77 | current_ = buffer_->GetNext(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 78 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 79 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 80 | } |
| 81 | |
| 82 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 83 | template <class Iterator, class EndMark> |
| 84 | static bool SubStringEquals(Iterator* current, |
| 85 | EndMark end, |
| 86 | const char* substring) { |
| 87 | ASSERT(**current == *substring); |
| 88 | for (substring++; *substring != '\0'; substring++) { |
| 89 | ++*current; |
| 90 | if (*current == end || **current != *substring) return false; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 91 | } |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 92 | ++*current; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 93 | return true; |
| 94 | } |
| 95 | |
| 96 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 97 | // Maximum number of significant digits in decimal representation. |
| 98 | // The longest possible double in decimal representation is |
| 99 | // (2^53 - 1) * 2 ^ -1074 that is (2 ^ 53 - 1) * 5 ^ 1074 / 10 ^ 1074 |
| 100 | // (768 digits). If we parse a number whose first digits are equal to a |
| 101 | // mean of 2 adjacent doubles (that could have up to 769 digits) the result |
| 102 | // must be rounded to the bigger one unless the tail consists of zeros, so |
| 103 | // we don't need to preserve all the digits. |
| 104 | const int kMaxSignificantDigits = 772; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 105 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 106 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 107 | static const double JUNK_STRING_VALUE = OS::nan_value(); |
| 108 | |
| 109 | |
| 110 | // Returns true if a nonspace found and false if the end has reached. |
| 111 | template <class Iterator, class EndMark> |
| 112 | static inline bool AdvanceToNonspace(Iterator* current, EndMark end) { |
| 113 | while (*current != end) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 114 | if (!ScannerConstants::kIsWhiteSpace.get(**current)) return true; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 115 | ++*current; |
| 116 | } |
| 117 | return false; |
| 118 | } |
| 119 | |
| 120 | |
| 121 | static bool isDigit(int x, int radix) { |
| 122 | return (x >= '0' && x <= '9' && x < '0' + radix) |
| 123 | || (radix > 10 && x >= 'a' && x < 'a' + radix - 10) |
| 124 | || (radix > 10 && x >= 'A' && x < 'A' + radix - 10); |
| 125 | } |
| 126 | |
| 127 | |
| 128 | static double SignedZero(bool sign) { |
| 129 | return sign ? -0.0 : 0.0; |
| 130 | } |
| 131 | |
| 132 | |
| 133 | // Parsing integers with radix 2, 4, 8, 16, 32. Assumes current != end. |
| 134 | template <int radix_log_2, class Iterator, class EndMark> |
| 135 | static double InternalStringToIntDouble(Iterator current, |
| 136 | EndMark end, |
| 137 | bool sign, |
| 138 | bool allow_trailing_junk) { |
| 139 | ASSERT(current != end); |
| 140 | |
| 141 | // Skip leading 0s. |
| 142 | while (*current == '0') { |
| 143 | ++current; |
| 144 | if (current == end) return SignedZero(sign); |
| 145 | } |
| 146 | |
| 147 | int64_t number = 0; |
| 148 | int exponent = 0; |
| 149 | const int radix = (1 << radix_log_2); |
| 150 | |
| 151 | do { |
| 152 | int digit; |
| 153 | if (*current >= '0' && *current <= '9' && *current < '0' + radix) { |
| 154 | digit = static_cast<char>(*current) - '0'; |
| 155 | } else if (radix > 10 && *current >= 'a' && *current < 'a' + radix - 10) { |
| 156 | digit = static_cast<char>(*current) - 'a' + 10; |
| 157 | } else if (radix > 10 && *current >= 'A' && *current < 'A' + radix - 10) { |
| 158 | digit = static_cast<char>(*current) - 'A' + 10; |
| 159 | } else { |
| 160 | if (allow_trailing_junk || !AdvanceToNonspace(¤t, end)) { |
| 161 | break; |
| 162 | } else { |
| 163 | return JUNK_STRING_VALUE; |
| 164 | } |
| 165 | } |
| 166 | |
| 167 | number = number * radix + digit; |
| 168 | int overflow = static_cast<int>(number >> 53); |
| 169 | if (overflow != 0) { |
| 170 | // Overflow occurred. Need to determine which direction to round the |
| 171 | // result. |
| 172 | int overflow_bits_count = 1; |
| 173 | while (overflow > 1) { |
| 174 | overflow_bits_count++; |
| 175 | overflow >>= 1; |
| 176 | } |
| 177 | |
| 178 | int dropped_bits_mask = ((1 << overflow_bits_count) - 1); |
| 179 | int dropped_bits = static_cast<int>(number) & dropped_bits_mask; |
| 180 | number >>= overflow_bits_count; |
| 181 | exponent = overflow_bits_count; |
| 182 | |
| 183 | bool zero_tail = true; |
| 184 | while (true) { |
| 185 | ++current; |
| 186 | if (current == end || !isDigit(*current, radix)) break; |
| 187 | zero_tail = zero_tail && *current == '0'; |
| 188 | exponent += radix_log_2; |
| 189 | } |
| 190 | |
| 191 | if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { |
| 192 | return JUNK_STRING_VALUE; |
| 193 | } |
| 194 | |
| 195 | int middle_value = (1 << (overflow_bits_count - 1)); |
| 196 | if (dropped_bits > middle_value) { |
| 197 | number++; // Rounding up. |
| 198 | } else if (dropped_bits == middle_value) { |
| 199 | // Rounding to even to consistency with decimals: half-way case rounds |
| 200 | // up if significant part is odd and down otherwise. |
| 201 | if ((number & 1) != 0 || !zero_tail) { |
| 202 | number++; // Rounding up. |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | // Rounding up may cause overflow. |
| 207 | if ((number & ((int64_t)1 << 53)) != 0) { |
| 208 | exponent++; |
| 209 | number >>= 1; |
| 210 | } |
| 211 | break; |
| 212 | } |
| 213 | ++current; |
| 214 | } while (current != end); |
| 215 | |
| 216 | ASSERT(number < ((int64_t)1 << 53)); |
| 217 | ASSERT(static_cast<int64_t>(static_cast<double>(number)) == number); |
| 218 | |
| 219 | if (exponent == 0) { |
| 220 | if (sign) { |
| 221 | if (number == 0) return -0.0; |
| 222 | number = -number; |
| 223 | } |
| 224 | return static_cast<double>(number); |
| 225 | } |
| 226 | |
| 227 | ASSERT(number != 0); |
| 228 | // The double could be constructed faster from number (mantissa), exponent |
| 229 | // and sign. Assuming it's a rare case more simple code is used. |
| 230 | return static_cast<double>(sign ? -number : number) * pow(2.0, exponent); |
| 231 | } |
| 232 | |
| 233 | |
| 234 | template <class Iterator, class EndMark> |
| 235 | static double InternalStringToInt(Iterator current, EndMark end, int radix) { |
| 236 | const bool allow_trailing_junk = true; |
| 237 | const double empty_string_val = JUNK_STRING_VALUE; |
| 238 | |
| 239 | if (!AdvanceToNonspace(¤t, end)) return empty_string_val; |
| 240 | |
| 241 | bool sign = false; |
| 242 | bool leading_zero = false; |
| 243 | |
| 244 | if (*current == '+') { |
| 245 | // Ignore leading sign; skip following spaces. |
| 246 | ++current; |
| 247 | if (!AdvanceToNonspace(¤t, end)) return JUNK_STRING_VALUE; |
| 248 | } else if (*current == '-') { |
| 249 | ++current; |
| 250 | if (!AdvanceToNonspace(¤t, end)) return JUNK_STRING_VALUE; |
| 251 | sign = true; |
| 252 | } |
| 253 | |
| 254 | if (radix == 0) { |
| 255 | // Radix detection. |
| 256 | if (*current == '0') { |
| 257 | ++current; |
| 258 | if (current == end) return SignedZero(sign); |
| 259 | if (*current == 'x' || *current == 'X') { |
| 260 | radix = 16; |
| 261 | ++current; |
| 262 | if (current == end) return JUNK_STRING_VALUE; |
| 263 | } else { |
| 264 | radix = 8; |
| 265 | leading_zero = true; |
| 266 | } |
| 267 | } else { |
| 268 | radix = 10; |
| 269 | } |
| 270 | } else if (radix == 16) { |
| 271 | if (*current == '0') { |
| 272 | // Allow "0x" prefix. |
| 273 | ++current; |
| 274 | if (current == end) return SignedZero(sign); |
| 275 | if (*current == 'x' || *current == 'X') { |
| 276 | ++current; |
| 277 | if (current == end) return JUNK_STRING_VALUE; |
| 278 | } else { |
| 279 | leading_zero = true; |
| 280 | } |
| 281 | } |
| 282 | } |
| 283 | |
| 284 | if (radix < 2 || radix > 36) return JUNK_STRING_VALUE; |
| 285 | |
| 286 | // Skip leading zeros. |
| 287 | while (*current == '0') { |
| 288 | leading_zero = true; |
| 289 | ++current; |
| 290 | if (current == end) return SignedZero(sign); |
| 291 | } |
| 292 | |
| 293 | if (!leading_zero && !isDigit(*current, radix)) { |
| 294 | return JUNK_STRING_VALUE; |
| 295 | } |
| 296 | |
| 297 | if (IsPowerOf2(radix)) { |
| 298 | switch (radix) { |
| 299 | case 2: |
| 300 | return InternalStringToIntDouble<1>( |
| 301 | current, end, sign, allow_trailing_junk); |
| 302 | case 4: |
| 303 | return InternalStringToIntDouble<2>( |
| 304 | current, end, sign, allow_trailing_junk); |
| 305 | case 8: |
| 306 | return InternalStringToIntDouble<3>( |
| 307 | current, end, sign, allow_trailing_junk); |
| 308 | |
| 309 | case 16: |
| 310 | return InternalStringToIntDouble<4>( |
| 311 | current, end, sign, allow_trailing_junk); |
| 312 | |
| 313 | case 32: |
| 314 | return InternalStringToIntDouble<5>( |
| 315 | current, end, sign, allow_trailing_junk); |
| 316 | default: |
| 317 | UNREACHABLE(); |
| 318 | } |
| 319 | } |
| 320 | |
| 321 | if (radix == 10) { |
| 322 | // Parsing with strtod. |
| 323 | const int kMaxSignificantDigits = 309; // Doubles are less than 1.8e308. |
| 324 | // The buffer may contain up to kMaxSignificantDigits + 1 digits and a zero |
| 325 | // end. |
| 326 | const int kBufferSize = kMaxSignificantDigits + 2; |
| 327 | char buffer[kBufferSize]; |
| 328 | int buffer_pos = 0; |
| 329 | while (*current >= '0' && *current <= '9') { |
| 330 | if (buffer_pos <= kMaxSignificantDigits) { |
| 331 | // If the number has more than kMaxSignificantDigits it will be parsed |
| 332 | // as infinity. |
| 333 | ASSERT(buffer_pos < kBufferSize); |
| 334 | buffer[buffer_pos++] = static_cast<char>(*current); |
| 335 | } |
| 336 | ++current; |
| 337 | if (current == end) break; |
| 338 | } |
| 339 | |
| 340 | if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { |
| 341 | return JUNK_STRING_VALUE; |
| 342 | } |
| 343 | |
| 344 | ASSERT(buffer_pos < kBufferSize); |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 345 | buffer[buffer_pos] = '\0'; |
| 346 | Vector<const char> buffer_vector(buffer, buffer_pos); |
| 347 | return sign ? -Strtod(buffer_vector, 0) : Strtod(buffer_vector, 0); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // The following code causes accumulating rounding error for numbers greater |
| 351 | // than ~2^56. It's explicitly allowed in the spec: "if R is not 2, 4, 8, 10, |
| 352 | // 16, or 32, then mathInt may be an implementation-dependent approximation to |
| 353 | // the mathematical integer value" (15.1.2.2). |
| 354 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 355 | int lim_0 = '0' + (radix < 10 ? radix : 10); |
| 356 | int lim_a = 'a' + (radix - 10); |
| 357 | int lim_A = 'A' + (radix - 10); |
| 358 | |
| 359 | // NOTE: The code for computing the value may seem a bit complex at |
| 360 | // first glance. It is structured to use 32-bit multiply-and-add |
| 361 | // loops as long as possible to avoid loosing precision. |
| 362 | |
| 363 | double v = 0.0; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 364 | bool done = false; |
| 365 | do { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 366 | // Parse the longest part of the string starting at index j |
| 367 | // possible while keeping the multiplier, and thus the part |
| 368 | // itself, within 32 bits. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 369 | unsigned int part = 0, multiplier = 1; |
| 370 | while (true) { |
| 371 | int d; |
| 372 | if (*current >= '0' && *current < lim_0) { |
| 373 | d = *current - '0'; |
| 374 | } else if (*current >= 'a' && *current < lim_a) { |
| 375 | d = *current - 'a' + 10; |
| 376 | } else if (*current >= 'A' && *current < lim_A) { |
| 377 | d = *current - 'A' + 10; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 378 | } else { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 379 | done = true; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 380 | break; |
| 381 | } |
| 382 | |
| 383 | // Update the value of the part as long as the multiplier fits |
| 384 | // in 32 bits. When we can't guarantee that the next iteration |
| 385 | // will not overflow the multiplier, we stop parsing the part |
| 386 | // by leaving the loop. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 387 | const unsigned int kMaximumMultiplier = 0xffffffffU / 36; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 388 | uint32_t m = multiplier * radix; |
| 389 | if (m > kMaximumMultiplier) break; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 390 | part = part * radix + d; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 391 | multiplier = m; |
| 392 | ASSERT(multiplier > part); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 393 | |
| 394 | ++current; |
| 395 | if (current == end) { |
| 396 | done = true; |
| 397 | break; |
| 398 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 399 | } |
| 400 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 401 | // Update the value and skip the part in the string. |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 402 | v = v * multiplier + part; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 403 | } while (!done); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 404 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 405 | if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 406 | return JUNK_STRING_VALUE; |
| 407 | } |
| 408 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 409 | return sign ? -v : v; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 410 | } |
| 411 | |
| 412 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 413 | // Converts a string to a double value. Assumes the Iterator supports |
| 414 | // the following operations: |
| 415 | // 1. current == end (other ops are not allowed), current != end. |
| 416 | // 2. *current - gets the current character in the sequence. |
| 417 | // 3. ++current (advances the position). |
| 418 | template <class Iterator, class EndMark> |
| 419 | static double InternalStringToDouble(Iterator current, |
| 420 | EndMark end, |
| 421 | int flags, |
| 422 | double empty_string_val) { |
| 423 | // To make sure that iterator dereferencing is valid the following |
| 424 | // convention is used: |
| 425 | // 1. Each '++current' statement is followed by check for equality to 'end'. |
| 426 | // 2. If AdvanceToNonspace returned false then current == end. |
| 427 | // 3. If 'current' becomes be equal to 'end' the function returns or goes to |
| 428 | // 'parsing_done'. |
| 429 | // 4. 'current' is not dereferenced after the 'parsing_done' label. |
| 430 | // 5. Code before 'parsing_done' may rely on 'current != end'. |
| 431 | if (!AdvanceToNonspace(¤t, end)) return empty_string_val; |
| 432 | |
| 433 | const bool allow_trailing_junk = (flags & ALLOW_TRAILING_JUNK) != 0; |
| 434 | |
| 435 | // The longest form of simplified number is: "-<significant digits>'.1eXXX\0". |
| 436 | const int kBufferSize = kMaxSignificantDigits + 10; |
| 437 | char buffer[kBufferSize]; // NOLINT: size is known at compile time. |
| 438 | int buffer_pos = 0; |
| 439 | |
| 440 | // Exponent will be adjusted if insignificant digits of the integer part |
| 441 | // or insignificant leading zeros of the fractional part are dropped. |
| 442 | int exponent = 0; |
| 443 | int significant_digits = 0; |
| 444 | int insignificant_digits = 0; |
| 445 | bool nonzero_digit_dropped = false; |
| 446 | bool fractional_part = false; |
| 447 | |
| 448 | bool sign = false; |
| 449 | |
| 450 | if (*current == '+') { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 451 | // Ignore leading sign. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 452 | ++current; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 453 | if (current == end) return JUNK_STRING_VALUE; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 454 | } else if (*current == '-') { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 455 | ++current; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 456 | if (current == end) return JUNK_STRING_VALUE; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 457 | sign = true; |
| 458 | } |
| 459 | |
| 460 | static const char kInfinitySymbol[] = "Infinity"; |
| 461 | if (*current == kInfinitySymbol[0]) { |
| 462 | if (!SubStringEquals(¤t, end, kInfinitySymbol)) { |
| 463 | return JUNK_STRING_VALUE; |
| 464 | } |
| 465 | |
| 466 | if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { |
| 467 | return JUNK_STRING_VALUE; |
| 468 | } |
| 469 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 470 | ASSERT(buffer_pos == 0); |
| 471 | return sign ? -V8_INFINITY : V8_INFINITY; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | bool leading_zero = false; |
| 475 | if (*current == '0') { |
| 476 | ++current; |
| 477 | if (current == end) return SignedZero(sign); |
| 478 | |
| 479 | leading_zero = true; |
| 480 | |
| 481 | // It could be hexadecimal value. |
| 482 | if ((flags & ALLOW_HEX) && (*current == 'x' || *current == 'X')) { |
| 483 | ++current; |
| 484 | if (current == end || !isDigit(*current, 16)) { |
| 485 | return JUNK_STRING_VALUE; // "0x". |
| 486 | } |
| 487 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 488 | return InternalStringToIntDouble<4>(current, |
| 489 | end, |
| 490 | sign, |
| 491 | allow_trailing_junk); |
| 492 | } |
| 493 | |
| 494 | // Ignore leading zeros in the integer part. |
| 495 | while (*current == '0') { |
| 496 | ++current; |
| 497 | if (current == end) return SignedZero(sign); |
| 498 | } |
| 499 | } |
| 500 | |
| 501 | bool octal = leading_zero && (flags & ALLOW_OCTALS) != 0; |
| 502 | |
| 503 | // Copy significant digits of the integer part (if any) to the buffer. |
| 504 | while (*current >= '0' && *current <= '9') { |
| 505 | if (significant_digits < kMaxSignificantDigits) { |
| 506 | ASSERT(buffer_pos < kBufferSize); |
| 507 | buffer[buffer_pos++] = static_cast<char>(*current); |
| 508 | significant_digits++; |
| 509 | // Will later check if it's an octal in the buffer. |
| 510 | } else { |
| 511 | insignificant_digits++; // Move the digit into the exponential part. |
| 512 | nonzero_digit_dropped = nonzero_digit_dropped || *current != '0'; |
| 513 | } |
| 514 | octal = octal && *current < '8'; |
| 515 | ++current; |
| 516 | if (current == end) goto parsing_done; |
| 517 | } |
| 518 | |
| 519 | if (significant_digits == 0) { |
| 520 | octal = false; |
| 521 | } |
| 522 | |
| 523 | if (*current == '.') { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 524 | if (octal && !allow_trailing_junk) return JUNK_STRING_VALUE; |
| 525 | if (octal) goto parsing_done; |
| 526 | |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 527 | ++current; |
| 528 | if (current == end) { |
| 529 | if (significant_digits == 0 && !leading_zero) { |
| 530 | return JUNK_STRING_VALUE; |
| 531 | } else { |
| 532 | goto parsing_done; |
| 533 | } |
| 534 | } |
| 535 | |
| 536 | if (significant_digits == 0) { |
| 537 | // octal = false; |
| 538 | // Integer part consists of 0 or is absent. Significant digits start after |
| 539 | // leading zeros (if any). |
| 540 | while (*current == '0') { |
| 541 | ++current; |
| 542 | if (current == end) return SignedZero(sign); |
| 543 | exponent--; // Move this 0 into the exponent. |
| 544 | } |
| 545 | } |
| 546 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 547 | // We don't emit a '.', but adjust the exponent instead. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 548 | fractional_part = true; |
| 549 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 550 | // There is a fractional part. |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 551 | while (*current >= '0' && *current <= '9') { |
| 552 | if (significant_digits < kMaxSignificantDigits) { |
| 553 | ASSERT(buffer_pos < kBufferSize); |
| 554 | buffer[buffer_pos++] = static_cast<char>(*current); |
| 555 | significant_digits++; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 556 | exponent--; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 557 | } else { |
| 558 | // Ignore insignificant digits in the fractional part. |
| 559 | nonzero_digit_dropped = nonzero_digit_dropped || *current != '0'; |
| 560 | } |
| 561 | ++current; |
| 562 | if (current == end) goto parsing_done; |
| 563 | } |
| 564 | } |
| 565 | |
| 566 | if (!leading_zero && exponent == 0 && significant_digits == 0) { |
| 567 | // If leading_zeros is true then the string contains zeros. |
| 568 | // If exponent < 0 then string was [+-]\.0*... |
| 569 | // If significant_digits != 0 the string is not equal to 0. |
| 570 | // Otherwise there are no digits in the string. |
| 571 | return JUNK_STRING_VALUE; |
| 572 | } |
| 573 | |
| 574 | // Parse exponential part. |
| 575 | if (*current == 'e' || *current == 'E') { |
| 576 | if (octal) return JUNK_STRING_VALUE; |
| 577 | ++current; |
| 578 | if (current == end) { |
| 579 | if (allow_trailing_junk) { |
| 580 | goto parsing_done; |
| 581 | } else { |
| 582 | return JUNK_STRING_VALUE; |
| 583 | } |
| 584 | } |
| 585 | char sign = '+'; |
| 586 | if (*current == '+' || *current == '-') { |
| 587 | sign = static_cast<char>(*current); |
| 588 | ++current; |
| 589 | if (current == end) { |
| 590 | if (allow_trailing_junk) { |
| 591 | goto parsing_done; |
| 592 | } else { |
| 593 | return JUNK_STRING_VALUE; |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | if (current == end || *current < '0' || *current > '9') { |
| 599 | if (allow_trailing_junk) { |
| 600 | goto parsing_done; |
| 601 | } else { |
| 602 | return JUNK_STRING_VALUE; |
| 603 | } |
| 604 | } |
| 605 | |
| 606 | const int max_exponent = INT_MAX / 2; |
| 607 | ASSERT(-max_exponent / 2 <= exponent && exponent <= max_exponent / 2); |
| 608 | int num = 0; |
| 609 | do { |
| 610 | // Check overflow. |
| 611 | int digit = *current - '0'; |
| 612 | if (num >= max_exponent / 10 |
| 613 | && !(num == max_exponent / 10 && digit <= max_exponent % 10)) { |
| 614 | num = max_exponent; |
| 615 | } else { |
| 616 | num = num * 10 + digit; |
| 617 | } |
| 618 | ++current; |
| 619 | } while (current != end && *current >= '0' && *current <= '9'); |
| 620 | |
| 621 | exponent += (sign == '-' ? -num : num); |
| 622 | } |
| 623 | |
| 624 | if (!allow_trailing_junk && AdvanceToNonspace(¤t, end)) { |
| 625 | return JUNK_STRING_VALUE; |
| 626 | } |
| 627 | |
| 628 | parsing_done: |
| 629 | exponent += insignificant_digits; |
| 630 | |
| 631 | if (octal) { |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 632 | return InternalStringToIntDouble<3>(buffer, |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 633 | buffer + buffer_pos, |
| 634 | sign, |
| 635 | allow_trailing_junk); |
| 636 | } |
| 637 | |
| 638 | if (nonzero_digit_dropped) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 639 | buffer[buffer_pos++] = '1'; |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 640 | exponent--; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 641 | } |
| 642 | |
| 643 | ASSERT(buffer_pos < kBufferSize); |
| 644 | buffer[buffer_pos] = '\0'; |
| 645 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 646 | double converted = Strtod(Vector<const char>(buffer, buffer_pos), exponent); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 647 | return sign ? -converted : converted; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 648 | } |
| 649 | |
Ben Murdoch | f87a203 | 2010-10-22 12:50:53 +0100 | [diff] [blame] | 650 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 651 | double StringToDouble(String* str, int flags, double empty_string_val) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 652 | StringShape shape(str); |
| 653 | if (shape.IsSequentialAscii()) { |
| 654 | const char* begin = SeqAsciiString::cast(str)->GetChars(); |
| 655 | const char* end = begin + str->length(); |
| 656 | return InternalStringToDouble(begin, end, flags, empty_string_val); |
| 657 | } else if (shape.IsSequentialTwoByte()) { |
| 658 | const uc16* begin = SeqTwoByteString::cast(str)->GetChars(); |
| 659 | const uc16* end = begin + str->length(); |
| 660 | return InternalStringToDouble(begin, end, flags, empty_string_val); |
| 661 | } else { |
| 662 | StringInputBuffer buffer(str); |
| 663 | return InternalStringToDouble(StringInputBufferIterator(&buffer), |
| 664 | StringInputBufferIterator::EndMarker(), |
| 665 | flags, |
| 666 | empty_string_val); |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | |
| 671 | double StringToInt(String* str, int radix) { |
| 672 | StringShape shape(str); |
| 673 | if (shape.IsSequentialAscii()) { |
| 674 | const char* begin = SeqAsciiString::cast(str)->GetChars(); |
| 675 | const char* end = begin + str->length(); |
| 676 | return InternalStringToInt(begin, end, radix); |
| 677 | } else if (shape.IsSequentialTwoByte()) { |
| 678 | const uc16* begin = SeqTwoByteString::cast(str)->GetChars(); |
| 679 | const uc16* end = begin + str->length(); |
| 680 | return InternalStringToInt(begin, end, radix); |
| 681 | } else { |
| 682 | StringInputBuffer buffer(str); |
| 683 | return InternalStringToInt(StringInputBufferIterator(&buffer), |
| 684 | StringInputBufferIterator::EndMarker(), |
| 685 | radix); |
| 686 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 687 | } |
| 688 | |
| 689 | |
| 690 | double StringToDouble(const char* str, int flags, double empty_string_val) { |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 691 | const char* end = str + StrLength(str); |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 692 | return InternalStringToDouble(str, end, flags, empty_string_val); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 693 | } |
| 694 | |
| 695 | |
Kristian Monsen | 80d68ea | 2010-09-08 11:05:35 +0100 | [diff] [blame] | 696 | double StringToDouble(Vector<const char> str, |
| 697 | int flags, |
| 698 | double empty_string_val) { |
| 699 | const char* end = str.start() + str.length(); |
| 700 | return InternalStringToDouble(str.start(), end, flags, empty_string_val); |
| 701 | } |
| 702 | |
| 703 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 704 | const char* DoubleToCString(double v, Vector<char> buffer) { |
| 705 | StringBuilder builder(buffer.start(), buffer.length()); |
| 706 | |
| 707 | switch (fpclassify(v)) { |
| 708 | case FP_NAN: |
| 709 | builder.AddString("NaN"); |
| 710 | break; |
| 711 | |
| 712 | case FP_INFINITE: |
| 713 | if (v < 0.0) { |
| 714 | builder.AddString("-Infinity"); |
| 715 | } else { |
| 716 | builder.AddString("Infinity"); |
| 717 | } |
| 718 | break; |
| 719 | |
| 720 | case FP_ZERO: |
| 721 | builder.AddCharacter('0'); |
| 722 | break; |
| 723 | |
| 724 | default: { |
| 725 | int decimal_point; |
| 726 | int sign; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 727 | const int kV8DtoaBufferCapacity = kBase10MaximalLength + 1; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 728 | char decimal_rep[kV8DtoaBufferCapacity]; |
Steve Block | 6ded16b | 2010-05-10 14:33:55 +0100 | [diff] [blame] | 729 | int length; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 730 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 731 | DoubleToAscii(v, DTOA_SHORTEST, 0, |
| 732 | Vector<char>(decimal_rep, kV8DtoaBufferCapacity), |
| 733 | &sign, &length, &decimal_point); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 734 | |
| 735 | if (sign) builder.AddCharacter('-'); |
| 736 | |
| 737 | if (length <= decimal_point && decimal_point <= 21) { |
| 738 | // ECMA-262 section 9.8.1 step 6. |
| 739 | builder.AddString(decimal_rep); |
| 740 | builder.AddPadding('0', decimal_point - length); |
| 741 | |
| 742 | } else if (0 < decimal_point && decimal_point <= 21) { |
| 743 | // ECMA-262 section 9.8.1 step 7. |
| 744 | builder.AddSubstring(decimal_rep, decimal_point); |
| 745 | builder.AddCharacter('.'); |
| 746 | builder.AddString(decimal_rep + decimal_point); |
| 747 | |
| 748 | } else if (decimal_point <= 0 && decimal_point > -6) { |
| 749 | // ECMA-262 section 9.8.1 step 8. |
| 750 | builder.AddString("0."); |
| 751 | builder.AddPadding('0', -decimal_point); |
| 752 | builder.AddString(decimal_rep); |
| 753 | |
| 754 | } else { |
| 755 | // ECMA-262 section 9.8.1 step 9 and 10 combined. |
| 756 | builder.AddCharacter(decimal_rep[0]); |
| 757 | if (length != 1) { |
| 758 | builder.AddCharacter('.'); |
| 759 | builder.AddString(decimal_rep + 1); |
| 760 | } |
| 761 | builder.AddCharacter('e'); |
| 762 | builder.AddCharacter((decimal_point >= 0) ? '+' : '-'); |
| 763 | int exponent = decimal_point - 1; |
| 764 | if (exponent < 0) exponent = -exponent; |
| 765 | builder.AddFormatted("%d", exponent); |
| 766 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 767 | } |
| 768 | } |
| 769 | return builder.Finalize(); |
| 770 | } |
| 771 | |
| 772 | |
| 773 | const char* IntToCString(int n, Vector<char> buffer) { |
| 774 | bool negative = false; |
| 775 | if (n < 0) { |
| 776 | // We must not negate the most negative int. |
| 777 | if (n == kMinInt) return DoubleToCString(n, buffer); |
| 778 | negative = true; |
| 779 | n = -n; |
| 780 | } |
| 781 | // Build the string backwards from the least significant digit. |
| 782 | int i = buffer.length(); |
| 783 | buffer[--i] = '\0'; |
| 784 | do { |
| 785 | buffer[--i] = '0' + (n % 10); |
| 786 | n /= 10; |
| 787 | } while (n); |
| 788 | if (negative) buffer[--i] = '-'; |
| 789 | return buffer.start() + i; |
| 790 | } |
| 791 | |
| 792 | |
| 793 | char* DoubleToFixedCString(double value, int f) { |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 794 | const int kMaxDigitsBeforePoint = 21; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 795 | const double kFirstNonFixed = 1e21; |
| 796 | const int kMaxDigitsAfterPoint = 20; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 797 | ASSERT(f >= 0); |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 798 | ASSERT(f <= kMaxDigitsAfterPoint); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 799 | |
| 800 | bool negative = false; |
| 801 | double abs_value = value; |
| 802 | if (value < 0) { |
| 803 | abs_value = -value; |
| 804 | negative = true; |
| 805 | } |
| 806 | |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 807 | // If abs_value has more than kMaxDigitsBeforePoint digits before the point |
| 808 | // use the non-fixed conversion routine. |
| 809 | if (abs_value >= kFirstNonFixed) { |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 810 | char arr[100]; |
| 811 | Vector<char> buffer(arr, ARRAY_SIZE(arr)); |
| 812 | return StrDup(DoubleToCString(value, buffer)); |
| 813 | } |
| 814 | |
| 815 | // Find a sufficiently precise decimal representation of n. |
| 816 | int decimal_point; |
| 817 | int sign; |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 818 | // Add space for the '\0' byte. |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 819 | const int kDecimalRepCapacity = |
Teng-Hui Zhu | 3e5fa29 | 2010-11-09 16:16:48 -0800 | [diff] [blame] | 820 | kMaxDigitsBeforePoint + kMaxDigitsAfterPoint + 1; |
Kristian Monsen | 25f6136 | 2010-05-21 11:50:48 +0100 | [diff] [blame] | 821 | char decimal_rep[kDecimalRepCapacity]; |
| 822 | int decimal_rep_length; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 823 | DoubleToAscii(value, DTOA_FIXED, f, |
| 824 | Vector<char>(decimal_rep, kDecimalRepCapacity), |
| 825 | &sign, &decimal_rep_length, &decimal_point); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 826 | |
| 827 | // Create a representation that is padded with zeros if needed. |
| 828 | int zero_prefix_length = 0; |
| 829 | int zero_postfix_length = 0; |
| 830 | |
| 831 | if (decimal_point <= 0) { |
| 832 | zero_prefix_length = -decimal_point + 1; |
| 833 | decimal_point = 1; |
| 834 | } |
| 835 | |
| 836 | if (zero_prefix_length + decimal_rep_length < decimal_point + f) { |
| 837 | zero_postfix_length = decimal_point + f - decimal_rep_length - |
| 838 | zero_prefix_length; |
| 839 | } |
| 840 | |
| 841 | unsigned rep_length = |
| 842 | zero_prefix_length + decimal_rep_length + zero_postfix_length; |
| 843 | StringBuilder rep_builder(rep_length + 1); |
| 844 | rep_builder.AddPadding('0', zero_prefix_length); |
| 845 | rep_builder.AddString(decimal_rep); |
| 846 | rep_builder.AddPadding('0', zero_postfix_length); |
| 847 | char* rep = rep_builder.Finalize(); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 848 | |
| 849 | // Create the result string by appending a minus and putting in a |
| 850 | // decimal point if needed. |
| 851 | unsigned result_size = decimal_point + f + 2; |
| 852 | StringBuilder builder(result_size + 1); |
| 853 | if (negative) builder.AddCharacter('-'); |
| 854 | builder.AddSubstring(rep, decimal_point); |
| 855 | if (f > 0) { |
| 856 | builder.AddCharacter('.'); |
| 857 | builder.AddSubstring(rep + decimal_point, f); |
| 858 | } |
| 859 | DeleteArray(rep); |
| 860 | return builder.Finalize(); |
| 861 | } |
| 862 | |
| 863 | |
| 864 | static char* CreateExponentialRepresentation(char* decimal_rep, |
| 865 | int exponent, |
| 866 | bool negative, |
| 867 | int significant_digits) { |
| 868 | bool negative_exponent = false; |
| 869 | if (exponent < 0) { |
| 870 | negative_exponent = true; |
| 871 | exponent = -exponent; |
| 872 | } |
| 873 | |
| 874 | // Leave room in the result for appending a minus, for a period, the |
| 875 | // letter 'e', a minus or a plus depending on the exponent, and a |
| 876 | // three digit exponent. |
| 877 | unsigned result_size = significant_digits + 7; |
| 878 | StringBuilder builder(result_size + 1); |
| 879 | |
| 880 | if (negative) builder.AddCharacter('-'); |
| 881 | builder.AddCharacter(decimal_rep[0]); |
| 882 | if (significant_digits != 1) { |
| 883 | builder.AddCharacter('.'); |
| 884 | builder.AddString(decimal_rep + 1); |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 885 | int rep_length = StrLength(decimal_rep); |
| 886 | builder.AddPadding('0', significant_digits - rep_length); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 887 | } |
| 888 | |
| 889 | builder.AddCharacter('e'); |
| 890 | builder.AddCharacter(negative_exponent ? '-' : '+'); |
| 891 | builder.AddFormatted("%d", exponent); |
| 892 | return builder.Finalize(); |
| 893 | } |
| 894 | |
| 895 | |
| 896 | |
| 897 | char* DoubleToExponentialCString(double value, int f) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 898 | const int kMaxDigitsAfterPoint = 20; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 899 | // f might be -1 to signal that f was undefined in JavaScript. |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 900 | ASSERT(f >= -1 && f <= kMaxDigitsAfterPoint); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 901 | |
| 902 | bool negative = false; |
| 903 | if (value < 0) { |
| 904 | value = -value; |
| 905 | negative = true; |
| 906 | } |
| 907 | |
| 908 | // Find a sufficiently precise decimal representation of n. |
| 909 | int decimal_point; |
| 910 | int sign; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 911 | // f corresponds to the digits after the point. There is always one digit |
| 912 | // before the point. The number of requested_digits equals hence f + 1. |
| 913 | // And we have to add one character for the null-terminator. |
| 914 | const int kV8DtoaBufferCapacity = kMaxDigitsAfterPoint + 1 + 1; |
| 915 | // Make sure that the buffer is big enough, even if we fall back to the |
| 916 | // shortest representation (which happens when f equals -1). |
| 917 | ASSERT(kBase10MaximalLength <= kMaxDigitsAfterPoint + 1); |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 918 | char decimal_rep[kV8DtoaBufferCapacity]; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 919 | int decimal_rep_length; |
| 920 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 921 | if (f == -1) { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 922 | DoubleToAscii(value, DTOA_SHORTEST, 0, |
| 923 | Vector<char>(decimal_rep, kV8DtoaBufferCapacity), |
| 924 | &sign, &decimal_rep_length, &decimal_point); |
| 925 | f = decimal_rep_length - 1; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 926 | } else { |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 927 | DoubleToAscii(value, DTOA_PRECISION, f + 1, |
| 928 | Vector<char>(decimal_rep, kV8DtoaBufferCapacity), |
| 929 | &sign, &decimal_rep_length, &decimal_point); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 930 | } |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 931 | ASSERT(decimal_rep_length > 0); |
| 932 | ASSERT(decimal_rep_length <= f + 1); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 933 | |
| 934 | int exponent = decimal_point - 1; |
| 935 | char* result = |
| 936 | CreateExponentialRepresentation(decimal_rep, exponent, negative, f+1); |
| 937 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 938 | return result; |
| 939 | } |
| 940 | |
| 941 | |
| 942 | char* DoubleToPrecisionCString(double value, int p) { |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 943 | const int kMinimalDigits = 1; |
| 944 | const int kMaximalDigits = 21; |
| 945 | ASSERT(p >= kMinimalDigits && p <= kMaximalDigits); |
| 946 | USE(kMinimalDigits); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 947 | |
| 948 | bool negative = false; |
| 949 | if (value < 0) { |
| 950 | value = -value; |
| 951 | negative = true; |
| 952 | } |
| 953 | |
| 954 | // Find a sufficiently precise decimal representation of n. |
| 955 | int decimal_point; |
| 956 | int sign; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 957 | // Add one for the terminating null character. |
| 958 | const int kV8DtoaBufferCapacity = kMaximalDigits + 1; |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 959 | char decimal_rep[kV8DtoaBufferCapacity]; |
Kristian Monsen | 0d5e116 | 2010-09-30 15:31:59 +0100 | [diff] [blame] | 960 | int decimal_rep_length; |
| 961 | |
Shimeng (Simon) Wang | 8a31eba | 2010-12-06 19:01:33 -0800 | [diff] [blame] | 962 | DoubleToAscii(value, DTOA_PRECISION, p, |
| 963 | Vector<char>(decimal_rep, kV8DtoaBufferCapacity), |
| 964 | &sign, &decimal_rep_length, &decimal_point); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 965 | ASSERT(decimal_rep_length <= p); |
| 966 | |
| 967 | int exponent = decimal_point - 1; |
| 968 | |
| 969 | char* result = NULL; |
| 970 | |
| 971 | if (exponent < -6 || exponent >= p) { |
| 972 | result = |
| 973 | CreateExponentialRepresentation(decimal_rep, exponent, negative, p); |
| 974 | } else { |
| 975 | // Use fixed notation. |
| 976 | // |
| 977 | // Leave room in the result for appending a minus, a period and in |
| 978 | // the case where decimal_point is not positive for a zero in |
| 979 | // front of the period. |
| 980 | unsigned result_size = (decimal_point <= 0) |
| 981 | ? -decimal_point + p + 3 |
| 982 | : p + 2; |
| 983 | StringBuilder builder(result_size + 1); |
| 984 | if (negative) builder.AddCharacter('-'); |
| 985 | if (decimal_point <= 0) { |
| 986 | builder.AddString("0."); |
| 987 | builder.AddPadding('0', -decimal_point); |
| 988 | builder.AddString(decimal_rep); |
| 989 | builder.AddPadding('0', p - decimal_rep_length); |
| 990 | } else { |
| 991 | const int m = Min(decimal_rep_length, decimal_point); |
| 992 | builder.AddSubstring(decimal_rep, m); |
| 993 | builder.AddPadding('0', decimal_point - decimal_rep_length); |
| 994 | if (decimal_point < p) { |
| 995 | builder.AddCharacter('.'); |
| 996 | const int extra = negative ? 2 : 1; |
| 997 | if (decimal_rep_length > decimal_point) { |
Steve Block | d0582a6 | 2009-12-15 09:54:21 +0000 | [diff] [blame] | 998 | const int len = StrLength(decimal_rep + decimal_point); |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 999 | const int n = Min(len, p - (builder.position() - extra)); |
| 1000 | builder.AddSubstring(decimal_rep + decimal_point, n); |
| 1001 | } |
| 1002 | builder.AddPadding('0', extra + (p - builder.position())); |
| 1003 | } |
| 1004 | } |
| 1005 | result = builder.Finalize(); |
| 1006 | } |
| 1007 | |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1008 | return result; |
| 1009 | } |
| 1010 | |
| 1011 | |
| 1012 | char* DoubleToRadixCString(double value, int radix) { |
| 1013 | ASSERT(radix >= 2 && radix <= 36); |
| 1014 | |
| 1015 | // Character array used for conversion. |
| 1016 | static const char chars[] = "0123456789abcdefghijklmnopqrstuvwxyz"; |
| 1017 | |
| 1018 | // Buffer for the integer part of the result. 1024 chars is enough |
| 1019 | // for max integer value in radix 2. We need room for a sign too. |
| 1020 | static const int kBufferSize = 1100; |
| 1021 | char integer_buffer[kBufferSize]; |
| 1022 | integer_buffer[kBufferSize - 1] = '\0'; |
| 1023 | |
| 1024 | // Buffer for the decimal part of the result. We only generate up |
| 1025 | // to kBufferSize - 1 chars for the decimal part. |
| 1026 | char decimal_buffer[kBufferSize]; |
| 1027 | decimal_buffer[kBufferSize - 1] = '\0'; |
| 1028 | |
| 1029 | // Make sure the value is positive. |
| 1030 | bool is_negative = value < 0.0; |
| 1031 | if (is_negative) value = -value; |
| 1032 | |
| 1033 | // Get the integer part and the decimal part. |
| 1034 | double integer_part = floor(value); |
| 1035 | double decimal_part = value - integer_part; |
| 1036 | |
| 1037 | // Convert the integer part starting from the back. Always generate |
| 1038 | // at least one digit. |
| 1039 | int integer_pos = kBufferSize - 2; |
| 1040 | do { |
| 1041 | integer_buffer[integer_pos--] = |
Steve Block | 3ce2e20 | 2009-11-05 08:53:23 +0000 | [diff] [blame] | 1042 | chars[static_cast<int>(modulo(integer_part, radix))]; |
Steve Block | a7e24c1 | 2009-10-30 11:49:00 +0000 | [diff] [blame] | 1043 | integer_part /= radix; |
| 1044 | } while (integer_part >= 1.0); |
| 1045 | // Sanity check. |
| 1046 | ASSERT(integer_pos > 0); |
| 1047 | // Add sign if needed. |
| 1048 | if (is_negative) integer_buffer[integer_pos--] = '-'; |
| 1049 | |
| 1050 | // Convert the decimal part. Repeatedly multiply by the radix to |
| 1051 | // generate the next char. Never generate more than kBufferSize - 1 |
| 1052 | // chars. |
| 1053 | // |
| 1054 | // TODO(1093998): We will often generate a full decimal_buffer of |
| 1055 | // chars because hitting zero will often not happen. The right |
| 1056 | // solution would be to continue until the string representation can |
| 1057 | // be read back and yield the original value. To implement this |
| 1058 | // efficiently, we probably have to modify dtoa. |
| 1059 | int decimal_pos = 0; |
| 1060 | while ((decimal_part > 0.0) && (decimal_pos < kBufferSize - 1)) { |
| 1061 | decimal_part *= radix; |
| 1062 | decimal_buffer[decimal_pos++] = |
| 1063 | chars[static_cast<int>(floor(decimal_part))]; |
| 1064 | decimal_part -= floor(decimal_part); |
| 1065 | } |
| 1066 | decimal_buffer[decimal_pos] = '\0'; |
| 1067 | |
| 1068 | // Compute the result size. |
| 1069 | int integer_part_size = kBufferSize - 2 - integer_pos; |
| 1070 | // Make room for zero termination. |
| 1071 | unsigned result_size = integer_part_size + decimal_pos; |
| 1072 | // If the number has a decimal part, leave room for the period. |
| 1073 | if (decimal_pos > 0) result_size++; |
| 1074 | // Allocate result and fill in the parts. |
| 1075 | StringBuilder builder(result_size + 1); |
| 1076 | builder.AddSubstring(integer_buffer + integer_pos + 1, integer_part_size); |
| 1077 | if (decimal_pos > 0) builder.AddCharacter('.'); |
| 1078 | builder.AddSubstring(decimal_buffer, decimal_pos); |
| 1079 | return builder.Finalize(); |
| 1080 | } |
| 1081 | |
| 1082 | |
| 1083 | } } // namespace v8::internal |