| OLD | NEW |
| 1 /* | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
| 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010 Apple Inc. All rights | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 * reserved. | 3 // found in the LICENSE file. |
| 4 * Copyright (C) Research In Motion Limited 2011. All rights reserved. | |
| 5 * | |
| 6 * This library is free software; you can redistribute it and/or | |
| 7 * modify it under the terms of the GNU Library General Public | |
| 8 * License as published by the Free Software Foundation; either | |
| 9 * version 2 of the License, or (at your option) any later version. | |
| 10 * | |
| 11 * This library is distributed in the hope that it will be useful, | |
| 12 * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
| 13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | |
| 14 * Library General Public License for more details. | |
| 15 * | |
| 16 * You should have received a copy of the GNU Library General Public License | |
| 17 * along with this library; see the file COPYING.LIB. If not, write to | |
| 18 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
| 19 * Boston, MA 02110-1301, USA. | |
| 20 * | |
| 21 */ | |
| 22 | 4 |
| 23 #ifndef StringOperators_h | 5 #include "platform/wtf/text/StringOperators.h" |
| 24 #define StringOperators_h | |
| 25 | 6 |
| 26 #include "wtf/Allocator.h" | 7 // The contents of this header was moved to platform/wtf as part of |
| 27 #include "wtf/text/StringConcatenate.h" | 8 // WTF migration project. See the following post for details: |
| 28 | 9 // https://groups.google.com/a/chromium.org/d/msg/blink-dev/tLdAZCTlcAA/bYXVT8gY
CAAJ |
| 29 namespace WTF { | |
| 30 | |
| 31 template <typename StringType1, typename StringType2> | |
| 32 class StringAppend final { | |
| 33 STACK_ALLOCATED(); | |
| 34 | |
| 35 public: | |
| 36 StringAppend(StringType1 string1, StringType2 string2); | |
| 37 | |
| 38 operator String() const; | |
| 39 operator AtomicString() const; | |
| 40 | |
| 41 unsigned length() const; | |
| 42 bool is8Bit() const; | |
| 43 | |
| 44 void writeTo(LChar* destination) const; | |
| 45 void writeTo(UChar* destination) const; | |
| 46 | |
| 47 private: | |
| 48 const StringType1 m_string1; | |
| 49 const StringType2 m_string2; | |
| 50 }; | |
| 51 | |
| 52 template <typename StringType1, typename StringType2> | |
| 53 StringAppend<StringType1, StringType2>::StringAppend(StringType1 string1, | |
| 54 StringType2 string2) | |
| 55 : m_string1(string1), m_string2(string2) {} | |
| 56 | |
| 57 template <typename StringType1, typename StringType2> | |
| 58 StringAppend<StringType1, StringType2>::operator String() const { | |
| 59 if (is8Bit()) { | |
| 60 LChar* buffer; | |
| 61 RefPtr<StringImpl> result = | |
| 62 StringImpl::createUninitialized(length(), buffer); | |
| 63 writeTo(buffer); | |
| 64 return result.release(); | |
| 65 } | |
| 66 UChar* buffer; | |
| 67 RefPtr<StringImpl> result = StringImpl::createUninitialized(length(), buffer); | |
| 68 writeTo(buffer); | |
| 69 return result.release(); | |
| 70 } | |
| 71 | |
| 72 template <typename StringType1, typename StringType2> | |
| 73 StringAppend<StringType1, StringType2>::operator AtomicString() const { | |
| 74 return AtomicString(static_cast<String>(*this)); | |
| 75 } | |
| 76 | |
| 77 template <typename StringType1, typename StringType2> | |
| 78 bool StringAppend<StringType1, StringType2>::is8Bit() const { | |
| 79 StringTypeAdapter<StringType1> adapter1(m_string1); | |
| 80 StringTypeAdapter<StringType2> adapter2(m_string2); | |
| 81 return adapter1.is8Bit() && adapter2.is8Bit(); | |
| 82 } | |
| 83 | |
| 84 template <typename StringType1, typename StringType2> | |
| 85 void StringAppend<StringType1, StringType2>::writeTo(LChar* destination) const { | |
| 86 DCHECK(is8Bit()); | |
| 87 StringTypeAdapter<StringType1> adapter1(m_string1); | |
| 88 StringTypeAdapter<StringType2> adapter2(m_string2); | |
| 89 adapter1.writeTo(destination); | |
| 90 adapter2.writeTo(destination + adapter1.length()); | |
| 91 } | |
| 92 | |
| 93 template <typename StringType1, typename StringType2> | |
| 94 void StringAppend<StringType1, StringType2>::writeTo(UChar* destination) const { | |
| 95 StringTypeAdapter<StringType1> adapter1(m_string1); | |
| 96 StringTypeAdapter<StringType2> adapter2(m_string2); | |
| 97 adapter1.writeTo(destination); | |
| 98 adapter2.writeTo(destination + adapter1.length()); | |
| 99 } | |
| 100 | |
| 101 template <typename StringType1, typename StringType2> | |
| 102 unsigned StringAppend<StringType1, StringType2>::length() const { | |
| 103 StringTypeAdapter<StringType1> adapter1(m_string1); | |
| 104 StringTypeAdapter<StringType2> adapter2(m_string2); | |
| 105 unsigned total = adapter1.length() + adapter2.length(); | |
| 106 // Guard against overflow. | |
| 107 RELEASE_ASSERT(total >= adapter1.length() && total >= adapter2.length()); | |
| 108 return total; | |
| 109 } | |
| 110 | |
| 111 template <typename StringType1, typename StringType2> | |
| 112 class StringTypeAdapter<StringAppend<StringType1, StringType2>> { | |
| 113 STACK_ALLOCATED(); | |
| 114 | |
| 115 public: | |
| 116 StringTypeAdapter<StringAppend<StringType1, StringType2>>( | |
| 117 const StringAppend<StringType1, StringType2>& buffer) | |
| 118 : m_buffer(buffer) {} | |
| 119 | |
| 120 unsigned length() const { return m_buffer.length(); } | |
| 121 bool is8Bit() const { return m_buffer.is8Bit(); } | |
| 122 | |
| 123 void writeTo(LChar* destination) const { m_buffer.writeTo(destination); } | |
| 124 void writeTo(UChar* destination) const { m_buffer.writeTo(destination); } | |
| 125 | |
| 126 private: | |
| 127 const StringAppend<StringType1, StringType2>& m_buffer; | |
| 128 }; | |
| 129 | |
| 130 inline StringAppend<const char*, String> operator+(const char* string1, | |
| 131 const String& string2) { | |
| 132 return StringAppend<const char*, String>(string1, string2); | |
| 133 } | |
| 134 | |
| 135 inline StringAppend<const char*, AtomicString> operator+( | |
| 136 const char* string1, | |
| 137 const AtomicString& string2) { | |
| 138 return StringAppend<const char*, AtomicString>(string1, string2); | |
| 139 } | |
| 140 | |
| 141 inline StringAppend<const char*, StringView> operator+( | |
| 142 const char* string1, | |
| 143 const StringView& string2) { | |
| 144 return StringAppend<const char*, StringView>(string1, string2); | |
| 145 } | |
| 146 | |
| 147 template <typename U, typename V> | |
| 148 inline StringAppend<const char*, StringAppend<U, V>> operator+( | |
| 149 const char* string1, | |
| 150 const StringAppend<U, V>& string2) { | |
| 151 return StringAppend<const char*, StringAppend<U, V>>(string1, string2); | |
| 152 } | |
| 153 | |
| 154 inline StringAppend<const UChar*, String> operator+(const UChar* string1, | |
| 155 const String& string2) { | |
| 156 return StringAppend<const UChar*, String>(string1, string2); | |
| 157 } | |
| 158 | |
| 159 inline StringAppend<const UChar*, AtomicString> operator+( | |
| 160 const UChar* string1, | |
| 161 const AtomicString& string2) { | |
| 162 return StringAppend<const UChar*, AtomicString>(string1, string2); | |
| 163 } | |
| 164 | |
| 165 inline StringAppend<const UChar*, StringView> operator+( | |
| 166 const UChar* string1, | |
| 167 const StringView& string2) { | |
| 168 return StringAppend<const UChar*, StringView>(string1, string2); | |
| 169 } | |
| 170 | |
| 171 template <typename U, typename V> | |
| 172 inline StringAppend<const UChar*, StringAppend<U, V>> operator+( | |
| 173 const UChar* string1, | |
| 174 const StringAppend<U, V>& string2) { | |
| 175 return StringAppend<const UChar*, StringAppend<U, V>>(string1, string2); | |
| 176 } | |
| 177 | |
| 178 template <typename T> | |
| 179 StringAppend<String, T> operator+(const String& string1, T string2) { | |
| 180 return StringAppend<String, T>(string1, string2); | |
| 181 } | |
| 182 | |
| 183 template <typename T> | |
| 184 StringAppend<AtomicString, T> operator+(const AtomicString& string1, | |
| 185 T string2) { | |
| 186 return StringAppend<AtomicString, T>(string1, string2); | |
| 187 } | |
| 188 | |
| 189 template <typename T> | |
| 190 StringAppend<StringView, T> operator+(const StringView& string1, T string2) { | |
| 191 return StringAppend<StringView, T>(string1, string2); | |
| 192 } | |
| 193 | |
| 194 template <typename U, typename V, typename W> | |
| 195 StringAppend<StringAppend<U, V>, W> operator+(const StringAppend<U, V>& string1, | |
| 196 W string2) { | |
| 197 return StringAppend<StringAppend<U, V>, W>(string1, string2); | |
| 198 } | |
| 199 | |
| 200 } // namespace WTF | |
| 201 | |
| 202 #endif // StringOperators_h | |
| OLD | NEW |