Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(132)

Side by Side Diff: third_party/WebKit/Source/wtf/text/StringConcatenate.cpp

Issue 2245773002: Simplify and clean up StringTypeAdaptor and StringAppend. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: const ref it all. Created 4 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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 // This macro is helpful for testing how many intermediate Strings are created w hile evaluating an 9 // This macro is helpful for testing how many intermediate Strings are created w hile evaluating an
10 // expression containing operator+. 10 // expression containing operator+.
11 #ifndef WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING 11 #ifndef WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING
12 #define WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING() ((void)0) 12 #define WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING() ((void)0)
13 #endif 13 #endif
14 14
15 void WTF::StringTypeAdapter<char*>::writeTo(LChar* destination) 15 void WTF::StringTypeAdapter<char*>::writeTo(LChar* destination) const
16 { 16 {
17 for (unsigned i = 0; i < m_length; ++i) 17 for (unsigned i = 0; i < m_length; ++i)
18 destination[i] = static_cast<LChar>(m_buffer[i]); 18 destination[i] = static_cast<LChar>(m_buffer[i]);
19 } 19 }
20 20
21 void WTF::StringTypeAdapter<char*>::writeTo(UChar* destination) 21 void WTF::StringTypeAdapter<char*>::writeTo(UChar* destination) const
22 { 22 {
23 for (unsigned i = 0; i < m_length; ++i) { 23 for (unsigned i = 0; i < m_length; ++i) {
24 unsigned char c = m_buffer[i]; 24 unsigned char c = m_buffer[i];
25 destination[i] = c; 25 destination[i] = c;
26 } 26 }
27 } 27 }
28 28
29 WTF::StringTypeAdapter<LChar*>::StringTypeAdapter(LChar* buffer) 29 WTF::StringTypeAdapter<LChar*>::StringTypeAdapter(LChar* buffer)
30 : m_buffer(buffer) 30 : m_buffer(buffer)
31 , m_length(strlen(reinterpret_cast<char*>(buffer))) 31 , m_length(strlen(reinterpret_cast<char*>(buffer)))
32 { 32 {
33 } 33 }
34 34
35 void WTF::StringTypeAdapter<LChar*>::writeTo(LChar* destination) 35 void WTF::StringTypeAdapter<LChar*>::writeTo(LChar* destination) const
36 { 36 {
37 memcpy(destination, m_buffer, m_length * sizeof(LChar)); 37 memcpy(destination, m_buffer, m_length * sizeof(LChar));
38 } 38 }
39 39
40 void WTF::StringTypeAdapter<LChar*>::writeTo(UChar* destination) 40 void WTF::StringTypeAdapter<LChar*>::writeTo(UChar* destination) const
41 { 41 {
42 StringImpl::copyChars(destination, m_buffer, m_length); 42 StringImpl::copyChars(destination, m_buffer, m_length);
43 } 43 }
44 44
45 WTF::StringTypeAdapter<const UChar*>::StringTypeAdapter(const UChar* buffer) 45 WTF::StringTypeAdapter<const UChar*>::StringTypeAdapter(const UChar* buffer)
46 : m_buffer(buffer) 46 : m_buffer(buffer)
47 { 47 {
48 size_t len = 0; 48 size_t len = 0;
49 while (m_buffer[len] != UChar(0)) 49 while (m_buffer[len] != UChar(0))
50 ++len; 50 ++len;
51 51
52 RELEASE_ASSERT(len <= std::numeric_limits<unsigned>::max()); 52 RELEASE_ASSERT(len <= std::numeric_limits<unsigned>::max());
53 53
54 m_length = len; 54 m_length = len;
55 } 55 }
56 56
57 void WTF::StringTypeAdapter<const UChar*>::writeTo(UChar* destination) 57 void WTF::StringTypeAdapter<const UChar*>::writeTo(UChar* destination) const
58 { 58 {
59 memcpy(destination, m_buffer, m_length * sizeof(UChar)); 59 memcpy(destination, m_buffer, m_length * sizeof(UChar));
60 } 60 }
61 61
62 WTF::StringTypeAdapter<const char*>::StringTypeAdapter(const char* buffer) 62 WTF::StringTypeAdapter<const char*>::StringTypeAdapter(const char* buffer)
63 : m_buffer(buffer) 63 : m_buffer(buffer)
64 , m_length(strlen(buffer)) 64 , m_length(strlen(buffer))
65 { 65 {
66 } 66 }
67 67
68 void WTF::StringTypeAdapter<const char*>::writeTo(LChar* destination) 68 void WTF::StringTypeAdapter<const char*>::writeTo(LChar* destination) const
69 { 69 {
70 memcpy(destination, m_buffer, static_cast<size_t>(m_length) * sizeof(LChar)) ; 70 memcpy(destination, m_buffer, static_cast<size_t>(m_length) * sizeof(LChar)) ;
71 } 71 }
72 72
73 void WTF::StringTypeAdapter<const char*>::writeTo(UChar* destination) 73 void WTF::StringTypeAdapter<const char*>::writeTo(UChar* destination) const
74 { 74 {
75 for (unsigned i = 0; i < m_length; ++i) { 75 for (unsigned i = 0; i < m_length; ++i) {
76 unsigned char c = m_buffer[i]; 76 unsigned char c = m_buffer[i];
77 destination[i] = c; 77 destination[i] = c;
78 } 78 }
79 } 79 }
80 80
81 WTF::StringTypeAdapter<const LChar*>::StringTypeAdapter(const LChar* buffer) 81 WTF::StringTypeAdapter<const LChar*>::StringTypeAdapter(const LChar* buffer)
82 : m_buffer(buffer) 82 : m_buffer(buffer)
83 , m_length(strlen(reinterpret_cast<const char*>(buffer))) 83 , m_length(strlen(reinterpret_cast<const char*>(buffer)))
84 { 84 {
85 } 85 }
86 86
87 void WTF::StringTypeAdapter<const LChar*>::writeTo(LChar* destination) 87 void WTF::StringTypeAdapter<const LChar*>::writeTo(LChar* destination) const
88 { 88 {
89 memcpy(destination, m_buffer, static_cast<size_t>(m_length) * sizeof(LChar)) ; 89 memcpy(destination, m_buffer, static_cast<size_t>(m_length) * sizeof(LChar)) ;
90 } 90 }
91 91
92 void WTF::StringTypeAdapter<const LChar*>::writeTo(UChar* destination) 92 void WTF::StringTypeAdapter<const LChar*>::writeTo(UChar* destination) const
93 { 93 {
94 StringImpl::copyChars(destination, m_buffer, m_length); 94 StringImpl::copyChars(destination, m_buffer, m_length);
95 } 95 }
96 96
97 void WTF::StringTypeAdapter<Vector<char>>::writeTo(LChar* destination) 97 void WTF::StringTypeAdapter<String>::writeTo(LChar* destination) const
98 {
99 for (size_t i = 0; i < m_buffer.size(); ++i)
100 destination[i] = static_cast<unsigned char>(m_buffer[i]);
101 }
102
103 void WTF::StringTypeAdapter<Vector<char>>::writeTo(UChar* destination)
104 {
105 for (size_t i = 0; i < m_buffer.size(); ++i)
106 destination[i] = static_cast<unsigned char>(m_buffer[i]);
107 }
108
109 void WTF::StringTypeAdapter<Vector<LChar>>::writeTo(LChar* destination)
110 {
111 for (size_t i = 0; i < m_buffer.size(); ++i)
112 destination[i] = m_buffer[i];
113 }
114
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 { 98 {
123 unsigned length = m_buffer.length(); 99 unsigned length = m_buffer.length();
124 100
125 ASSERT(is8Bit()); 101 ASSERT(is8Bit());
126 const LChar* data = m_buffer.characters8(); 102 const LChar* data = m_buffer.characters8();
127 for (unsigned i = 0; i < length; ++i) 103 for (unsigned i = 0; i < length; ++i)
128 destination[i] = data[i]; 104 destination[i] = data[i];
129 105
130 WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING(); 106 WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING();
131 } 107 }
132 108
133 void WTF::StringTypeAdapter<String>::writeTo(UChar* destination) 109 void WTF::StringTypeAdapter<String>::writeTo(UChar* destination) const
134 { 110 {
135 unsigned length = m_buffer.length(); 111 unsigned length = m_buffer.length();
136 112
137 if (is8Bit()) { 113 if (is8Bit()) {
138 const LChar* data = m_buffer.characters8(); 114 const LChar* data = m_buffer.characters8();
139 for (unsigned i = 0; i < length; ++i) 115 for (unsigned i = 0; i < length; ++i)
140 destination[i] = data[i]; 116 destination[i] = data[i];
141 } else { 117 } else {
142 const UChar* data = m_buffer.characters16(); 118 const UChar* data = m_buffer.characters16();
143 for (unsigned i = 0; i < length; ++i) 119 for (unsigned i = 0; i < length; ++i)
144 destination[i] = data[i]; 120 destination[i] = data[i];
145 } 121 }
146 122
147 WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING(); 123 WTF_STRINGTYPEADAPTER_COPIED_WTF_STRING();
148 } 124 }
149 125
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/text/StringConcatenate.h ('k') | third_party/WebKit/Source/wtf/text/StringOperators.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698