| 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 "wtf/Allocator.h" | 25 #include "wtf/Allocator.h" |
| 26 #include "wtf/text/StringConcatenate.h" | 26 #include "wtf/text/StringConcatenate.h" |
| 27 | 27 |
| 28 namespace WTF { | 28 namespace WTF { |
| 29 | 29 |
| 30 template<typename StringType1, typename StringType2> | 30 template <typename StringType1, typename StringType2> |
| 31 class StringAppend final { | 31 class StringAppend final { |
| 32 STACK_ALLOCATED(); | 32 STACK_ALLOCATED(); |
| 33 public: | |
| 34 StringAppend(StringType1 string1, StringType2 string2); | |
| 35 | 33 |
| 36 operator String() const; | 34 public: |
| 35 StringAppend(StringType1 string1, StringType2 string2); |
| 37 | 36 |
| 38 operator AtomicString() const; | 37 operator String() const; |
| 39 | 38 |
| 40 bool is8Bit(); | 39 operator AtomicString() const; |
| 41 | 40 |
| 42 void writeTo(LChar* destination); | 41 bool is8Bit(); |
| 43 | 42 |
| 44 void writeTo(UChar* destination); | 43 void writeTo(LChar* destination); |
| 45 | 44 |
| 46 unsigned length(); | 45 void writeTo(UChar* destination); |
| 47 | 46 |
| 48 private: | 47 unsigned length(); |
| 49 StringType1 m_string1; | 48 |
| 50 StringType2 m_string2; | 49 private: |
| 50 StringType1 m_string1; |
| 51 StringType2 m_string2; |
| 51 }; | 52 }; |
| 52 | 53 |
| 53 template<typename StringType1, typename StringType2> | 54 template <typename StringType1, typename StringType2> |
| 54 StringAppend<StringType1, StringType2>::StringAppend(StringType1 string1, String
Type2 string2) | 55 StringAppend<StringType1, StringType2>::StringAppend(StringType1 string1, |
| 55 : m_string1(string1) | 56 StringType2 string2) |
| 56 , m_string2(string2) | 57 : m_string1(string1), m_string2(string2) {} |
| 57 { | 58 |
| 59 template <typename StringType1, typename StringType2> |
| 60 StringAppend<StringType1, StringType2>::operator String() const { |
| 61 return String(makeString(m_string1, m_string2)); |
| 58 } | 62 } |
| 59 | 63 |
| 60 template<typename StringType1, typename StringType2> | 64 template <typename StringType1, typename StringType2> |
| 61 StringAppend<StringType1, StringType2>::operator String() const | 65 StringAppend<StringType1, StringType2>::operator AtomicString() const { |
| 62 { | 66 return AtomicString(makeString(m_string1, m_string2)); |
| 63 return String(makeString(m_string1, m_string2)); | |
| 64 } | 67 } |
| 65 | 68 |
| 66 template<typename StringType1, typename StringType2> | 69 template <typename StringType1, typename StringType2> |
| 67 StringAppend<StringType1, StringType2>::operator AtomicString() const | 70 bool StringAppend<StringType1, StringType2>::is8Bit() { |
| 68 { | 71 StringTypeAdapter<StringType1> adapter1(m_string1); |
| 69 return AtomicString(makeString(m_string1, m_string2)); | 72 StringTypeAdapter<StringType2> adapter2(m_string2); |
| 73 return adapter1.is8Bit() && adapter2.is8Bit(); |
| 70 } | 74 } |
| 71 | 75 |
| 72 template<typename StringType1, typename StringType2> | 76 template <typename StringType1, typename StringType2> |
| 73 bool StringAppend<StringType1, StringType2>::is8Bit() | 77 void StringAppend<StringType1, StringType2>::writeTo(LChar* destination) { |
| 74 { | 78 ASSERT(is8Bit()); |
| 75 StringTypeAdapter<StringType1> adapter1(m_string1); | 79 StringTypeAdapter<StringType1> adapter1(m_string1); |
| 76 StringTypeAdapter<StringType2> adapter2(m_string2); | 80 StringTypeAdapter<StringType2> adapter2(m_string2); |
| 77 return adapter1.is8Bit() && adapter2.is8Bit(); | 81 adapter1.writeTo(destination); |
| 82 adapter2.writeTo(destination + adapter1.length()); |
| 78 } | 83 } |
| 79 | 84 |
| 80 template<typename StringType1, typename StringType2> | 85 template <typename StringType1, typename StringType2> |
| 81 void StringAppend<StringType1, StringType2>::writeTo(LChar* destination) | 86 void StringAppend<StringType1, StringType2>::writeTo(UChar* destination) { |
| 82 { | 87 StringTypeAdapter<StringType1> adapter1(m_string1); |
| 83 ASSERT(is8Bit()); | 88 StringTypeAdapter<StringType2> adapter2(m_string2); |
| 84 StringTypeAdapter<StringType1> adapter1(m_string1); | 89 adapter1.writeTo(destination); |
| 85 StringTypeAdapter<StringType2> adapter2(m_string2); | 90 adapter2.writeTo(destination + adapter1.length()); |
| 86 adapter1.writeTo(destination); | |
| 87 adapter2.writeTo(destination + adapter1.length()); | |
| 88 } | 91 } |
| 89 | 92 |
| 90 template<typename StringType1, typename StringType2> | 93 template <typename StringType1, typename StringType2> |
| 91 void StringAppend<StringType1, StringType2>::writeTo(UChar* destination) | 94 unsigned StringAppend<StringType1, StringType2>::length() { |
| 92 { | 95 StringTypeAdapter<StringType1> adapter1(m_string1); |
| 93 StringTypeAdapter<StringType1> adapter1(m_string1); | 96 StringTypeAdapter<StringType2> adapter2(m_string2); |
| 94 StringTypeAdapter<StringType2> adapter2(m_string2); | 97 return adapter1.length() + adapter2.length(); |
| 95 adapter1.writeTo(destination); | |
| 96 adapter2.writeTo(destination + adapter1.length()); | |
| 97 } | 98 } |
| 98 | 99 |
| 99 template<typename StringType1, typename StringType2> | 100 template <typename StringType1, typename StringType2> |
| 100 unsigned StringAppend<StringType1, StringType2>::length() | 101 class StringTypeAdapter<StringAppend<StringType1, StringType2>> { |
| 101 { | 102 STACK_ALLOCATED(); |
| 102 StringTypeAdapter<StringType1> adapter1(m_string1); | 103 |
| 103 StringTypeAdapter<StringType2> adapter2(m_string2); | 104 public: |
| 104 return adapter1.length() + adapter2.length(); | 105 StringTypeAdapter<StringAppend<StringType1, StringType2>>( |
| 106 StringAppend<StringType1, StringType2>& buffer) |
| 107 : m_buffer(buffer) {} |
| 108 |
| 109 unsigned length() { return m_buffer.length(); } |
| 110 |
| 111 bool is8Bit() { return m_buffer.is8Bit(); } |
| 112 |
| 113 void writeTo(LChar* destination) { m_buffer.writeTo(destination); } |
| 114 void writeTo(UChar* destination) { m_buffer.writeTo(destination); } |
| 115 |
| 116 private: |
| 117 StringAppend<StringType1, StringType2>& m_buffer; |
| 118 }; |
| 119 |
| 120 inline StringAppend<const char*, String> operator+(const char* string1, |
| 121 const String& string2) { |
| 122 return StringAppend<const char*, String>(string1, string2); |
| 105 } | 123 } |
| 106 | 124 |
| 107 template<typename StringType1, typename StringType2> | 125 inline StringAppend<const char*, AtomicString> operator+( |
| 108 class StringTypeAdapter<StringAppend<StringType1, StringType2>> { | 126 const char* string1, |
| 109 STACK_ALLOCATED(); | 127 const AtomicString& string2) { |
| 110 public: | 128 return StringAppend<const char*, AtomicString>(string1, string2); |
| 111 StringTypeAdapter<StringAppend<StringType1, StringType2>>(StringAppend<Strin
gType1, StringType2>& buffer) | |
| 112 : m_buffer(buffer) | |
| 113 { | |
| 114 } | |
| 115 | |
| 116 unsigned length() { return m_buffer.length(); } | |
| 117 | |
| 118 bool is8Bit() { return m_buffer.is8Bit(); } | |
| 119 | |
| 120 void writeTo(LChar* destination) { m_buffer.writeTo(destination); } | |
| 121 void writeTo(UChar* destination) { m_buffer.writeTo(destination); } | |
| 122 | |
| 123 private: | |
| 124 StringAppend<StringType1, StringType2>& m_buffer; | |
| 125 }; | |
| 126 | |
| 127 inline StringAppend<const char*, String> operator+(const char* string1, const St
ring& string2) | |
| 128 { | |
| 129 return StringAppend<const char*, String>(string1, string2); | |
| 130 } | 129 } |
| 131 | 130 |
| 132 inline StringAppend<const char*, AtomicString> operator+(const char* string1, co
nst AtomicString& string2) | 131 template <typename U, typename V> |
| 133 { | 132 inline StringAppend<const char*, StringAppend<U, V>> operator+( |
| 134 return StringAppend<const char*, AtomicString>(string1, string2); | 133 const char* string1, |
| 134 const StringAppend<U, V>& string2) { |
| 135 return StringAppend<const char*, StringAppend<U, V>>(string1, string2); |
| 135 } | 136 } |
| 136 | 137 |
| 137 template<typename U, typename V> | 138 inline StringAppend<const UChar*, String> operator+(const UChar* string1, |
| 138 inline StringAppend<const char*, StringAppend<U, V>> operator+(const char* strin
g1, const StringAppend<U, V>& string2) | 139 const String& string2) { |
| 139 { | 140 return StringAppend<const UChar*, String>(string1, string2); |
| 140 return StringAppend<const char*, StringAppend<U, V>>(string1, string2); | |
| 141 } | 141 } |
| 142 | 142 |
| 143 inline StringAppend<const UChar*, String> operator+(const UChar* string1, const
String& string2) | 143 inline StringAppend<const UChar*, AtomicString> operator+( |
| 144 { | 144 const UChar* string1, |
| 145 return StringAppend<const UChar*, String>(string1, string2); | 145 const AtomicString& string2) { |
| 146 return StringAppend<const UChar*, AtomicString>(string1, string2); |
| 146 } | 147 } |
| 147 | 148 |
| 148 inline StringAppend<const UChar*, AtomicString> operator+(const UChar* string1,
const AtomicString& string2) | 149 template <typename U, typename V> |
| 149 { | 150 inline StringAppend<const UChar*, StringAppend<U, V>> operator+( |
| 150 return StringAppend<const UChar*, AtomicString>(string1, string2); | 151 const UChar* string1, |
| 152 const StringAppend<U, V>& string2) { |
| 153 return StringAppend<const UChar*, StringAppend<U, V>>(string1, string2); |
| 151 } | 154 } |
| 152 | 155 |
| 153 template<typename U, typename V> | 156 template <typename T> |
| 154 inline StringAppend<const UChar*, StringAppend<U, V>> operator+(const UChar* str
ing1, const StringAppend<U, V>& string2) | 157 StringAppend<String, T> operator+(const String& string1, T string2) { |
| 155 { | 158 return StringAppend<String, T>(string1, string2); |
| 156 return StringAppend<const UChar*, StringAppend<U, V>>(string1, string2); | |
| 157 } | 159 } |
| 158 | 160 |
| 159 template<typename T> | 161 template <typename U, typename V, typename W> |
| 160 StringAppend<String, T> operator+(const String& string1, T string2) | 162 StringAppend<StringAppend<U, V>, W> operator+(const StringAppend<U, V>& string1, |
| 161 { | 163 W string2) { |
| 162 return StringAppend<String, T>(string1, string2); | 164 return StringAppend<StringAppend<U, V>, W>(string1, string2); |
| 163 } | 165 } |
| 164 | 166 |
| 165 template<typename U, typename V, typename W> | 167 } // namespace WTF |
| 166 StringAppend<StringAppend<U, V>, W> operator+(const StringAppend<U, V>& string1,
W string2) | |
| 167 { | |
| 168 return StringAppend<StringAppend<U, V>, W>(string1, string2); | |
| 169 } | |
| 170 | 168 |
| 171 } // namespace WTF | 169 #endif // StringOperators_h |
| 172 | |
| 173 #endif // StringOperators_h | |
| OLD | NEW |