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

Side by Side Diff: Source/wtf/text/StringBuilder.cpp

Issue 212603003: De-bloat SVGPathStringBuilder (Closed) Base URL: svn://svn.chromium.org/blink/trunk
Patch Set: Drop ASSERT. Created 6 years, 8 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 | Annotate | Revision Log
« no previous file with comments | « Source/wtf/text/StringBuilder.h ('k') | Source/wtf/text/StringBuilderTest.cpp » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 /* 1 /*
2 * Copyright (C) 2010 Apple Inc. All rights reserved. 2 * Copyright (C) 2010 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 11 matching lines...) Expand all
22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 22 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 24 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */ 25 */
26 26
27 #include "config.h" 27 #include "config.h"
28 #include "StringBuilder.h" 28 #include "StringBuilder.h"
29 29
30 #include "IntegerToStringConversion.h" 30 #include "IntegerToStringConversion.h"
31 #include "WTFString.h" 31 #include "WTFString.h"
32 #include "wtf/dtoa.h"
32 33
33 namespace WTF { 34 namespace WTF {
34 35
35 static unsigned expandedCapacity(unsigned capacity, unsigned requiredLength) 36 static unsigned expandedCapacity(unsigned capacity, unsigned requiredLength)
36 { 37 {
37 static const unsigned minimumCapacity = 16; 38 static const unsigned minimumCapacity = 16;
38 return std::max(requiredLength, std::max(minimumCapacity, capacity * 2)); 39 return std::max(requiredLength, std::max(minimumCapacity, capacity * 2));
39 } 40 }
40 41
41 void StringBuilder::reifyString() 42 void StringBuilder::reifyString()
(...skipping 297 matching lines...) Expand 10 before | Expand all | Expand 10 after
339 void StringBuilder::appendNumber(long long number) 340 void StringBuilder::appendNumber(long long number)
340 { 341 {
341 numberToStringSigned<StringBuilder>(number, this); 342 numberToStringSigned<StringBuilder>(number, this);
342 } 343 }
343 344
344 void StringBuilder::appendNumber(unsigned long long number) 345 void StringBuilder::appendNumber(unsigned long long number)
345 { 346 {
346 numberToStringUnsigned<StringBuilder>(number, this); 347 numberToStringUnsigned<StringBuilder>(number, this);
347 } 348 }
348 349
350 static void expandLCharToUCharInplace(UChar* buffer, size_t length)
351 {
352 const LChar* sourceEnd = reinterpret_cast<LChar*>(buffer) + length;
353 UChar* current = buffer + length;
354 while (current != buffer)
355 *--current = *--sourceEnd;
356 }
357
358 void StringBuilder::appendNumber(double number, unsigned precision, TrailingZero sTruncatingPolicy trailingZerosTruncatingPolicy)
359 {
360 bool truncateTrailingZeros = trailingZerosTruncatingPolicy == TruncateTraili ngZeros;
361 size_t numberLength;
362 if (m_is8Bit) {
363 LChar* dest = appendUninitialized<LChar>(NumberToStringBufferLength);
364 const char* result = numberToFixedPrecisionString(number, precision, rei nterpret_cast<char*>(dest), truncateTrailingZeros);
365 numberLength = strlen(result);
366 } else {
367 UChar* dest = appendUninitialized<UChar>(NumberToStringBufferLength);
368 const char* result = numberToFixedPrecisionString(number, precision, rei nterpret_cast<char*>(dest), truncateTrailingZeros);
369 numberLength = strlen(result);
370 expandLCharToUCharInplace(dest, numberLength);
371 }
372 ASSERT(m_length >= NumberToStringBufferLength);
373 // Remove what appendUninitialized added.
374 m_length -= NumberToStringBufferLength;
375 ASSERT(numberLength <= NumberToStringBufferLength);
376 m_length += numberLength;
377 }
378
349 bool StringBuilder::canShrink() const 379 bool StringBuilder::canShrink() const
350 { 380 {
351 // Only shrink the buffer if it's less than 80% full. Need to tune this heur istic! 381 // Only shrink the buffer if it's less than 80% full. Need to tune this heur istic!
352 return m_buffer && m_buffer->length() > (m_length + (m_length >> 2)); 382 return m_buffer && m_buffer->length() > (m_length + (m_length >> 2));
353 } 383 }
354 384
355 void StringBuilder::shrinkToFit() 385 void StringBuilder::shrinkToFit()
356 { 386 {
357 if (!canShrink()) 387 if (!canShrink())
358 return; 388 return;
359 if (m_is8Bit) 389 if (m_is8Bit)
360 reallocateBuffer<LChar>(m_length); 390 reallocateBuffer<LChar>(m_length);
361 else 391 else
362 reallocateBuffer<UChar>(m_length); 392 reallocateBuffer<UChar>(m_length);
363 m_string = m_buffer.release(); 393 m_string = m_buffer.release();
364 } 394 }
365 395
366 } // namespace WTF 396 } // namespace WTF
OLDNEW
« no previous file with comments | « Source/wtf/text/StringBuilder.h ('k') | Source/wtf/text/StringBuilderTest.cpp » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698