Changeset 128564 in webkit


Ignore:
Timestamp:
Sep 14, 2012 12:09:03 AM (12 years ago)
Author:
Patrick Gansterer
Message:

Reuse floating point formatting of TextStream in [SVG]RenderTreeAsText.cpp
https://bugs.webkit.org/show_bug.cgi?id=96264

Reviewed by Benjamin Poulain.

RenderTreeAsText uses the same format for streaming out floats as TextStream
does. Replace formatNumberRespectingIntegers() with a new overload to the
operator<< to reuse the special cases for numbers which take advantage of
StringBuilder::appendNumber() and avoid a temporary StringImpl.

  • platform/text/TextStream.cpp:

(WebCore::hasFractions):
(WebCore::TextStream::operator<<):

  • platform/text/TextStream.h:
  • rendering/RenderTreeAsText.cpp:

(WebCore::operator<<):

  • rendering/RenderTreeAsText.h:
  • rendering/svg/SVGRenderTreeAsText.cpp:

(WebCore::operator<<):

Location:
trunk/Source/WebCore
Files:
6 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebCore/ChangeLog

    r128559 r128564  
     12012-09-13  Patrick Gansterer  <paroga@webkit.org>
     2
     3        Reuse floating point formatting of TextStream in [SVG]RenderTreeAsText.cpp
     4        https://bugs.webkit.org/show_bug.cgi?id=96264
     5
     6        Reviewed by Benjamin Poulain.
     7
     8        RenderTreeAsText uses the same format for streaming out floats as TextStream
     9        does. Replace formatNumberRespectingIntegers() with a new overload to the
     10        operator<< to reuse the special cases for numbers which take advantage of
     11        StringBuilder::appendNumber() and avoid a temporary StringImpl.
     12
     13        * platform/text/TextStream.cpp:
     14        (WebCore::hasFractions):
     15        (WebCore::TextStream::operator<<):
     16        * platform/text/TextStream.h:
     17        * rendering/RenderTreeAsText.cpp:
     18        (WebCore::operator<<):
     19        * rendering/RenderTreeAsText.h:
     20        * rendering/svg/SVGRenderTreeAsText.cpp:
     21        (WebCore::operator<<):
     22
    1232012-09-13  Sheriff Bot  <webkit.review.bot@gmail.com>
    224
  • trunk/Source/WebCore/platform/text/TextStream.cpp

    r128014 r128564  
    2727#include "TextStream.h"
    2828
     29#include <wtf/MathExtras.h>
    2930#include <wtf/StringExtras.h>
    3031#include <wtf/text/WTFString.h>
     
    3536
    3637static const size_t printBufferSize = 100; // large enough for any integer or floating point value in string format, including trailing null character
     38
     39static inline bool hasFractions(double val)
     40{
     41    static const double s_epsilon = 0.0001;
     42    int ival = static_cast<int>(val);
     43    double dval = static_cast<double>(ival);
     44    return fabs(val - dval) > s_epsilon;
     45}
    3746
    3847TextStream& TextStream::operator<<(bool b)
     
    108117}
    109118
     119TextStream& TextStream::operator<<(const FormatNumberRespectingIntegers& numberToFormat)
     120{
     121    if (hasFractions(numberToFormat.value))
     122        return *this << numberToFormat.value;
     123
     124    m_text.appendNumber(static_cast<int>(numberToFormat.value));
     125    return *this;
     126}
     127
    110128String TextStream::release()
    111129{
  • trunk/Source/WebCore/platform/text/TextStream.h

    r128014 r128564  
    3535class TextStream {
    3636public:
     37    struct FormatNumberRespectingIntegers {
     38        FormatNumberRespectingIntegers(double number) : value(number) { }
     39        double value;
     40    };
     41
    3742    TextStream& operator<<(bool);
    3843    TextStream& operator<<(int);
     
    4752    TextStream& operator<<(const void*);
    4853    TextStream& operator<<(const String&);
     54    TextStream& operator<<(const FormatNumberRespectingIntegers&);
    4955
    5056    String release();
  • trunk/Source/WebCore/rendering/RenderTreeAsText.cpp

    r128014 r128564  
    8080static void writeLayers(TextStream&, const RenderLayer* rootLayer, RenderLayer*, const LayoutRect& paintDirtyRect, int indent = 0, RenderAsTextBehavior = RenderAsTextBehaviorNormal);
    8181
    82 static inline bool hasFractions(double val)
    83 {
    84     static const double s_epsilon = 0.0001;
    85     int ival = static_cast<int>(val);
    86     double dval = static_cast<double>(ival);
    87     return fabs(val - dval) > s_epsilon;
    88 }
    89 
    90 String formatNumberRespectingIntegers(double value)
    91 {
    92     if (!hasFractions(value))
    93         return String::number(static_cast<int>(value));
    94     return String::number(value, ShouldRoundDecimalPlaces, 2);
    95 }
    96 
    9782TextStream& operator<<(TextStream& ts, const IntRect& r)
    9883{
     
    11398TextStream& operator<<(TextStream& ts, const FloatPoint& p)
    11499{
    115     ts << "(" << formatNumberRespectingIntegers(p.x());
    116     ts << "," << formatNumberRespectingIntegers(p.y());
     100    ts << "(" << TextStream::FormatNumberRespectingIntegers(p.x());
     101    ts << "," << TextStream::FormatNumberRespectingIntegers(p.y());
    117102    ts << ")";
    118103    return ts;
     
    121106TextStream& operator<<(TextStream& ts, const FloatSize& s)
    122107{
    123     ts << "width=" << formatNumberRespectingIntegers(s.width());
    124     ts << " height=" << formatNumberRespectingIntegers(s.height());
     108    ts << "width=" << TextStream::FormatNumberRespectingIntegers(s.width());
     109    ts << " height=" << TextStream::FormatNumberRespectingIntegers(s.height());
    125110    return ts;
    126111}
  • trunk/Source/WebCore/rendering/RenderTreeAsText.h

    r116009 r128564  
    2929
    3030#include <wtf/Forward.h>
    31 #include <wtf/MathExtras.h>
    3231
    3332namespace WebCore {
     
    9998String markerTextForListItem(Element*);
    10099
    101 String formatNumberRespectingIntegers(double);
    102 
    103100} // namespace WebCore
    104101
  • trunk/Source/WebCore/rendering/svg/SVGRenderTreeAsText.cpp

    r123025 r128564  
    135135TextStream& operator<<(TextStream& ts, const FloatRect& r)
    136136{
    137     ts << "at (" << formatNumberRespectingIntegers(r.x());
    138     ts << "," << formatNumberRespectingIntegers(r.y());
    139     ts << ") size " << formatNumberRespectingIntegers(r.width());
    140     ts << "x" << formatNumberRespectingIntegers(r.height());
     137    ts << "at (" << TextStream::FormatNumberRespectingIntegers(r.x());
     138    ts << "," << TextStream::FormatNumberRespectingIntegers(r.y());
     139    ts << ") size " << TextStream::FormatNumberRespectingIntegers(r.width());
     140    ts << "x" << TextStream::FormatNumberRespectingIntegers(r.height());
    141141    return ts;
    142142}
Note: See TracChangeset for help on using the changeset viewer.