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