Back to index

lightning-sunbird  0.9+nobinonly
Functions
nsScannerString.cpp File Reference
#include <stdlib.h>
#include "nsScannerString.h"

Go to the source code of this file.

Functions

void CopyUnicodeTo (const nsScannerIterator &aSrcStart, const nsScannerIterator &aSrcEnd, nsAString &aDest)
 utils -- based on code from nsReadableUtils.cpp
void AppendUnicodeTo (const nsScannerIterator &aSrcStart, const nsScannerIterator &aSrcEnd, nsScannerSharedSubstring &aDest)
void AppendUnicodeTo (const nsScannerIterator &aSrcStart, const nsScannerIterator &aSrcEnd, nsAString &aDest)
PRBool FindCharInReadable (PRUnichar aChar, nsScannerIterator &aSearchStart, const nsScannerIterator &aSearchEnd)
PRBool FindInReadable (const nsAString &aPattern, nsScannerIterator &aSearchStart, nsScannerIterator &aSearchEnd, const nsStringComparator &compare)
PRBool RFindInReadable (const nsAString &aPattern, nsScannerIterator &aSearchStart, nsScannerIterator &aSearchEnd, const nsStringComparator &aComparator)
 This implementation is simple, but does too much work.

Function Documentation

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:

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 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:

PRBool FindCharInReadable ( PRUnichar  aChar,
nsScannerIterator aSearchStart,
const nsScannerIterator aSearchEnd 
)

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 aSearchStart,
nsScannerIterator aSearchEnd,
const nsStringComparator &  compare 
)

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 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: