OLD | NEW |
1 /* | 1 /* |
2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All r
ights reserved. | 2 * Copyright (C) 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2012 Apple Inc. All r
ights reserved. |
3 * Copyright (C) 2005 Alexey Proskuryakov. | 3 * Copyright (C) 2005 Alexey Proskuryakov. |
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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
48 #include "platform/text/TextBoundaries.h" | 48 #include "platform/text/TextBoundaries.h" |
49 #include "platform/text/TextBreakIteratorInternalICU.h" | 49 #include "platform/text/TextBreakIteratorInternalICU.h" |
50 #include "platform/text/UnicodeUtilities.h" | 50 #include "platform/text/UnicodeUtilities.h" |
51 #include "wtf/text/CString.h" | 51 #include "wtf/text/CString.h" |
52 #include "wtf/text/StringBuilder.h" | 52 #include "wtf/text/StringBuilder.h" |
53 #include "wtf/unicode/CharacterNames.h" | 53 #include "wtf/unicode/CharacterNames.h" |
54 #include <unicode/usearch.h> | 54 #include <unicode/usearch.h> |
55 #include <unicode/utf16.h> | 55 #include <unicode/utf16.h> |
56 | 56 |
57 using namespace WTF::Unicode; | 57 using namespace WTF::Unicode; |
58 using namespace std; | |
59 | 58 |
60 namespace WebCore { | 59 namespace WebCore { |
61 | 60 |
62 using namespace HTMLNames; | 61 using namespace HTMLNames; |
63 | 62 |
64 // Buffer that knows how to compare with a search target. | 63 // Buffer that knows how to compare with a search target. |
65 // Keeps enough of the previous text to be able to search in the future, but no
more. | 64 // Keeps enough of the previous text to be able to search in the future, but no
more. |
66 // Non-breaking spaces are always equal to normal spaces. | 65 // Non-breaking spaces are always equal to normal spaces. |
67 // Case folding is also done if the CaseInsensitive option is specified. | 66 // Case folding is also done if the CaseInsensitive option is specified. |
68 // Matches are further filtered if the AtWordStarts option is specified, althoug
h some | 67 // Matches are further filtered if the AtWordStarts option is specified, althoug
h some |
69 // matches inside a word are permitted if TreatMedialCapitalAsWordStart is speci
fied as well. | 68 // matches inside a word are permitted if TreatMedialCapitalAsWordStart is speci
fied as well. |
70 class SearchBuffer { | 69 class SearchBuffer { |
71 WTF_MAKE_NONCOPYABLE(SearchBuffer); | 70 WTF_MAKE_NONCOPYABLE(SearchBuffer); |
72 public: | 71 public: |
73 SearchBuffer(const String& target, FindOptions); | 72 SearchBuffer(const String& target, FindOptions); |
74 ~SearchBuffer(); | 73 ~SearchBuffer(); |
75 | 74 |
76 // Returns number of characters appended; guaranteed to be in the range [1,
length]. | 75 // Returns number of characters appended; guaranteed to be in the range [1,
length]. |
77 template<typename CharType> | 76 template<typename CharType> |
78 void append(const CharType*, size_t length); | 77 void append(const CharType*, std::size_t length); |
79 size_t numberOfCharactersJustAppended() const { return m_numberOfCharactersJ
ustAppended; } | 78 std::size_t numberOfCharactersJustAppended() const { return m_numberOfCharac
tersJustAppended; } |
80 | 79 |
81 bool needsMoreContext() const; | 80 bool needsMoreContext() const; |
82 void prependContext(const UChar*, size_t length); | 81 void prependContext(const UChar*, std::size_t length); |
83 void reachedBreak(); | 82 void reachedBreak(); |
84 | 83 |
85 // Result is the size in characters of what was found. | 84 // Result is the size in characters of what was found. |
86 // And <startOffset> is the number of characters back to the start of what w
as found. | 85 // And <startOffset> is the number of characters back to the start of what w
as found. |
87 size_t search(size_t& startOffset); | 86 std::size_t search(std::size_t& startOffset); |
88 bool atBreak() const; | 87 bool atBreak() const; |
89 | 88 |
90 private: | 89 private: |
91 bool isBadMatch(const UChar*, size_t length) const; | 90 bool isBadMatch(const UChar*, std::size_t length) const; |
92 bool isWordStartMatch(size_t start, size_t length) const; | 91 bool isWordStartMatch(std::size_t start, std::size_t length) const; |
93 | 92 |
94 Vector<UChar> m_target; | 93 Vector<UChar> m_target; |
95 FindOptions m_options; | 94 FindOptions m_options; |
96 | 95 |
97 Vector<UChar> m_buffer; | 96 Vector<UChar> m_buffer; |
98 size_t m_overlap; | 97 std::size_t m_overlap; |
99 size_t m_prefixLength; | 98 std::size_t m_prefixLength; |
100 size_t m_numberOfCharactersJustAppended; | 99 std::size_t m_numberOfCharactersJustAppended; |
101 bool m_atBreak; | 100 bool m_atBreak; |
102 bool m_needsMoreContext; | 101 bool m_needsMoreContext; |
103 | 102 |
104 bool m_targetRequiresKanaWorkaround; | 103 bool m_targetRequiresKanaWorkaround; |
105 Vector<UChar> m_normalizedTarget; | 104 Vector<UChar> m_normalizedTarget; |
106 mutable Vector<UChar> m_normalizedMatch; | 105 mutable Vector<UChar> m_normalizedMatch; |
107 }; | 106 }; |
108 | 107 |
109 // -------- | 108 // -------- |
110 | 109 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
223 } | 222 } |
224 | 223 |
225 static void setUpFullyClippedStack(BitStack& stack, Node* node) | 224 static void setUpFullyClippedStack(BitStack& stack, Node* node) |
226 { | 225 { |
227 // Put the nodes in a vector so we can iterate in reverse order. | 226 // Put the nodes in a vector so we can iterate in reverse order. |
228 Vector<Node*, 100> ancestry; | 227 Vector<Node*, 100> ancestry; |
229 for (Node* parent = node->parentOrShadowHostNode(); parent; parent = parent-
>parentOrShadowHostNode()) | 228 for (Node* parent = node->parentOrShadowHostNode(); parent; parent = parent-
>parentOrShadowHostNode()) |
230 ancestry.append(parent); | 229 ancestry.append(parent); |
231 | 230 |
232 // Call pushFullyClippedState on each node starting with the earliest ancest
or. | 231 // Call pushFullyClippedState on each node starting with the earliest ancest
or. |
233 size_t size = ancestry.size(); | 232 std::size_t size = ancestry.size(); |
234 for (size_t i = 0; i < size; ++i) | 233 for (std::size_t i = 0; i < size; ++i) |
235 pushFullyClippedState(stack, ancestry[size - i - 1]); | 234 pushFullyClippedState(stack, ancestry[size - i - 1]); |
236 pushFullyClippedState(stack, node); | 235 pushFullyClippedState(stack, node); |
237 | 236 |
238 ASSERT(stack.size() == 1 + depthCrossingShadowBoundaries(node)); | 237 ASSERT(stack.size() == 1 + depthCrossingShadowBoundaries(node)); |
239 } | 238 } |
240 | 239 |
241 // -------- | 240 // -------- |
242 | 241 |
243 TextIterator::TextIterator(const Range* range, TextIteratorBehaviorFlags behavio
r) | 242 TextIterator::TextIterator(const Range* range, TextIteratorBehaviorFlags behavio
r) |
244 : m_startContainer(nullptr) | 243 : m_startContainer(nullptr) |
(...skipping 366 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
611 emitText(m_node, m_firstLetterText, m_offset, m_offset + firstLe
tter.length()); | 610 emitText(m_node, m_firstLetterText, m_offset, m_offset + firstLe
tter.length()); |
612 m_firstLetterText = 0; | 611 m_firstLetterText = 0; |
613 m_textBox = 0; | 612 m_textBox = 0; |
614 return false; | 613 return false; |
615 } | 614 } |
616 } | 615 } |
617 if (renderer->style()->visibility() != VISIBLE && !m_ignoresStyleVisibil
ity) | 616 if (renderer->style()->visibility() != VISIBLE && !m_ignoresStyleVisibil
ity) |
618 return false; | 617 return false; |
619 int strLength = str.length(); | 618 int strLength = str.length(); |
620 int end = (m_node == m_endContainer) ? m_endOffset : INT_MAX; | 619 int end = (m_node == m_endContainer) ? m_endOffset : INT_MAX; |
621 int runEnd = min(strLength, end); | 620 int runEnd = std::min(strLength, end); |
622 | 621 |
623 if (runStart >= runEnd) | 622 if (runStart >= runEnd) |
624 return true; | 623 return true; |
625 | 624 |
626 emitText(m_node, runStart, runEnd); | 625 emitText(m_node, runStart, runEnd); |
627 return true; | 626 return true; |
628 } | 627 } |
629 | 628 |
630 if (renderer->firstTextBox()) | 629 if (renderer->firstTextBox()) |
631 m_textBox = renderer->firstTextBox(); | 630 m_textBox = renderer->firstTextBox(); |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
664 RenderText* renderer = m_firstLetterText ? m_firstLetterText : toRenderText(
m_node->renderer()); | 663 RenderText* renderer = m_firstLetterText ? m_firstLetterText : toRenderText(
m_node->renderer()); |
665 if (renderer->style()->visibility() != VISIBLE && !m_ignoresStyleVisibility)
{ | 664 if (renderer->style()->visibility() != VISIBLE && !m_ignoresStyleVisibility)
{ |
666 m_textBox = 0; | 665 m_textBox = 0; |
667 return; | 666 return; |
668 } | 667 } |
669 String str = renderer->text(); | 668 String str = renderer->text(); |
670 unsigned start = m_offset; | 669 unsigned start = m_offset; |
671 unsigned end = (m_node == m_endContainer) ? static_cast<unsigned>(m_endOffse
t) : INT_MAX; | 670 unsigned end = (m_node == m_endContainer) ? static_cast<unsigned>(m_endOffse
t) : INT_MAX; |
672 while (m_textBox) { | 671 while (m_textBox) { |
673 unsigned textBoxStart = m_textBox->start(); | 672 unsigned textBoxStart = m_textBox->start(); |
674 unsigned runStart = max(textBoxStart, start); | 673 unsigned runStart = std::max(textBoxStart, start); |
675 | 674 |
676 // Check for collapsed space at the start of this run. | 675 // Check for collapsed space at the start of this run. |
677 InlineTextBox* firstTextBox = renderer->containsReversedText() ? (m_sort
edTextBoxes.isEmpty() ? 0 : m_sortedTextBoxes[0]) : renderer->firstTextBox(); | 676 InlineTextBox* firstTextBox = renderer->containsReversedText() ? (m_sort
edTextBoxes.isEmpty() ? 0 : m_sortedTextBoxes[0]) : renderer->firstTextBox(); |
678 bool needSpace = m_lastTextNodeEndedWithCollapsedSpace | 677 bool needSpace = m_lastTextNodeEndedWithCollapsedSpace |
679 || (m_textBox == firstTextBox && textBoxStart == runStart && runStar
t > 0); | 678 || (m_textBox == firstTextBox && textBoxStart == runStart && runStar
t > 0); |
680 if (needSpace && !renderer->style()->isCollapsibleWhiteSpace(m_lastChara
cter) && m_lastCharacter) { | 679 if (needSpace && !renderer->style()->isCollapsibleWhiteSpace(m_lastChara
cter) && m_lastCharacter) { |
681 if (m_lastTextNode == m_node && runStart > 0 && str[runStart - 1] ==
' ') { | 680 if (m_lastTextNode == m_node && runStart > 0 && str[runStart - 1] ==
' ') { |
682 unsigned spaceRunStart = runStart - 1; | 681 unsigned spaceRunStart = runStart - 1; |
683 while (spaceRunStart > 0 && str[spaceRunStart - 1] == ' ') | 682 while (spaceRunStart > 0 && str[spaceRunStart - 1] == ' ') |
684 --spaceRunStart; | 683 --spaceRunStart; |
685 emitText(m_node, renderer, spaceRunStart, spaceRunStart + 1); | 684 emitText(m_node, renderer, spaceRunStart, spaceRunStart + 1); |
686 } else { | 685 } else { |
687 emitCharacter(' ', m_node, 0, runStart, runStart); | 686 emitCharacter(' ', m_node, 0, runStart, runStart); |
688 } | 687 } |
689 return; | 688 return; |
690 } | 689 } |
691 unsigned textBoxEnd = textBoxStart + m_textBox->len(); | 690 unsigned textBoxEnd = textBoxStart + m_textBox->len(); |
692 unsigned runEnd = min(textBoxEnd, end); | 691 unsigned runEnd = std::min(textBoxEnd, end); |
693 | 692 |
694 // Determine what the next text box will be, but don't advance yet | 693 // Determine what the next text box will be, but don't advance yet |
695 InlineTextBox* nextTextBox = 0; | 694 InlineTextBox* nextTextBox = 0; |
696 if (renderer->containsReversedText()) { | 695 if (renderer->containsReversedText()) { |
697 if (m_sortedTextBoxesPosition + 1 < m_sortedTextBoxes.size()) | 696 if (m_sortedTextBoxesPosition + 1 < m_sortedTextBoxes.size()) |
698 nextTextBox = m_sortedTextBoxes[m_sortedTextBoxesPosition + 1]; | 697 nextTextBox = m_sortedTextBoxes[m_sortedTextBoxesPosition + 1]; |
699 } else { | 698 } else { |
700 nextTextBox = m_textBox->nextTextBox(); | 699 nextTextBox = m_textBox->nextTextBox(); |
701 } | 700 } |
702 ASSERT(!nextTextBox || nextTextBox->renderer() == renderer); | 701 ASSERT(!nextTextBox || nextTextBox->renderer() == renderer); |
703 | 702 |
704 if (runStart < runEnd) { | 703 if (runStart < runEnd) { |
705 // Handle either a single newline character (which becomes a space), | 704 // Handle either a single newline character (which becomes a space), |
706 // or a run of characters that does not include a newline. | 705 // or a run of characters that does not include a newline. |
707 // This effectively translates newlines to spaces without copying th
e text. | 706 // This effectively translates newlines to spaces without copying th
e text. |
708 if (str[runStart] == '\n') { | 707 if (str[runStart] == '\n') { |
709 emitCharacter(' ', m_node, 0, runStart, runStart + 1); | 708 emitCharacter(' ', m_node, 0, runStart, runStart + 1); |
710 m_offset = runStart + 1; | 709 m_offset = runStart + 1; |
711 } else { | 710 } else { |
712 size_t subrunEnd = str.find('\n', runStart); | 711 std::size_t subrunEnd = str.find('\n', runStart); |
713 if (subrunEnd == kNotFound || subrunEnd > runEnd) | 712 if (subrunEnd == kNotFound || subrunEnd > runEnd) |
714 subrunEnd = runEnd; | 713 subrunEnd = runEnd; |
715 | 714 |
716 m_offset = subrunEnd; | 715 m_offset = subrunEnd; |
717 emitText(m_node, renderer, runStart, subrunEnd); | 716 emitText(m_node, renderer, runStart, subrunEnd); |
718 } | 717 } |
719 | 718 |
720 // If we are doing a subrun that doesn't go to the end of the text b
ox, | 719 // If we are doing a subrun that doesn't go to the end of the text b
ox, |
721 // come back again to finish handling this text box; don't advance t
o the next one. | 720 // come back again to finish handling this text box; don't advance t
o the next one. |
722 if (static_cast<unsigned>(m_positionEndOffset) < textBoxEnd) | 721 if (static_cast<unsigned>(m_positionEndOffset) < textBoxEnd) |
(...skipping 1005 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1728 | 1727 |
1729 UChar WordAwareIterator::characterAt(unsigned index) const | 1728 UChar WordAwareIterator::characterAt(unsigned index) const |
1730 { | 1729 { |
1731 if (!m_buffer.isEmpty()) | 1730 if (!m_buffer.isEmpty()) |
1732 return m_buffer[index]; | 1731 return m_buffer[index]; |
1733 return m_textIterator.characterAt(index); | 1732 return m_textIterator.characterAt(index); |
1734 } | 1733 } |
1735 | 1734 |
1736 // -------- | 1735 // -------- |
1737 | 1736 |
1738 static const size_t minimumSearchBufferSize = 8192; | 1737 static const std::size_t minimumSearchBufferSize = 8192; |
1739 | 1738 |
1740 #ifndef NDEBUG | 1739 #ifndef NDEBUG |
1741 static bool searcherInUse; | 1740 static bool searcherInUse; |
1742 #endif | 1741 #endif |
1743 | 1742 |
1744 static UStringSearch* createSearcher() | 1743 static UStringSearch* createSearcher() |
1745 { | 1744 { |
1746 // Provide a non-empty pattern and non-empty text so usearch_open will not f
ail, | 1745 // Provide a non-empty pattern and non-empty text so usearch_open will not f
ail, |
1747 // but it doesn't matter exactly what it is, since we don't perform any sear
ches | 1746 // but it doesn't matter exactly what it is, since we don't perform any sear
ches |
1748 // without setting both the pattern and the text. | 1747 // without setting both the pattern and the text. |
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1784 , m_targetRequiresKanaWorkaround(containsKanaLetters(target)) | 1783 , m_targetRequiresKanaWorkaround(containsKanaLetters(target)) |
1785 { | 1784 { |
1786 ASSERT(!target.isEmpty()); | 1785 ASSERT(!target.isEmpty()); |
1787 target.appendTo(m_target); | 1786 target.appendTo(m_target); |
1788 | 1787 |
1789 // FIXME: We'd like to tailor the searcher to fold quote marks for us instea
d | 1788 // FIXME: We'd like to tailor the searcher to fold quote marks for us instea
d |
1790 // of doing it in a separate replacement pass here, but ICU doesn't offer a
way | 1789 // of doing it in a separate replacement pass here, but ICU doesn't offer a
way |
1791 // to add tailoring on top of the locale-specific tailoring as of this writi
ng. | 1790 // to add tailoring on top of the locale-specific tailoring as of this writi
ng. |
1792 foldQuoteMarksAndSoftHyphens(m_target.data(), m_target.size()); | 1791 foldQuoteMarksAndSoftHyphens(m_target.data(), m_target.size()); |
1793 | 1792 |
1794 size_t targetLength = m_target.size(); | 1793 std::size_t targetLength = m_target.size(); |
1795 m_buffer.reserveInitialCapacity(max(targetLength * 8, minimumSearchBufferSiz
e)); | 1794 m_buffer.reserveInitialCapacity(std::max(targetLength * 8, minimumSearchBuff
erSize)); |
1796 m_overlap = m_buffer.capacity() / 4; | 1795 m_overlap = m_buffer.capacity() / 4; |
1797 | 1796 |
1798 if ((m_options & AtWordStarts) && targetLength) { | 1797 if ((m_options & AtWordStarts) && targetLength) { |
1799 UChar32 targetFirstCharacter; | 1798 UChar32 targetFirstCharacter; |
1800 U16_GET(m_target.data(), 0, 0, targetLength, targetFirstCharacter); | 1799 U16_GET(m_target.data(), 0, 0, targetLength, targetFirstCharacter); |
1801 // Characters in the separator category never really occur at the beginn
ing of a word, | 1800 // Characters in the separator category never really occur at the beginn
ing of a word, |
1802 // so if the target begins with such a character, we just ignore the AtW
ordStart option. | 1801 // so if the target begins with such a character, we just ignore the AtW
ordStart option. |
1803 if (isSeparator(targetFirstCharacter)) { | 1802 if (isSeparator(targetFirstCharacter)) { |
1804 m_options &= ~AtWordStarts; | 1803 m_options &= ~AtWordStarts; |
1805 m_needsMoreContext = false; | 1804 m_needsMoreContext = false; |
(...skipping 27 matching lines...) Expand all Loading... |
1833 { | 1832 { |
1834 // Leave the static object pointing to a valid string. | 1833 // Leave the static object pointing to a valid string. |
1835 UErrorCode status = U_ZERO_ERROR; | 1834 UErrorCode status = U_ZERO_ERROR; |
1836 usearch_setPattern(WebCore::searcher(), &newlineCharacter, 1, &status); | 1835 usearch_setPattern(WebCore::searcher(), &newlineCharacter, 1, &status); |
1837 ASSERT(status == U_ZERO_ERROR); | 1836 ASSERT(status == U_ZERO_ERROR); |
1838 | 1837 |
1839 unlockSearcher(); | 1838 unlockSearcher(); |
1840 } | 1839 } |
1841 | 1840 |
1842 template<typename CharType> | 1841 template<typename CharType> |
1843 inline void SearchBuffer::append(const CharType* characters, size_t length) | 1842 inline void SearchBuffer::append(const CharType* characters, std::size_t length) |
1844 { | 1843 { |
1845 ASSERT(length); | 1844 ASSERT(length); |
1846 | 1845 |
1847 if (m_atBreak) { | 1846 if (m_atBreak) { |
1848 m_buffer.shrink(0); | 1847 m_buffer.shrink(0); |
1849 m_prefixLength = 0; | 1848 m_prefixLength = 0; |
1850 m_atBreak = false; | 1849 m_atBreak = false; |
1851 } else if (m_buffer.size() == m_buffer.capacity()) { | 1850 } else if (m_buffer.size() == m_buffer.capacity()) { |
1852 memcpy(m_buffer.data(), m_buffer.data() + m_buffer.size() - m_overlap, m
_overlap * sizeof(UChar)); | 1851 memcpy(m_buffer.data(), m_buffer.data() + m_buffer.size() - m_overlap, m
_overlap * sizeof(UChar)); |
1853 m_prefixLength -= min(m_prefixLength, m_buffer.size() - m_overlap); | 1852 m_prefixLength -= std::min(m_prefixLength, m_buffer.size() - m_overlap); |
1854 m_buffer.shrink(m_overlap); | 1853 m_buffer.shrink(m_overlap); |
1855 } | 1854 } |
1856 | 1855 |
1857 size_t oldLength = m_buffer.size(); | 1856 std::size_t oldLength = m_buffer.size(); |
1858 size_t usableLength = min(m_buffer.capacity() - oldLength, length); | 1857 std::size_t usableLength = std::min(m_buffer.capacity() - oldLength, length)
; |
1859 ASSERT(usableLength); | 1858 ASSERT(usableLength); |
1860 m_buffer.resize(oldLength + usableLength); | 1859 m_buffer.resize(oldLength + usableLength); |
1861 UChar* destination = m_buffer.data() + oldLength; | 1860 UChar* destination = m_buffer.data() + oldLength; |
1862 StringImpl::copyChars(destination, characters, usableLength); | 1861 StringImpl::copyChars(destination, characters, usableLength); |
1863 foldQuoteMarksAndSoftHyphens(destination, usableLength); | 1862 foldQuoteMarksAndSoftHyphens(destination, usableLength); |
1864 m_numberOfCharactersJustAppended = usableLength; | 1863 m_numberOfCharactersJustAppended = usableLength; |
1865 } | 1864 } |
1866 | 1865 |
1867 inline bool SearchBuffer::needsMoreContext() const | 1866 inline bool SearchBuffer::needsMoreContext() const |
1868 { | 1867 { |
1869 return m_needsMoreContext; | 1868 return m_needsMoreContext; |
1870 } | 1869 } |
1871 | 1870 |
1872 inline void SearchBuffer::prependContext(const UChar* characters, size_t length) | 1871 inline void SearchBuffer::prependContext(const UChar* characters, std::size_t le
ngth) |
1873 { | 1872 { |
1874 ASSERT(m_needsMoreContext); | 1873 ASSERT(m_needsMoreContext); |
1875 ASSERT(m_prefixLength == m_buffer.size()); | 1874 ASSERT(m_prefixLength == m_buffer.size()); |
1876 | 1875 |
1877 if (!length) | 1876 if (!length) |
1878 return; | 1877 return; |
1879 | 1878 |
1880 m_atBreak = false; | 1879 m_atBreak = false; |
1881 | 1880 |
1882 size_t wordBoundaryContextStart = length; | 1881 std::size_t wordBoundaryContextStart = length; |
1883 if (wordBoundaryContextStart) { | 1882 if (wordBoundaryContextStart) { |
1884 U16_BACK_1(characters, 0, wordBoundaryContextStart); | 1883 U16_BACK_1(characters, 0, wordBoundaryContextStart); |
1885 wordBoundaryContextStart = startOfLastWordBoundaryContext(characters, wo
rdBoundaryContextStart); | 1884 wordBoundaryContextStart = startOfLastWordBoundaryContext(characters, wo
rdBoundaryContextStart); |
1886 } | 1885 } |
1887 | 1886 |
1888 size_t usableLength = min(m_buffer.capacity() - m_prefixLength, length - wor
dBoundaryContextStart); | 1887 std::size_t usableLength = std::min(m_buffer.capacity() - m_prefixLength, le
ngth - wordBoundaryContextStart); |
1889 m_buffer.prepend(characters + length - usableLength, usableLength); | 1888 m_buffer.prepend(characters + length - usableLength, usableLength); |
1890 m_prefixLength += usableLength; | 1889 m_prefixLength += usableLength; |
1891 | 1890 |
1892 if (wordBoundaryContextStart || m_prefixLength == m_buffer.capacity()) | 1891 if (wordBoundaryContextStart || m_prefixLength == m_buffer.capacity()) |
1893 m_needsMoreContext = false; | 1892 m_needsMoreContext = false; |
1894 } | 1893 } |
1895 | 1894 |
1896 inline bool SearchBuffer::atBreak() const | 1895 inline bool SearchBuffer::atBreak() const |
1897 { | 1896 { |
1898 return m_atBreak; | 1897 return m_atBreak; |
1899 } | 1898 } |
1900 | 1899 |
1901 inline void SearchBuffer::reachedBreak() | 1900 inline void SearchBuffer::reachedBreak() |
1902 { | 1901 { |
1903 m_atBreak = true; | 1902 m_atBreak = true; |
1904 } | 1903 } |
1905 | 1904 |
1906 inline bool SearchBuffer::isBadMatch(const UChar* match, size_t matchLength) con
st | 1905 inline bool SearchBuffer::isBadMatch(const UChar* match, std::size_t matchLength
) const |
1907 { | 1906 { |
1908 // This function implements the kana workaround. If usearch treats | 1907 // This function implements the kana workaround. If usearch treats |
1909 // it as a match, but we do not want to, then it's a "bad match". | 1908 // it as a match, but we do not want to, then it's a "bad match". |
1910 if (!m_targetRequiresKanaWorkaround) | 1909 if (!m_targetRequiresKanaWorkaround) |
1911 return false; | 1910 return false; |
1912 | 1911 |
1913 // Normalize into a match buffer. We reuse a single buffer rather than | 1912 // Normalize into a match buffer. We reuse a single buffer rather than |
1914 // creating a new one each time. | 1913 // creating a new one each time. |
1915 normalizeCharactersIntoNFCForm(match, matchLength, m_normalizedMatch); | 1914 normalizeCharactersIntoNFCForm(match, matchLength, m_normalizedMatch); |
1916 | 1915 |
1917 return !checkOnlyKanaLettersInStrings(m_normalizedTarget.begin(), m_normaliz
edTarget.size(), m_normalizedMatch.begin(), m_normalizedMatch.size()); | 1916 return !checkOnlyKanaLettersInStrings(m_normalizedTarget.begin(), m_normaliz
edTarget.size(), m_normalizedMatch.begin(), m_normalizedMatch.size()); |
1918 } | 1917 } |
1919 | 1918 |
1920 inline bool SearchBuffer::isWordStartMatch(size_t start, size_t length) const | 1919 inline bool SearchBuffer::isWordStartMatch(std::size_t start, std::size_t length
) const |
1921 { | 1920 { |
1922 ASSERT(m_options & AtWordStarts); | 1921 ASSERT(m_options & AtWordStarts); |
1923 | 1922 |
1924 if (!start) | 1923 if (!start) |
1925 return true; | 1924 return true; |
1926 | 1925 |
1927 int size = m_buffer.size(); | 1926 int size = m_buffer.size(); |
1928 int offset = start; | 1927 int offset = start; |
1929 UChar32 firstCharacter; | 1928 UChar32 firstCharacter; |
1930 U16_GET(m_buffer.data(), 0, offset, size, firstCharacter); | 1929 U16_GET(m_buffer.data(), 0, offset, size, firstCharacter); |
(...skipping 28 matching lines...) Expand all Loading... |
1959 // except after an uppercase. ("org" in "webkit.org", but not "ore"
in "WebCore"). | 1958 // except after an uppercase. ("org" in "webkit.org", but not "ore"
in "WebCore"). |
1960 return true; | 1959 return true; |
1961 } | 1960 } |
1962 } | 1961 } |
1963 | 1962 |
1964 // Chinese and Japanese lack word boundary marks, and there is no clear agre
ement on what constitutes | 1963 // Chinese and Japanese lack word boundary marks, and there is no clear agre
ement on what constitutes |
1965 // a word, so treat the position before any CJK character as a word start. | 1964 // a word, so treat the position before any CJK character as a word start. |
1966 if (Character::isCJKIdeographOrSymbol(firstCharacter)) | 1965 if (Character::isCJKIdeographOrSymbol(firstCharacter)) |
1967 return true; | 1966 return true; |
1968 | 1967 |
1969 size_t wordBreakSearchStart = start + length; | 1968 std::size_t wordBreakSearchStart = start + length; |
1970 while (wordBreakSearchStart > start) | 1969 while (wordBreakSearchStart > start) |
1971 wordBreakSearchStart = findNextWordFromIndex(m_buffer.data(), m_buffer.s
ize(), wordBreakSearchStart, false /* backwards */); | 1970 wordBreakSearchStart = findNextWordFromIndex(m_buffer.data(), m_buffer.s
ize(), wordBreakSearchStart, false /* backwards */); |
1972 return wordBreakSearchStart == start; | 1971 return wordBreakSearchStart == start; |
1973 } | 1972 } |
1974 | 1973 |
1975 inline size_t SearchBuffer::search(size_t& start) | 1974 inline std::size_t SearchBuffer::search(std::size_t& start) |
1976 { | 1975 { |
1977 size_t size = m_buffer.size(); | 1976 std::size_t size = m_buffer.size(); |
1978 if (m_atBreak) { | 1977 if (m_atBreak) { |
1979 if (!size) | 1978 if (!size) |
1980 return 0; | 1979 return 0; |
1981 } else { | 1980 } else { |
1982 if (size != m_buffer.capacity()) | 1981 if (size != m_buffer.capacity()) |
1983 return 0; | 1982 return 0; |
1984 } | 1983 } |
1985 | 1984 |
1986 UStringSearch* searcher = WebCore::searcher(); | 1985 UStringSearch* searcher = WebCore::searcher(); |
1987 | 1986 |
1988 UErrorCode status = U_ZERO_ERROR; | 1987 UErrorCode status = U_ZERO_ERROR; |
1989 usearch_setText(searcher, m_buffer.data(), size, &status); | 1988 usearch_setText(searcher, m_buffer.data(), size, &status); |
1990 ASSERT(status == U_ZERO_ERROR); | 1989 ASSERT(status == U_ZERO_ERROR); |
1991 | 1990 |
1992 usearch_setOffset(searcher, m_prefixLength, &status); | 1991 usearch_setOffset(searcher, m_prefixLength, &status); |
1993 ASSERT(status == U_ZERO_ERROR); | 1992 ASSERT(status == U_ZERO_ERROR); |
1994 | 1993 |
1995 int matchStart = usearch_next(searcher, &status); | 1994 int matchStart = usearch_next(searcher, &status); |
1996 ASSERT(status == U_ZERO_ERROR); | 1995 ASSERT(status == U_ZERO_ERROR); |
1997 | 1996 |
1998 nextMatch: | 1997 nextMatch: |
1999 if (!(matchStart >= 0 && static_cast<size_t>(matchStart) < size)) { | 1998 if (!(matchStart >= 0 && static_cast<std::size_t>(matchStart) < size)) { |
2000 ASSERT(matchStart == USEARCH_DONE); | 1999 ASSERT(matchStart == USEARCH_DONE); |
2001 return 0; | 2000 return 0; |
2002 } | 2001 } |
2003 | 2002 |
2004 // Matches that start in the overlap area are only tentative. | 2003 // Matches that start in the overlap area are only tentative. |
2005 // The same match may appear later, matching more characters, | 2004 // The same match may appear later, matching more characters, |
2006 // possibly including a combining character that's not yet in the buffer. | 2005 // possibly including a combining character that's not yet in the buffer. |
2007 if (!m_atBreak && static_cast<size_t>(matchStart) >= size - m_overlap) { | 2006 if (!m_atBreak && static_cast<std::size_t>(matchStart) >= size - m_overlap)
{ |
2008 size_t overlap = m_overlap; | 2007 std::size_t overlap = m_overlap; |
2009 if (m_options & AtWordStarts) { | 2008 if (m_options & AtWordStarts) { |
2010 // Ensure that there is sufficient context before matchStart the nex
t time around for | 2009 // Ensure that there is sufficient context before matchStart the nex
t time around for |
2011 // determining if it is at a word boundary. | 2010 // determining if it is at a word boundary. |
2012 int wordBoundaryContextStart = matchStart; | 2011 int wordBoundaryContextStart = matchStart; |
2013 U16_BACK_1(m_buffer.data(), 0, wordBoundaryContextStart); | 2012 U16_BACK_1(m_buffer.data(), 0, wordBoundaryContextStart); |
2014 wordBoundaryContextStart = startOfLastWordBoundaryContext(m_buffer.d
ata(), wordBoundaryContextStart); | 2013 wordBoundaryContextStart = startOfLastWordBoundaryContext(m_buffer.d
ata(), wordBoundaryContextStart); |
2015 overlap = min(size - 1, max(overlap, size - wordBoundaryContextStart
)); | 2014 overlap = std::min(size - 1, std::max(overlap, size - wordBoundaryCo
ntextStart)); |
2016 } | 2015 } |
2017 memcpy(m_buffer.data(), m_buffer.data() + size - overlap, overlap * size
of(UChar)); | 2016 memcpy(m_buffer.data(), m_buffer.data() + size - overlap, overlap * size
of(UChar)); |
2018 m_prefixLength -= min(m_prefixLength, size - overlap); | 2017 m_prefixLength -= std::min(m_prefixLength, size - overlap); |
2019 m_buffer.shrink(overlap); | 2018 m_buffer.shrink(overlap); |
2020 return 0; | 2019 return 0; |
2021 } | 2020 } |
2022 | 2021 |
2023 size_t matchedLength = usearch_getMatchedLength(searcher); | 2022 std::size_t matchedLength = usearch_getMatchedLength(searcher); |
2024 ASSERT_WITH_SECURITY_IMPLICATION(matchStart + matchedLength <= size); | 2023 ASSERT_WITH_SECURITY_IMPLICATION(matchStart + matchedLength <= size); |
2025 | 2024 |
2026 // If this match is "bad", move on to the next match. | 2025 // If this match is "bad", move on to the next match. |
2027 if (isBadMatch(m_buffer.data() + matchStart, matchedLength) || ((m_options &
AtWordStarts) && !isWordStartMatch(matchStart, matchedLength))) { | 2026 if (isBadMatch(m_buffer.data() + matchStart, matchedLength) || ((m_options &
AtWordStarts) && !isWordStartMatch(matchStart, matchedLength))) { |
2028 matchStart = usearch_next(searcher, &status); | 2027 matchStart = usearch_next(searcher, &status); |
2029 ASSERT(status == U_ZERO_ERROR); | 2028 ASSERT(status == U_ZERO_ERROR); |
2030 goto nextMatch; | 2029 goto nextMatch; |
2031 } | 2030 } |
2032 | 2031 |
2033 size_t newSize = size - (matchStart + 1); | 2032 std::size_t newSize = size - (matchStart + 1); |
2034 memmove(m_buffer.data(), m_buffer.data() + matchStart + 1, newSize * sizeof(
UChar)); | 2033 memmove(m_buffer.data(), m_buffer.data() + matchStart + 1, newSize * sizeof(
UChar)); |
2035 m_prefixLength -= min<size_t>(m_prefixLength, matchStart + 1); | 2034 m_prefixLength -= std::min<std::size_t>(m_prefixLength, matchStart + 1); |
2036 m_buffer.shrink(newSize); | 2035 m_buffer.shrink(newSize); |
2037 | 2036 |
2038 start = size - matchStart; | 2037 start = size - matchStart; |
2039 return matchedLength; | 2038 return matchedLength; |
2040 } | 2039 } |
2041 | 2040 |
2042 // -------- | 2041 // -------- |
2043 | 2042 |
2044 int TextIterator::rangeLength(const Range* r, bool forSelectionPreservation) | 2043 int TextIterator::rangeLength(const Range* r, bool forSelectionPreservation) |
2045 { | 2044 { |
(...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
2088 return result.release(); | 2087 return result.release(); |
2089 } | 2088 } |
2090 | 2089 |
2091 // Check if there's any unpaird surrogate code point. | 2090 // Check if there's any unpaird surrogate code point. |
2092 // Non-character code points are not checked. | 2091 // Non-character code points are not checked. |
2093 static bool isValidUTF16(const String& s) | 2092 static bool isValidUTF16(const String& s) |
2094 { | 2093 { |
2095 if (s.is8Bit()) | 2094 if (s.is8Bit()) |
2096 return true; | 2095 return true; |
2097 const UChar* ustr = s.characters16(); | 2096 const UChar* ustr = s.characters16(); |
2098 size_t length = s.length(); | 2097 std::size_t length = s.length(); |
2099 size_t position = 0; | 2098 std::size_t position = 0; |
2100 while (position < length) { | 2099 while (position < length) { |
2101 UChar32 character; | 2100 UChar32 character; |
2102 U16_NEXT(ustr, position, length, character); | 2101 U16_NEXT(ustr, position, length, character); |
2103 if (U_IS_SURROGATE(character)) | 2102 if (U_IS_SURROGATE(character)) |
2104 return false; | 2103 return false; |
2105 } | 2104 } |
2106 return true; | 2105 return true; |
2107 } | 2106 } |
2108 | 2107 |
2109 static size_t findPlainTextInternal(CharacterIterator& it, const String& target,
FindOptions options, size_t& matchStart) | 2108 static std::size_t findPlainTextInternal(CharacterIterator& it, const String& ta
rget, FindOptions options, std::size_t& matchStart) |
2110 { | 2109 { |
2111 matchStart = 0; | 2110 matchStart = 0; |
2112 size_t matchLength = 0; | 2111 std::size_t matchLength = 0; |
2113 | 2112 |
2114 if (!isValidUTF16(target)) | 2113 if (!isValidUTF16(target)) |
2115 return 0; | 2114 return 0; |
2116 | 2115 |
2117 SearchBuffer buffer(target, options); | 2116 SearchBuffer buffer(target, options); |
2118 | 2117 |
2119 if (buffer.needsMoreContext()) { | 2118 if (buffer.needsMoreContext()) { |
2120 RefPtrWillBeRawPtr<Range> startRange = it.range(); | 2119 RefPtrWillBeRawPtr<Range> startRange = it.range(); |
2121 RefPtrWillBeRawPtr<Range> beforeStartRange = startRange->ownerDocument()
.createRange(); | 2120 RefPtrWillBeRawPtr<Range> beforeStartRange = startRange->ownerDocument()
.createRange(); |
2122 beforeStartRange->setEnd(startRange->startContainer(), startRange->start
Offset(), IGNORE_EXCEPTION); | 2121 beforeStartRange->setEnd(startRange->startContainer(), startRange->start
Offset(), IGNORE_EXCEPTION); |
2123 for (SimplifiedBackwardsTextIterator backwardsIterator(beforeStartRange.
get()); !backwardsIterator.atEnd(); backwardsIterator.advance()) { | 2122 for (SimplifiedBackwardsTextIterator backwardsIterator(beforeStartRange.
get()); !backwardsIterator.atEnd(); backwardsIterator.advance()) { |
2124 Vector<UChar, 1024> characters; | 2123 Vector<UChar, 1024> characters; |
2125 backwardsIterator.prependTextTo(characters); | 2124 backwardsIterator.prependTextTo(characters); |
2126 buffer.prependContext(characters.data(), characters.size()); | 2125 buffer.prependContext(characters.data(), characters.size()); |
2127 if (!buffer.needsMoreContext()) | 2126 if (!buffer.needsMoreContext()) |
2128 break; | 2127 break; |
2129 } | 2128 } |
2130 } | 2129 } |
2131 | 2130 |
2132 while (!it.atEnd()) { | 2131 while (!it.atEnd()) { |
2133 it.appendTextTo(buffer); | 2132 it.appendTextTo(buffer); |
2134 it.advance(buffer.numberOfCharactersJustAppended()); | 2133 it.advance(buffer.numberOfCharactersJustAppended()); |
2135 tryAgain: | 2134 tryAgain: |
2136 size_t matchStartOffset; | 2135 std::size_t matchStartOffset; |
2137 if (size_t newMatchLength = buffer.search(matchStartOffset)) { | 2136 if (std::size_t newMatchLength = buffer.search(matchStartOffset)) { |
2138 // Note that we found a match, and where we found it. | 2137 // Note that we found a match, and where we found it. |
2139 size_t lastCharacterInBufferOffset = it.characterOffset(); | 2138 std::size_t lastCharacterInBufferOffset = it.characterOffset(); |
2140 ASSERT(lastCharacterInBufferOffset >= matchStartOffset); | 2139 ASSERT(lastCharacterInBufferOffset >= matchStartOffset); |
2141 matchStart = lastCharacterInBufferOffset - matchStartOffset; | 2140 matchStart = lastCharacterInBufferOffset - matchStartOffset; |
2142 matchLength = newMatchLength; | 2141 matchLength = newMatchLength; |
2143 // If searching forward, stop on the first match. | 2142 // If searching forward, stop on the first match. |
2144 // If searching backward, don't stop, so we end up with the last mat
ch. | 2143 // If searching backward, don't stop, so we end up with the last mat
ch. |
2145 if (!(options & Backwards)) | 2144 if (!(options & Backwards)) |
2146 break; | 2145 break; |
2147 goto tryAgain; | 2146 goto tryAgain; |
2148 } | 2147 } |
2149 if (it.atBreak() && !buffer.atBreak()) { | 2148 if (it.atBreak() && !buffer.atBreak()) { |
2150 buffer.reachedBreak(); | 2149 buffer.reachedBreak(); |
2151 goto tryAgain; | 2150 goto tryAgain; |
2152 } | 2151 } |
2153 } | 2152 } |
2154 | 2153 |
2155 return matchLength; | 2154 return matchLength; |
2156 } | 2155 } |
2157 | 2156 |
2158 static const TextIteratorBehaviorFlags iteratorFlagsForFindPlainText = TextItera
torEntersTextControls | TextIteratorEntersAuthorShadowRoots; | 2157 static const TextIteratorBehaviorFlags iteratorFlagsForFindPlainText = TextItera
torEntersTextControls | TextIteratorEntersAuthorShadowRoots; |
2159 | 2158 |
2160 PassRefPtrWillBeRawPtr<Range> findPlainText(const Range* range, const String& ta
rget, FindOptions options) | 2159 PassRefPtrWillBeRawPtr<Range> findPlainText(const Range* range, const String& ta
rget, FindOptions options) |
2161 { | 2160 { |
2162 // CharacterIterator requires renderers to be up-to-date | 2161 // CharacterIterator requires renderers to be up-to-date |
2163 range->ownerDocument().updateLayout(); | 2162 range->ownerDocument().updateLayout(); |
2164 | 2163 |
2165 // First, find the text. | 2164 // First, find the text. |
2166 size_t matchStart; | 2165 std::size_t matchStart; |
2167 size_t matchLength; | 2166 std::size_t matchLength; |
2168 { | 2167 { |
2169 CharacterIterator findIterator(range, iteratorFlagsForFindPlainText); | 2168 CharacterIterator findIterator(range, iteratorFlagsForFindPlainText); |
2170 matchLength = findPlainTextInternal(findIterator, target, options, match
Start); | 2169 matchLength = findPlainTextInternal(findIterator, target, options, match
Start); |
2171 if (!matchLength) | 2170 if (!matchLength) |
2172 return collapsedToBoundary(range, !(options & Backwards)); | 2171 return collapsedToBoundary(range, !(options & Backwards)); |
2173 } | 2172 } |
2174 | 2173 |
2175 // Then, find the document position of the start and the end of the text. | 2174 // Then, find the document position of the start and the end of the text. |
2176 CharacterIterator computeRangeIterator(range, iteratorFlagsForFindPlainText)
; | 2175 CharacterIterator computeRangeIterator(range, iteratorFlagsForFindPlainText)
; |
2177 Position resultStart; | 2176 Position resultStart; |
2178 Position resultEnd; | 2177 Position resultEnd; |
2179 calculateCharacterSubrange(computeRangeIterator, matchStart, matchLength, re
sultStart, resultEnd); | 2178 calculateCharacterSubrange(computeRangeIterator, matchStart, matchLength, re
sultStart, resultEnd); |
2180 return Range::create(range->ownerDocument(), resultStart, resultEnd); | 2179 return Range::create(range->ownerDocument(), resultStart, resultEnd); |
2181 } | 2180 } |
2182 | 2181 |
2183 void findPlainText(const Position& inputStart, const Position& inputEnd, const S
tring& target, FindOptions options, Position& resultStart, Position& resultEnd) | 2182 void findPlainText(const Position& inputStart, const Position& inputEnd, const S
tring& target, FindOptions options, Position& resultStart, Position& resultEnd) |
2184 { | 2183 { |
2185 resultStart.clear(); | 2184 resultStart.clear(); |
2186 resultEnd.clear(); | 2185 resultEnd.clear(); |
2187 // CharacterIterator requires renderers to be up-to-date. | 2186 // CharacterIterator requires renderers to be up-to-date. |
2188 if (!inputStart.inDocument()) | 2187 if (!inputStart.inDocument()) |
2189 return; | 2188 return; |
2190 ASSERT(inputStart.document() == inputEnd.document()); | 2189 ASSERT(inputStart.document() == inputEnd.document()); |
2191 inputStart.document()->updateLayout(); | 2190 inputStart.document()->updateLayout(); |
2192 | 2191 |
2193 // FIXME: Reduce the code duplication with above (but how?). | 2192 // FIXME: Reduce the code duplication with above (but how?). |
2194 size_t matchStart; | 2193 std::size_t matchStart; |
2195 size_t matchLength; | 2194 std::size_t matchLength; |
2196 { | 2195 { |
2197 CharacterIterator findIterator(inputStart, inputEnd, iteratorFlagsForFin
dPlainText); | 2196 CharacterIterator findIterator(inputStart, inputEnd, iteratorFlagsForFin
dPlainText); |
2198 matchLength = findPlainTextInternal(findIterator, target, options, match
Start); | 2197 matchLength = findPlainTextInternal(findIterator, target, options, match
Start); |
2199 if (!matchLength) { | 2198 if (!matchLength) { |
2200 const Position& collapseTo = options & Backwards ? inputStart : inpu
tEnd; | 2199 const Position& collapseTo = options & Backwards ? inputStart : inpu
tEnd; |
2201 resultStart = collapseTo; | 2200 resultStart = collapseTo; |
2202 resultEnd = collapseTo; | 2201 resultEnd = collapseTo; |
2203 return; | 2202 return; |
2204 } | 2203 } |
2205 } | 2204 } |
2206 | 2205 |
2207 CharacterIterator computeRangeIterator(inputStart, inputEnd, iteratorFlagsFo
rFindPlainText); | 2206 CharacterIterator computeRangeIterator(inputStart, inputEnd, iteratorFlagsFo
rFindPlainText); |
2208 calculateCharacterSubrange(computeRangeIterator, matchStart, matchLength, re
sultStart, resultEnd); | 2207 calculateCharacterSubrange(computeRangeIterator, matchStart, matchLength, re
sultStart, resultEnd); |
2209 } | 2208 } |
2210 | 2209 |
2211 } | 2210 } |
OLD | NEW |