1/*
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights reserved.
3 * Copyright (C) Research In Motion Limited 2011. All rights reserved.
4 *
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Library General Public
7 * License as published by the Free Software Foundation; either
8 * version 2 of the License, or (at your option) any later version.
9 *
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 * Library General Public License for more details.
14 *
15 * You should have received a copy of the GNU Library General Public License
16 * along with this library; see the file COPYING.LIB.  If not, write to
17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
18 * Boston, MA 02110-1301, USA.
19 *
20 */
21
22#ifndef StringOperators_h
23#define StringOperators_h
24
25#include "StringConcatenate.h"
26
27namespace WTF {
28
29template<typename StringType1, typename StringType2>
30class StringAppend {
31public:
32    StringAppend(StringType1 string1, StringType2 string2);
33
34    operator String() const;
35
36    operator AtomicString() const;
37
38    bool is8Bit();
39
40    void writeTo(LChar* destination);
41
42    void writeTo(UChar* destination);
43
44    unsigned length();
45
46private:
47    StringType1 m_string1;
48    StringType2 m_string2;
49};
50
51template<typename StringType1, typename StringType2>
52StringAppend<StringType1, StringType2>::StringAppend(StringType1 string1, StringType2 string2)
53    : m_string1(string1)
54    , m_string2(string2)
55{
56}
57
58template<typename StringType1, typename StringType2>
59StringAppend<StringType1, StringType2>::operator String() const
60{
61    return String(makeString(m_string1, m_string2));
62}
63
64template<typename StringType1, typename StringType2>
65StringAppend<StringType1, StringType2>::operator AtomicString() const
66{
67    return AtomicString(makeString(m_string1, m_string2));
68}
69
70template<typename StringType1, typename StringType2>
71bool StringAppend<StringType1, StringType2>::is8Bit()
72{
73    StringTypeAdapter<StringType1> adapter1(m_string1);
74    StringTypeAdapter<StringType2> adapter2(m_string2);
75    return adapter1.is8Bit() && adapter2.is8Bit();
76}
77
78template<typename StringType1, typename StringType2>
79void StringAppend<StringType1, StringType2>::writeTo(LChar* destination)
80{
81    ASSERT(is8Bit());
82    StringTypeAdapter<StringType1> adapter1(m_string1);
83    StringTypeAdapter<StringType2> adapter2(m_string2);
84    adapter1.writeTo(destination);
85    adapter2.writeTo(destination + adapter1.length());
86}
87
88template<typename StringType1, typename StringType2>
89void StringAppend<StringType1, StringType2>::writeTo(UChar* destination)
90{
91    StringTypeAdapter<StringType1> adapter1(m_string1);
92    StringTypeAdapter<StringType2> adapter2(m_string2);
93    adapter1.writeTo(destination);
94    adapter2.writeTo(destination + adapter1.length());
95}
96
97template<typename StringType1, typename StringType2>
98unsigned StringAppend<StringType1, StringType2>::length()
99{
100    StringTypeAdapter<StringType1> adapter1(m_string1);
101    StringTypeAdapter<StringType2> adapter2(m_string2);
102    return adapter1.length() + adapter2.length();
103}
104
105template<typename StringType1, typename StringType2>
106class StringTypeAdapter<StringAppend<StringType1, StringType2> > {
107public:
108    StringTypeAdapter<StringAppend<StringType1, StringType2> >(StringAppend<StringType1, StringType2>& buffer)
109        : m_buffer(buffer)
110    {
111    }
112
113    unsigned length() { return m_buffer.length(); }
114
115    bool is8Bit() { return m_buffer.is8Bit(); }
116
117    void writeTo(LChar* destination) { m_buffer.writeTo(destination); }
118    void writeTo(UChar* destination) { m_buffer.writeTo(destination); }
119
120private:
121    StringAppend<StringType1, StringType2>& m_buffer;
122};
123
124inline StringAppend<const char*, String> operator+(const char* string1, const String& string2)
125{
126    return StringAppend<const char*, String>(string1, string2);
127}
128
129inline StringAppend<const char*, AtomicString> operator+(const char* string1, const AtomicString& string2)
130{
131    return StringAppend<const char*, AtomicString>(string1, string2);
132}
133
134template<typename U, typename V>
135inline StringAppend<const char*, StringAppend<U, V> > operator+(const char* string1, const StringAppend<U, V>& string2)
136{
137    return StringAppend<const char*, StringAppend<U, V> >(string1, string2);
138}
139
140inline StringAppend<const UChar*, String> operator+(const UChar* string1, const String& string2)
141{
142    return StringAppend<const UChar*, String>(string1, string2);
143}
144
145inline StringAppend<const UChar*, AtomicString> operator+(const UChar* string1, const AtomicString& string2)
146{
147    return StringAppend<const UChar*, AtomicString>(string1, string2);
148}
149
150template<typename U, typename V>
151inline StringAppend<const UChar*, StringAppend<U, V> > operator+(const UChar* string1, const StringAppend<U, V>& string2)
152{
153    return StringAppend<const UChar*, StringAppend<U, V> >(string1, string2);
154}
155
156template<typename T>
157StringAppend<String, T> operator+(const String& string1, T string2)
158{
159    return StringAppend<String, T>(string1, string2);
160}
161
162template<typename U, typename V, typename W>
163StringAppend<StringAppend<U, V>, W> operator+(const StringAppend<U, V>& string1, W string2)
164{
165    return StringAppend<StringAppend<U, V>, W>(string1, string2);
166}
167
168} // namespace WTF
169
170#endif // StringOperators_h
171