1 /*
2 * Copyright (C) 2004, 2008 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 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.
24 */
25
26 #include "config.h"
27 #include "TextStream.h"
28
29 #include "PlatformString.h"
30 #include <wtf/StringExtras.h>
31
32 namespace WebCore {
33
34 static const size_t printBufferSize = 100; // large enough for any integer or floating point value in string format, including trailing null character
35
operator <<(bool b)36 TextStream& TextStream::operator<<(bool b)
37 {
38 return *this << (b ? "1" : "0");
39 }
40
operator <<(int i)41 TextStream& TextStream::operator<<(int i)
42 {
43 char buffer[printBufferSize];
44 snprintf(buffer, sizeof(buffer) - 1, "%d", i);
45 return *this << buffer;
46 }
47
operator <<(unsigned i)48 TextStream& TextStream::operator<<(unsigned i)
49 {
50 char buffer[printBufferSize];
51 snprintf(buffer, sizeof(buffer) - 1, "%u", i);
52 return *this << buffer;
53 }
54
operator <<(long i)55 TextStream& TextStream::operator<<(long i)
56 {
57 char buffer[printBufferSize];
58 snprintf(buffer, sizeof(buffer) - 1, "%ld", i);
59 return *this << buffer;
60 }
61
operator <<(unsigned long i)62 TextStream& TextStream::operator<<(unsigned long i)
63 {
64 char buffer[printBufferSize];
65 snprintf(buffer, sizeof(buffer) - 1, "%lu", i);
66 return *this << buffer;
67 }
68
operator <<(float f)69 TextStream& TextStream::operator<<(float f)
70 {
71 char buffer[printBufferSize];
72 snprintf(buffer, sizeof(buffer) - 1, "%.2f", f);
73 return *this << buffer;
74 }
75
operator <<(double d)76 TextStream& TextStream::operator<<(double d)
77 {
78 char buffer[printBufferSize];
79 snprintf(buffer, sizeof(buffer) - 1, "%.2f", d);
80 return *this << buffer;
81 }
82
operator <<(const char * string)83 TextStream& TextStream::operator<<(const char* string)
84 {
85 size_t stringLength = strlen(string);
86 size_t textLength = m_text.size();
87 m_text.grow(textLength + stringLength);
88 for (size_t i = 0; i < stringLength; ++i)
89 m_text[textLength + i] = string[i];
90 return *this;
91 }
92
operator <<(const String & string)93 TextStream& TextStream::operator<<(const String& string)
94 {
95 append(m_text, string);
96 return *this;
97 }
98
release()99 String TextStream::release()
100 {
101 return String::adopt(m_text);
102 }
103
104 #if PLATFORM(WIN_OS) && PLATFORM(X86_64) && COMPILER(MSVC)
operator <<(__int64 i)105 TextStream& TextStream::operator<<(__int64 i)
106 {
107 char buffer[printBufferSize];
108 snprintf(buffer, sizeof(buffer) - 1, "%I64i", i);
109 return *this << buffer;
110 }
operator <<(unsigned __int64 i)111 TextStream& TextStream::operator<<(unsigned __int64 i)
112 {
113 char buffer[printBufferSize];
114 snprintf(buffer, sizeof(buffer) - 1, "%I64u", i);
115 return *this << buffer;
116 }
117 #endif
118
119 }
120