initial import
[vuplus_webkit] / Source / JavaScriptCore / wtf / text / StringOperators.h
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 namespace WTF {
26
27 template<typename StringType1, typename StringType2>
28 class StringAppend {
29 public:
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
64 private:
65     StringType1 m_string1;
66     StringType2 m_string2;
67 };
68
69 template<typename StringType1, typename StringType2>
70 class StringTypeAdapter<StringAppend<StringType1, StringType2> > {
71 public:
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
80 private:
81     StringAppend<StringType1, StringType2>& m_buffer;
82 };
83
84 inline StringAppend<const char*, String> operator+(const char* string1, const String& string2)
85 {
86     return StringAppend<const char*, String>(string1, string2);
87 }
88
89 inline StringAppend<const char*, AtomicString> operator+(const char* string1, const AtomicString& string2)
90 {
91     return StringAppend<const char*, AtomicString>(string1, string2);
92 }
93
94 template<typename U, typename V>
95 StringAppend<const char*, StringAppend<U, V> > operator+(const char* string1, const StringAppend<U, V>& string2)
96 {
97     return StringAppend<const char*, StringAppend<U, V> >(string1, string2);
98 }
99
100 inline StringAppend<const UChar*, String> operator+(const UChar* string1, const String& string2)
101 {
102     return StringAppend<const UChar*, String>(string1, string2);
103 }
104
105 inline StringAppend<const UChar*, AtomicString> operator+(const UChar* string1, const AtomicString& string2)
106 {
107     return StringAppend<const UChar*, AtomicString>(string1, string2);
108 }
109
110 template<typename U, typename V>
111 StringAppend<const UChar*, StringAppend<U, V> > operator+(const UChar* string1, const StringAppend<U, V>& string2)
112 {
113     return StringAppend<const UChar*, StringAppend<U, V> >(string1, string2);
114 }
115
116 template<typename T>
117 StringAppend<String, T> operator+(const String& string1, T string2)
118 {
119     return StringAppend<String, T>(string1, string2);
120 }
121
122 template<typename U, typename V, typename W>
123 StringAppend<StringAppend<U, V>, W> operator+(const StringAppend<U, V>& string1, W string2)
124 {
125     return StringAppend<StringAppend<U, V>, W>(string1, string2);
126 }
127
128 } // namespace WTF
129
130 #endif // StringOperators_h