Changeset 51168 in webkit for trunk/JavaScriptCore/runtime/UString.cpp
- Timestamp:
- Nov 18, 2009, 7:12:50 PM (16 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/runtime/UString.cpp
r48298 r51168 579 579 } 580 580 581 static 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 581 598 UString::UString(const char* c) 582 599 : m_rep(createRep(c)) 600 { 601 } 602 603 UString::UString(const char* c, int length) 604 : m_rep(createRep(c, length)) 583 605 { 584 606 } … … 1026 1048 UString UString::from(double d) 1027 1049 { 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); 1091 1054 } 1092 1055
Note:
See TracChangeset
for help on using the changeset viewer.