Changeset 65959 in webkit for trunk/JavaScriptCore/wtf/dtoa.cpp
- Timestamp:
- Aug 24, 2010, 6:04:31 PM (15 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/wtf/dtoa.cpp
r65588 r65959 2286 2286 } 2287 2287 2288 static ALWAYS_INLINE void append(char*& next, const char* src, unsigned size)2288 double intPow10(int e) 2289 2289 { 2290 for (unsigned i = 0; i < size; ++i) 2291 *next++ = *src++; 2290 // This function uses the "exponentiation by squaring" algorithm and 2291 // long double to quickly and precisely calculate integer powers of 10.0. 2292 2293 // This is a handy workaround for <rdar://problem/4494756> 2294 2295 if (!e) 2296 return 1.0; 2297 2298 bool negative = e < 0; 2299 unsigned exp = negative ? -e : e; 2300 2301 long double result = 10.0; 2302 bool foundOne = false; 2303 for (int bit = 31; bit >= 0; bit--) { 2304 if (!foundOne) { 2305 if ((exp >> bit) & 1) 2306 foundOne = true; 2307 } else { 2308 result = result * result; 2309 if ((exp >> bit) & 1) 2310 result = result * 10.0; 2311 } 2312 } 2313 2314 if (negative) 2315 return static_cast<double>(1.0 / result); 2316 return static_cast<double>(result); 2292 2317 } 2293 2318 2294 void doubleToStringInJavaScriptFormat(double d, DtoaBuffer buffer, unsigned* resultLength)2295 {2296 ASSERT(buffer);2297 2298 // avoid ever printing -NaN, in JS conceptually there is only one NaN value2299 if (isnan(d)) {2300 append(buffer, "NaN", 3);2301 if (resultLength)2302 *resultLength = 3;2303 return;2304 }2305 // -0 -> "0"2306 if (!d) {2307 buffer[0] = '0';2308 if (resultLength)2309 *resultLength = 1;2310 return;2311 }2312 2313 int decimalPoint;2314 int sign;2315 2316 DtoaBuffer result;2317 char* resultEnd = 0;2318 WTF::dtoa(result, d, 0, &decimalPoint, &sign, &resultEnd);2319 int length = resultEnd - result;2320 2321 char* next = buffer;2322 if (sign)2323 *next++ = '-';2324 2325 if (decimalPoint <= 0 && decimalPoint > -6) {2326 *next++ = '0';2327 *next++ = '.';2328 for (int j = decimalPoint; j < 0; j++)2329 *next++ = '0';2330 append(next, result, length);2331 } else if (decimalPoint <= 21 && decimalPoint > 0) {2332 if (length <= decimalPoint) {2333 append(next, result, length);2334 for (int j = 0; j < decimalPoint - length; j++)2335 *next++ = '0';2336 } else {2337 append(next, result, decimalPoint);2338 *next++ = '.';2339 append(next, result + decimalPoint, length - decimalPoint);2340 }2341 } else if (result[0] < '0' || result[0] > '9')2342 append(next, result, length);2343 else {2344 *next++ = result[0];2345 if (length > 1) {2346 *next++ = '.';2347 append(next, result + 1, length - 1);2348 }2349 2350 *next++ = 'e';2351 *next++ = (decimalPoint >= 0) ? '+' : '-';2352 // decimalPoint can't be more than 3 digits decimal given the2353 // nature of float representation2354 int exponential = decimalPoint - 1;2355 if (exponential < 0)2356 exponential = -exponential;2357 if (exponential >= 100)2358 *next++ = static_cast<char>('0' + exponential / 100);2359 if (exponential >= 10)2360 *next++ = static_cast<char>('0' + (exponential % 100) / 10);2361 *next++ = static_cast<char>('0' + exponential % 10);2362 }2363 if (resultLength)2364 *resultLength = next - buffer;2365 }2366 2367 2319 } // namespace WTF
Note:
See TracChangeset
for help on using the changeset viewer.