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

Side by Side Diff: third_party/WebKit/Source/wtf/text/StringBuilder.h

Issue 2106283004: Allocate space in StringBuilder for m_string and the new bytes. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 5 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 (C) 2009, 2010, 2012, 2013 Apple Inc. All rights reserved. 2 * Copyright (C) 2009, 2010, 2012, 2013 Apple Inc. All rights reserved.
3 * Copyright (C) 2012 Google Inc. All rights reserved. 3 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * 4 *
5 * Redistribution and use in source and binary forms, with or without 5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions 6 * modification, are permitted provided that the following conditions
7 * are met: 7 * are met:
8 * 1. Redistributions of source code must retain the above copyright 8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer. 9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright 10 * 2. Redistributions in binary form must reproduce the above copyright
(...skipping 97 matching lines...) Expand 10 before | Expand all | Expand 10 after
108 else 108 else
109 append(string.characters16(), string.length()); 109 append(string.characters16(), string.length());
110 } 110 }
111 111
112 void append(UChar c) 112 void append(UChar c)
113 { 113 {
114 if (m_is8Bit && c <= 0xFF) { 114 if (m_is8Bit && c <= 0xFF) {
115 append(static_cast<LChar>(c)); 115 append(static_cast<LChar>(c));
116 return; 116 return;
117 } 117 }
118 ensureBuffer16(); 118 ensureBuffer16(1);
119 m_string = String(); 119 m_string = String();
120 m_buffer16->append(c); 120 m_buffer16->append(c);
121 ++m_length; 121 ++m_length;
122 } 122 }
123 123
124 void append(LChar c) 124 void append(LChar c)
125 { 125 {
126 if (!m_is8Bit) { 126 if (!m_is8Bit) {
127 append(static_cast<UChar>(c)); 127 append(static_cast<UChar>(c));
128 return; 128 return;
129 } 129 }
130 ensureBuffer8(); 130 ensureBuffer8(1);
131 m_string = String(); 131 m_string = String();
132 m_buffer8->append(c); 132 m_buffer8->append(c);
133 ++m_length; 133 ++m_length;
134 } 134 }
135 135
136 void append(char c) 136 void append(char c)
137 { 137 {
138 append(static_cast<LChar>(c)); 138 append(static_cast<LChar>(c));
139 } 139 }
140 140
(...skipping 57 matching lines...) Expand 10 before | Expand all | Expand 10 after
198 DCHECK(m_buffer16); 198 DCHECK(m_buffer16);
199 return m_buffer16->data(); 199 return m_buffer16->data();
200 } 200 }
201 201
202 bool is8Bit() const { return m_is8Bit; } 202 bool is8Bit() const { return m_is8Bit; }
203 203
204 void clear(); 204 void clear();
205 void swap(StringBuilder&); 205 void swap(StringBuilder&);
206 206
207 private: 207 private:
208 typedef Vector<LChar, 16> Buffer8; 208 static const unsigned kInlineBufferSize = 16;
209 typedef Vector<UChar, 16> Buffer16; 209 static unsigned initialBufferSize() { return kInlineBufferSize; }
esprehn 2016/06/30 23:23:15 You have to have a method since using kInlineBuffe
210 210
211 void ensureBuffer8() 211 typedef Vector<LChar, kInlineBufferSize> Buffer8;
212 typedef Vector<UChar, kInlineBufferSize> Buffer16;
213
214 void ensureBuffer8(unsigned addedSize)
212 { 215 {
213 DCHECK(m_is8Bit); 216 DCHECK(m_is8Bit);
214 if (!hasBuffer()) 217 if (!hasBuffer())
215 createBuffer8(); 218 createBuffer8(addedSize);
216 } 219 }
217 220
218 void ensureBuffer16() 221 void ensureBuffer16(unsigned addedSize)
219 { 222 {
220 if (m_is8Bit || !hasBuffer()) 223 if (m_is8Bit || !hasBuffer())
221 createBuffer16(); 224 createBuffer16(addedSize);
222 } 225 }
223 226
224 void createBuffer8(); 227 void createBuffer8(unsigned addedSize);
225 void createBuffer16(); 228 void createBuffer16(unsigned addedSize);
226 229
227 bool hasBuffer() const { return m_buffer; } 230 bool hasBuffer() const { return m_buffer; }
228 231
229 String m_string; 232 String m_string;
230 union { 233 union {
231 Buffer8* m_buffer8; 234 Buffer8* m_buffer8;
232 Buffer16* m_buffer16; 235 Buffer16* m_buffer16;
233 void* m_buffer; 236 void* m_buffer;
234 }; 237 };
235 unsigned m_length; 238 unsigned m_length;
(...skipping 74 matching lines...) Expand 10 before | Expand all | Expand 10 after
310 inline bool operator==(const StringBuilder& a, const String& b) { return equal(a , b); } 313 inline bool operator==(const StringBuilder& a, const String& b) { return equal(a , b); }
311 inline bool operator!=(const StringBuilder& a, const String& b) { return !equal( a, b); } 314 inline bool operator!=(const StringBuilder& a, const String& b) { return !equal( a, b); }
312 inline bool operator==(const String& a, const StringBuilder& b) { return equal(b , a); } 315 inline bool operator==(const String& a, const StringBuilder& b) { return equal(b , a); }
313 inline bool operator!=(const String& a, const StringBuilder& b) { return !equal( b, a); } 316 inline bool operator!=(const String& a, const StringBuilder& b) { return !equal( b, a); }
314 317
315 } // namespace WTF 318 } // namespace WTF
316 319
317 using WTF::StringBuilder; 320 using WTF::StringBuilder;
318 321
319 #endif // StringBuilder_h 322 #endif // StringBuilder_h
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698