Changeset 65100 in webkit


Ignore:
Timestamp:
Aug 10, 2010 4:46:54 PM (14 years ago)
Author:
abarth@webkit.org
Message:

2010-08-10 Adam Barth <abarth@webkit.org>

Reviewed by Eric Seidel.

SegmentedString should keep track of how many characters it consumes
https://bugs.webkit.org/show_bug.cgi?id=43765

The easiest way to keep track of how many characters we've consumed
would be to increment a counter every time we advance, but that's too
slow. Instead, we keep track lazily and update our summary durning the
slow case of advance (which is sufficiently rare).

There's some subtly to how this works w.r.t. "unconsuming" characters
by pushing them back on the front of the segmented string. This isn't
really a "right answer" here because the notion of unconsuming input
isn't present in the API. This patch makes some assumptions about how
clients of this class use its API. In a future patch, we might want to
rename some of the method names to make this more explicit.

  • platform/text/SegmentedString.cpp: (WebCore::SegmentedString::operator=): (WebCore::SegmentedString::append): (WebCore::SegmentedString::prepend): (WebCore::SegmentedString::advanceSubstring):
  • platform/text/SegmentedString.h: (WebCore::SegmentedSubstring::numberOfCharactersConsumed): (WebCore::SegmentedString::SegmentedString): (WebCore::SegmentedString::numberOfCharactersConsumed):
Location:
trunk/WebCore
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/WebCore/ChangeLog

    r65099 r65100  
     12010-08-10  Adam Barth  <abarth@webkit.org>
     2
     3        Reviewed by Eric Seidel.
     4
     5        SegmentedString should keep track of how many characters it consumes
     6        https://bugs.webkit.org/show_bug.cgi?id=43765
     7
     8        The easiest way to keep track of how many characters we've consumed
     9        would be to increment a counter every time we advance, but that's too
     10        slow.  Instead, we keep track lazily and update our summary durning the
     11        slow case of advance (which is sufficiently rare).
     12
     13        There's some subtly to how this works w.r.t. "unconsuming" characters
     14        by pushing them back on the front of the segmented string.  This isn't
     15        really a "right answer" here because the notion of unconsuming input
     16        isn't present in the API.  This patch makes some assumptions about how
     17        clients of this class use its API.  In a future patch, we might want to
     18        rename some of the method names to make this more explicit.
     19
     20        * platform/text/SegmentedString.cpp:
     21        (WebCore::SegmentedString::operator=):
     22        (WebCore::SegmentedString::append):
     23        (WebCore::SegmentedString::prepend):
     24        (WebCore::SegmentedString::advanceSubstring):
     25        * platform/text/SegmentedString.h:
     26        (WebCore::SegmentedSubstring::numberOfCharactersConsumed):
     27        (WebCore::SegmentedString::SegmentedString):
     28        (WebCore::SegmentedString::numberOfCharactersConsumed):
     29
    1302010-08-10  Gavin Barraclough  <barraclough@apple.com>
    231
  • trunk/WebCore/platform/text/SegmentedString.cpp

    r62172 r65100  
    5353        m_currentChar = other.m_currentChar;
    5454    m_closed = other.m_closed;
     55    m_numberOfCharactersConsumedPriorToCurrentString = other.m_numberOfCharactersConsumedPriorToCurrentString;
    5556    return *this;
    5657}
     
    100101    if (s.m_length) {
    101102        if (!m_currentString.m_length) {
     103            m_numberOfCharactersConsumedPriorToCurrentString += m_currentString.numberOfCharactersConsumed();
    102104            m_currentString = s;
    103105        } else {
     
    111113{
    112114    ASSERT(!escaped());
     115    // FIXME: Add this ASSERT once we've deleted the LegacyHTMLDocumentParser.
     116    // ASSERT(!s.numberOfCharactersConsumed());
    113117    if (s.m_length) {
     118        // FIXME: We're assuming that the prepend were originally consumed by
     119        //        this SegmentedString.  We're also ASSERTing that s is a fresh
     120        //        SegmentedSubstring.  These assumptions are sufficient for our
     121        //        current use, but we might need to handle the more elaborate
     122        //        cases in the future.
     123        m_numberOfCharactersConsumedPriorToCurrentString += m_currentString.numberOfCharactersConsumed();
     124        m_numberOfCharactersConsumedPriorToCurrentString -= s.m_length;
    114125        if (!m_currentString.m_length)
    115126            m_currentString = s;
     
    161172{
    162173    if (m_composite) {
     174        m_numberOfCharactersConsumedPriorToCurrentString += m_currentString.numberOfCharactersConsumed();
    163175        m_currentString = m_substrings.takeFirst();
     176        // If we've previously consumed some characters of the non-current
     177        // string, we now account for those characters as part of the current
     178        // string, not as part of "prior to current string."
     179        m_numberOfCharactersConsumedPriorToCurrentString -= m_currentString.numberOfCharactersConsumed();
    164180        if (m_substrings.isEmpty())
    165181            m_composite = false;
  • trunk/WebCore/platform/text/SegmentedString.h

    r65031 r65100  
    4646    void setExcludeLineNumbers() { m_doNotExcludeLineNumbers = false; }
    4747
     48    int numberOfCharactersConsumed() const { return m_string.length() - m_length; }
     49
    4850    void appendTo(String& str) const
    4951    {
     
    7072public:
    7173    SegmentedString()
    72         : m_pushedChar1(0), m_pushedChar2(0), m_currentChar(0), m_composite(false), m_closed(false) {}
     74        : m_pushedChar1(0)
     75        , m_pushedChar2(0)
     76        , m_currentChar(0)
     77        , m_numberOfCharactersConsumedPriorToCurrentString(0)
     78        , m_composite(false)
     79        , m_closed(false)
     80    {
     81    }
     82
    7383    SegmentedString(const String& str)
    74         : m_pushedChar1(0), m_pushedChar2(0), m_currentString(str)
    75         , m_currentChar(m_currentString.m_current), m_composite(false), m_closed(false) {}
     84        : m_pushedChar1(0)
     85        , m_pushedChar2(0)
     86        , m_currentString(str)
     87        , m_currentChar(m_currentString.m_current)
     88        , m_numberOfCharactersConsumedPriorToCurrentString(0)
     89        , m_composite(false)
     90        , m_closed(false)
     91    {
     92    }
     93
    7694    SegmentedString(const SegmentedString&);
    7795
     
    173191
    174192    bool escaped() const { return m_pushedChar1; }
     193
     194    int numberOfCharactersConsumed()
     195    {
     196        // We don't currently handle the case when there are pushed character.
     197        ASSERT(!m_pushedChar1);
     198        return m_numberOfCharactersConsumedPriorToCurrentString + m_currentString.numberOfCharactersConsumed();
     199    }
    175200
    176201    String toString() const;
     
    222247    SegmentedSubstring m_currentString;
    223248    const UChar* m_currentChar;
     249    int m_numberOfCharactersConsumedPriorToCurrentString;
    224250    Deque<SegmentedSubstring> m_substrings;
    225251    bool m_composite;
Note: See TracChangeset for help on using the changeset viewer.