Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / text / StringConcatenate.h
blob01e14636154941dc6cdad7f64c3f4cf02f651573
1 /*
2 * Copyright (C) 2010 Apple 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
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.
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.
26 #ifndef StringConcatenate_h
27 #define StringConcatenate_h
29 #include <string.h>
31 #ifndef WTFString_h
32 #include "wtf/text/AtomicString.h"
33 #endif
35 // This macro is helpful for testing how many intermediate Strings are created while evaluating an
36 // expression containing operator+.
37 #ifndef WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING
38 #define WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING() ((void)0)
39 #endif
41 namespace WTF {
43 template<typename StringType>
44 class StringTypeAdapter {
47 template<>
48 class StringTypeAdapter<char> {
49 public:
50 StringTypeAdapter<char>(char buffer)
51 : m_buffer(buffer)
55 unsigned length() { return 1; }
57 bool is8Bit() { return true; }
59 void writeTo(LChar* destination)
61 *destination = m_buffer;
64 void writeTo(UChar* destination) { *destination = m_buffer; }
66 private:
67 unsigned char m_buffer;
70 template<>
71 class StringTypeAdapter<LChar> {
72 public:
73 StringTypeAdapter<LChar>(LChar buffer)
74 : m_buffer(buffer)
78 unsigned length() { return 1; }
80 bool is8Bit() { return true; }
82 void writeTo(LChar* destination)
84 *destination = m_buffer;
87 void writeTo(UChar* destination) { *destination = m_buffer; }
89 private:
90 LChar m_buffer;
93 template<>
94 class StringTypeAdapter<UChar> {
95 public:
96 StringTypeAdapter<UChar>(UChar buffer)
97 : m_buffer(buffer)
101 unsigned length() { return 1; }
103 bool is8Bit() { return m_buffer <= 0xff; }
105 void writeTo(LChar* destination)
107 ASSERT(is8Bit());
108 *destination = static_cast<LChar>(m_buffer);
111 void writeTo(UChar* destination) { *destination = m_buffer; }
113 private:
114 UChar m_buffer;
117 template<>
118 class WTF_EXPORT StringTypeAdapter<char*> {
119 public:
120 StringTypeAdapter<char*>(char* buffer)
121 : m_buffer(buffer)
122 , m_length(strlen(buffer))
126 unsigned length() { return m_length; }
128 bool is8Bit() { return true; }
130 void writeTo(LChar* destination);
132 void writeTo(UChar* destination);
134 private:
135 const char* m_buffer;
136 unsigned m_length;
139 template<>
140 class WTF_EXPORT StringTypeAdapter<LChar*> {
141 public:
142 StringTypeAdapter<LChar*>(LChar* buffer);
144 unsigned length() { return m_length; }
146 bool is8Bit() { return true; }
148 void writeTo(LChar* destination);
150 void writeTo(UChar* destination);
152 private:
153 const LChar* m_buffer;
154 unsigned m_length;
157 template<>
158 class WTF_EXPORT StringTypeAdapter<const UChar*> {
159 public:
160 StringTypeAdapter(const UChar* buffer);
162 unsigned length() { return m_length; }
164 bool is8Bit() { return false; }
166 NO_RETURN_DUE_TO_CRASH void writeTo(LChar*)
168 RELEASE_ASSERT(false);
171 void writeTo(UChar* destination);
173 private:
174 const UChar* m_buffer;
175 unsigned m_length;
178 template<>
179 class WTF_EXPORT StringTypeAdapter<const char*> {
180 public:
181 StringTypeAdapter<const char*>(const char* buffer);
183 unsigned length() { return m_length; }
185 bool is8Bit() { return true; }
187 void writeTo(LChar* destination);
189 void writeTo(UChar* destination);
191 private:
192 const char* m_buffer;
193 unsigned m_length;
196 template<>
197 class WTF_EXPORT StringTypeAdapter<const LChar*> {
198 public:
199 StringTypeAdapter<const LChar*>(const LChar* buffer);
201 unsigned length() { return m_length; }
203 bool is8Bit() { return true; }
205 void writeTo(LChar* destination);
207 void writeTo(UChar* destination);
209 private:
210 const LChar* m_buffer;
211 unsigned m_length;
214 template<>
215 class WTF_EXPORT StringTypeAdapter<Vector<char>> {
216 public:
217 StringTypeAdapter<Vector<char>>(const Vector<char>& buffer)
218 : m_buffer(buffer)
222 size_t length() { return m_buffer.size(); }
224 bool is8Bit() { return true; }
226 void writeTo(LChar* destination);
228 void writeTo(UChar* destination);
230 private:
231 const Vector<char>& m_buffer;
234 template<>
235 class StringTypeAdapter<Vector<LChar>> {
236 public:
237 StringTypeAdapter<Vector<LChar>>(const Vector<LChar>& buffer)
238 : m_buffer(buffer)
242 size_t length() { return m_buffer.size(); }
244 bool is8Bit() { return true; }
246 void writeTo(LChar* destination);
248 void writeTo(UChar* destination);
250 private:
251 const Vector<LChar>& m_buffer;
254 template<>
255 class WTF_EXPORT StringTypeAdapter<String> {
256 public:
257 StringTypeAdapter<String>(const String& string)
258 : m_buffer(string)
262 unsigned length() { return m_buffer.length(); }
264 bool is8Bit() { return m_buffer.isNull() || m_buffer.is8Bit(); }
266 void writeTo(LChar* destination);
268 void writeTo(UChar* destination);
270 private:
271 const String& m_buffer;
274 template<>
275 class StringTypeAdapter<AtomicString> {
276 public:
277 StringTypeAdapter<AtomicString>(const AtomicString& string)
278 : m_adapter(string.string())
282 unsigned length() { return m_adapter.length(); }
284 bool is8Bit() { return m_adapter.is8Bit(); }
286 void writeTo(LChar* destination) { m_adapter.writeTo(destination); }
287 void writeTo(UChar* destination) { m_adapter.writeTo(destination); }
289 private:
290 StringTypeAdapter<String> m_adapter;
293 inline void sumWithOverflow(unsigned& total, unsigned addend, bool& overflow)
295 unsigned oldTotal = total;
296 total = oldTotal + addend;
297 if (total < oldTotal)
298 overflow = true;
301 template<typename StringType1, typename StringType2>
302 PassRefPtr<StringImpl> makeString(StringType1 string1, StringType2 string2)
304 StringTypeAdapter<StringType1> adapter1(string1);
305 StringTypeAdapter<StringType2> adapter2(string2);
307 bool overflow = false;
308 unsigned length = adapter1.length();
309 sumWithOverflow(length, adapter2.length(), overflow);
310 if (overflow)
311 return nullptr;
313 if (adapter1.is8Bit() && adapter2.is8Bit()) {
314 LChar* buffer;
315 RefPtr<StringImpl> resultImpl = StringImpl::createUninitialized(length, buffer);
316 if (!resultImpl)
317 return nullptr;
319 LChar* result = buffer;
320 adapter1.writeTo(result);
321 result += adapter1.length();
322 adapter2.writeTo(result);
324 return resultImpl.release();
327 UChar* buffer;
328 RefPtr<StringImpl> resultImpl = StringImpl::createUninitialized(length, buffer);
329 if (!resultImpl)
330 return nullptr;
332 UChar* result = buffer;
333 adapter1.writeTo(result);
334 result += adapter1.length();
335 adapter2.writeTo(result);
337 return resultImpl.release();
340 } // namespace WTF
342 #include "wtf/text/StringOperators.h"
343 #endif