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/runtime/UString.cpp

    r48298 r51168  
    579579}
    580580
     581static inline PassRefPtr<UString::Rep> createRep(const char* c, int length)
     582{
     583    if (!c)
     584        return &UString::Rep::null();
     585
     586    if (!length)
     587        return &UString::Rep::empty();
     588
     589    UChar* d;
     590    if (!allocChars(length).getValue(d))
     591        return &UString::Rep::null();
     592
     593    for (int i = 0; i < length; i++)
     594        d[i] = static_cast<unsigned char>(c[i]); // use unsigned char to zero-extend instead of sign-extend
     595    return UString::Rep::create(d, length);
     596}
     597
    581598UString::UString(const char* c)
    582599    : m_rep(createRep(c))
     600{
     601}
     602
     603UString::UString(const char* c, int length)
     604    : m_rep(createRep(c, length))
    583605{
    584606}
     
    10261048UString UString::from(double d)
    10271049{
    1028     // avoid ever printing -NaN, in JS conceptually there is only one NaN value
    1029     if (isnan(d))
    1030         return "NaN";
    1031     if (!d)
    1032         return "0"; // -0 -> "0"
    1033 
    1034     char buf[80];
    1035     int decimalPoint;
    1036     int sign;
    1037    
    1038     char result[80];
    1039     WTF::dtoa(result, d, 0, &decimalPoint, &sign, NULL);
    1040     int length = static_cast<int>(strlen(result));
    1041  
    1042     int i = 0;
    1043     if (sign)
    1044         buf[i++] = '-';
    1045  
    1046     if (decimalPoint <= 0 && decimalPoint > -6) {
    1047         buf[i++] = '0';
    1048         buf[i++] = '.';
    1049         for (int j = decimalPoint; j < 0; j++)
    1050             buf[i++] = '0';
    1051         strcpy(buf + i, result);
    1052     } else if (decimalPoint <= 21 && decimalPoint > 0) {
    1053         if (length <= decimalPoint) {
    1054             strcpy(buf + i, result);
    1055             i += length;
    1056             for (int j = 0; j < decimalPoint - length; j++)
    1057                 buf[i++] = '0';
    1058             buf[i] = '\0';
    1059         } else {
    1060             strncpy(buf + i, result, decimalPoint);
    1061             i += decimalPoint;
    1062             buf[i++] = '.';
    1063             strcpy(buf + i, result + decimalPoint);
    1064         }
    1065     } else if (result[0] < '0' || result[0] > '9')
    1066         strcpy(buf + i, result);
    1067     else {
    1068         buf[i++] = result[0];
    1069         if (length > 1) {
    1070             buf[i++] = '.';
    1071             strcpy(buf + i, result + 1);
    1072             i += length - 1;
    1073         }
    1074        
    1075         buf[i++] = 'e';
    1076         buf[i++] = (decimalPoint >= 0) ? '+' : '-';
    1077         // decimalPoint can't be more than 3 digits decimal given the
    1078         // nature of float representation
    1079         int exponential = decimalPoint - 1;
    1080         if (exponential < 0)
    1081             exponential = -exponential;
    1082         if (exponential >= 100)
    1083             buf[i++] = static_cast<char>('0' + exponential / 100);
    1084         if (exponential >= 10)
    1085             buf[i++] = static_cast<char>('0' + (exponential % 100) / 10);
    1086         buf[i++] = static_cast<char>('0' + exponential % 10);
    1087         buf[i++] = '\0';
    1088     }
    1089    
    1090     return UString(buf);
     1050    DtoaBuffer buffer;
     1051    unsigned length;
     1052    doubleToStringInJavaScriptFormat(d, buffer, &length);
     1053    return UString(buffer, length);
    10911054}
    10921055
Note: See TracChangeset for help on using the changeset viewer.