Changeset 51168 in webkit for trunk/JavaScriptCore/wtf/dtoa.cpp
- Timestamp:
- Nov 18, 2009, 7:12:50 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/dtoa.cpp
r48248 r51168 149 149 #include <wtf/Assertions.h> 150 150 #include <wtf/FastMalloc.h> 151 #include <wtf/MathExtras.h> 151 152 #include <wtf/Vector.h> 152 153 #include <wtf/Threading.h> … … 1870 1871 */ 1871 1872 1872 void dtoa( char*result, double dd, int ndigits, int* decpt, int* sign, char** rve)1873 void dtoa(DtoaBuffer result, double dd, int ndigits, int* decpt, int* sign, char** rve) 1873 1874 { 1874 1875 /* … … 1909 1910 /* Infinity or NaN */ 1910 1911 *decpt = 9999; 1911 if (!word1(&u) && !(word0(&u) & 0xfffff)) 1912 if (!word1(&u) && !(word0(&u) & 0xfffff)) { 1912 1913 strcpy(result, "Infinity"); 1913 else 1914 if (rve) 1915 *rve = result + 8; 1916 } else { 1914 1917 strcpy(result, "NaN"); 1918 if (rve) 1919 *rve = result + 3; 1920 } 1915 1921 return; 1916 1922 } … … 1919 1925 result[0] = '0'; 1920 1926 result[1] = '\0'; 1927 if (rve) 1928 *rve = result + 1; 1921 1929 return; 1922 1930 } … … 2377 2385 } 2378 2386 2387 static ALWAYS_INLINE void append(char*& next, const char* src, unsigned size) 2388 { 2389 for (unsigned i = 0; i < size; ++i) 2390 *next++ = *src++; 2391 } 2392 2393 void doubleToStringInJavaScriptFormat(double d, DtoaBuffer buffer, unsigned* resultLength) 2394 { 2395 ASSERT(buffer); 2396 2397 // avoid ever printing -NaN, in JS conceptually there is only one NaN value 2398 if (isnan(d)) { 2399 append(buffer, "NaN", 3); 2400 if (resultLength) 2401 *resultLength = 3; 2402 return; 2403 } 2404 // -0 -> "0" 2405 if (!d) { 2406 buffer[0] = '0'; 2407 if (resultLength) 2408 *resultLength = 1; 2409 return; 2410 } 2411 2412 int decimalPoint; 2413 int sign; 2414 2415 DtoaBuffer result; 2416 char* resultEnd = 0; 2417 WTF::dtoa(result, d, 0, &decimalPoint, &sign, &resultEnd); 2418 int length = resultEnd - result; 2419 2420 char* next = buffer; 2421 if (sign) 2422 *next++ = '-'; 2423 2424 if (decimalPoint <= 0 && decimalPoint > -6) { 2425 *next++ = '0'; 2426 *next++ = '.'; 2427 for (int j = decimalPoint; j < 0; j++) 2428 *next++ = '0'; 2429 append(next, result, length); 2430 } else if (decimalPoint <= 21 && decimalPoint > 0) { 2431 if (length <= decimalPoint) { 2432 append(next, result, length); 2433 for (int j = 0; j < decimalPoint - length; j++) 2434 *next++ = '0'; 2435 } else { 2436 append(next, result, decimalPoint); 2437 *next++ = '.'; 2438 append(next, result + decimalPoint, length - decimalPoint); 2439 } 2440 } else if (result[0] < '0' || result[0] > '9') 2441 append(next, result, length); 2442 else { 2443 *next++ = result[0]; 2444 if (length > 1) { 2445 *next++ = '.'; 2446 append(next, result + 1, length - 1); 2447 } 2448 2449 *next++ = 'e'; 2450 *next++ = (decimalPoint >= 0) ? '+' : '-'; 2451 // decimalPoint can't be more than 3 digits decimal given the 2452 // nature of float representation 2453 int exponential = decimalPoint - 1; 2454 if (exponential < 0) 2455 exponential = -exponential; 2456 if (exponential >= 100) 2457 *next++ = static_cast<char>('0' + exponential / 100); 2458 if (exponential >= 10) 2459 *next++ = static_cast<char>('0' + (exponential % 100) / 10); 2460 *next++ = static_cast<char>('0' + exponential % 10); 2461 } 2462 if (resultLength) 2463 *resultLength = next - buffer; 2464 } 2465 2379 2466 } // namespace WTF
Note:
See TracChangeset
for help on using the changeset viewer.