Changeset 51168 in webkit for trunk/JavaScriptCore/wtf/dtoa.cpp


Ignore:
Timestamp:
Nov 18, 2009, 7:12:50 PM (16 years ago)
Author:
[email protected]
Message:

2009-11-18 Kent Tamura <[email protected]>

Reviewed by Darin Adler.

Move UString::from(double) implementation to new
WTF::doubleToStringInJavaScriptFormat(), and expose it because WebCore
code will use it.
https://bugs.webkit.org/show_bug.cgi?id=31330

  • Introduce new function createRep(const char*, unsigned) and UString::UString(const char*, unsigned) to reduce 2 calls to strlen().
  • Fix a bug that dtoa() doesn't update *rve if the input value is NaN or Infinity.

No new tests because this doesn't change the behavior.

  • JavaScriptCore.exp:
  • JavaScriptCore.vcproj/JavaScriptCore/JavaScriptCore.def:
  • runtime/UString.cpp: (JSC::createRep): (JSC::UString::UString): (JSC::UString::from): Move the code to doubleToStringInJavaScriptFormat().
  • runtime/UString.h:
  • wtf/dtoa.cpp: (WTF::dtoa): Fix a bug about rve. (WTF::append): A helper for doubleToStringInJavaScriptFormat(). (WTF::doubleToStringInJavaScriptFormat): Move the code from UString::from(double).
  • wtf/dtoa.h:
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/wtf/dtoa.cpp

    r48248 r51168  
    149149#include <wtf/Assertions.h>
    150150#include <wtf/FastMalloc.h>
     151#include <wtf/MathExtras.h>
    151152#include <wtf/Vector.h>
    152153#include <wtf/Threading.h>
     
    18701871 */
    18711872
    1872 void dtoa(char* result, double dd, int ndigits, int* decpt, int* sign, char** rve)
     1873void dtoa(DtoaBuffer result, double dd, int ndigits, int* decpt, int* sign, char** rve)
    18731874{
    18741875    /*
     
    19091910        /* Infinity or NaN */
    19101911        *decpt = 9999;
    1911         if (!word1(&u) && !(word0(&u) & 0xfffff))
     1912        if (!word1(&u) && !(word0(&u) & 0xfffff)) {
    19121913            strcpy(result, "Infinity");
    1913         else
     1914            if (rve)
     1915                *rve = result + 8;
     1916        } else {
    19141917            strcpy(result, "NaN");
     1918            if (rve)
     1919                *rve = result + 3;
     1920        }
    19151921        return;
    19161922    }
     
    19191925        result[0] = '0';
    19201926        result[1] = '\0';
     1927        if (rve)
     1928            *rve = result + 1;
    19211929        return;
    19221930    }
     
    23772385}
    23782386
     2387static 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
     2393void 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
    23792466} // namespace WTF
Note: See TracChangeset for help on using the changeset viewer.