1/*
2 * Copyright (C) 2004-2019 Apple Inc. All rights reserved.
3 *
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
6 * are met:
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.
12 *
13 * THIS SOFTWARE IS PROVIDED BY APPLE 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 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.
24 */
25
26#include "config.h"
27#include <wtf/text/TextStream.h>
28
29#include <wtf/MathExtras.h>
30#include <wtf/text/WTFString.h>
31
32namespace WTF {
33
34static constexpr size_t printBufferSize = 100; // large enough for any integer or floating point value in string format, including trailing null character
35
36static inline bool hasFractions(double val)
37{
38 static constexpr double s_epsilon = 0.0001;
39 int ival = static_cast<int>(val);
40 double dval = static_cast<double>(ival);
41 return fabs(val - dval) > s_epsilon;
42}
43
44TextStream& TextStream::operator<<(bool b)
45{
46 return *this << (b ? "1" : "0");
47}
48
49TextStream& TextStream::operator<<(char c)
50{
51 m_text.append(c);
52 return *this;
53}
54
55TextStream& TextStream::operator<<(int i)
56{
57 m_text.appendNumber(i);
58 return *this;
59}
60
61TextStream& TextStream::operator<<(unsigned i)
62{
63 m_text.appendNumber(i);
64 return *this;
65}
66
67TextStream& TextStream::operator<<(long i)
68{
69 m_text.appendNumber(i);
70 return *this;
71}
72
73TextStream& TextStream::operator<<(unsigned long i)
74{
75 m_text.appendNumber(i);
76 return *this;
77}
78
79TextStream& TextStream::operator<<(long long i)
80{
81 m_text.appendNumber(i);
82 return *this;
83}
84
85TextStream& TextStream::operator<<(unsigned long long i)
86{
87 m_text.appendNumber(i);
88 return *this;
89}
90
91TextStream& TextStream::operator<<(float f)
92{
93 if (m_formattingFlags & Formatting::NumberRespectingIntegers)
94 return *this << FormatNumberRespectingIntegers(f);
95
96 m_text.appendFixedWidthNumber(f, 2);
97 return *this;
98}
99
100TextStream& TextStream::operator<<(double d)
101{
102 if (m_formattingFlags & Formatting::NumberRespectingIntegers)
103 return *this << FormatNumberRespectingIntegers(d);
104
105 m_text.appendFixedWidthNumber(d, 2);
106 return *this;
107}
108
109TextStream& TextStream::operator<<(const char* string)
110{
111 m_text.append(string);
112 return *this;
113}
114
115TextStream& TextStream::operator<<(const void* p)
116{
117 char buffer[printBufferSize];
118 snprintf(buffer, sizeof(buffer) - 1, "%p", p);
119 return *this << buffer;
120}
121
122TextStream& TextStream::operator<<(const String& string)
123{
124 m_text.append(string);
125 return *this;
126}
127
128TextStream& TextStream::operator<<(const FormatNumberRespectingIntegers& numberToFormat)
129{
130 if (hasFractions(numberToFormat.value)) {
131 m_text.appendFixedWidthNumber(numberToFormat.value, 2);
132 return *this;
133 }
134
135 m_text.appendNumber(static_cast<int>(numberToFormat.value));
136 return *this;
137}
138
139String TextStream::release()
140{
141 String result = m_text.toString();
142 m_text.clear();
143 return result;
144}
145
146void TextStream::startGroup()
147{
148 TextStream& ts = *this;
149
150 if (m_multiLineMode) {
151 ts << "\n";
152 ts.writeIndent();
153 ts << "(";
154 ts.increaseIndent();
155 } else
156 ts << " (";
157}
158
159void TextStream::endGroup()
160{
161 TextStream& ts = *this;
162 ts << ")";
163 if (m_multiLineMode)
164 ts.decreaseIndent();
165}
166
167void TextStream::nextLine()
168{
169 TextStream& ts = *this;
170 if (m_multiLineMode) {
171 ts << "\n";
172 ts.writeIndent();
173 } else
174 ts << " ";
175}
176
177void TextStream::writeIndent()
178{
179 if (m_multiLineMode)
180 WTF::writeIndent(*this, m_indent);
181}
182
183void writeIndent(TextStream& ts, int indent)
184{
185 for (int i = 0; i < indent; ++i)
186 ts << " ";
187}
188
189}
190