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

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

Issue 2655853003: Replace StringImpl::empty{16Bit}() with a static member (Closed)
Patch Set: Created 3 years, 10 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) 1999 Lars Knoll (knoll@kde.org) 2 * Copyright (C) 1999 Lars Knoll (knoll@kde.org)
3 * (C) 1999 Antti Koivisto (koivisto@kde.org) 3 * (C) 1999 Antti Koivisto (koivisto@kde.org)
4 * (C) 2001 Dirk Mueller ( mueller@kde.org ) 4 * (C) 2001 Dirk Mueller ( mueller@kde.org )
5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2013 Apple Inc. All 5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2013 Apple Inc. All
6 * rights reserved. 6 * rights reserved.
7 * Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net) 7 * Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net)
8 * 8 *
9 * This library is free software; you can redistribute it and/or 9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Library General Public 10 * modify it under the terms of the GNU Library General Public
(...skipping 10 matching lines...) Expand all
21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, 21 * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
22 * Boston, MA 02110-1301, USA. 22 * Boston, MA 02110-1301, USA.
23 * 23 *
24 */ 24 */
25 25
26 #include "wtf/text/StringImpl.h" 26 #include "wtf/text/StringImpl.h"
27 27
28 #include "wtf/DynamicAnnotations.h" 28 #include "wtf/DynamicAnnotations.h"
29 #include "wtf/LeakAnnotations.h" 29 #include "wtf/LeakAnnotations.h"
30 #include "wtf/PtrUtil.h" 30 #include "wtf/PtrUtil.h"
31 #include "wtf/StaticConstructors.h"
31 #include "wtf/StdLibExtras.h" 32 #include "wtf/StdLibExtras.h"
32 #include "wtf/allocator/Partitions.h" 33 #include "wtf/allocator/Partitions.h"
33 #include "wtf/text/AtomicString.h" 34 #include "wtf/text/AtomicString.h"
34 #include "wtf/text/AtomicStringTable.h" 35 #include "wtf/text/AtomicStringTable.h"
35 #include "wtf/text/CString.h" 36 #include "wtf/text/CString.h"
36 #include "wtf/text/CharacterNames.h" 37 #include "wtf/text/CharacterNames.h"
37 #include "wtf/text/StringBuffer.h" 38 #include "wtf/text/StringBuffer.h"
38 #include "wtf/text/StringHash.h" 39 #include "wtf/text/StringHash.h"
39 #include "wtf/text/StringToNumber.h" 40 #include "wtf/text/StringToNumber.h"
40 #include <algorithm> 41 #include <algorithm>
(...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after
337 std::string StringImpl::asciiForDebugging() const { 338 std::string StringImpl::asciiForDebugging() const {
338 CString ascii = String(isolatedCopy()->substring(0, 128)).ascii(); 339 CString ascii = String(isolatedCopy()->substring(0, 128)).ascii();
339 return std::string(ascii.data(), ascii.length()); 340 return std::string(ascii.data(), ascii.length());
340 } 341 }
341 #endif 342 #endif
342 343
343 PassRefPtr<StringImpl> StringImpl::createUninitialized(unsigned length, 344 PassRefPtr<StringImpl> StringImpl::createUninitialized(unsigned length,
344 LChar*& data) { 345 LChar*& data) {
345 if (!length) { 346 if (!length) {
346 data = 0; 347 data = 0;
347 return empty(); 348 return empty;
348 } 349 }
349 350
350 // Allocate a single buffer large enough to contain the StringImpl 351 // Allocate a single buffer large enough to contain the StringImpl
351 // struct as well as the data which it contains. This removes one 352 // struct as well as the data which it contains. This removes one
352 // heap allocation from this call. 353 // heap allocation from this call.
353 StringImpl* string = static_cast<StringImpl*>(Partitions::bufferMalloc( 354 StringImpl* string = static_cast<StringImpl*>(Partitions::bufferMalloc(
354 allocationSize<LChar>(length), "WTF::StringImpl")); 355 allocationSize<LChar>(length), "WTF::StringImpl"));
355 356
356 data = reinterpret_cast<LChar*>(string + 1); 357 data = reinterpret_cast<LChar*>(string + 1);
357 return adoptRef(new (string) StringImpl(length, Force8BitConstructor)); 358 return adoptRef(new (string) StringImpl(length, Force8BitConstructor));
358 } 359 }
359 360
360 PassRefPtr<StringImpl> StringImpl::createUninitialized(unsigned length, 361 PassRefPtr<StringImpl> StringImpl::createUninitialized(unsigned length,
361 UChar*& data) { 362 UChar*& data) {
362 if (!length) { 363 if (!length) {
363 data = 0; 364 data = 0;
364 return empty(); 365 return empty;
365 } 366 }
366 367
367 // Allocate a single buffer large enough to contain the StringImpl 368 // Allocate a single buffer large enough to contain the StringImpl
368 // struct as well as the data which it contains. This removes one 369 // struct as well as the data which it contains. This removes one
369 // heap allocation from this call. 370 // heap allocation from this call.
370 StringImpl* string = static_cast<StringImpl*>(Partitions::bufferMalloc( 371 StringImpl* string = static_cast<StringImpl*>(Partitions::bufferMalloc(
371 allocationSize<UChar>(length), "WTF::StringImpl")); 372 allocationSize<UChar>(length), "WTF::StringImpl"));
372 373
373 data = reinterpret_cast<UChar*>(string + 1); 374 data = reinterpret_cast<UChar*>(string + 1);
374 return adoptRef(new (string) StringImpl(length)); 375 return adoptRef(new (string) StringImpl(length));
(...skipping 15 matching lines...) Expand all
390 void StringImpl::freezeStaticStrings() { 391 void StringImpl::freezeStaticStrings() {
391 DCHECK(isMainThread()); 392 DCHECK(isMainThread());
392 393
393 #if DCHECK_IS_ON() 394 #if DCHECK_IS_ON()
394 s_allowCreationOfStaticStrings = false; 395 s_allowCreationOfStaticStrings = false;
395 #endif 396 #endif
396 } 397 }
397 398
398 unsigned StringImpl::m_highestStaticStringLength = 0; 399 unsigned StringImpl::m_highestStaticStringLength = 0;
399 400
401 DEFINE_GLOBAL(StringImpl, globalEmpty);
402 DEFINE_GLOBAL(StringImpl, globalEmpty16Bit);
403 // Callers need the global empty strings to be non-const.
404 StringImpl* StringImpl::empty = const_cast<StringImpl*>(&globalEmpty);
405 StringImpl* StringImpl::empty16Bit = const_cast<StringImpl*>(&globalEmpty16Bit);
406 void StringImpl::initStatics() {
407 new ((void*)empty) StringImpl(ConstructEmptyString);
408 new ((void*)empty16Bit) StringImpl(ConstructEmptyString16Bit);
409 }
410
400 StringImpl* StringImpl::createStatic(const char* string, 411 StringImpl* StringImpl::createStatic(const char* string,
401 unsigned length, 412 unsigned length,
402 unsigned hash) { 413 unsigned hash) {
403 #if DCHECK_IS_ON() 414 #if DCHECK_IS_ON()
404 DCHECK(s_allowCreationOfStaticStrings); 415 DCHECK(s_allowCreationOfStaticStrings);
405 #endif 416 #endif
406 DCHECK(string); 417 DCHECK(string);
407 DCHECK(length); 418 DCHECK(length);
408 419
409 StaticStringsTable::const_iterator it = staticStrings().find(hash); 420 StaticStringsTable::const_iterator it = staticStrings().find(hash);
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
444 void StringImpl::reserveStaticStringsCapacityForSize(unsigned size) { 455 void StringImpl::reserveStaticStringsCapacityForSize(unsigned size) {
445 #if DCHECK_IS_ON() 456 #if DCHECK_IS_ON()
446 DCHECK(s_allowCreationOfStaticStrings); 457 DCHECK(s_allowCreationOfStaticStrings);
447 #endif 458 #endif
448 staticStrings().reserveCapacityForSize(size); 459 staticStrings().reserveCapacityForSize(size);
449 } 460 }
450 461
451 PassRefPtr<StringImpl> StringImpl::create(const UChar* characters, 462 PassRefPtr<StringImpl> StringImpl::create(const UChar* characters,
452 unsigned length) { 463 unsigned length) {
453 if (!characters || !length) 464 if (!characters || !length)
454 return empty(); 465 return empty;
455 466
456 UChar* data; 467 UChar* data;
457 RefPtr<StringImpl> string = createUninitialized(length, data); 468 RefPtr<StringImpl> string = createUninitialized(length, data);
458 memcpy(data, characters, length * sizeof(UChar)); 469 memcpy(data, characters, length * sizeof(UChar));
459 return string.release(); 470 return string.release();
460 } 471 }
461 472
462 PassRefPtr<StringImpl> StringImpl::create(const LChar* characters, 473 PassRefPtr<StringImpl> StringImpl::create(const LChar* characters,
463 unsigned length) { 474 unsigned length) {
464 if (!characters || !length) 475 if (!characters || !length)
465 return empty(); 476 return empty;
466 477
467 LChar* data; 478 LChar* data;
468 RefPtr<StringImpl> string = createUninitialized(length, data); 479 RefPtr<StringImpl> string = createUninitialized(length, data);
469 memcpy(data, characters, length * sizeof(LChar)); 480 memcpy(data, characters, length * sizeof(LChar));
470 return string.release(); 481 return string.release();
471 } 482 }
472 483
473 PassRefPtr<StringImpl> StringImpl::create8BitIfPossible(const UChar* characters, 484 PassRefPtr<StringImpl> StringImpl::create8BitIfPossible(const UChar* characters,
474 unsigned length) { 485 unsigned length) {
475 if (!characters || !length) 486 if (!characters || !length)
476 return empty(); 487 return empty;
477 488
478 LChar* data; 489 LChar* data;
479 RefPtr<StringImpl> string = createUninitialized(length, data); 490 RefPtr<StringImpl> string = createUninitialized(length, data);
480 491
481 for (size_t i = 0; i < length; ++i) { 492 for (size_t i = 0; i < length; ++i) {
482 if (characters[i] & 0xff00) 493 if (characters[i] & 0xff00)
483 return create(characters, length); 494 return create(characters, length);
484 data[i] = static_cast<LChar>(characters[i]); 495 data[i] = static_cast<LChar>(characters[i]);
485 } 496 }
486 497
487 return string.release(); 498 return string.release();
488 } 499 }
489 500
490 PassRefPtr<StringImpl> StringImpl::create(const LChar* string) { 501 PassRefPtr<StringImpl> StringImpl::create(const LChar* string) {
491 if (!string) 502 if (!string)
492 return empty(); 503 return empty;
493 size_t length = strlen(reinterpret_cast<const char*>(string)); 504 size_t length = strlen(reinterpret_cast<const char*>(string));
494 RELEASE_ASSERT(length <= numeric_limits<unsigned>::max()); 505 RELEASE_ASSERT(length <= numeric_limits<unsigned>::max());
495 return create(string, length); 506 return create(string, length);
496 } 507 }
497 508
498 bool StringImpl::containsOnlyWhitespace() { 509 bool StringImpl::containsOnlyWhitespace() {
499 // FIXME: The definition of whitespace here includes a number of characters 510 // FIXME: The definition of whitespace here includes a number of characters
500 // that are not whitespace from the point of view of LayoutText; I wonder if 511 // that are not whitespace from the point of view of LayoutText; I wonder if
501 // that's a problem in practice. 512 // that's a problem in practice.
502 if (is8Bit()) { 513 if (is8Bit()) {
(...skipping 10 matching lines...) Expand all
513 UChar c = characters16()[i]; 524 UChar c = characters16()[i];
514 if (!isASCIISpace(c)) 525 if (!isASCIISpace(c))
515 return false; 526 return false;
516 } 527 }
517 return true; 528 return true;
518 } 529 }
519 530
520 PassRefPtr<StringImpl> StringImpl::substring(unsigned start, 531 PassRefPtr<StringImpl> StringImpl::substring(unsigned start,
521 unsigned length) const { 532 unsigned length) const {
522 if (start >= m_length) 533 if (start >= m_length)
523 return empty(); 534 return empty;
524 unsigned maxLength = m_length - start; 535 unsigned maxLength = m_length - start;
525 if (length >= maxLength) { 536 if (length >= maxLength) {
526 // PassRefPtr has trouble dealing with const arguments. It should be updated 537 // PassRefPtr has trouble dealing with const arguments. It should be updated
527 // so this const_cast is not necessary. 538 // so this const_cast is not necessary.
528 if (!start) 539 if (!start)
529 return const_cast<StringImpl*>(this); 540 return const_cast<StringImpl*>(this);
530 length = maxLength; 541 length = maxLength;
531 } 542 }
532 if (is8Bit()) 543 if (is8Bit())
533 return create(characters8() + start, length); 544 return create(characters8() + start, length);
(...skipping 418 matching lines...) Expand 10 before | Expand all | Expand 10 after
952 return this; 963 return this;
953 if (is8Bit()) 964 if (is8Bit())
954 return create(characters8(), length); 965 return create(characters8(), length);
955 return create(characters16(), length); 966 return create(characters16(), length);
956 } 967 }
957 968
958 template <class UCharPredicate> 969 template <class UCharPredicate>
959 inline PassRefPtr<StringImpl> StringImpl::stripMatchedCharacters( 970 inline PassRefPtr<StringImpl> StringImpl::stripMatchedCharacters(
960 UCharPredicate predicate) { 971 UCharPredicate predicate) {
961 if (!m_length) 972 if (!m_length)
962 return empty(); 973 return empty;
963 974
964 unsigned start = 0; 975 unsigned start = 0;
965 unsigned end = m_length - 1; 976 unsigned end = m_length - 1;
966 977
967 // skip white space from start 978 // skip white space from start
968 while (start <= end && 979 while (start <= end &&
969 predicate(is8Bit() ? characters8()[start] : characters16()[start])) 980 predicate(is8Bit() ? characters8()[start] : characters16()[start]))
970 ++start; 981 ++start;
971 982
972 // only white space 983 // only white space
973 if (start > end) 984 if (start > end)
974 return empty(); 985 return empty;
975 986
976 // skip white space from end 987 // skip white space from end
977 while (end && predicate(is8Bit() ? characters8()[end] : characters16()[end])) 988 while (end && predicate(is8Bit() ? characters8()[end] : characters16()[end]))
978 --end; 989 --end;
979 990
980 if (!start && end == m_length - 1) 991 if (!start && end == m_length - 1)
981 return this; 992 return this;
982 if (is8Bit()) 993 if (is8Bit())
983 return create(characters8() + start, end + 1 - start); 994 return create(characters8() + start, end + 1 - start);
984 return create(characters16() + start, end + 1 - start); 995 return create(characters16() + start, end + 1 - start);
(...skipping 1211 matching lines...) Expand 10 before | Expand all | Expand 10 after
2196 } else if (localeIdMatchesLang(localeIdentifier, "lt")) { 2207 } else if (localeIdMatchesLang(localeIdentifier, "lt")) {
2197 // TODO(rob.buis) implement upper-casing rules for lt 2208 // TODO(rob.buis) implement upper-casing rules for lt
2198 // like in StringImpl::upper(locale). 2209 // like in StringImpl::upper(locale).
2199 } 2210 }
2200 } 2211 }
2201 2212
2202 return toUpper(c); 2213 return toUpper(c);
2203 } 2214 }
2204 2215
2205 } // namespace WTF 2216 } // namespace WTF
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698