]> git.saurik.com Git - apple/javascriptcore.git/blame - wtf/text/StringOperators.h
JavaScriptCore-903.tar.gz
[apple/javascriptcore.git] / wtf / text / StringOperators.h
CommitLineData
14957cd0
A
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 RefPtr<StringImpl> resultImpl = tryMakeString(m_string1, m_string2);
39 if (!resultImpl)
40 CRASH();
41 return resultImpl.release();
42 }
43
44 operator AtomicString() const
45 {
46 return operator String();
47 }
48
49 void writeTo(UChar* destination)
50 {
51 StringTypeAdapter<StringType1> adapter1(m_string1);
52 StringTypeAdapter<StringType2> adapter2(m_string2);
53 adapter1.writeTo(destination);
54 adapter2.writeTo(destination + adapter1.length());
55 }
56
57 unsigned length()
58 {
59 StringTypeAdapter<StringType1> adapter1(m_string1);
60 StringTypeAdapter<StringType2> adapter2(m_string2);
61 return adapter1.length() + adapter2.length();
62 }
63
64private:
65 StringType1 m_string1;
66 StringType2 m_string2;
67};
68
69template<typename StringType1, typename StringType2>
70class StringTypeAdapter<StringAppend<StringType1, StringType2> > {
71public:
72 StringTypeAdapter<StringAppend<StringType1, StringType2> >(StringAppend<StringType1, StringType2>& buffer)
73 : m_buffer(buffer)
74 {
75 }
76
77 unsigned length() { return m_buffer.length(); }
78 void writeTo(UChar* destination) { m_buffer.writeTo(destination); }
79
80private:
81 StringAppend<StringType1, StringType2>& m_buffer;
82};
83
84inline StringAppend<const char*, String> operator+(const char* string1, const String& string2)
85{
86 return StringAppend<const char*, String>(string1, string2);
87}
88
89inline StringAppend<const char*, AtomicString> operator+(const char* string1, const AtomicString& string2)
90{
91 return StringAppend<const char*, AtomicString>(string1, string2);
92}
93
94template<typename T>
95StringAppend<String, T> operator+(const String& string1, T string2)
96{
97 return StringAppend<String, T>(string1, string2);
98}
99
100template<typename U, typename V, typename W>
101StringAppend<U, StringAppend<V, W> > operator+(U string1, const StringAppend<V, W>& string2)
102{
103 return StringAppend<U, StringAppend<V, W> >(string1, string2);
104}
105
106} // namespace WTF
107
108#endif // StringOperators_h