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

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

Issue 2241573004: Use StringView for String::replace. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: woops. 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 (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 1506 matching lines...) Expand 10 before | Expand all | Expand 10 after
1517 1517
1518 for (i = 0; i != m_length; ++i) { 1518 for (i = 0; i != m_length; ++i) {
1519 UChar ch = characters16()[i]; 1519 UChar ch = characters16()[i];
1520 if (ch == oldC) 1520 if (ch == oldC)
1521 ch = newC; 1521 ch = newC;
1522 data[i] = ch; 1522 data[i] = ch;
1523 } 1523 }
1524 return newImpl.release(); 1524 return newImpl.release();
1525 } 1525 }
1526 1526
1527 PassRefPtr<StringImpl> StringImpl::replace(unsigned position, unsigned lengthToR eplace, StringImpl* str) 1527 // TODO(esprehn): Passing a null replacement is the same as empty string for
1528 // this method but all others treat null as a no-op. We should choose one
1529 // behavior.
1530 PassRefPtr<StringImpl> StringImpl::replace(unsigned position, unsigned lengthToR eplace, const StringView& string)
1528 { 1531 {
1529 position = min(position, length()); 1532 position = min(position, length());
1530 lengthToReplace = min(lengthToReplace, length() - position); 1533 lengthToReplace = min(lengthToReplace, length() - position);
1531 unsigned lengthToInsert = str ? str->length() : 0; 1534 unsigned lengthToInsert = string.length();
1532 if (!lengthToReplace && !lengthToInsert) 1535 if (!lengthToReplace && !lengthToInsert)
1533 return this; 1536 return this;
1534 1537
1535 RELEASE_ASSERT((length() - lengthToReplace) < (numeric_limits<unsigned>::max () - lengthToInsert)); 1538 RELEASE_ASSERT((length() - lengthToReplace) < (numeric_limits<unsigned>::max () - lengthToInsert));
1536 1539
1537 if (is8Bit() && (!str || str->is8Bit())) { 1540 if (is8Bit() && (string.isNull() || string.is8Bit())) {
1538 LChar* data; 1541 LChar* data;
1539 RefPtr<StringImpl> newImpl = 1542 RefPtr<StringImpl> newImpl =
1540 createUninitialized(length() - lengthToReplace + lengthToInsert, data); 1543 createUninitialized(length() - lengthToReplace + lengthToInsert, data);
1541 memcpy(data, characters8(), position * sizeof(LChar)); 1544 memcpy(data, characters8(), position * sizeof(LChar));
1542 if (str) 1545 if (!string.isNull())
1543 memcpy(data + position, str->characters8(), lengthToInsert * sizeof( LChar)); 1546 memcpy(data + position, string.characters8(), lengthToInsert * sizeo f(LChar));
1544 memcpy(data + position + lengthToInsert, characters8() + position + leng thToReplace, 1547 memcpy(data + position + lengthToInsert, characters8() + position + leng thToReplace,
1545 (length() - position - lengthToReplace) * sizeof(LChar)); 1548 (length() - position - lengthToReplace) * sizeof(LChar));
1546 return newImpl.release(); 1549 return newImpl.release();
1547 } 1550 }
1548 UChar* data; 1551 UChar* data;
1549 RefPtr<StringImpl> newImpl = 1552 RefPtr<StringImpl> newImpl =
1550 createUninitialized(length() - lengthToReplace + lengthToInsert, data); 1553 createUninitialized(length() - lengthToReplace + lengthToInsert, data);
1551 if (is8Bit()) 1554 if (is8Bit())
1552 for (unsigned i = 0; i < position; ++i) 1555 for (unsigned i = 0; i < position; ++i)
1553 data[i] = characters8()[i]; 1556 data[i] = characters8()[i];
1554 else 1557 else
1555 memcpy(data, characters16(), position * sizeof(UChar)); 1558 memcpy(data, characters16(), position * sizeof(UChar));
1556 if (str) { 1559 if (!string.isNull()) {
1557 if (str->is8Bit()) 1560 if (string.is8Bit())
1558 for (unsigned i = 0; i < lengthToInsert; ++i) 1561 for (unsigned i = 0; i < lengthToInsert; ++i)
1559 data[i + position] = str->characters8()[i]; 1562 data[i + position] = string.characters8()[i];
1560 else 1563 else
1561 memcpy(data + position, str->characters16(), lengthToInsert * sizeof (UChar)); 1564 memcpy(data + position, string.characters16(), lengthToInsert * size of(UChar));
1562 } 1565 }
1563 if (is8Bit()) { 1566 if (is8Bit()) {
1564 for (unsigned i = 0; i < length() - position - lengthToReplace; ++i) 1567 for (unsigned i = 0; i < length() - position - lengthToReplace; ++i)
1565 data[i + position + lengthToInsert] = characters8()[i + position + l engthToReplace]; 1568 data[i + position + lengthToInsert] = characters8()[i + position + l engthToReplace];
1566 } else { 1569 } else {
1567 memcpy(data + position + lengthToInsert, characters16() + position + len gthToReplace, 1570 memcpy(data + position + lengthToInsert, characters16() + position + len gthToReplace,
1568 (length() - position - lengthToReplace) * sizeof(UChar)); 1571 (length() - position - lengthToReplace) * sizeof(UChar));
1569 } 1572 }
1570 return newImpl.release(); 1573 return newImpl.release();
1571 } 1574 }
1572 1575
1573 PassRefPtr<StringImpl> StringImpl::replace(UChar pattern, StringImpl* replacemen t) 1576 PassRefPtr<StringImpl> StringImpl::replace(UChar pattern, const StringView& repl acement)
1574 { 1577 {
1575 if (!replacement) 1578 if (replacement.isNull())
1576 return this; 1579 return this;
1577 1580 if (replacement.is8Bit())
1578 if (replacement->is8Bit()) 1581 return replace(pattern, replacement.characters8(), replacement.length()) ;
1579 return replace(pattern, replacement->characters8(), replacement->length( )); 1582 return replace(pattern, replacement.characters16(), replacement.length());
1580
1581 return replace(pattern, replacement->characters16(), replacement->length());
1582 } 1583 }
1583 1584
1584 PassRefPtr<StringImpl> StringImpl::replace(UChar pattern, const LChar* replaceme nt, unsigned repStrLength) 1585 PassRefPtr<StringImpl> StringImpl::replace(UChar pattern, const LChar* replaceme nt, unsigned repStrLength)
1585 { 1586 {
1586 ASSERT(replacement); 1587 ASSERT(replacement);
1587 1588
1588 size_t srcSegmentStart = 0; 1589 size_t srcSegmentStart = 0;
1589 unsigned matchCount = 0; 1590 unsigned matchCount = 0;
1590 1591
1591 // Count the matches. 1592 // Count the matches.
(...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after
1727 } 1728 }
1728 1729
1729 srcSegmentLength = m_length - srcSegmentStart; 1730 srcSegmentLength = m_length - srcSegmentStart;
1730 memcpy(data + dstOffset, characters16() + srcSegmentStart, srcSegmentLength * sizeof(UChar)); 1731 memcpy(data + dstOffset, characters16() + srcSegmentStart, srcSegmentLength * sizeof(UChar));
1731 1732
1732 ASSERT(dstOffset + srcSegmentLength == newImpl->length()); 1733 ASSERT(dstOffset + srcSegmentLength == newImpl->length());
1733 1734
1734 return newImpl.release(); 1735 return newImpl.release();
1735 } 1736 }
1736 1737
1737 PassRefPtr<StringImpl> StringImpl::replace(StringImpl* pattern, StringImpl* repl acement) 1738 PassRefPtr<StringImpl> StringImpl::replace(const StringView& pattern, const Stri ngView& replacement)
1738 { 1739 {
1739 if (!pattern || !replacement) 1740 if (pattern.isNull() || replacement.isNull())
1740 return this; 1741 return this;
1741 1742
1742 unsigned patternLength = pattern->length(); 1743 unsigned patternLength = pattern.length();
1743 if (!patternLength) 1744 if (!patternLength)
1744 return this; 1745 return this;
1745 1746
1746 unsigned repStrLength = replacement->length(); 1747 unsigned repStrLength = replacement.length();
1747 size_t srcSegmentStart = 0; 1748 size_t srcSegmentStart = 0;
1748 unsigned matchCount = 0; 1749 unsigned matchCount = 0;
1749 1750
1750 // Count the matches. 1751 // Count the matches.
1751 while ((srcSegmentStart = find(pattern, srcSegmentStart)) != kNotFound) { 1752 while ((srcSegmentStart = find(pattern, srcSegmentStart)) != kNotFound) {
1752 ++matchCount; 1753 ++matchCount;
1753 srcSegmentStart += patternLength; 1754 srcSegmentStart += patternLength;
1754 } 1755 }
1755 1756
1756 // If we have 0 matches, we don't have to do any more work 1757 // If we have 0 matches, we don't have to do any more work
1757 if (!matchCount) 1758 if (!matchCount)
1758 return this; 1759 return this;
1759 1760
1760 unsigned newSize = m_length - matchCount * patternLength; 1761 unsigned newSize = m_length - matchCount * patternLength;
1761 RELEASE_ASSERT(!repStrLength || matchCount <= numeric_limits<unsigned>::max( ) / repStrLength); 1762 RELEASE_ASSERT(!repStrLength || matchCount <= numeric_limits<unsigned>::max( ) / repStrLength);
1762 1763
1763 RELEASE_ASSERT(newSize <= (numeric_limits<unsigned>::max() - matchCount * re pStrLength)); 1764 RELEASE_ASSERT(newSize <= (numeric_limits<unsigned>::max() - matchCount * re pStrLength));
1764 1765
1765 newSize += matchCount * repStrLength; 1766 newSize += matchCount * repStrLength;
1766 1767
1767 1768
1768 // Construct the new data 1769 // Construct the new data
1769 size_t srcSegmentEnd; 1770 size_t srcSegmentEnd;
1770 unsigned srcSegmentLength; 1771 unsigned srcSegmentLength;
1771 srcSegmentStart = 0; 1772 srcSegmentStart = 0;
1772 unsigned dstOffset = 0; 1773 unsigned dstOffset = 0;
1773 bool srcIs8Bit = is8Bit(); 1774 bool srcIs8Bit = is8Bit();
1774 bool replacementIs8Bit = replacement->is8Bit(); 1775 bool replacementIs8Bit = replacement.is8Bit();
1775 1776
1776 // There are 4 cases: 1777 // There are 4 cases:
1777 // 1. This and replacement are both 8 bit. 1778 // 1. This and replacement are both 8 bit.
1778 // 2. This and replacement are both 16 bit. 1779 // 2. This and replacement are both 16 bit.
1779 // 3. This is 8 bit and replacement is 16 bit. 1780 // 3. This is 8 bit and replacement is 16 bit.
1780 // 4. This is 16 bit and replacement is 8 bit. 1781 // 4. This is 16 bit and replacement is 8 bit.
1781 if (srcIs8Bit && replacementIs8Bit) { 1782 if (srcIs8Bit && replacementIs8Bit) {
1782 // Case 1 1783 // Case 1
1783 LChar* data; 1784 LChar* data;
1784 RefPtr<StringImpl> newImpl = createUninitialized(newSize, data); 1785 RefPtr<StringImpl> newImpl = createUninitialized(newSize, data);
1785 while ((srcSegmentEnd = find(pattern, srcSegmentStart)) != kNotFound) { 1786 while ((srcSegmentEnd = find(pattern, srcSegmentStart)) != kNotFound) {
1786 srcSegmentLength = srcSegmentEnd - srcSegmentStart; 1787 srcSegmentLength = srcSegmentEnd - srcSegmentStart;
1787 memcpy(data + dstOffset, characters8() + srcSegmentStart, srcSegment Length * sizeof(LChar)); 1788 memcpy(data + dstOffset, characters8() + srcSegmentStart, srcSegment Length * sizeof(LChar));
1788 dstOffset += srcSegmentLength; 1789 dstOffset += srcSegmentLength;
1789 memcpy(data + dstOffset, replacement->characters8(), repStrLength * sizeof(LChar)); 1790 memcpy(data + dstOffset, replacement.characters8(), repStrLength * s izeof(LChar));
1790 dstOffset += repStrLength; 1791 dstOffset += repStrLength;
1791 srcSegmentStart = srcSegmentEnd + patternLength; 1792 srcSegmentStart = srcSegmentEnd + patternLength;
1792 } 1793 }
1793 1794
1794 srcSegmentLength = m_length - srcSegmentStart; 1795 srcSegmentLength = m_length - srcSegmentStart;
1795 memcpy(data + dstOffset, characters8() + srcSegmentStart, srcSegmentLeng th * sizeof(LChar)); 1796 memcpy(data + dstOffset, characters8() + srcSegmentStart, srcSegmentLeng th * sizeof(LChar));
1796 1797
1797 ASSERT(dstOffset + srcSegmentLength == newImpl->length()); 1798 ASSERT(dstOffset + srcSegmentLength == newImpl->length());
1798 1799
1799 return newImpl.release(); 1800 return newImpl.release();
1800 } 1801 }
1801 1802
1802 UChar* data; 1803 UChar* data;
1803 RefPtr<StringImpl> newImpl = createUninitialized(newSize, data); 1804 RefPtr<StringImpl> newImpl = createUninitialized(newSize, data);
1804 while ((srcSegmentEnd = find(pattern, srcSegmentStart)) != kNotFound) { 1805 while ((srcSegmentEnd = find(pattern, srcSegmentStart)) != kNotFound) {
1805 srcSegmentLength = srcSegmentEnd - srcSegmentStart; 1806 srcSegmentLength = srcSegmentEnd - srcSegmentStart;
1806 if (srcIs8Bit) { 1807 if (srcIs8Bit) {
1807 // Case 3. 1808 // Case 3.
1808 for (unsigned i = 0; i < srcSegmentLength; ++i) 1809 for (unsigned i = 0; i < srcSegmentLength; ++i)
1809 data[i + dstOffset] = characters8()[i + srcSegmentStart]; 1810 data[i + dstOffset] = characters8()[i + srcSegmentStart];
1810 } else { 1811 } else {
1811 // Case 2 & 4. 1812 // Case 2 & 4.
1812 memcpy(data + dstOffset, characters16() + srcSegmentStart, srcSegmen tLength * sizeof(UChar)); 1813 memcpy(data + dstOffset, characters16() + srcSegmentStart, srcSegmen tLength * sizeof(UChar));
1813 } 1814 }
1814 dstOffset += srcSegmentLength; 1815 dstOffset += srcSegmentLength;
1815 if (replacementIs8Bit) { 1816 if (replacementIs8Bit) {
1816 // Cases 2 & 3. 1817 // Cases 2 & 3.
1817 for (unsigned i = 0; i < repStrLength; ++i) 1818 for (unsigned i = 0; i < repStrLength; ++i)
1818 data[i + dstOffset] = replacement->characters8()[i]; 1819 data[i + dstOffset] = replacement.characters8()[i];
1819 } else { 1820 } else {
1820 // Case 4 1821 // Case 4
1821 memcpy(data + dstOffset, replacement->characters16(), repStrLength * sizeof(UChar)); 1822 memcpy(data + dstOffset, replacement.characters16(), repStrLength * sizeof(UChar));
1822 } 1823 }
1823 dstOffset += repStrLength; 1824 dstOffset += repStrLength;
1824 srcSegmentStart = srcSegmentEnd + patternLength; 1825 srcSegmentStart = srcSegmentEnd + patternLength;
1825 } 1826 }
1826 1827
1827 srcSegmentLength = m_length - srcSegmentStart; 1828 srcSegmentLength = m_length - srcSegmentStart;
1828 if (srcIs8Bit) { 1829 if (srcIs8Bit) {
1829 // Case 3. 1830 // Case 3.
1830 for (unsigned i = 0; i < srcSegmentLength; ++i) 1831 for (unsigned i = 0; i < srcSegmentLength; ++i)
1831 data[i + dstOffset] = characters8()[i + srcSegmentStart]; 1832 data[i + dstOffset] = characters8()[i + srcSegmentStart];
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
2003 } else if (localeIdMatchesLang(localeIdentifier, "lt")) { 2004 } else if (localeIdMatchesLang(localeIdentifier, "lt")) {
2004 // TODO(rob.buis) implement upper-casing rules for lt 2005 // TODO(rob.buis) implement upper-casing rules for lt
2005 // like in StringImpl::upper(locale). 2006 // like in StringImpl::upper(locale).
2006 } 2007 }
2007 } 2008 }
2008 2009
2009 return toUpper(c); 2010 return toUpper(c);
2010 } 2011 }
2011 2012
2012 } // namespace WTF 2013 } // 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