| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights
reserved. | 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. | 3 * Copyright (C) Research In Motion Limited 2011. All rights reserved. |
| 4 * | 4 * |
| 5 * This library is free software; you can redistribute it and/or | 5 * This library is free software; you can redistribute it and/or |
| 6 * modify it under the terms of the GNU Library General Public | 6 * modify it under the terms of the GNU Library General Public |
| 7 * License as published by the Free Software Foundation; either | 7 * License as published by the Free Software Foundation; either |
| 8 * version 2 of the License, or (at your option) any later version. | 8 * version 2 of the License, or (at your option) any later version. |
| 9 * | 9 * |
| 10 * This library is distributed in the hope that it will be useful, | 10 * This library is distributed in the hope that it will be useful, |
| 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of | 11 * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | 12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 * Library General Public License for more details. | 13 * Library General Public License for more details. |
| 14 * | 14 * |
| 15 * You should have received a copy of the GNU Library General Public License | 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 | 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, | 17 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, |
| 18 * Boston, MA 02110-1301, USA. | 18 * Boston, MA 02110-1301, USA. |
| 19 * | 19 * |
| 20 */ | 20 */ |
| 21 | 21 |
| 22 #ifndef StringOperators_h | 22 #ifndef StringOperators_h |
| 23 #define StringOperators_h | 23 #define StringOperators_h |
| 24 | 24 |
| 25 #include "StringConcatenate.h" | 25 #include "StringConcatenate.h" |
| 26 | 26 |
| 27 namespace WTF { | 27 namespace WTF { |
| 28 | 28 |
| 29 template<typename StringType1, typename StringType2> | 29 template <typename StringType1, typename StringType2> |
| 30 class StringAppend { | 30 class StringAppend { |
| 31 public: | 31 public: |
| 32 StringAppend(StringType1 string1, StringType2 string2); | 32 StringAppend(StringType1 string1, StringType2 string2); |
| 33 | 33 |
| 34 operator String() const; | 34 operator String() const; |
| 35 | 35 |
| 36 operator AtomicString() const; | 36 operator AtomicString() const; |
| 37 | 37 |
| 38 bool is8Bit(); | 38 bool is8Bit(); |
| 39 | 39 |
| 40 void writeTo(LChar* destination); | 40 void writeTo(LChar* destination); |
| 41 | 41 |
| 42 void writeTo(UChar* destination); | 42 void writeTo(UChar* destination); |
| 43 | 43 |
| 44 unsigned length(); | 44 unsigned length(); |
| 45 | 45 |
| 46 private: | 46 private: |
| 47 StringType1 m_string1; | 47 StringType1 m_string1; |
| 48 StringType2 m_string2; | 48 StringType2 m_string2; |
| 49 }; | 49 }; |
| 50 | 50 |
| 51 template<typename StringType1, typename StringType2> | 51 template <typename StringType1, typename StringType2> |
| 52 StringAppend<StringType1, StringType2>::StringAppend(StringType1 string1, String
Type2 string2) | 52 StringAppend<StringType1, StringType2>::StringAppend(StringType1 string1, String
Type2 string2) |
| 53 : m_string1(string1) | 53 : m_string1(string1), m_string2(string2) { |
| 54 , m_string2(string2) | |
| 55 { | |
| 56 } | 54 } |
| 57 | 55 |
| 58 template<typename StringType1, typename StringType2> | 56 template <typename StringType1, typename StringType2> |
| 59 StringAppend<StringType1, StringType2>::operator String() const | 57 StringAppend<StringType1, StringType2>::operator String() const { |
| 60 { | 58 return String(makeString(m_string1, m_string2)); |
| 61 return String(makeString(m_string1, m_string2)); | |
| 62 } | 59 } |
| 63 | 60 |
| 64 template<typename StringType1, typename StringType2> | 61 template <typename StringType1, typename StringType2> |
| 65 StringAppend<StringType1, StringType2>::operator AtomicString() const | 62 StringAppend<StringType1, StringType2>::operator AtomicString() const { |
| 66 { | 63 return AtomicString(makeString(m_string1, m_string2)); |
| 67 return AtomicString(makeString(m_string1, m_string2)); | |
| 68 } | 64 } |
| 69 | 65 |
| 70 template<typename StringType1, typename StringType2> | 66 template <typename StringType1, typename StringType2> |
| 71 bool StringAppend<StringType1, StringType2>::is8Bit() | 67 bool StringAppend<StringType1, StringType2>::is8Bit() { |
| 72 { | 68 StringTypeAdapter<StringType1> adapter1(m_string1); |
| 73 StringTypeAdapter<StringType1> adapter1(m_string1); | 69 StringTypeAdapter<StringType2> adapter2(m_string2); |
| 74 StringTypeAdapter<StringType2> adapter2(m_string2); | 70 return adapter1.is8Bit() && adapter2.is8Bit(); |
| 75 return adapter1.is8Bit() && adapter2.is8Bit(); | |
| 76 } | 71 } |
| 77 | 72 |
| 78 template<typename StringType1, typename StringType2> | 73 template <typename StringType1, typename StringType2> |
| 79 void StringAppend<StringType1, StringType2>::writeTo(LChar* destination) | 74 void StringAppend<StringType1, StringType2>::writeTo(LChar* destination) { |
| 80 { | 75 ASSERT(is8Bit()); |
| 81 ASSERT(is8Bit()); | 76 StringTypeAdapter<StringType1> adapter1(m_string1); |
| 82 StringTypeAdapter<StringType1> adapter1(m_string1); | 77 StringTypeAdapter<StringType2> adapter2(m_string2); |
| 83 StringTypeAdapter<StringType2> adapter2(m_string2); | 78 adapter1.writeTo(destination); |
| 84 adapter1.writeTo(destination); | 79 adapter2.writeTo(destination + adapter1.length()); |
| 85 adapter2.writeTo(destination + adapter1.length()); | |
| 86 } | 80 } |
| 87 | 81 |
| 88 template<typename StringType1, typename StringType2> | 82 template <typename StringType1, typename StringType2> |
| 89 void StringAppend<StringType1, StringType2>::writeTo(UChar* destination) | 83 void StringAppend<StringType1, StringType2>::writeTo(UChar* destination) { |
| 90 { | 84 StringTypeAdapter<StringType1> adapter1(m_string1); |
| 91 StringTypeAdapter<StringType1> adapter1(m_string1); | 85 StringTypeAdapter<StringType2> adapter2(m_string2); |
| 92 StringTypeAdapter<StringType2> adapter2(m_string2); | 86 adapter1.writeTo(destination); |
| 93 adapter1.writeTo(destination); | 87 adapter2.writeTo(destination + adapter1.length()); |
| 94 adapter2.writeTo(destination + adapter1.length()); | |
| 95 } | 88 } |
| 96 | 89 |
| 97 template<typename StringType1, typename StringType2> | 90 template <typename StringType1, typename StringType2> |
| 98 unsigned StringAppend<StringType1, StringType2>::length() | 91 unsigned StringAppend<StringType1, StringType2>::length() { |
| 99 { | 92 StringTypeAdapter<StringType1> adapter1(m_string1); |
| 100 StringTypeAdapter<StringType1> adapter1(m_string1); | 93 StringTypeAdapter<StringType2> adapter2(m_string2); |
| 101 StringTypeAdapter<StringType2> adapter2(m_string2); | 94 return adapter1.length() + adapter2.length(); |
| 102 return adapter1.length() + adapter2.length(); | |
| 103 } | 95 } |
| 104 | 96 |
| 105 template<typename StringType1, typename StringType2> | 97 template <typename StringType1, typename StringType2> |
| 106 class StringTypeAdapter<StringAppend<StringType1, StringType2>> { | 98 class StringTypeAdapter<StringAppend<StringType1, StringType2>> { |
| 107 public: | 99 public: |
| 108 StringTypeAdapter<StringAppend<StringType1, StringType2>>(StringAppend<Strin
gType1, StringType2>& buffer) | 100 StringTypeAdapter<StringAppend<StringType1, StringType2>>(StringAppend<StringT
ype1, StringType2>& buffer) |
| 109 : m_buffer(buffer) | 101 : m_buffer(buffer) { |
| 110 { | 102 } |
| 111 } | |
| 112 | 103 |
| 113 unsigned length() { return m_buffer.length(); } | 104 unsigned length() { return m_buffer.length(); } |
| 114 | 105 |
| 115 bool is8Bit() { return m_buffer.is8Bit(); } | 106 bool is8Bit() { return m_buffer.is8Bit(); } |
| 116 | 107 |
| 117 void writeTo(LChar* destination) { m_buffer.writeTo(destination); } | 108 void writeTo(LChar* destination) { m_buffer.writeTo(destination); } |
| 118 void writeTo(UChar* destination) { m_buffer.writeTo(destination); } | 109 void writeTo(UChar* destination) { m_buffer.writeTo(destination); } |
| 119 | 110 |
| 120 private: | 111 private: |
| 121 StringAppend<StringType1, StringType2>& m_buffer; | 112 StringAppend<StringType1, StringType2>& m_buffer; |
| 122 }; | 113 }; |
| 123 | 114 |
| 124 inline StringAppend<const char*, String> operator+(const char* string1, const St
ring& string2) | 115 inline StringAppend<const char*, String> operator+(const char* string1, const St
ring& string2) { |
| 125 { | 116 return StringAppend<const char*, String>(string1, string2); |
| 126 return StringAppend<const char*, String>(string1, string2); | |
| 127 } | 117 } |
| 128 | 118 |
| 129 inline StringAppend<const char*, AtomicString> operator+(const char* string1, co
nst AtomicString& string2) | 119 inline StringAppend<const char*, AtomicString> operator+(const char* string1, co
nst AtomicString& string2) { |
| 130 { | 120 return StringAppend<const char*, AtomicString>(string1, string2); |
| 131 return StringAppend<const char*, AtomicString>(string1, string2); | |
| 132 } | 121 } |
| 133 | 122 |
| 134 template<typename U, typename V> | 123 template <typename U, typename V> |
| 135 inline StringAppend<const char*, StringAppend<U, V>> operator+(const char* strin
g1, const StringAppend<U, V>& string2) | 124 inline StringAppend<const char*, StringAppend<U, V>> operator+(const char* strin
g1, const StringAppend<U, V>& string2) { |
| 136 { | 125 return StringAppend<const char*, StringAppend<U, V>>(string1, string2); |
| 137 return StringAppend<const char*, StringAppend<U, V>>(string1, string2); | |
| 138 } | 126 } |
| 139 | 127 |
| 140 inline StringAppend<const UChar*, String> operator+(const UChar* string1, const
String& string2) | 128 inline StringAppend<const UChar*, String> operator+(const UChar* string1, const
String& string2) { |
| 141 { | 129 return StringAppend<const UChar*, String>(string1, string2); |
| 142 return StringAppend<const UChar*, String>(string1, string2); | |
| 143 } | 130 } |
| 144 | 131 |
| 145 inline StringAppend<const UChar*, AtomicString> operator+(const UChar* string1,
const AtomicString& string2) | 132 inline StringAppend<const UChar*, AtomicString> operator+(const UChar* string1,
const AtomicString& string2) { |
| 146 { | 133 return StringAppend<const UChar*, AtomicString>(string1, string2); |
| 147 return StringAppend<const UChar*, AtomicString>(string1, string2); | |
| 148 } | 134 } |
| 149 | 135 |
| 150 template<typename U, typename V> | 136 template <typename U, typename V> |
| 151 inline StringAppend<const UChar*, StringAppend<U, V>> operator+(const UChar* str
ing1, const StringAppend<U, V>& string2) | 137 inline StringAppend<const UChar*, StringAppend<U, V>> operator+(const UChar* str
ing1, const StringAppend<U, V>& string2) { |
| 152 { | 138 return StringAppend<const UChar*, StringAppend<U, V>>(string1, string2); |
| 153 return StringAppend<const UChar*, StringAppend<U, V>>(string1, string2); | |
| 154 } | 139 } |
| 155 | 140 |
| 156 template<typename T> | 141 template <typename T> |
| 157 StringAppend<String, T> operator+(const String& string1, T string2) | 142 StringAppend<String, T> operator+(const String& string1, T string2) { |
| 158 { | 143 return StringAppend<String, T>(string1, string2); |
| 159 return StringAppend<String, T>(string1, string2); | |
| 160 } | 144 } |
| 161 | 145 |
| 162 template<typename U, typename V, typename W> | 146 template <typename U, typename V, typename W> |
| 163 StringAppend<StringAppend<U, V>, W> operator+(const StringAppend<U, V>& string1,
W string2) | 147 StringAppend<StringAppend<U, V>, W> operator+(const StringAppend<U, V>& string1,
W string2) { |
| 164 { | 148 return StringAppend<StringAppend<U, V>, W>(string1, string2); |
| 165 return StringAppend<StringAppend<U, V>, W>(string1, string2); | |
| 166 } | 149 } |
| 167 | 150 |
| 168 } // namespace WTF | 151 } // namespace WTF |
| 169 | 152 |
| 170 #endif // StringOperators_h | 153 #endif // StringOperators_h |
| OLD | NEW |