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