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 |
(...skipping 542 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); |
(...skipping 1082 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 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 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
1843 inline void SearchBuffer::append(const CharType* characters, size_t length) | 1842 inline void SearchBuffer::append(const CharType* characters, 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 size_t oldLength = m_buffer.size(); |
1858 size_t usableLength = min(m_buffer.capacity() - oldLength, length); | 1857 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, size_t length) |
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 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 size_t usableLength = std::min(m_buffer.capacity() - m_prefixLength, length
- 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; |
(...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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<size_t>(matchStart) >= size - m_overlap) { |
2008 size_t overlap = m_overlap; | 2007 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 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 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<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 156 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
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 |