summaryrefslogtreecommitdiffstats
path: root/WebCore/platform/text/TextStream.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'WebCore/platform/text/TextStream.cpp')
-rw-r--r--WebCore/platform/text/TextStream.cpp119
1 files changed, 95 insertions, 24 deletions
diff --git a/WebCore/platform/text/TextStream.cpp b/WebCore/platform/text/TextStream.cpp
index 5aafbc0..b23e769 100644
--- a/WebCore/platform/text/TextStream.cpp
+++ b/WebCore/platform/text/TextStream.cpp
@@ -26,74 +26,145 @@
#include "config.h"
#include "TextStream.h"
+#include "DeprecatedString.h"
+#include "Logging.h"
#include "PlatformString.h"
-#include <wtf/StringExtras.h>
+#include <wtf/Vector.h>
namespace WebCore {
-static const size_t printBufferSize = 100; // large enough for any integer or floating point value in string format, including trailing null character
+const size_t integerOrPointerAsStringBufferSize = 100; // large enough for any integer or pointer in string format, including trailing null character
+const char* const precisionFormats[7] = { "%.0f", "%.1f", "%.2f", "%.3f", "%.4f", "%.5f", "%.6f"};
+const int maxPrecision = 6; // must match size of precisionFormats
+const int defaultPrecision = 6; // matches qt and sprintf(.., "%f", ...) behaviour
+
+TextStream::TextStream(DeprecatedString* s)
+ : m_hasByteArray(false), m_string(s), m_precision(defaultPrecision)
+{
+}
+
+TextStream& TextStream::operator<<(char c)
+{
+ if (m_hasByteArray)
+ m_byteArray.append(c);
+
+ if (m_string)
+ m_string->append(DeprecatedChar(c));
+ return *this;
+}
+
+TextStream& TextStream::operator<<(short i)
+{
+ char buffer[integerOrPointerAsStringBufferSize];
+ sprintf(buffer, "%d", i);
+ return *this << buffer;
+}
+
+TextStream& TextStream::operator<<(unsigned short i)
+{
+ char buffer[integerOrPointerAsStringBufferSize];
+ sprintf(buffer, "%u", i);
+ return *this << buffer;
+}
TextStream& TextStream::operator<<(int i)
{
- char buffer[printBufferSize];
- snprintf(buffer, sizeof(buffer) - 1, "%d", i);
+ char buffer[integerOrPointerAsStringBufferSize];
+ sprintf(buffer, "%d", i);
return *this << buffer;
}
TextStream& TextStream::operator<<(unsigned i)
{
- char buffer[printBufferSize];
- snprintf(buffer, sizeof(buffer) - 1, "%u", i);
+ char buffer[integerOrPointerAsStringBufferSize];
+ sprintf(buffer, "%u", i);
return *this << buffer;
}
TextStream& TextStream::operator<<(long i)
{
- char buffer[printBufferSize];
- snprintf(buffer, sizeof(buffer) - 1, "%ld", i);
+ char buffer[integerOrPointerAsStringBufferSize];
+ sprintf(buffer, "%ld", i);
return *this << buffer;
}
TextStream& TextStream::operator<<(unsigned long i)
{
- char buffer[printBufferSize];
- snprintf(buffer, sizeof(buffer) - 1, "%lu", i);
+ char buffer[integerOrPointerAsStringBufferSize];
+ sprintf(buffer, "%lu", i);
return *this << buffer;
}
TextStream& TextStream::operator<<(float f)
{
- char buffer[printBufferSize];
- snprintf(buffer, sizeof(buffer) - 1, "%.2f", f);
+ char buffer[integerOrPointerAsStringBufferSize];
+ sprintf(buffer, precisionFormats[m_precision], f);
return *this << buffer;
}
TextStream& TextStream::operator<<(double d)
{
- char buffer[printBufferSize];
- snprintf(buffer, sizeof(buffer) - 1, "%.2f", d);
+ char buffer[integerOrPointerAsStringBufferSize];
+ sprintf(buffer, precisionFormats[m_precision], d);
return *this << buffer;
}
-TextStream& TextStream::operator<<(const char* string)
+TextStream& TextStream::operator<<(const char* s)
{
- size_t stringLength = strlen(string);
- size_t textLength = m_text.size();
- m_text.grow(textLength + stringLength);
- for (size_t i = 0; i < stringLength; ++i)
- m_text[textLength + i] = string[i];
+ if (m_hasByteArray) {
+ unsigned length = strlen(s);
+ unsigned oldSize = m_byteArray.size();
+ m_byteArray.grow(oldSize + length);
+ memcpy(m_byteArray.data() + oldSize, s, length);
+ }
+ if (m_string)
+ m_string->append(s);
return *this;
}
-TextStream& TextStream::operator<<(const String& string)
+TextStream& TextStream::operator<<(const DeprecatedString& s)
{
- append(m_text, string);
+ if (m_hasByteArray) {
+ unsigned length = s.length();
+ unsigned oldSize = m_byteArray.size();
+ m_byteArray.grow(oldSize + length);
+ memcpy(m_byteArray.data() + oldSize, s.latin1(), length);
+ }
+ if (m_string)
+ m_string->append(s);
return *this;
}
-String TextStream::release()
+TextStream& TextStream::operator<<(const String& s)
+{
+ return (*this) << s.deprecatedString();
+}
+
+TextStream& TextStream::operator<<(void* p)
+{
+ char buffer[integerOrPointerAsStringBufferSize];
+ sprintf(buffer, "%p", p);
+ return *this << buffer;
+}
+
+TextStream& TextStream::operator<<(const TextStreamManipulator& m)
+{
+ return m(*this);
+}
+
+int TextStream::precision(int p)
+{
+ int oldPrecision = m_precision;
+
+ if (p >= 0 && p <= maxPrecision)
+ m_precision = p;
+
+ return oldPrecision;
+}
+
+TextStream &endl(TextStream& stream)
{
- return String::adopt(m_text);
+ return stream << '\n';
}
}