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
25namespace WTF {
26
27template<typename StringType1, typename StringType2>
28class StringAppend {
29public:
30    StringAppend(StringType1 string1, StringType2 string2)
31        : m_string1(string1)
32        , m_string2(string2)
33    {
34    }
35
36    operator String() const
37    {
38        return String(makeString(m_string1, m_string2));
39    }
40
41    operator AtomicString() const
42    {
43        return AtomicString(makeString(m_string1, m_string2));
44    }
45
46    bool is8Bit()
47    {
48        StringTypeAdapter<StringType1> adapter1(m_string1);
49        StringTypeAdapter<StringType2> adapter2(m_string2);
50        return adapter1.is8Bit() && adapter2.is8Bit();
51    }
52
53    void writeTo(LChar* destination)
54    {
55        ASSERT(is8Bit());
56        StringTypeAdapter<StringType1> adapter1(m_string1);
57        StringTypeAdapter<StringType2> adapter2(m_string2);
58        adapter1.writeTo(destination);
59        adapter2.writeTo(destination + adapter1.length());
60    }
61
62    void writeTo(UChar* destination)
63    {
64        StringTypeAdapter<StringType1> adapter1(m_string1);
65        StringTypeAdapter<StringType2> adapter2(m_string2);
66        adapter1.writeTo(destination);
67        adapter2.writeTo(destination + adapter1.length());
68    }
69
70    unsigned length()
71    {
72        StringTypeAdapter<StringType1> adapter1(m_string1);
73        StringTypeAdapter<StringType2> adapter2(m_string2);
74        return adapter1.length() + adapter2.length();
75    }
76
77private:
78    StringType1 m_string1;
79    StringType2 m_string2;
80};
81
82template<typename StringType1, typename StringType2>
83class StringTypeAdapter<StringAppend<StringType1, StringType2> > {
84public:
85    StringTypeAdapter<StringAppend<StringType1, StringType2> >(StringAppend<StringType1, StringType2>& buffer)
86        : m_buffer(buffer)
87    {
88    }
89
90    unsigned length() { return m_buffer.length(); }
91
92    bool is8Bit() { return m_buffer.is8Bit(); }
93
94    void writeTo(LChar* destination) { m_buffer.writeTo(destination); }
95    void writeTo(UChar* destination) { m_buffer.writeTo(destination); }
96
97private:
98    StringAppend<StringType1, StringType2>& m_buffer;
99};
100
101inline StringAppend<const char*, String> operator+(const char* string1, const String& string2)
102{
103    return StringAppend<const char*, String>(string1, string2);
104}
105
106inline StringAppend<const char*, AtomicString> operator+(const char* string1, const AtomicString& string2)
107{
108    return StringAppend<const char*, AtomicString>(string1, string2);
109}
110
111template<typename U, typename V>
112inline StringAppend<const char*, StringAppend<U, V> > operator+(const char* string1, const StringAppend<U, V>& string2)
113{
114    return StringAppend<const char*, StringAppend<U, V> >(string1, string2);
115}
116
117inline StringAppend<const UChar*, String> operator+(const UChar* string1, const String& string2)
118{
119    return StringAppend<const UChar*, String>(string1, string2);
120}
121
122inline StringAppend<const UChar*, AtomicString> operator+(const UChar* string1, const AtomicString& string2)
123{
124    return StringAppend<const UChar*, AtomicString>(string1, string2);
125}
126
127template<typename U, typename V>
128inline StringAppend<const UChar*, StringAppend<U, V> > operator+(const UChar* string1, const StringAppend<U, V>& string2)
129{
130    return StringAppend<const UChar*, StringAppend<U, V> >(string1, string2);
131}
132
133template<typename T>
134StringAppend<String, T> operator+(const String& string1, T string2)
135{
136    return StringAppend<String, T>(string1, string2);
137}
138
139template<typename U, typename V, typename W>
140StringAppend<StringAppend<U, V>, W> operator+(const StringAppend<U, V>& string1, W string2)
141{
142    return StringAppend<StringAppend<U, V>, W>(string1, string2);
143}
144
145} // namespace WTF
146
147#endif // StringOperators_h
148