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

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

Issue 1511813004: Refactor StringImpl::{start,end}sWith(StringImpl*, ...) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years 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 r ights reserved. 5 * Copyright (C) 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2013 Apple Inc. All r ights reserved.
6 * Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net) 6 * Copyright (C) 2006 Andrew Wellington (proton@wiretapped.net)
7 * 7 *
8 * This library is free software; you can redistribute it and/or 8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Library General Public 9 * modify it under the terms of the GNU Library General Public
10 * License as published by the Free Software Foundation; either 10 * License as published by the Free Software Foundation; either
(...skipping 1472 matching lines...) Expand 10 before | Expand all | Expand 10 after
1483 } 1483 }
1484 1484
1485 bool StringImpl::startsWith(const char* matchString, unsigned matchLength) const 1485 bool StringImpl::startsWith(const char* matchString, unsigned matchLength) const
1486 { 1486 {
1487 ASSERT(matchLength); 1487 ASSERT(matchLength);
1488 if (matchLength > length()) 1488 if (matchLength > length())
1489 return false; 1489 return false;
1490 return equalInner(this, 0, reinterpret_cast<const LChar*>(matchString), matc hLength); 1490 return equalInner(this, 0, reinterpret_cast<const LChar*>(matchString), matc hLength);
1491 } 1491 }
1492 1492
1493 bool StringImpl::endsWith(StringImpl* matchString, TextCaseSensitivity caseSensi tivity) 1493 ALWAYS_INLINE static bool equalSubstring(const StringImpl* stringImpl, unsigned startOffset, const StringImpl* matchString)
1494 { 1494 {
1495 ASSERT(stringImpl);
1495 ASSERT(matchString); 1496 ASSERT(matchString);
1496 if (m_length >= matchString->m_length) { 1497 ASSERT(matchString->length() <= stringImpl->length());
1497 unsigned start = m_length - matchString->m_length; 1498 ASSERT(startOffset + matchString->length() <= stringImpl->length());
1498 if (caseSensitivity == TextCaseSensitive) 1499
1499 return find(matchString, start) == start; 1500 unsigned matchLength = matchString->length();
1500 return findIgnoringCase(matchString, start) == start; 1501 if (stringImpl->is8Bit()) {
1502 const LChar* start = stringImpl->characters8() + startOffset;
1503 if (matchString->is8Bit())
1504 return equal(start, matchString->characters8(), matchLength);
1505 return equal(start, matchString->characters16(), matchLength);
1501 } 1506 }
1502 return false; 1507 const UChar* start = stringImpl->characters16() + startOffset;
1508 if (matchString->is8Bit())
1509 return equal(start, matchString->characters8(), matchLength);
1510 return equal(start, matchString->characters16(), matchLength);
1511 }
1512
1513 bool StringImpl::startsWith(const StringImpl* prefix) const
1514 {
1515 ASSERT(prefix);
1516 if (prefix->length() > length())
1517 return false;
1518 return equalSubstring(this, 0, prefix);
1519 }
1520
1521 ALWAYS_INLINE static bool equalSubstringIgnoringCase(const StringImpl* stringImp l, unsigned startOffset, const StringImpl* matchString)
1522 {
1523 ASSERT(stringImpl);
1524 ASSERT(matchString);
1525 ASSERT(matchString->length() <= stringImpl->length());
1526 ASSERT(startOffset + matchString->length() <= stringImpl->length());
1527
1528 unsigned matchLength = matchString->length();
1529 if (stringImpl->is8Bit()) {
1530 const LChar* start = stringImpl->characters8() + startOffset;
1531 if (matchString->is8Bit())
1532 return equalIgnoringCase(start, matchString->characters8(), matchLen gth);
1533 return equalIgnoringCase(start, matchString->characters16(), matchLength );
1534 }
1535 const UChar* start = stringImpl->characters16() + startOffset;
1536 if (matchString->is8Bit())
1537 return equalIgnoringCase(start, matchString->characters8(), matchLength) ;
1538 return equalIgnoringCase(start, matchString->characters16(), matchLength);
1539 }
1540
1541 bool StringImpl::startsWithIgnoringCase(const StringImpl* prefix) const
1542 {
1543 ASSERT(prefix);
1544 if (prefix->length() > length())
1545 return false;
1546 return equalSubstringIgnoringCase(this, 0, prefix);
1503 } 1547 }
1504 1548
1505 bool StringImpl::endsWith(UChar character) const 1549 bool StringImpl::endsWith(UChar character) const
1506 { 1550 {
1507 return m_length && (*this)[m_length - 1] == character; 1551 return m_length && (*this)[m_length - 1] == character;
1508 } 1552 }
1509 1553
1510 bool StringImpl::endsWith(const char* matchString, unsigned matchLength) const 1554 bool StringImpl::endsWith(const char* matchString, unsigned matchLength) const
1511 { 1555 {
1512 ASSERT(matchLength); 1556 ASSERT(matchLength);
1513 if (matchLength > length()) 1557 if (matchLength > length())
1514 return false; 1558 return false;
1515 return equalInner(this, length() - matchLength, reinterpret_cast<const LChar *>(matchString), matchLength); 1559 return equalInner(this, length() - matchLength, reinterpret_cast<const LChar *>(matchString), matchLength);
1516 } 1560 }
1517 1561
1562 bool StringImpl::endsWith(const StringImpl* suffix) const
1563 {
1564 ASSERT(suffix);
1565 unsigned suffixLength = suffix->length();
1566 if (suffixLength > length())
1567 return false;
1568 return equalSubstring(this, length() - suffixLength, suffix);
1569 }
1570
1571 bool StringImpl::endsWithIgnoringCase(const StringImpl* suffix) const
1572 {
1573 ASSERT(suffix);
1574 unsigned suffixLength = suffix->length();
1575 if (suffixLength > length())
1576 return false;
1577 return equalSubstringIgnoringCase(this, length() - suffixLength, suffix);
1578 }
1579
1518 PassRefPtr<StringImpl> StringImpl::replace(UChar oldC, UChar newC) 1580 PassRefPtr<StringImpl> StringImpl::replace(UChar oldC, UChar newC)
1519 { 1581 {
1520 if (oldC == newC) 1582 if (oldC == newC)
1521 return this; 1583 return this;
1522 1584
1523 if (find(oldC) == kNotFound) 1585 if (find(oldC) == kNotFound)
1524 return this; 1586 return this;
1525 1587
1526 unsigned i; 1588 unsigned i;
1527 if (is8Bit()) { 1589 if (is8Bit()) {
(...skipping 578 matching lines...) Expand 10 before | Expand all | Expand 10 after
2106 } else if (localeIdMatchesLang(localeIdentifier, "lt")) { 2168 } else if (localeIdMatchesLang(localeIdentifier, "lt")) {
2107 // TODO(rob.buis) implement upper-casing rules for lt 2169 // TODO(rob.buis) implement upper-casing rules for lt
2108 // like in StringImpl::upper(locale). 2170 // like in StringImpl::upper(locale).
2109 } 2171 }
2110 } 2172 }
2111 2173
2112 return toUpper(c); 2174 return toUpper(c);
2113 } 2175 }
2114 2176
2115 } // namespace WTF 2177 } // namespace WTF
OLDNEW
« no previous file with comments | « third_party/WebKit/Source/wtf/text/StringImpl.h ('k') | third_party/WebKit/Source/wtf/text/WTFString.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698