2 * Copyright (C) 2004 Apple Computer, Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include "TextStream.h"
29 #include "DeprecatedString.h"
31 #include "PlatformString.h"
32 #include <wtf/Vector.h>
36 const size_t integerOrPointerAsStringBufferSize = 100; // large enough for any integer or pointer in string format, including trailing null character
37 const char* const precisionFormats[6] = { "%.0f", "%.1f", "%.2f", "%.3f", "%.4f", "%.5f" "%.6f"};
38 const int maxPrecision = 6; // must match size of precisionFormats
39 const int defaultPrecision = 6; // matches qt and sprintf(.., "%f", ...) behaviour
41 TextStream::TextStream(DeprecatedString* s)
42 : m_hasByteArray(false), m_string(s), m_precision(defaultPrecision)
46 TextStream& TextStream::operator<<(char c)
49 m_byteArray.append(c);
52 m_string->append(DeprecatedChar(c));
56 TextStream& TextStream::operator<<(short i)
58 char buffer[integerOrPointerAsStringBufferSize];
59 sprintf(buffer, "%d", i);
60 return *this << buffer;
63 TextStream& TextStream::operator<<(unsigned short i)
65 char buffer[integerOrPointerAsStringBufferSize];
66 sprintf(buffer, "%u", i);
67 return *this << buffer;
70 TextStream& TextStream::operator<<(int i)
72 char buffer[integerOrPointerAsStringBufferSize];
73 sprintf(buffer, "%d", i);
74 return *this << buffer;
77 TextStream& TextStream::operator<<(unsigned i)
79 char buffer[integerOrPointerAsStringBufferSize];
80 sprintf(buffer, "%u", i);
81 return *this << buffer;
84 TextStream& TextStream::operator<<(long i)
86 char buffer[integerOrPointerAsStringBufferSize];
87 sprintf(buffer, "%ld", i);
88 return *this << buffer;
91 TextStream& TextStream::operator<<(unsigned long i)
93 char buffer[integerOrPointerAsStringBufferSize];
94 sprintf(buffer, "%lu", i);
95 return *this << buffer;
98 TextStream& TextStream::operator<<(float f)
100 char buffer[integerOrPointerAsStringBufferSize];
101 sprintf(buffer, precisionFormats[m_precision], f);
102 return *this << buffer;
105 TextStream& TextStream::operator<<(double d)
107 char buffer[integerOrPointerAsStringBufferSize];
108 sprintf(buffer, precisionFormats[m_precision], d);
109 return *this << buffer;
112 TextStream& TextStream::operator<<(const char* s)
114 if (m_hasByteArray) {
115 unsigned length = strlen(s);
116 unsigned oldSize = m_byteArray.size();
117 m_byteArray.resize(oldSize + length);
118 memcpy(m_byteArray.data() + oldSize, s, length);
125 TextStream& TextStream::operator<<(const DeprecatedString& s)
127 if (m_hasByteArray) {
128 unsigned length = s.length();
129 unsigned oldSize = m_byteArray.size();
130 m_byteArray.resize(oldSize + length);
131 memcpy(m_byteArray.data() + oldSize, s.latin1(), length);
138 TextStream& TextStream::operator<<(const String& s)
140 return (*this) << s.deprecatedString();
143 TextStream& TextStream::operator<<(void* p)
145 char buffer[integerOrPointerAsStringBufferSize];
146 sprintf(buffer, "%p", p);
147 return *this << buffer;
150 TextStream& TextStream::operator<<(const TextStreamManipulator& m)
155 int TextStream::precision(int p)
157 int oldPrecision = m_precision;
159 if (p >= 0 && p <= maxPrecision)
165 TextStream &endl(TextStream& stream)
167 return stream << '\n';