Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Reuse floating point formatting of TextStream in [SVG]RenderTreeAsTex…
…t.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<<):

Canonical link: https://commits.webkit.org/114650@main
git-svn-id: https://svn.webkit.org/repository/webkit/trunk@128564 268f45cc-cd09-0410-ab3c-d52691b4dbfc
  • Loading branch information
paroga committed Sep 14, 2012
1 parent d088302 commit 37a516e
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 26 deletions.
22 changes: 22 additions & 0 deletions Source/WebCore/ChangeLog
@@ -1,3 +1,25 @@
2012-09-13 Patrick Gansterer <paroga@webkit.org>

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<<):

2012-09-13 Sheriff Bot <webkit.review.bot@gmail.com>

Unreviewed, rolling out r128552.
Expand Down
18 changes: 18 additions & 0 deletions Source/WebCore/platform/text/TextStream.cpp
Expand Up @@ -26,6 +26,7 @@
#include "config.h"
#include "TextStream.h"

#include <wtf/MathExtras.h>
#include <wtf/StringExtras.h>
#include <wtf/text/WTFString.h>

Expand All @@ -35,6 +36,14 @@ namespace WebCore {

static const size_t printBufferSize = 100; // large enough for any integer or floating point value in string format, including trailing null character

static inline bool hasFractions(double val)
{
static const double s_epsilon = 0.0001;
int ival = static_cast<int>(val);
double dval = static_cast<double>(ival);
return fabs(val - dval) > s_epsilon;
}

TextStream& TextStream::operator<<(bool b)
{
return *this << (b ? "1" : "0");
Expand Down Expand Up @@ -107,6 +116,15 @@ TextStream& TextStream::operator<<(const String& string)
return *this;
}

TextStream& TextStream::operator<<(const FormatNumberRespectingIntegers& numberToFormat)
{
if (hasFractions(numberToFormat.value))
return *this << numberToFormat.value;

m_text.appendNumber(static_cast<int>(numberToFormat.value));
return *this;
}

String TextStream::release()
{
String result = m_text.toString();
Expand Down
6 changes: 6 additions & 0 deletions Source/WebCore/platform/text/TextStream.h
Expand Up @@ -34,6 +34,11 @@ namespace WebCore {

class TextStream {
public:
struct FormatNumberRespectingIntegers {
FormatNumberRespectingIntegers(double number) : value(number) { }
double value;
};

TextStream& operator<<(bool);
TextStream& operator<<(int);
TextStream& operator<<(unsigned);
Expand All @@ -46,6 +51,7 @@ class TextStream {
TextStream& operator<<(const char*);
TextStream& operator<<(const void*);
TextStream& operator<<(const String&);
TextStream& operator<<(const FormatNumberRespectingIntegers&);

String release();

Expand Down
23 changes: 4 additions & 19 deletions Source/WebCore/rendering/RenderTreeAsText.cpp
Expand Up @@ -79,21 +79,6 @@ using namespace HTMLNames;

static void writeLayers(TextStream&, const RenderLayer* rootLayer, RenderLayer*, const LayoutRect& paintDirtyRect, int indent = 0, RenderAsTextBehavior = RenderAsTextBehaviorNormal);

static inline bool hasFractions(double val)
{
static const double s_epsilon = 0.0001;
int ival = static_cast<int>(val);
double dval = static_cast<double>(ival);
return fabs(val - dval) > s_epsilon;
}

String formatNumberRespectingIntegers(double value)
{
if (!hasFractions(value))
return String::number(static_cast<int>(value));
return String::number(value, ShouldRoundDecimalPlaces, 2);
}

TextStream& operator<<(TextStream& ts, const IntRect& r)
{
return ts << "at (" << r.x() << "," << r.y() << ") size " << r.width() << "x" << r.height();
Expand All @@ -112,16 +97,16 @@ TextStream& operator<<(TextStream& ts, const FractionalLayoutPoint& p)

TextStream& operator<<(TextStream& ts, const FloatPoint& p)
{
ts << "(" << formatNumberRespectingIntegers(p.x());
ts << "," << formatNumberRespectingIntegers(p.y());
ts << "(" << TextStream::FormatNumberRespectingIntegers(p.x());
ts << "," << TextStream::FormatNumberRespectingIntegers(p.y());
ts << ")";
return ts;
}

TextStream& operator<<(TextStream& ts, const FloatSize& s)
{
ts << "width=" << formatNumberRespectingIntegers(s.width());
ts << " height=" << formatNumberRespectingIntegers(s.height());
ts << "width=" << TextStream::FormatNumberRespectingIntegers(s.width());
ts << " height=" << TextStream::FormatNumberRespectingIntegers(s.height());
return ts;
}

Expand Down
3 changes: 0 additions & 3 deletions Source/WebCore/rendering/RenderTreeAsText.h
Expand Up @@ -28,7 +28,6 @@
#include "TextStream.h"

#include <wtf/Forward.h>
#include <wtf/MathExtras.h>

namespace WebCore {

Expand Down Expand Up @@ -98,8 +97,6 @@ String counterValueForElement(Element*);

String markerTextForListItem(Element*);

String formatNumberRespectingIntegers(double);

} // namespace WebCore

#endif // RenderTreeAsText_h
8 changes: 4 additions & 4 deletions Source/WebCore/rendering/svg/SVGRenderTreeAsText.cpp
Expand Up @@ -134,10 +134,10 @@ static void writeIfNotDefault(TextStream& ts, const char* name, ValueType value,

TextStream& operator<<(TextStream& ts, const FloatRect& r)
{
ts << "at (" << formatNumberRespectingIntegers(r.x());
ts << "," << formatNumberRespectingIntegers(r.y());
ts << ") size " << formatNumberRespectingIntegers(r.width());
ts << "x" << formatNumberRespectingIntegers(r.height());
ts << "at (" << TextStream::FormatNumberRespectingIntegers(r.x());
ts << "," << TextStream::FormatNumberRespectingIntegers(r.y());
ts << ") size " << TextStream::FormatNumberRespectingIntegers(r.width());
ts << "x" << TextStream::FormatNumberRespectingIntegers(r.height());
return ts;
}

Expand Down

0 comments on commit 37a516e

Please sign in to comment.