Back to index

lightning-sunbird  0.9+nobinonly
Classes | Functions
nsScannerString.h File Reference
#include "nsString.h"
#include "nsUnicharUtils.h"
#include "prclist.h"
This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Classes

class  nsScannerBufferList
 nsScannerBufferList More...
class  nsScannerBufferList::Buffer
 Buffer objects are directly followed by a data segment. More...
class  nsScannerBufferList::Position
 Position objects serve as lightweight pointers into a buffer list. More...
struct  nsScannerFragment
 nsScannerFragment represents a "slice" of a Buffer object. More...
class  nsScannerSubstring
 nsScannerSubstring is the base class for nsScannerString. More...
class  nsScannerString
 nsScannerString provides methods to grow and modify a buffer list. More...
class  nsScannerSharedSubstring
 nsScannerSharedSubstring implements copy-on-write semantics for nsScannerSubstring. More...
class  nsScannerIterator
 nsScannerIterator works just like nsReadingIterator<CharT> except that it knows how to iterate over a list of scanner buffers. More...
struct  nsCharSourceTraits< nsScannerIterator >
 this class is needed in order to make use of the methods in nsAlgorithm.h More...

Functions

PRBool SameFragment (const nsScannerIterator &a, const nsScannerIterator &b)
PRBool operator== (const nsScannerIterator &lhs, const nsScannerIterator &rhs)
PRBool operator!= (const nsScannerIterator &lhs, const nsScannerIterator &rhs)
size_t Distance (const nsScannerIterator &aStart, const nsScannerIterator &aEnd)
 scanner string utils
void CopyUnicodeTo (const nsScannerIterator &aSrcStart, const nsScannerIterator &aSrcEnd, nsAString &aDest)
 utils -- based on code from nsReadableUtils.cpp
void CopyUnicodeTo (const nsScannerSubstring &aSrc, nsAString &aDest)
void AppendUnicodeTo (const nsScannerIterator &aSrcStart, const nsScannerIterator &aSrcEnd, nsAString &aDest)
void AppendUnicodeTo (const nsScannerSubstring &aSrc, nsAString &aDest)
void AppendUnicodeTo (const nsScannerIterator &aSrcStart, const nsScannerIterator &aSrcEnd, nsScannerSharedSubstring &aDest)
PRBool FindCharInReadable (PRUnichar aChar, nsScannerIterator &aStart, const nsScannerIterator &aEnd)
PRBool FindInReadable (const nsAString &aPattern, nsScannerIterator &aStart, nsScannerIterator &aEnd, const nsStringComparator &=nsDefaultStringComparator())
PRBool RFindInReadable (const nsAString &aPattern, nsScannerIterator &aStart, nsScannerIterator &aEnd, const nsStringComparator &=nsDefaultStringComparator())
 This implementation is simple, but does too much work.
PRBool CaseInsensitiveFindInReadable (const nsAString &aPattern, nsScannerIterator &aStart, nsScannerIterator &aEnd)

Function Documentation

void AppendUnicodeTo ( const nsScannerIterator aSrcStart,
const nsScannerIterator aSrcEnd,
nsAString &  aDest 
)

Definition at line 519 of file nsScannerString.cpp.

  {
    nsAString::iterator writer;
    PRUint32 oldLength = aDest.Length();
    if (!EnsureStringLength(aDest, oldLength + Distance(aSrcStart, aSrcEnd)))
      return; // out of memory
    aDest.BeginWriting(writer).advance(oldLength);
    nsScannerIterator fromBegin(aSrcStart);
    
    copy_string(fromBegin, aSrcEnd, writer);
  }

Here is the call graph for this function:

Here is the caller graph for this function:

void AppendUnicodeTo ( const nsScannerSubstring aSrc,
nsAString &  aDest 
) [inline]

Definition at line 599 of file nsScannerString.h.

  {
    nsScannerIterator begin, end;
    AppendUnicodeTo(aSrc.BeginReading(begin), aSrc.EndReading(end), aDest);
  }

Here is the call graph for this function:

Definition at line 503 of file nsScannerString.cpp.

  {
    // Check whether we can just create a dependent string.
    if (aDest.str().IsEmpty()) {
      // We can just make |aDest| point to the buffer.
      // This will take care of copying if the buffer spans fragments.
      aDest.Rebind(aSrcStart, aSrcEnd);
    } else {
      // The dest string is not empty, so it can't be a dependent substring.
      AppendUnicodeTo(aSrcStart, aSrcEnd, aDest.writable());
    }
  }

Here is the call graph for this function:

PRBool CaseInsensitiveFindInReadable ( const nsAString &  aPattern,
nsScannerIterator aStart,
nsScannerIterator aEnd 
) [inline]

Definition at line 629 of file nsScannerString.h.

  {
    return FindInReadable(aPattern, aStart, aEnd,
                          nsCaseInsensitiveStringComparator());
  }

Here is the call graph for this function:

void CopyUnicodeTo ( const nsScannerIterator aSrcStart,
const nsScannerIterator aSrcEnd,
nsAString &  aDest 
)

utils -- based on code from nsReadableUtils.cpp

Definition at line 487 of file nsScannerString.cpp.

  {
    nsAString::iterator writer;
    if (!EnsureStringLength(aDest, Distance(aSrcStart, aSrcEnd))) {
      aDest.Truncate();
      return; // out of memory
    }
    aDest.BeginWriting(writer);
    nsScannerIterator fromBegin(aSrcStart);
    
    copy_string(fromBegin, aSrcEnd, writer);
  }

Here is the call graph for this function:

Here is the caller graph for this function:

void CopyUnicodeTo ( const nsScannerSubstring aSrc,
nsAString &  aDest 
) [inline]

Definition at line 586 of file nsScannerString.h.

  {
    nsScannerIterator begin, end;
    CopyUnicodeTo(aSrc.BeginReading(begin), aSrc.EndReading(end), aDest);
  }

Here is the call graph for this function:

size_t Distance ( const nsScannerIterator aStart,
const nsScannerIterator aEnd 
) [inline]

scanner string utils

These methods mimic the API provided by nsReadableUtils in xpcom/string. Here we provide only the methods that the htmlparser module needs.

Definition at line 573 of file nsScannerString.h.

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 534 of file nsScannerString.cpp.

  {
    while ( aSearchStart != aSearchEnd )
      {
        PRInt32 fragmentLength;
        if ( SameFragment(aSearchStart, aSearchEnd) ) 
          fragmentLength = aSearchEnd.get() - aSearchStart.get();
        else
          fragmentLength = aSearchStart.size_forward();

        const PRUnichar* charFoundAt = nsCharTraits<PRUnichar>::find(aSearchStart.get(), fragmentLength, aChar);
        if ( charFoundAt ) {
          aSearchStart.advance( charFoundAt - aSearchStart.get() );
          return PR_TRUE;
        }

        aSearchStart.advance(fragmentLength);
      }

    return PR_FALSE;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool FindInReadable ( const nsAString &  aPattern,
nsScannerIterator aStart,
nsScannerIterator aEnd,
const nsStringComparator &  = nsDefaultStringComparator() 
)

Definition at line 559 of file nsScannerString.cpp.

  {
    PRBool found_it = PR_FALSE;

      // only bother searching at all if we're given a non-empty range to search
    if ( aSearchStart != aSearchEnd )
      {
        nsAString::const_iterator aPatternStart, aPatternEnd;
        aPattern.BeginReading(aPatternStart);
        aPattern.EndReading(aPatternEnd);

          // outer loop keeps searching till we find it or run out of string to search
        while ( !found_it )
          {
              // fast inner loop (that's what it's called, not what it is) looks for a potential match
            while ( aSearchStart != aSearchEnd &&
                    compare(*aPatternStart, *aSearchStart) )
              ++aSearchStart;

              // if we broke out of the `fast' loop because we're out of string ... we're done: no match
            if ( aSearchStart == aSearchEnd )
              break;

              // otherwise, we're at a potential match, let's see if we really hit one
            nsAString::const_iterator testPattern(aPatternStart);
            nsScannerIterator testSearch(aSearchStart);

              // slow inner loop verifies the potential match (found by the `fast' loop) at the current position
            for(;;)
              {
                  // we already compared the first character in the outer loop,
                  //  so we'll advance before the next comparison
                ++testPattern;
                ++testSearch;

                  // if we verified all the way to the end of the pattern, then we found it!
                if ( testPattern == aPatternEnd )
                  {
                    found_it = PR_TRUE;
                    aSearchEnd = testSearch; // return the exact found range through the parameters
                    break;
                  }

                  // if we got to end of the string we're searching before we hit the end of the
                  //  pattern, we'll never find what we're looking for
                if ( testSearch == aSearchEnd )
                  {
                    aSearchStart = aSearchEnd;
                    break;
                  }

                  // else if we mismatched ... it's time to advance to the next search position
                  //  and get back into the `fast' loop
                if ( compare(*testPattern, *testSearch) )
                  {
                    ++aSearchStart;
                    break;
                  }
              }
          }
      }

    return found_it;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool operator!= ( const nsScannerIterator lhs,
const nsScannerIterator rhs 
) [inline]

Definition at line 542 of file nsScannerString.h.

  {
    return lhs.get() != rhs.get();
  }

Here is the call graph for this function:

PRBool operator== ( const nsScannerIterator lhs,
const nsScannerIterator rhs 
) [inline]

Definition at line 535 of file nsScannerString.h.

  {
    return lhs.get() == rhs.get();
  }

Here is the call graph for this function:

Here is the caller graph for this function:

PRBool RFindInReadable ( const nsAString &  aPattern,
nsScannerIterator aSearchStart,
nsScannerIterator aSearchEnd,
const nsStringComparator &  aComparator 
)

This implementation is simple, but does too much work.

It searches the entire string from left to right, and returns the last match found, if any. This implementation will be replaced when I get |reverse_iterator|s working.

Definition at line 633 of file nsScannerString.cpp.

  {
    PRBool found_it = PR_FALSE;

    nsScannerIterator savedSearchEnd(aSearchEnd);
    nsScannerIterator searchStart(aSearchStart), searchEnd(aSearchEnd);

    while ( searchStart != searchEnd )
      {
        if ( FindInReadable(aPattern, searchStart, searchEnd, aComparator) )
          {
            found_it = PR_TRUE;

              // this is the best match so far, so remember it
            aSearchStart = searchStart;
            aSearchEnd = searchEnd;

              // ...and get ready to search some more
              //  (it's tempting to set |searchStart=searchEnd| ... but that misses overlapping patterns)
            ++searchStart;
            searchEnd = savedSearchEnd;
          }
      }

      // if we never found it, return an empty range
    if ( !found_it )
      aSearchStart = aSearchEnd;

    return found_it;
  }

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 476 of file nsScannerString.h.

Here is the call graph for this function:

Here is the caller graph for this function: