Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / wtf / text / StringOperators.h
blobb2e5385f8f9f5cf534b2c149acb8b774b1222323
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.
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.
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.
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.
22 #ifndef StringOperators_h
23 #define StringOperators_h
25 #include "StringConcatenate.h"
27 namespace WTF {
29 template<typename StringType1, typename StringType2>
30 class StringAppend {
31 public:
32 StringAppend(StringType1 string1, StringType2 string2);
34 operator String() const;
36 operator AtomicString() const;
38 bool is8Bit();
40 void writeTo(LChar* destination);
42 void writeTo(UChar* destination);
44 unsigned length();
46 private:
47 StringType1 m_string1;
48 StringType2 m_string2;
51 template<typename StringType1, typename StringType2>
52 StringAppend<StringType1, StringType2>::StringAppend(StringType1 string1, StringType2 string2)
53 : m_string1(string1)
54 , m_string2(string2)
58 template<typename StringType1, typename StringType2>
59 StringAppend<StringType1, StringType2>::operator String() const
61 return String(makeString(m_string1, m_string2));
64 template<typename StringType1, typename StringType2>
65 StringAppend<StringType1, StringType2>::operator AtomicString() const
67 return AtomicString(makeString(m_string1, m_string2));
70 template<typename StringType1, typename StringType2>
71 bool StringAppend<StringType1, StringType2>::is8Bit()
73 StringTypeAdapter<StringType1> adapter1(m_string1);
74 StringTypeAdapter<StringType2> adapter2(m_string2);
75 return adapter1.is8Bit() && adapter2.is8Bit();
78 template<typename StringType1, typename StringType2>
79 void StringAppend<StringType1, StringType2>::writeTo(LChar* destination)
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());
88 template<typename StringType1, typename StringType2>
89 void StringAppend<StringType1, StringType2>::writeTo(UChar* destination)
91 StringTypeAdapter<StringType1> adapter1(m_string1);
92 StringTypeAdapter<StringType2> adapter2(m_string2);
93 adapter1.writeTo(destination);
94 adapter2.writeTo(destination + adapter1.length());
97 template<typename StringType1, typename StringType2>
98 unsigned StringAppend<StringType1, StringType2>::length()
100 StringTypeAdapter<StringType1> adapter1(m_string1);
101 StringTypeAdapter<StringType2> adapter2(m_string2);
102 return adapter1.length() + adapter2.length();
105 template<typename StringType1, typename StringType2>
106 class StringTypeAdapter<StringAppend<StringType1, StringType2>> {
107 public:
108 StringTypeAdapter<StringAppend<StringType1, StringType2>>(StringAppend<StringType1, StringType2>& buffer)
109 : m_buffer(buffer)
113 unsigned length() { return m_buffer.length(); }
115 bool is8Bit() { return m_buffer.is8Bit(); }
117 void writeTo(LChar* destination) { m_buffer.writeTo(destination); }
118 void writeTo(UChar* destination) { m_buffer.writeTo(destination); }
120 private:
121 StringAppend<StringType1, StringType2>& m_buffer;
124 inline StringAppend<const char*, String> operator+(const char* string1, const String& string2)
126 return StringAppend<const char*, String>(string1, string2);
129 inline StringAppend<const char*, AtomicString> operator+(const char* string1, const AtomicString& string2)
131 return StringAppend<const char*, AtomicString>(string1, string2);
134 template<typename U, typename V>
135 inline StringAppend<const char*, StringAppend<U, V>> operator+(const char* string1, const StringAppend<U, V>& string2)
137 return StringAppend<const char*, StringAppend<U, V>>(string1, string2);
140 inline StringAppend<const UChar*, String> operator+(const UChar* string1, const String& string2)
142 return StringAppend<const UChar*, String>(string1, string2);
145 inline StringAppend<const UChar*, AtomicString> operator+(const UChar* string1, const AtomicString& string2)
147 return StringAppend<const UChar*, AtomicString>(string1, string2);
150 template<typename U, typename V>
151 inline StringAppend<const UChar*, StringAppend<U, V>> operator+(const UChar* string1, const StringAppend<U, V>& string2)
153 return StringAppend<const UChar*, StringAppend<U, V>>(string1, string2);
156 template<typename T>
157 StringAppend<String, T> operator+(const String& string1, T string2)
159 return StringAppend<String, T>(string1, string2);
162 template<typename U, typename V, typename W>
163 StringAppend<StringAppend<U, V>, W> operator+(const StringAppend<U, V>& string1, W string2)
165 return StringAppend<StringAppend<U, V>, W>(string1, string2);
168 } // namespace WTF
170 #endif // StringOperators_h