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: StringAppend(StringType1 string1,StringType2 string2)30 StringAppend(StringType1 string1, StringType2 string2) 31 : m_string1(string1) 32 , m_string2(string2) 33 { 34 } 35 String()36 operator String() const 37 { 38 return String(makeString(m_string1, m_string2)); 39 } 40 AtomicString()41 operator AtomicString() const 42 { 43 return AtomicString(makeString(m_string1, m_string2)); 44 } 45 is8Bit()46 bool is8Bit() 47 { 48 StringTypeAdapter<StringType1> adapter1(m_string1); 49 StringTypeAdapter<StringType2> adapter2(m_string2); 50 return adapter1.is8Bit() && adapter2.is8Bit(); 51 } 52 writeTo(LChar * destination)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 writeTo(UChar * destination)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 length()70 unsigned length() 71 { 72 StringTypeAdapter<StringType1> adapter1(m_string1); 73 StringTypeAdapter<StringType2> adapter2(m_string2); 74 return adapter1.length() + adapter2.length(); 75 } 76 77 private: 78 StringType1 m_string1; 79 StringType2 m_string2; 80 }; 81 82 template<typename StringType1, typename StringType2> 83 class StringTypeAdapter<StringAppend<StringType1, StringType2> > { 84 public: 85 StringTypeAdapter<StringAppend<StringType1, StringType2> >(StringAppend<StringType1, StringType2>& buffer) m_buffer(buffer)86 : m_buffer(buffer) 87 { 88 } 89 length()90 unsigned length() { return m_buffer.length(); } 91 is8Bit()92 bool is8Bit() { return m_buffer.is8Bit(); } 93 writeTo(LChar * destination)94 void writeTo(LChar* destination) { m_buffer.writeTo(destination); } writeTo(UChar * destination)95 void writeTo(UChar* destination) { m_buffer.writeTo(destination); } 96 97 private: 98 StringAppend<StringType1, StringType2>& m_buffer; 99 }; 100 101 inline StringAppend<const char*, String> operator+(const char* string1, const String& string2) 102 { 103 return StringAppend<const char*, String>(string1, string2); 104 } 105 106 inline StringAppend<const char*, AtomicString> operator+(const char* string1, const AtomicString& string2) 107 { 108 return StringAppend<const char*, AtomicString>(string1, string2); 109 } 110 111 template<typename U, typename V> 112 inline 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 117 inline StringAppend<const UChar*, String> operator+(const UChar* string1, const String& string2) 118 { 119 return StringAppend<const UChar*, String>(string1, string2); 120 } 121 122 inline StringAppend<const UChar*, AtomicString> operator+(const UChar* string1, const AtomicString& string2) 123 { 124 return StringAppend<const UChar*, AtomicString>(string1, string2); 125 } 126 127 template<typename U, typename V> 128 inline 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 133 template<typename T> 134 StringAppend<String, T> operator+(const String& string1, T string2) 135 { 136 return StringAppend<String, T>(string1, string2); 137 } 138 139 template<typename U, typename V, typename W> 140 StringAppend<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