Changeset 101056 in webkit for trunk/Source/JavaScriptCore/wtf/dtoa.cpp
- Timestamp:
- Nov 23, 2011, 1:22:55 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Source/JavaScriptCore/wtf/dtoa.cpp
r95511 r101056 1793 1793 } 1794 1794 1795 1796 const char *numberToString(double d, NumberToStringBuffer buffer) 1795 const char* numberToString(double d, NumberToStringBuffer buffer) 1797 1796 { 1798 1797 double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength); … … 1802 1801 } 1803 1802 1803 static inline const char* formatStringTruncatingTrailingZerosIfNeeded(NumberToStringBuffer buffer, double_conversion::StringBuilder& builder) 1804 { 1805 size_t length = builder.position(); 1806 size_t decimalPointPosition = 0; 1807 for (; decimalPointPosition < length; ++decimalPointPosition) { 1808 if (buffer[decimalPointPosition] == '.') 1809 break; 1810 } 1811 1812 // No decimal seperator found, early exit. 1813 if (decimalPointPosition == length) 1814 return builder.Finalize(); 1815 1816 size_t truncatedLength = length - 1; 1817 for (; truncatedLength > decimalPointPosition; --truncatedLength) { 1818 if (buffer[truncatedLength] != '0') 1819 break; 1820 } 1821 1822 // No trailing zeros found to strip. 1823 if (truncatedLength == length - 1) 1824 return builder.Finalize(); 1825 1826 // If we removed all trailing zeros, remove the decimal point as well. 1827 if (truncatedLength == decimalPointPosition) { 1828 ASSERT(truncatedLength > 0); 1829 --truncatedLength; 1830 } 1831 1832 // Truncate the StringBuilder, and return the final result. 1833 builder.SetPosition(truncatedLength + 1); 1834 return builder.Finalize(); 1835 } 1836 1837 const char* numberToFixedPrecisionString(double d, unsigned significantFigures, NumberToStringBuffer buffer, bool truncateTrailingZeros) 1838 { 1839 // Mimic String::format("%.[precision]g", ...), but use dtoas rounding facilities. 1840 // "g": Signed value printed in f or e format, whichever is more compact for the given value and precision. 1841 // The e format is used only when the exponent of the value is less than –4 or greater than or equal to the 1842 // precision argument. Trailing zeros are truncated, and the decimal point appears only if one or more digits follow it. 1843 // "precision": The precision specifies the maximum number of significant digits printed. 1844 double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength); 1845 const double_conversion::DoubleToStringConverter& converter = double_conversion::DoubleToStringConverter::EcmaScriptConverter(); 1846 converter.ToPrecision(d, significantFigures, &builder); 1847 if (!truncateTrailingZeros) 1848 return builder.Finalize(); 1849 return formatStringTruncatingTrailingZerosIfNeeded(buffer, builder); 1850 } 1851 1852 const char* numberToFixedWidthString(double d, unsigned decimalPlaces, NumberToStringBuffer buffer) 1853 { 1854 // Mimic String::format("%.[precision]f", ...), but use dtoas rounding facilities. 1855 // "f": Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits. 1856 // The number of digits before the decimal point depends on the magnitude of the number, and 1857 // the number of digits after the decimal point depends on the requested precision. 1858 // "precision": The precision value specifies the number of digits after the decimal point. 1859 // If a decimal point appears, at least one digit appears before it. 1860 // The value is rounded to the appropriate number of digits. 1861 double_conversion::StringBuilder builder(buffer, NumberToStringBufferLength); 1862 const double_conversion::DoubleToStringConverter& converter = double_conversion::DoubleToStringConverter::EcmaScriptConverter(); 1863 converter.ToFixed(d, decimalPlaces, &builder); 1864 return builder.Finalize(); 1865 } 1866 1804 1867 } // namespace WTF
Note:
See TracChangeset
for help on using the changeset viewer.