⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 242360 in webkit


Ignore:
Timestamp:
Mar 4, 2019, 8:58:33 AM (7 years ago)
Author:
Darin Adler
Message:

Prepare to improve handling of conversion of float to strings
https://bugs.webkit.org/show_bug.cgi?id=195262

Reviewed by Daniel Bates.

Source/WTF:

  • wtf/dtoa.cpp:

(WTF::truncateTrailingZeros): Renamed from
formatStringTruncatingTrailingZerosIfNeeded and removed the calls
to double_conversion::StringBuilder::Finalizer, since the caller
already does that.
(WTF::numberToFixedPrecisionString): Added an overload for float
and updated to use the new truncateTrailingZeros.
(WTF::numberToFixedWidthString): Added an overload for float.

  • wtf/text/AtomicString.cpp:

(WTF::AtomicString::number): Added float overload. This is a
behavior change, but in all cases for the better. The old behavior
was to convert to double first and then do "shortest form"
conversion, and it's always better to just do that as float.

  • wtf/text/AtomicString.h: Added float overload of AtomicString::number.
  • wtf/text/StringBuilder.cpp:

(WTF::StringBuilder::appendFixedPrecisionNumber): Added float
overload.
(WTF::StringBuilder::appendShortestFormNumber): Renamed from
appendECMAScriptNumber and did the above.
(WTF::StringBuilder::appendFixedWidthNumber): Ditto.

  • wtf/text/StringBuilder.h: Added overloads for float and

appendShortestFormNumber. The appendNumber and appendECMAScriptNumber
functions are now inlines in the header, since they are expressed
entirely in terms of the other functions.

  • wtf/text/WTFString.cpp:

(WTF::String::numberToStringFixedPrecision): Added float overload.
Removed unnecessary explicit conversion to String.
(WTF::String::numberToStringShortest): Renamed from
numberToStringECMAScript and did the above.
(WTF::String::numberToStringFixedWidth): Ditto.

  • wtf/text/WTFString.h: Added overloads for float and

numberToStringShortest. The number and numberToStringECMAScript
functions are now inlines in the header, since they are expressed
entirely in terms of the other functions.

LayoutTests:

  • svg/dom/SVGAngle-expected.txt:
  • svg/dom/SVGAngle.html:
  • svg/dom/SVGLength-px-expected.txt:
  • svg/dom/SVGLength-px-with-context-expected.txt:
  • svg/dom/SVGLength-px-with-context.html:
  • svg/dom/SVGLength-px.html:

Remove checks that depend on the precision and number of serialization.
The current tests depend on behavior that we'd like to change in the future and
some of them had already been updated since the behavior is different in different
web browsers. Tricky issue because of the mix of single and double precision in
the SVG engine and the JavaScript language, and straightforward to keep the tests
useful without this unnecessary dependency. Generally we check the units of the
result of valueAsString, and not the numeric result. Another idea would be to
check the numeric part of the result by converting it back to a number and
comparing it as a number rather than as a string.

Location:
trunk
Files:
15 edited

Legend:

Unmodified
Added
Removed
  • trunk/LayoutTests/ChangeLog

    r242358 r242360  
     12019-03-03  Darin Adler  <darin@apple.com>
     2
     3        Prepare to improve handling of conversion of float to strings
     4        https://bugs.webkit.org/show_bug.cgi?id=195262
     5
     6        Reviewed by Daniel Bates.
     7
     8        * svg/dom/SVGAngle-expected.txt:
     9        * svg/dom/SVGAngle.html:
     10        * svg/dom/SVGLength-px-expected.txt:
     11        * svg/dom/SVGLength-px-with-context-expected.txt:
     12        * svg/dom/SVGLength-px-with-context.html:
     13        * svg/dom/SVGLength-px.html:
     14        Remove checks that depend on the precision and number of serialization.
     15        The current tests depend on behavior that we'd like to change in the future and
     16        some of them had already been updated since the behavior is different in different
     17        web browsers. Tricky issue because of the mix of single and double precision in
     18        the SVG engine and the JavaScript language, and straightforward to keep the tests
     19        useful without this unnecessary dependency. Generally we check the units of the
     20        result of valueAsString, and not the numeric result. Another idea would be to
     21        check the numeric part of the result by converting it back to a number and
     22        comparing it as a number rather than as a string.
     23
    1242019-03-04  Truitt Savell  <tsavell@apple.com>
    225
  • trunk/LayoutTests/svg/dom/SVGAngle-expected.txt

    r219663 r242360  
    6767Check valid arguments for 'newValueSpecifiedUnits', that should only modify the 'valueAsString'
    6868PASS angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_RAD, parseFloat(Math.PI.toFixed(5))) is undefined.
    69 PASS angle.valueAsString is "3.14159rad"
     69PASS angle.valueAsString.slice(-3) is "rad"
    7070PASS angle.value.toFixed(1) is "180.0"
    7171PASS angle.valueInSpecifiedUnits.toFixed(5) is Math.PI.toFixed(5)
     
    171171PASS angle.value.toFixed(1) is "180.0"
    172172PASS angle.valueInSpecifiedUnits.toFixed(5) is "3.14159"
    173 PASS angle.valueAsString is "3.14159rad"
     173PASS angle.valueAsString.slice(-3) is "rad"
    174174PASS angle.unitType is SVGAngle.SVG_ANGLETYPE_RAD
    175175
     
    178178PASS angle.value.toFixed(1) is "180.0"
    179179PASS angle.valueInSpecifiedUnits.toFixed(5) is "3.14159"
    180 PASS angle.valueAsString is "3.14159rad"
     180PASS angle.valueAsString.slice(-3) is "rad"
    181181PASS angle.unitType is SVGAngle.SVG_ANGLETYPE_RAD
    182182
  • trunk/LayoutTests/svg/dom/SVGAngle.html

    r217390 r242360  
    9090debug("Check valid arguments for 'newValueSpecifiedUnits', that should only modify the 'valueAsString'");
    9191shouldBeUndefined("angle.newValueSpecifiedUnits(SVGAngle.SVG_ANGLETYPE_RAD, parseFloat(Math.PI.toFixed(5)))");
    92 shouldBeEqualToString("angle.valueAsString", Math.PI.toFixed(5) + "rad");
     92// Exact result of valueAsString depends on precision, no trivial way to correctly test it, especially across engines.
     93shouldBeEqualToString("angle.valueAsString.slice(-3)", "rad");
    9394shouldBeEqualToString("angle.value.toFixed(1)", "180.0");
    9495shouldBe("angle.valueInSpecifiedUnits.toFixed(5)", "Math.PI.toFixed(5)");
     
    214215shouldBeEqualToString("angle.value.toFixed(1)", "180.0");
    215216shouldBeEqualToString("angle.valueInSpecifiedUnits.toFixed(5)", Math.PI.toFixed(5));
    216 shouldBeEqualToString("angle.valueAsString", Math.PI.toFixed(5) + "rad");
     217// Exact result of valueAsString depends on precision, no trivial way to correctly test it, especially across engines.
     218shouldBeEqualToString("angle.valueAsString.slice(-3)", "rad");
    217219shouldBe("angle.unitType", "SVGAngle.SVG_ANGLETYPE_RAD");
    218220
     
    222224shouldBeEqualToString("angle.value.toFixed(1)", "180.0");
    223225shouldBeEqualToString("angle.valueInSpecifiedUnits.toFixed(5)", Math.PI.toFixed(5));
    224 shouldBeEqualToString("angle.valueAsString", Math.PI.toFixed(5) + "rad");
     226// Exact result of valueAsString depends on precision, no trivial way to correctly test it, especially across engines.
     227shouldBeEqualToString("angle.valueAsString.slice(-3)", "rad");
    225228shouldBe("angle.unitType", "SVGAngle.SVG_ANGLETYPE_RAD");
    226229
  • trunk/LayoutTests/svg/dom/SVGLength-px-expected.txt

    r219663 r242360  
    4949Convert from px to cm
    5050PASS length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_CM) is undefined.
    51 PASS length.valueAsString is "0.0529167cm"
     51PASS length.valueAsString.slice(-2) is "cm"
    5252PASS length.valueInSpecifiedUnits.toFixed(7) is "0.0529167"
    5353PASS length.value.toFixed(1) is "2.0"
     
    5858Convert from px to mm
    5959PASS length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_MM) is undefined.
    60 PASS length.valueAsString is "0.529167mm"
     60PASS length.valueAsString.slice(-2) is "mm"
    6161PASS length.valueInSpecifiedUnits.toFixed(6) is "0.529167"
    6262PASS length.value.toFixed(1) is "2.0"
     
    6767Convert from px to in
    6868PASS length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_IN) is undefined.
    69 PASS length.valueAsString is "0.0208333in"
     69PASS length.valueAsString.slice(-2) is "in"
    7070PASS length.valueInSpecifiedUnits.toFixed(7) is "0.0208333"
    7171PASS length.value.toFixed(1) is "2.0"
  • trunk/LayoutTests/svg/dom/SVGLength-px-with-context-expected.txt

    r71014 r242360  
    2222Convert from px to percentage
    2323PASS length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PERCENTAGE) is undefined.
    24 PASS length.valueAsString is "1.33333%"
     24PASS length.valueAsString.slice(-1) is "%"
    2525PASS length.valueInSpecifiedUnits.toFixed(5) is "1.33333"
    2626PASS length.value.toFixed(1) is "2.0"
     
    3131Convert from px to ems
    3232PASS length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EMS) is undefined.
    33 PASS length.valueAsString is "0.166667em"
     33PASS length.valueAsString.slice(-2) is "em"
    3434PASS length.valueInSpecifiedUnits.toFixed(6) is "0.166667"
    3535PASS length.value.toFixed(1) is "2.0"
     
    4040Convert from px to exs
    4141PASS length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EXS) is undefined.
     42PASS length.valueAsString.slice(-2) is "ex"
    4243PASS length.valueInSpecifiedUnits.toFixed(1) is "0.2"
    4344PASS length.value.toFixed(1) is "2.0"
     
    4849Convert from px to cm
    4950PASS length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_CM) is undefined.
    50 PASS length.valueAsString is "0.0529167cm"
     51PASS length.valueAsString.slice(-2) is "cm"
    5152PASS length.valueInSpecifiedUnits.toFixed(7) is "0.0529167"
    5253PASS length.value.toFixed(1) is "2.0"
     
    5758Convert from px to mm
    5859PASS length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_MM) is undefined.
    59 PASS length.valueAsString is "0.529167mm"
     60PASS length.valueAsString.slice(-2) is "mm"
    6061PASS length.valueInSpecifiedUnits.toFixed(6) is "0.529167"
    6162PASS length.value.toFixed(1) is "2.0"
     
    6667Convert from px to in
    6768PASS length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_IN) is undefined.
    68 PASS length.valueAsString is "0.0208333in"
     69PASS length.valueAsString.slice(-2) is "in"
    6970PASS length.valueInSpecifiedUnits.toFixed(7) is "0.0208333"
    7071PASS length.value.toFixed(1) is "2.0"
     
    8485Convert from px to pc
    8586PASS length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PC) is undefined.
     87PASS length.valueAsString.slice(-2) is "pc"
    8688PASS length.valueInSpecifiedUnits.toFixed(3) is "0.125"
    8789PASS length.value.toFixed(1) is "2.0"
  • trunk/LayoutTests/svg/dom/SVGLength-px-with-context.html

    r217390 r242360  
    8181shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PERCENTAGE)");
    8282referenceValue = Number(2 / svgWidth * 100).toFixed(5);
    83 shouldBeEqualToString("length.valueAsString", referenceValue + "%");
     83// Exact result of valueAsString depends on precision, no trivial way to correctly test it, especially across engines.
     84shouldBeEqualToString("length.valueAsString.slice(-1)", "%");
    8485shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(5)", referenceValue);
    8586shouldBeEqualToString("length.value.toFixed(1)", "2.0");
     
    9495shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EMS)");
    9596referenceValue = Number(2 / fontSize).toFixed(6);
    96 shouldBeEqualToString("length.valueAsString", referenceValue + "em");
     97// Exact result of valueAsString depends on precision, no trivial way to correctly test it, especially across engines.
     98shouldBeEqualToString("length.valueAsString.slice(-2)", "em");
    9799shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(6)", referenceValue);
    98100shouldBeEqualToString("length.value.toFixed(1)", "2.0");
     
    107109shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_EXS)");
    108110referenceValue = Number(2 / calculateXHeight()).toFixed(1);
    109 // Don't check valueAsString here, it's unreliable across browsers.
     111// Exact result of valueAsString depends on precision, no trivial way to correctly test it, especially across engines.
     112shouldBeEqualToString("length.valueAsString.slice(-2)", "ex");
    110113shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(1)", referenceValue);
    111114shouldBeEqualToString("length.value.toFixed(1)", "2.0");
     
    120123shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_CM)");
    121124referenceValue = Number(2 * 2.54 / cssPixelsPerInch).toFixed(7);
    122 shouldBeEqualToString("length.valueAsString", referenceValue + "cm");
     125// Exact result of valueAsString depends on precision, no trivial way to correctly test it, especially across engines.
     126shouldBeEqualToString("length.valueAsString.slice(-2)", "cm");
    123127shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(7)", referenceValue);
    124128shouldBeEqualToString("length.value.toFixed(1)", "2.0");
     
    133137shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_MM)");
    134138referenceValue = Number(2 * 25.4 / cssPixelsPerInch).toFixed(6);
    135 shouldBeEqualToString("length.valueAsString", referenceValue + "mm");
     139// Exact result of valueAsString depends on precision, no trivial way to correctly test it, especially across engines.
     140shouldBeEqualToString("length.valueAsString.slice(-2)", "mm");
    136141shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(6)", referenceValue);
    137142shouldBeEqualToString("length.value.toFixed(1)", "2.0");
     
    146151shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_IN)");
    147152referenceValue = Number(2 / cssPixelsPerInch).toFixed(7);
    148 shouldBeEqualToString("length.valueAsString", referenceValue + "in");
     153// Exact result of valueAsString depends on precision, no trivial way to correctly test it, especially across engines.
     154shouldBeEqualToString("length.valueAsString.slice(-2)", "in");
    149155shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(7)", referenceValue);
    150156shouldBeEqualToString("length.value.toFixed(1)", "2.0");
     
    172178shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_PC)");
    173179referenceValue = Number(2 / cssPixelsPerInch * 6).toFixed(3);
    174 // Don't check valueAsString here, it's unreliable across browsers.
     180// Exact result of valueAsString depends on precision, no trivial way to correctly test it, especially across engines.
     181shouldBeEqualToString("length.valueAsString.slice(-2)", "pc");
    175182shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(3)", referenceValue);
    176183shouldBeEqualToString("length.value.toFixed(1)", "2.0");
  • trunk/LayoutTests/svg/dom/SVGLength-px.html

    r217390 r242360  
    9494shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_CM)");
    9595referenceValue = Number(2 * 2.54 / cssPixelsPerInch).toFixed(7);
    96 shouldBeEqualToString("length.valueAsString", referenceValue + "cm");
     96// Exact result of valueAsString depends on precision, no trivial way to correctly test it, especially across engines.
     97shouldBeEqualToString("length.valueAsString.slice(-2)", "cm");
    9798shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(7)", referenceValue);
    9899shouldBeEqualToString("length.value.toFixed(1)", "2.0");
     
    107108shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_MM)");
    108109referenceValue = Number(2 * 25.4 / cssPixelsPerInch).toFixed(6);
    109 shouldBeEqualToString("length.valueAsString", referenceValue + "mm");
     110// Exact result of valueAsString depends on precision, no trivial way to correctly test it, especially across engines.
     111shouldBeEqualToString("length.valueAsString.slice(-2)", "mm");
    110112shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(6)", referenceValue);
    111113shouldBeEqualToString("length.value.toFixed(1)", "2.0");
     
    120122shouldBeUndefined("length.convertToSpecifiedUnits(SVGLength.SVG_LENGTHTYPE_IN)");
    121123referenceValue = Number(2 / cssPixelsPerInch).toFixed(7);
    122 shouldBeEqualToString("length.valueAsString", referenceValue + "in");
     124// Exact result of valueAsString depends on precision, no trivial way to correctly test it, especially across engines.
     125shouldBeEqualToString("length.valueAsString.slice(-2)", "in");
    123126shouldBeEqualToString("length.valueInSpecifiedUnits.toFixed(7)", referenceValue);
    124127shouldBeEqualToString("length.value.toFixed(1)", "2.0");
  • trunk/Source/WTF/ChangeLog

    r242356 r242360  
     12019-03-03  Darin Adler  <darin@apple.com>
     2
     3        Prepare to improve handling of conversion of float to strings
     4        https://bugs.webkit.org/show_bug.cgi?id=195262
     5
     6        Reviewed by Daniel Bates.
     7
     8        * wtf/dtoa.cpp:
     9        (WTF::truncateTrailingZeros): Renamed from
     10        formatStringTruncatingTrailingZerosIfNeeded and removed the calls
     11        to double_conversion::StringBuilder::Finalizer, since the caller
     12        already does that.
     13        (WTF::numberToFixedPrecisionString): Added an overload for float
     14        and updated to use the new truncateTrailingZeros.
     15        (WTF::numberToFixedWidthString): Added an overload for float.
     16
     17        * wtf/text/AtomicString.cpp:
     18        (WTF::AtomicString::number): Added float overload. This is a
     19        behavior change, but in all cases for the better. The old behavior
     20        was to convert to double first and then do "shortest form"
     21        conversion, and it's always better to just do that as float.
     22        * wtf/text/AtomicString.h: Added float overload of AtomicString::number.
     23
     24        * wtf/text/StringBuilder.cpp:
     25        (WTF::StringBuilder::appendFixedPrecisionNumber): Added float
     26        overload.
     27        (WTF::StringBuilder::appendShortestFormNumber): Renamed from
     28        appendECMAScriptNumber and did the above.
     29        (WTF::StringBuilder::appendFixedWidthNumber): Ditto.
     30        * wtf/text/StringBuilder.h: Added overloads for float and
     31        appendShortestFormNumber. The appendNumber and appendECMAScriptNumber
     32        functions are now inlines in the header, since they are expressed
     33        entirely in terms of the other functions.
     34
     35        * wtf/text/WTFString.cpp:
     36        (WTF::String::numberToStringFixedPrecision): Added float overload.
     37        Removed unnecessary explicit conversion to String.
     38        (WTF::String::numberToStringShortest): Renamed from
     39        numberToStringECMAScript and did the above.
     40        (WTF::String::numberToStringFixedWidth): Ditto.
     41
     42        * wtf/text/WTFString.h: Added overloads for float and
     43        numberToStringShortest. The number and numberToStringECMAScript
     44        functions are now inlines in the header, since they are expressed
     45        entirely in terms of the other functions.
     46
    1472019-03-04  Andy Estes  <aestes@apple.com>
    248
  • trunk/Source/WTF/wtf/dtoa.cpp

    r242330 r242360  
    3939}
    4040
    41 static inline const char* formatStringTruncatingTrailingZerosIfNeeded(NumberToStringBuffer& buffer, double_conversion::StringBuilder& builder)
     41static inline void truncateTrailingZeros(NumberToStringBuffer& buffer, double_conversion::StringBuilder& builder)
    4242{
    4343    size_t length = builder.position();
     
    5050    // No decimal separator found, early exit.
    5151    if (decimalPointPosition == length)
    52         return builder.Finalize();
     52        return;
    5353
    5454    size_t pastMantissa = decimalPointPosition + 1;
     
    6666    // No trailing zeros found to strip.
    6767    if (truncatedLength == pastMantissa)
    68         return builder.Finalize();
     68        return;
    6969
    7070    // If we removed all trailing zeros, remove the decimal point as well.
     
    7474    // Truncate the mantissa, and return the final result.
    7575    builder.RemoveCharacters(truncatedLength, pastMantissa);
    76     return builder.Finalize();
    7776}
    7877
    79 const char* numberToFixedPrecisionString(double d, unsigned significantFigures, NumberToStringBuffer& buffer, bool truncateTrailingZeros)
     78const char* numberToFixedPrecisionString(float number, unsigned significantFigures, NumberToStringBuffer& buffer, bool shouldTruncateTrailingZeros)
    8079{
    81     // Mimic sprintf("%.[precision]g", ...), but use dtoas rounding facilities.
     80    // For now, just call the double precision version.
     81    // Do that here instead of at callers to pave the way to add a more efficient code path later.
     82    return numberToFixedPrecisionString(static_cast<double>(number), significantFigures, buffer, shouldTruncateTrailingZeros);
     83}
     84
     85const char* numberToFixedPrecisionString(double d, unsigned significantFigures, NumberToStringBuffer& buffer, bool shouldTruncateTrailingZeros)
     86{
     87    // Mimic sprintf("%.[precision]g", ...).
    8288    // "g": Signed value printed in f or e format, whichever is more compact for the given value and precision.
    8389    // The e format is used only when the exponent of the value is less than –4 or greater than or equal to the
     
    8793    auto& converter = double_conversion::DoubleToStringConverter::EcmaScriptConverter();
    8894    converter.ToPrecision(d, significantFigures, &builder);
    89     if (!truncateTrailingZeros)
    90         return builder.Finalize();
    91     return formatStringTruncatingTrailingZerosIfNeeded(buffer, builder);
     95    if (shouldTruncateTrailingZeros)
     96        truncateTrailingZeros(buffer, builder);
     97    return builder.Finalize();
     98}
     99
     100const char* numberToFixedWidthString(float number, unsigned decimalPlaces, NumberToStringBuffer& buffer)
     101{
     102    // For now, just call the double precision version.
     103    // Do that here instead of at callers to pave the way to add a more efficient code path later.
     104    return numberToFixedWidthString(static_cast<double>(number), decimalPlaces, buffer);
    92105}
    93106
    94107const char* numberToFixedWidthString(double d, unsigned decimalPlaces, NumberToStringBuffer& buffer)
    95108{
    96     // Mimic sprintf("%.[precision]f", ...), but use dtoas rounding facilities.
     109    // Mimic sprintf("%.[precision]f", ...).
    97110    // "f": Signed value having the form [ – ]dddd.dddd, where dddd is one or more decimal digits.
    98111    // The number of digits before the decimal point depends on the magnitude of the number, and
  • trunk/Source/WTF/wtf/text/AtomicString.cpp

    r237099 r242360  
    102102}
    103103
     104AtomicString AtomicString::number(float number)
     105{
     106    NumberToStringBuffer buffer;
     107    return numberToString(number, buffer);
     108}
     109
    104110AtomicString AtomicString::number(double number)
    105111{
  • trunk/Source/WTF/wtf/text/AtomicString.h

    r237099 r242360  
    108108    WTF_EXPORT_PRIVATE static AtomicString number(unsigned long);
    109109    WTF_EXPORT_PRIVATE static AtomicString number(unsigned long long);
     110    WTF_EXPORT_PRIVATE static AtomicString number(float);
    110111    WTF_EXPORT_PRIVATE static AtomicString number(double);
    111112    // If we need more overloads of the number function, we can add all the others that String has, but these seem to do for now.
  • trunk/Source/WTF/wtf/text/StringBuilder.cpp

    r238143 r242360  
    379379}
    380380
    381 void StringBuilder::appendNumber(unsigned int number)
     381void StringBuilder::appendNumber(unsigned number)
    382382{
    383383    numberToStringUnsigned<StringBuilder>(number, this);
     
    404404}
    405405
    406 void StringBuilder::appendNumber(double number, unsigned precision, TrailingZerosTruncatingPolicy trailingZerosTruncatingPolicy)
    407 {
    408     NumberToStringBuffer buffer;
    409     append(numberToFixedPrecisionString(number, precision, buffer, trailingZerosTruncatingPolicy == TruncateTrailingZeros));
    410 }
    411 
    412 void StringBuilder::appendECMAScriptNumber(double number)
     406void StringBuilder::appendFixedPrecisionNumber(float number, unsigned precision, TrailingZerosTruncatingPolicy policy)
     407{
     408    NumberToStringBuffer buffer;
     409    append(numberToFixedPrecisionString(number, precision, buffer, policy == TruncateTrailingZeros));
     410}
     411
     412void StringBuilder::appendFixedPrecisionNumber(double number, unsigned precision, TrailingZerosTruncatingPolicy policy)
     413{
     414    NumberToStringBuffer buffer;
     415    append(numberToFixedPrecisionString(number, precision, buffer, policy == TruncateTrailingZeros));
     416}
     417
     418void StringBuilder::appendShortestFormNumber(float number)
    413419{
    414420    NumberToStringBuffer buffer;
    415421    append(numberToString(number, buffer));
     422}
     423
     424void StringBuilder::appendShortestFormNumber(double number)
     425{
     426    NumberToStringBuffer buffer;
     427    append(numberToString(number, buffer));
     428}
     429
     430void StringBuilder::appendFixedWidthNumber(float number, unsigned decimalPlaces)
     431{
     432    NumberToStringBuffer buffer;
     433    append(numberToFixedWidthString(number, decimalPlaces, buffer));
    416434}
    417435
  • trunk/Source/WTF/wtf/text/StringBuilder.h

    r238143 r242360  
    217217
    218218    WTF_EXPORT_PRIVATE void appendNumber(int);
    219     WTF_EXPORT_PRIVATE void appendNumber(unsigned int);
     219    WTF_EXPORT_PRIVATE void appendNumber(unsigned);
    220220    WTF_EXPORT_PRIVATE void appendNumber(long);
    221221    WTF_EXPORT_PRIVATE void appendNumber(unsigned long);
    222222    WTF_EXPORT_PRIVATE void appendNumber(long long);
    223223    WTF_EXPORT_PRIVATE void appendNumber(unsigned long long);
    224     WTF_EXPORT_PRIVATE void appendNumber(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
    225     WTF_EXPORT_PRIVATE void appendECMAScriptNumber(double);
     224    // FIXME: Change appendNumber to be appendShortestFormNumber instead of appendFixedPrecisionNumber.
     225    void appendNumber(float);
     226    void appendNumber(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
     227
     228    WTF_EXPORT_PRIVATE void appendShortestFormNumber(float);
     229    WTF_EXPORT_PRIVATE void appendShortestFormNumber(double);
     230    WTF_EXPORT_PRIVATE void appendFixedPrecisionNumber(float, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
     231    WTF_EXPORT_PRIVATE void appendFixedPrecisionNumber(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
     232    WTF_EXPORT_PRIVATE void appendFixedWidthNumber(float, unsigned decimalPlaces);
    226233    WTF_EXPORT_PRIVATE void appendFixedWidthNumber(double, unsigned decimalPlaces);
     234
     235    // FIXME: Delete in favor of the name appendShortestFormNumber or just appendNumber.
     236    void appendECMAScriptNumber(float);
     237    void appendECMAScriptNumber(double);
    227238
    228239    String toString()
     
    383394}
    384395
     396inline void StringBuilder::appendNumber(float number)
     397{
     398    appendFixedPrecisionNumber(number);
     399}
     400
     401inline void StringBuilder::appendNumber(double number, unsigned precision, TrailingZerosTruncatingPolicy policy)
     402{
     403    appendFixedPrecisionNumber(number, precision, policy);
     404}
     405
     406inline void StringBuilder::appendECMAScriptNumber(float number)
     407{
     408    // FIXME: This preserves existing behavior but is not what we want.
     409    // In the future, this should either be a compilation error or call appendShortestFormNumber without converting to double.
     410    appendShortestFormNumber(static_cast<double>(number));
     411}
     412
     413inline void StringBuilder::appendECMAScriptNumber(double number)
     414{
     415    appendShortestFormNumber(number);
     416}
     417
    385418template <typename CharType>
    386419bool equal(const StringBuilder& s, const CharType* buffer, unsigned length)
  • trunk/Source/WTF/wtf/text/WTFString.cpp

    r242308 r242360  
    455455}
    456456
    457 String String::number(unsigned int number)
     457String String::number(unsigned number)
    458458{
    459459    return numberToStringUnsigned<String>(number);
     
    480480}
    481481
    482 String String::number(double number, unsigned precision, TrailingZerosTruncatingPolicy trailingZerosTruncatingPolicy)
     482String String::numberToStringFixedPrecision(float number, unsigned precision, TrailingZerosTruncatingPolicy trailingZerosTruncatingPolicy)
    483483{
    484484    NumberToStringBuffer buffer;
    485     return String(numberToFixedPrecisionString(number, precision, buffer, trailingZerosTruncatingPolicy == TruncateTrailingZeros));
    486 }
    487 
    488 String String::numberToStringECMAScript(double number)
     485    return numberToFixedPrecisionString(number, precision, buffer, trailingZerosTruncatingPolicy == TruncateTrailingZeros);
     486}
     487
     488String String::numberToStringFixedPrecision(double number, unsigned precision, TrailingZerosTruncatingPolicy trailingZerosTruncatingPolicy)
    489489{
    490490    NumberToStringBuffer buffer;
    491     return String(numberToString(number, buffer));
     491    return numberToFixedPrecisionString(number, precision, buffer, trailingZerosTruncatingPolicy == TruncateTrailingZeros);
     492}
     493
     494String String::numberToStringShortest(float number)
     495{
     496    NumberToStringBuffer buffer;
     497    return numberToString(number, buffer);
     498}
     499
     500String String::numberToStringShortest(double number)
     501{
     502    NumberToStringBuffer buffer;
     503    return numberToString(number, buffer);
    492504}
    493505
     
    495507{
    496508    NumberToStringBuffer buffer;
    497     return String(numberToFixedWidthString(number, decimalPlaces, buffer));
     509    return numberToFixedWidthString(number, decimalPlaces, buffer);
    498510}
    499511
  • trunk/Source/WTF/wtf/text/WTFString.h

    r242308 r242360  
    176176    WTF_EXPORT_PRIVATE static String number(long long);
    177177    WTF_EXPORT_PRIVATE static String number(unsigned long long);
    178 
    179     WTF_EXPORT_PRIVATE static String number(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
    180 
    181     // Number to String conversion following the ECMAScript definition.
    182     WTF_EXPORT_PRIVATE static String numberToStringECMAScript(double);
     178    // FIXME: Change number to be numberToStringShortest instead of numberToStringFixedPrecision.
     179    static String number(float);
     180    static String number(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
     181
     182    WTF_EXPORT_PRIVATE static String numberToStringShortest(float);
     183    WTF_EXPORT_PRIVATE static String numberToStringShortest(double);
     184    WTF_EXPORT_PRIVATE static String numberToStringFixedPrecision(float, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
     185    WTF_EXPORT_PRIVATE static String numberToStringFixedPrecision(double, unsigned precision = 6, TrailingZerosTruncatingPolicy = TruncateTrailingZeros);
     186    WTF_EXPORT_PRIVATE static String numberToStringFixedWidth(float, unsigned decimalPlaces);
    183187    WTF_EXPORT_PRIVATE static String numberToStringFixedWidth(double, unsigned decimalPlaces);
     188
     189    // FIXME: Delete in favor of the name numberToStringShortest or just number.
     190    static String numberToStringECMAScript(float);
     191    static String numberToStringECMAScript(double);
    184192
    185193    // Find a single character or string, also with match function & latin1 forms.
     
    629637{
    630638    return startsWithLettersIgnoringASCIICase(string.impl(), lowercaseLetters);
     639}
     640
     641inline String String::number(float number)
     642{
     643    return numberToStringFixedPrecision(number);
     644}
     645
     646inline String String::number(double number, unsigned precision, TrailingZerosTruncatingPolicy policy)
     647{
     648    return numberToStringFixedPrecision(number, precision, policy);
     649}
     650
     651inline String String::numberToStringECMAScript(float number)
     652{
     653    // FIXME: This preserves existing behavior but is not what we want.
     654    // In the future, this should either be a compilation error or call numberToStringShortest without converting to double.
     655    return numberToStringShortest(static_cast<double>(number));
     656}
     657
     658inline String String::numberToStringECMAScript(double number)
     659{
     660    return numberToStringShortest(number);
    631661}
    632662
Note: See TracChangeset for help on using the changeset viewer.