| OLD | NEW |
| 1 /* | 1 /* |
| 2 * Copyright 2014 The Chromium Authors. All rights reserved. | 2 * Copyright 2014 The Chromium Authors. All rights reserved. |
| 3 * Use of this source code is governed by a BSD-style license that can be | 3 * Use of this source code is governed by a BSD-style license that can be |
| 4 * found in the LICENSE file. | 4 * found in the LICENSE file. |
| 5 */ | 5 */ |
| 6 | 6 |
| 7 #include "config.h" | 7 #include "config.h" |
| 8 #include "StringConcatenate.h" | 8 #include "StringConcatenate.h" |
| 9 | 9 |
| 10 // This macro is helpful for testing how many intermediate Strings are created w
hile evaluating an | 10 // This macro is helpful for testing how many intermediate Strings are created w
hile evaluating an |
| 11 // expression containing operator+. | 11 // expression containing operator+. |
| 12 #ifndef WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING | 12 #ifndef WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING |
| 13 #define WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING() ((void)0) | 13 #define WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING() ((void)0) |
| 14 #endif | 14 #endif |
| 15 | 15 |
| 16 void WTF::StringTypeAdapter<char*>::writeTo(LChar* destination) | 16 void WTF::StringTypeAdapter<char*>::writeTo(LChar* destination) { |
| 17 { | 17 for (unsigned i = 0; i < m_length; ++i) |
| 18 for (unsigned i = 0; i < m_length; ++i) | 18 destination[i] = static_cast<LChar>(m_buffer[i]); |
| 19 destination[i] = static_cast<LChar>(m_buffer[i]); | |
| 20 } | 19 } |
| 21 | 20 |
| 22 void WTF::StringTypeAdapter<char*>::writeTo(UChar* destination) | 21 void WTF::StringTypeAdapter<char*>::writeTo(UChar* destination) { |
| 23 { | 22 for (unsigned i = 0; i < m_length; ++i) { |
| 24 for (unsigned i = 0; i < m_length; ++i) { | 23 unsigned char c = m_buffer[i]; |
| 25 unsigned char c = m_buffer[i]; | 24 destination[i] = c; |
| 26 destination[i] = c; | 25 } |
| 27 } | |
| 28 } | 26 } |
| 29 | 27 |
| 30 WTF::StringTypeAdapter<LChar*>::StringTypeAdapter(LChar* buffer) | 28 WTF::StringTypeAdapter<LChar*>::StringTypeAdapter(LChar* buffer) |
| 31 : m_buffer(buffer) | 29 : m_buffer(buffer), m_length(strlen(reinterpret_cast<char*>(buffer))) { |
| 32 , m_length(strlen(reinterpret_cast<char*>(buffer))) | |
| 33 { | |
| 34 } | 30 } |
| 35 | 31 |
| 36 void WTF::StringTypeAdapter<LChar*>::writeTo(LChar* destination) | 32 void WTF::StringTypeAdapter<LChar*>::writeTo(LChar* destination) { |
| 37 { | 33 memcpy(destination, m_buffer, m_length * sizeof(LChar)); |
| 38 memcpy(destination, m_buffer, m_length * sizeof(LChar)); | |
| 39 } | 34 } |
| 40 | 35 |
| 41 void WTF::StringTypeAdapter<LChar*>::writeTo(UChar* destination) | 36 void WTF::StringTypeAdapter<LChar*>::writeTo(UChar* destination) { |
| 42 { | 37 StringImpl::copyChars(destination, m_buffer, m_length); |
| 43 StringImpl::copyChars(destination, m_buffer, m_length); | |
| 44 } | 38 } |
| 45 | 39 |
| 46 WTF::StringTypeAdapter<const UChar*>::StringTypeAdapter(const UChar* buffer) | 40 WTF::StringTypeAdapter<const UChar*>::StringTypeAdapter(const UChar* buffer) |
| 47 : m_buffer(buffer) | 41 : m_buffer(buffer) { |
| 48 { | 42 size_t len = 0; |
| 49 size_t len = 0; | 43 while (m_buffer[len] != UChar(0)) |
| 50 while (m_buffer[len] != UChar(0)) | 44 ++len; |
| 51 ++len; | |
| 52 | 45 |
| 53 RELEASE_ASSERT(len <= std::numeric_limits<unsigned>::max()); | 46 RELEASE_ASSERT(len <= std::numeric_limits<unsigned>::max()); |
| 54 | 47 |
| 55 m_length = len; | 48 m_length = len; |
| 56 } | 49 } |
| 57 | 50 |
| 58 void WTF::StringTypeAdapter<const UChar*>::writeTo(UChar* destination) | 51 void WTF::StringTypeAdapter<const UChar*>::writeTo(UChar* destination) { |
| 59 { | 52 memcpy(destination, m_buffer, m_length * sizeof(UChar)); |
| 60 memcpy(destination, m_buffer, m_length * sizeof(UChar)); | |
| 61 } | 53 } |
| 62 | 54 |
| 63 WTF::StringTypeAdapter<const char*>::StringTypeAdapter(const char* buffer) | 55 WTF::StringTypeAdapter<const char*>::StringTypeAdapter(const char* buffer) |
| 64 : m_buffer(buffer) | 56 : m_buffer(buffer), m_length(strlen(buffer)) { |
| 65 , m_length(strlen(buffer)) | |
| 66 { | |
| 67 } | 57 } |
| 68 | 58 |
| 69 void WTF::StringTypeAdapter<const char*>::writeTo(LChar* destination) | 59 void WTF::StringTypeAdapter<const char*>::writeTo(LChar* destination) { |
| 70 { | 60 memcpy(destination, m_buffer, static_cast<size_t>(m_length) * sizeof(LChar)); |
| 71 memcpy(destination, m_buffer, static_cast<size_t>(m_length) * sizeof(LChar))
; | |
| 72 } | 61 } |
| 73 | 62 |
| 74 void WTF::StringTypeAdapter<const char*>::writeTo(UChar* destination) | 63 void WTF::StringTypeAdapter<const char*>::writeTo(UChar* destination) { |
| 75 { | 64 for (unsigned i = 0; i < m_length; ++i) { |
| 76 for (unsigned i = 0; i < m_length; ++i) { | 65 unsigned char c = m_buffer[i]; |
| 77 unsigned char c = m_buffer[i]; | 66 destination[i] = c; |
| 78 destination[i] = c; | 67 } |
| 79 } | |
| 80 } | 68 } |
| 81 | 69 |
| 82 WTF::StringTypeAdapter<const LChar*>::StringTypeAdapter(const LChar* buffer) | 70 WTF::StringTypeAdapter<const LChar*>::StringTypeAdapter(const LChar* buffer) |
| 83 : m_buffer(buffer) | 71 : m_buffer(buffer), m_length(strlen(reinterpret_cast<const char*>(buffer)))
{ |
| 84 , m_length(strlen(reinterpret_cast<const char*>(buffer))) | |
| 85 { | |
| 86 } | 72 } |
| 87 | 73 |
| 88 void WTF::StringTypeAdapter<const LChar*>::writeTo(LChar* destination) | 74 void WTF::StringTypeAdapter<const LChar*>::writeTo(LChar* destination) { |
| 89 { | 75 memcpy(destination, m_buffer, static_cast<size_t>(m_length) * sizeof(LChar)); |
| 90 memcpy(destination, m_buffer, static_cast<size_t>(m_length) * sizeof(LChar))
; | |
| 91 } | 76 } |
| 92 | 77 |
| 93 void WTF::StringTypeAdapter<const LChar*>::writeTo(UChar* destination) | 78 void WTF::StringTypeAdapter<const LChar*>::writeTo(UChar* destination) { |
| 94 { | 79 StringImpl::copyChars(destination, m_buffer, m_length); |
| 95 StringImpl::copyChars(destination, m_buffer, m_length); | |
| 96 } | 80 } |
| 97 | 81 |
| 98 void WTF::StringTypeAdapter<Vector<char>>::writeTo(LChar* destination) | 82 void WTF::StringTypeAdapter<Vector<char>>::writeTo(LChar* destination) { |
| 99 { | 83 for (size_t i = 0; i < m_buffer.size(); ++i) |
| 100 for (size_t i = 0; i < m_buffer.size(); ++i) | 84 destination[i] = static_cast<unsigned char>(m_buffer[i]); |
| 101 destination[i] = static_cast<unsigned char>(m_buffer[i]); | |
| 102 } | 85 } |
| 103 | 86 |
| 104 void WTF::StringTypeAdapter<Vector<char>>::writeTo(UChar* destination) | 87 void WTF::StringTypeAdapter<Vector<char>>::writeTo(UChar* destination) { |
| 105 { | 88 for (size_t i = 0; i < m_buffer.size(); ++i) |
| 106 for (size_t i = 0; i < m_buffer.size(); ++i) | 89 destination[i] = static_cast<unsigned char>(m_buffer[i]); |
| 107 destination[i] = static_cast<unsigned char>(m_buffer[i]); | |
| 108 } | 90 } |
| 109 | 91 |
| 110 void WTF::StringTypeAdapter<Vector<LChar>>::writeTo(LChar* destination) | 92 void WTF::StringTypeAdapter<Vector<LChar>>::writeTo(LChar* destination) { |
| 111 { | 93 for (size_t i = 0; i < m_buffer.size(); ++i) |
| 112 for (size_t i = 0; i < m_buffer.size(); ++i) | 94 destination[i] = m_buffer[i]; |
| 113 destination[i] = m_buffer[i]; | |
| 114 } | 95 } |
| 115 | 96 |
| 116 void WTF::StringTypeAdapter<Vector<LChar>>::writeTo(UChar* destination) | 97 void WTF::StringTypeAdapter<Vector<LChar>>::writeTo(UChar* destination) { |
| 117 { | 98 for (size_t i = 0; i < m_buffer.size(); ++i) |
| 118 for (size_t i = 0; i < m_buffer.size(); ++i) | 99 destination[i] = m_buffer[i]; |
| 119 destination[i] = m_buffer[i]; | |
| 120 } | 100 } |
| 121 | 101 |
| 122 void WTF::StringTypeAdapter<String>::writeTo(LChar* destination) | 102 void WTF::StringTypeAdapter<String>::writeTo(LChar* destination) { |
| 123 { | 103 unsigned length = m_buffer.length(); |
| 124 unsigned length = m_buffer.length(); | |
| 125 | 104 |
| 126 ASSERT(is8Bit()); | 105 ASSERT(is8Bit()); |
| 106 const LChar* data = m_buffer.characters8(); |
| 107 for (unsigned i = 0; i < length; ++i) |
| 108 destination[i] = data[i]; |
| 109 |
| 110 WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING(); |
| 111 } |
| 112 |
| 113 void WTF::StringTypeAdapter<String>::writeTo(UChar* destination) { |
| 114 unsigned length = m_buffer.length(); |
| 115 |
| 116 if (is8Bit()) { |
| 127 const LChar* data = m_buffer.characters8(); | 117 const LChar* data = m_buffer.characters8(); |
| 128 for (unsigned i = 0; i < length; ++i) | 118 for (unsigned i = 0; i < length; ++i) |
| 129 destination[i] = data[i]; | 119 destination[i] = data[i]; |
| 120 } else { |
| 121 const UChar* data = m_buffer.characters16(); |
| 122 for (unsigned i = 0; i < length; ++i) |
| 123 destination[i] = data[i]; |
| 124 } |
| 130 | 125 |
| 131 WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING(); | 126 WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING(); |
| 132 } | 127 } |
| 133 | |
| 134 void WTF::StringTypeAdapter<String>::writeTo(UChar* destination) | |
| 135 { | |
| 136 unsigned length = m_buffer.length(); | |
| 137 | |
| 138 if (is8Bit()) { | |
| 139 const LChar* data = m_buffer.characters8(); | |
| 140 for (unsigned i = 0; i < length; ++i) | |
| 141 destination[i] = data[i]; | |
| 142 } else { | |
| 143 const UChar* data = m_buffer.characters16(); | |
| 144 for (unsigned i = 0; i < length; ++i) | |
| 145 destination[i] = data[i]; | |
| 146 } | |
| 147 | |
| 148 WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING(); | |
| 149 } | |
| 150 | |
| OLD | NEW |