Back to index

lightning-sunbird  0.9+nobinonly
Classes | Functions | Variables
nsReadableUtils.cpp File Reference
#include "nsReadableUtils.h"
#include "nsMemory.h"
#include "nsString.h"
#include "nsUTF8Utils.h"

Go to the source code of this file.

Classes

class  ConvertToUpperCase
 A character sink for in-place case conversion. More...
class  CopyToUpperCase
 A character sink for copying with case conversion. More...
class  ConvertToLowerCase
 A character sink for case conversion. More...
class  CopyToLowerCase
 A character sink for copying with case conversion. More...

Functions

NS_COM void LossyCopyUTF16toASCII (const nsAString &aSource, nsACString &aDest)
NS_COM void CopyASCIItoUTF16 (const nsACString &aSource, nsAString &aDest)
NS_COM void LossyCopyUTF16toASCII (const PRUnichar *aSource, nsACString &aDest)
NS_COM void CopyASCIItoUTF16 (const char *aSource, nsAString &aDest)
NS_COM void CopyUTF16toUTF8 (const nsAString &aSource, nsACString &aDest)
NS_COM void CopyUTF8toUTF16 (const nsACString &aSource, nsAString &aDest)
NS_COM void CopyUTF16toUTF8 (const PRUnichar *aSource, nsACString &aDest)
NS_COM void CopyUTF8toUTF16 (const char *aSource, nsAString &aDest)
NS_COM void LossyAppendUTF16toASCII (const nsAString &aSource, nsACString &aDest)
NS_COM void AppendASCIItoUTF16 (const nsACString &aSource, nsAString &aDest)
NS_COM void LossyAppendUTF16toASCII (const PRUnichar *aSource, nsACString &aDest)
NS_COM void AppendASCIItoUTF16 (const char *aSource, nsAString &aDest)
NS_COM void AppendUTF16toUTF8 (const nsAString &aSource, nsACString &aDest)
NS_COM void AppendUTF8toUTF16 (const nsACString &aSource, nsAString &aDest)
NS_COM void AppendUTF16toUTF8 (const PRUnichar *aSource, nsACString &aDest)
NS_COM void AppendUTF8toUTF16 (const char *aSource, nsAString &aDest)
template<class FromStringT , class ToCharT >
ToCharT * AllocateStringCopy (const FromStringT &aSource, ToCharT *)
 A helper function that allocates a buffer of the desired character type big enough to hold a copy of the supplied string (plus a zero terminator).
NS_COM char * ToNewCString (const nsAString &aSource)
 Returns a new |char| buffer containing a zero-terminated copy of |aSource|.
NS_COM char * ToNewUTF8String (const nsAString &aSource, PRUint32 *aUTF8Count)
 Returns a new |char| buffer containing a zero-terminated copy of |aSource|.
NS_COM char * ToNewCString (const nsACString &aSource)
 Returns a new |char| buffer containing a zero-terminated copy of |aSource|.
NS_COM PRUnicharToNewUnicode (const nsAString &aSource)
 Returns a new |PRUnichar| buffer containing a zero-terminated copy of |aSource|.
NS_COM PRUnicharToNewUnicode (const nsACString &aSource)
 Returns a new |PRUnichar| buffer containing a zero-terminated copy of |aSource|.
NS_COM PRUnicharUTF8ToNewUnicode (const nsACString &aSource, PRUint32 *aUTF16Count)
 Returns a new |PRUnichar| buffer containing a zero-terminated copy of |aSource|.
NS_COM PRUnicharCopyUnicodeTo (const nsAString &aSource, PRUint32 aSrcOffset, PRUnichar *aDest, PRUint32 aLength)
 Copies |aLength| 16-bit code units from the start of |aSource| to the |PRUnichar| buffer |aDest|.
NS_COM void CopyUnicodeTo (const nsAString::const_iterator &aSrcStart, const nsAString::const_iterator &aSrcEnd, nsAString &aDest)
 Copies 16-bit characters between iterators |aSrcStart| and |aSrcEnd| to the writable string |aDest|.
NS_COM void AppendUnicodeTo (const nsAString::const_iterator &aSrcStart, const nsAString::const_iterator &aSrcEnd, nsAString &aDest)
 Appends 16-bit characters between iterators |aSrcStart| and |aSrcEnd| to the writable string |aDest|.
NS_COM PRBool IsASCII (const nsAString &aString)
 Returns |PR_TRUE| if |aString| contains only ASCII characters, that is, characters in the range (0x00, 0x7F).
NS_COM PRBool IsASCII (const nsACString &aString)
 Returns |PR_TRUE| if |aString| contains only ASCII characters, that is, characters in the range (0x00, 0x7F).
NS_COM PRBool IsUTF8 (const nsACString &aString)
 Returns |PR_TRUE| if |aString| is a valid UTF-8 string.
NS_COM void ToUpperCase (nsCSubstring &aCString)
 Converts case in place in the argument string.
NS_COM void ToUpperCase (const nsACString &aSource, nsACString &aDest)
 Converts case from string aSource to aDest.
NS_COM void ToLowerCase (nsCSubstring &aCString)
NS_COM void ToLowerCase (const nsACString &aSource, nsACString &aDest)
template<class StringT , class IteratorT , class Comparator >
PRBool FindInReadable_Impl (const StringT &aPattern, IteratorT &aSearchStart, IteratorT &aSearchEnd, const Comparator &compare)
NS_COM PRBool FindInReadable (const nsAString &aPattern, nsAString::const_iterator &aSearchStart, nsAString::const_iterator &aSearchEnd, const nsStringComparator &aComparator)
 Finds the leftmost occurance of |aPattern|, if any in the range |aSearchStart|..|aSearchEnd|.
NS_COM PRBool FindInReadable (const nsACString &aPattern, nsACString::const_iterator &aSearchStart, nsACString::const_iterator &aSearchEnd, const nsCStringComparator &aComparator)
NS_COM PRBool CaseInsensitiveFindInReadable (const nsACString &aPattern, nsACString::const_iterator &aSearchStart, nsACString::const_iterator &aSearchEnd)
NS_COM PRBool RFindInReadable (const nsAString &aPattern, nsAString::const_iterator &aSearchStart, nsAString::const_iterator &aSearchEnd, const nsStringComparator &aComparator)
 This implementation is simple, but does too much work.
NS_COM PRBool RFindInReadable (const nsACString &aPattern, nsACString::const_iterator &aSearchStart, nsACString::const_iterator &aSearchEnd, const nsCStringComparator &aComparator)
NS_COM PRBool FindCharInReadable (PRUnichar aChar, nsAString::const_iterator &aSearchStart, const nsAString::const_iterator &aSearchEnd)
 Finds the leftmost occurance of |aChar|, if any in the range |aSearchStart|..|aSearchEnd|.
NS_COM PRBool FindCharInReadable (char aChar, nsACString::const_iterator &aSearchStart, const nsACString::const_iterator &aSearchEnd)
NS_COM PRUint32 CountCharInReadable (const nsAString &aStr, PRUnichar aChar)
 Finds the number of occurences of |aChar| in the string |aStr|.
NS_COM PRUint32 CountCharInReadable (const nsACString &aStr, char aChar)
NS_COM PRBool StringBeginsWith (const nsAString &aSource, const nsAString &aSubstring, const nsStringComparator &aComparator)
NS_COM PRBool StringBeginsWith (const nsACString &aSource, const nsACString &aSubstring, const nsCStringComparator &aComparator)
NS_COM PRBool StringEndsWith (const nsAString &aSource, const nsAString &aSubstring, const nsStringComparator &aComparator)
NS_COM PRBool StringEndsWith (const nsACString &aSource, const nsACString &aSubstring, const nsCStringComparator &aComparator)
NS_COM const nsAFlatStringEmptyString ()
NS_COM const nsAFlatCStringEmptyCString ()
NS_COM void AppendUCS4ToUTF16 (const PRUint32 aSource, nsAString &aDest)

Variables

static const PRUnichar empty_buffer [1] = { '\0' }

Function Documentation

template<class FromStringT , class ToCharT >
ToCharT* AllocateStringCopy ( const FromStringT &  aSource,
ToCharT *   
) [inline]

A helper function that allocates a buffer of the desired character type big enough to hold a copy of the supplied string (plus a zero terminator).

Parameters:
aSourcean string you will eventually be making a copy of
Returns:
a new buffer (of the type specified by the second parameter) which you must free with |nsMemory::Free|.

Definition at line 308 of file nsReadableUtils.cpp.

  {
    return NS_STATIC_CAST(ToCharT*, nsMemory::Alloc((aSource.Length()+1) * sizeof(ToCharT)));
  }

Here is the call graph for this function:

Here is the caller graph for this function:

NS_COM void AppendASCIItoUTF16 ( const nsACString &  aSource,
nsAString &  aDest 
)

Definition at line 134 of file nsReadableUtils.cpp.

  {
    PRUint32 old_dest_length = aDest.Length();
    aDest.SetLength(old_dest_length + aSource.Length());

    nsACString::const_iterator fromBegin, fromEnd;

    nsAString::iterator dest;
    aDest.BeginWriting(dest);

    dest.advance(old_dest_length);

      // right now, this won't work on multi-fragment destinations
    LossyConvertEncoding<char, PRUnichar> converter(dest.get());

    copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd), converter);
  }

Here is the call graph for this function:

Here is the caller graph for this function:

NS_COM void AppendASCIItoUTF16 ( const char *  aSource,
nsAString &  aDest 
)

Definition at line 163 of file nsReadableUtils.cpp.

  {
    if (aSource) {
      AppendASCIItoUTF16(nsDependentCString(aSource), aDest);
    }
  }

Here is the call graph for this function:

NS_COM void AppendUCS4ToUTF16 ( const PRUint32  aSource,
nsAString &  aDest 
)

Definition at line 1100 of file nsReadableUtils.cpp.

  {
    NS_ASSERTION(IS_VALID_CHAR(aSource), "Invalid UCS4 char");
    if (IS_IN_BMP(aSource))
      {
        aDest.Append(PRUnichar(aSource));
      }
    else
      {
        aDest.Append(H_SURROGATE(aSource));
        aDest.Append(L_SURROGATE(aSource));
      }
  }

Here is the caller graph for this function:

NS_COM void AppendUnicodeTo ( const nsAString::const_iterator &  aSrcStart,
const nsAString::const_iterator &  aSrcEnd,
nsAString &  aDest 
)

Appends 16-bit characters between iterators |aSrcStart| and |aSrcEnd| to the writable string |aDest|.

After this operation |aDest| is not null terminated.

Parameters:
aSrcStartstart source iterator
aSrcEndend source iterator
aDestdestination for the copy

Definition at line 450 of file nsReadableUtils.cpp.

  {
    nsAString::iterator writer;
    PRUint32 oldLength = aDest.Length();
    aDest.SetLength(oldLength + Distance(aSrcStart, aSrcEnd));
    aDest.BeginWriting(writer).advance(oldLength);
    nsAString::const_iterator fromBegin(aSrcStart);
    
    copy_string(fromBegin, aSrcEnd, writer);
  }

Here is the call graph for this function:

NS_COM void AppendUTF16toUTF8 ( const nsAString &  aSource,
nsACString &  aDest 
)

Definition at line 172 of file nsReadableUtils.cpp.

  {
    nsAString::const_iterator source_start, source_end;
    CalculateUTF8Size calculator;
    copy_string(aSource.BeginReading(source_start),
                aSource.EndReading(source_end), calculator);

    PRUint32 count = calculator.Size();

    if (count)
      {
        PRUint32 old_dest_length = aDest.Length();

        // Grow the buffer if we need to.
        aDest.SetLength(old_dest_length + count);

        nsACString::iterator dest;
        aDest.BeginWriting(dest);

        dest.advance(old_dest_length);

        if (count <= (PRUint32)dest.size_forward())
          {
            // aDest has enough room in the fragment just past the end
            // of its old data that it can hold what we're about to
            // append. Append using copy_string().

            // All ready? Time to convert

            ConvertUTF16toUTF8 converter(dest.get());
            copy_string(aSource.BeginReading(source_start),
                        aSource.EndReading(source_end), converter);

            if (converter.Size() != count)
              {
                NS_ERROR("Input invalid or incorrect length was calculated");

                aDest.SetLength(old_dest_length);
              }
          }
        else
          {
            // This isn't the fastest way to do this, but it gets
            // complicated to convert UTF16 into a fragmented UTF8
            // string, so we'll take the easy way out here in this
            // rare situation.

            aDest.Replace(old_dest_length, count,
                          NS_ConvertUTF16toUTF8(aSource));
          }
      }
  }

Here is the call graph for this function:

Here is the caller graph for this function:

NS_COM void AppendUTF16toUTF8 ( const PRUnichar aSource,
nsACString &  aDest 
)

Definition at line 281 of file nsReadableUtils.cpp.

  {
    if (aSource) {
      AppendUTF16toUTF8(nsDependentString(aSource), aDest);
    }
  }

Here is the call graph for this function:

NS_COM void AppendUTF8toUTF16 ( const nsACString &  aSource,
nsAString &  aDest 
)

Definition at line 227 of file nsReadableUtils.cpp.

  {
    nsACString::const_iterator source_start, source_end;
    CalculateUTF8Length calculator;
    copy_string(aSource.BeginReading(source_start),
                aSource.EndReading(source_end), calculator);

    PRUint32 count = calculator.Length();

    if (count)
      {
        PRUint32 old_dest_length = aDest.Length();

        // Grow the buffer if we need to.
        aDest.SetLength(old_dest_length + count);

        nsAString::iterator dest;
        aDest.BeginWriting(dest);

        dest.advance(old_dest_length);

        if (count <= (PRUint32)dest.size_forward())
          {
            // aDest has enough room in the fragment just past the end
            // of its old data that it can hold what we're about to
            // append. Append using copy_string().

            // All ready? Time to convert

            ConvertUTF8toUTF16 converter(dest.get());
            copy_string(aSource.BeginReading(source_start),
                        aSource.EndReading(source_end), converter);

            if (converter.Length() != count)
              {
                NS_ERROR("Input wasn't UTF8 or incorrect length was calculated");
                aDest.SetLength(old_dest_length);
              }
          }
        else
          {
            // This isn't the fastest way to do this, but it gets
            // complicated to convert parts of a UTF8 string into a
            // UTF16 string, so we'll take the easy way out here in
            // this rare situation.

            aDest.Replace(old_dest_length, count,
                          NS_ConvertUTF8toUTF16(aSource));
          }
      }
  }

Here is the call graph for this function:

Here is the caller graph for this function:

NS_COM void AppendUTF8toUTF16 ( const char *  aSource,
nsAString &  aDest 
)

Definition at line 290 of file nsReadableUtils.cpp.

  {
    if (aSource) {
      AppendUTF8toUTF16(nsDependentCString(aSource), aDest);
    }
  }

Here is the call graph for this function:

NS_COM PRBool CaseInsensitiveFindInReadable ( const nsACString &  aPattern,
nsACString::const_iterator &  aSearchStart,
nsACString::const_iterator &  aSearchEnd 
)

Definition at line 884 of file nsReadableUtils.cpp.

  {
    return FindInReadable_Impl(aPattern, aSearchStart, aSearchEnd, nsCaseInsensitiveCStringComparator());
  }

Here is the call graph for this function:

NS_COM void CopyASCIItoUTF16 ( const nsACString &  aSource,
nsAString &  aDest 
)

Definition at line 54 of file nsReadableUtils.cpp.

  {
    aDest.Truncate();
    AppendASCIItoUTF16(aSource, aDest);
  }

Here is the call graph for this function:

NS_COM void CopyASCIItoUTF16 ( const char *  aSource,
nsAString &  aDest 
)

Definition at line 72 of file nsReadableUtils.cpp.

  {
    aDest.Truncate();
    if (aSource) {
      AppendASCIItoUTF16(nsDependentCString(aSource), aDest);
    }
  }

Here is the call graph for this function:

NS_COM PRUnichar* CopyUnicodeTo ( const nsAString &  aSource,
PRUint32  aSrcOffset,
PRUnichar aDest,
PRUint32  aLength 
)

Copies |aLength| 16-bit code units from the start of |aSource| to the |PRUnichar| buffer |aDest|.

After this operation |aDest| is not null terminated.

Parameters:
aSourcea UTF-16 string
aSrcOffsetstart offset in the source string
aDesta |PRUnichar| buffer
aLengththe number of 16-bit code units to copy
Returns:
pointer to destination buffer - identical to |aDest|

Definition at line 426 of file nsReadableUtils.cpp.

  {
    nsAString::const_iterator fromBegin, fromEnd;
    PRUnichar* toBegin = aDest;    
    copy_string(aSource.BeginReading(fromBegin).advance( PRInt32(aSrcOffset) ), aSource.BeginReading(fromEnd).advance( PRInt32(aSrcOffset+aLength) ), toBegin);
    return aDest;
  }

Here is the call graph for this function:

NS_COM void CopyUnicodeTo ( const nsAString::const_iterator &  aSrcStart,
const nsAString::const_iterator &  aSrcEnd,
nsAString &  aDest 
)

Copies 16-bit characters between iterators |aSrcStart| and |aSrcEnd| to the writable string |aDest|.

Similar to the |nsString::Mid| method.

After this operation |aDest| is not null terminated.

Parameters:
aSrcStartstart source iterator
aSrcEndend source iterator
aDestdestination for the copy

Definition at line 436 of file nsReadableUtils.cpp.

  {
    nsAString::iterator writer;
    aDest.SetLength(Distance(aSrcStart, aSrcEnd));
    aDest.BeginWriting(writer);
    nsAString::const_iterator fromBegin(aSrcStart);
    
    copy_string(fromBegin, aSrcEnd, writer);
  }

Here is the call graph for this function:

NS_COM void CopyUTF16toUTF8 ( const nsAString &  aSource,
nsACString &  aDest 
)

Definition at line 82 of file nsReadableUtils.cpp.

  {
    aDest.Truncate();
    AppendUTF16toUTF8(aSource, aDest);
  }

Here is the call graph for this function:

NS_COM void CopyUTF16toUTF8 ( const PRUnichar aSource,
nsACString &  aDest 
)

Definition at line 98 of file nsReadableUtils.cpp.

  {
    aDest.Truncate();
    AppendUTF16toUTF8(aSource, aDest);
  }

Here is the call graph for this function:

NS_COM void CopyUTF8toUTF16 ( const nsACString &  aSource,
nsAString &  aDest 
)

Definition at line 90 of file nsReadableUtils.cpp.

  {
    aDest.Truncate();
    AppendUTF8toUTF16(aSource, aDest);
  }

Here is the call graph for this function:

NS_COM void CopyUTF8toUTF16 ( const char *  aSource,
nsAString &  aDest 
)

Definition at line 106 of file nsReadableUtils.cpp.

  {
    aDest.Truncate();
    AppendUTF8toUTF16(aSource, aDest);
  }

Here is the call graph for this function:

NS_COM PRUint32 CountCharInReadable ( const nsAString &  aStr,
PRUnichar  aChar 
)

Finds the number of occurences of |aChar| in the string |aStr|.

Definition at line 994 of file nsReadableUtils.cpp.

{
  PRUint32 count = 0;
  nsAString::const_iterator begin, end;
  
  aStr.BeginReading(begin);
  aStr.EndReading(end);
  
  while (begin != end) {
    if (*begin == aChar) {
      ++count;
    }
    ++begin;
  }

  return count;
}
NS_COM PRUint32 CountCharInReadable ( const nsACString &  aStr,
char  aChar 
)

Definition at line 1015 of file nsReadableUtils.cpp.

{
  PRUint32 count = 0;
  nsACString::const_iterator begin, end;
  
  aStr.BeginReading(begin);
  aStr.EndReading(end);
  
  while (begin != end) {
    if (*begin == aChar) {
      ++count;
    }
    ++begin;
  }

  return count;
}

Definition at line 1091 of file nsReadableUtils.cpp.

  {
    static const nsDependentCString sEmpty((const char *)empty_buffer);

    return sEmpty;
  }

Definition at line 1084 of file nsReadableUtils.cpp.

  {
    static const nsDependentString sEmpty(empty_buffer);

    return sEmpty;
  }
NS_COM PRBool FindCharInReadable ( PRUnichar  aChar,
nsAString::const_iterator &  aSearchStart,
const nsAString::const_iterator &  aSearchEnd 
)

Finds the leftmost occurance of |aChar|, if any in the range |aSearchStart|..|aSearchEnd|.

Returns |PR_TRUE| if a match was found, and adjusts |aSearchStart| to point to the match. If no match was found, returns |PR_FALSE| and makes |aSearchStart == aSearchEnd|.

Definition at line 962 of file nsReadableUtils.cpp.

  {
    PRInt32 fragmentLength = aSearchEnd.get() - aSearchStart.get();

    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;
  }
NS_COM PRBool FindCharInReadable ( char  aChar,
nsACString::const_iterator &  aSearchStart,
const nsACString::const_iterator &  aSearchEnd 
)

Definition at line 978 of file nsReadableUtils.cpp.

  {
    PRInt32 fragmentLength = aSearchEnd.get() - aSearchStart.get();

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

    aSearchStart.advance(fragmentLength);
    return PR_FALSE;
  } 
NS_COM PRBool FindInReadable ( const nsAString &  aPattern,
nsAString::const_iterator &  ,
nsAString::const_iterator &  ,
const nsStringComparator &  = nsDefaultStringComparator() 
)

Finds the leftmost occurance of |aPattern|, if any in the range |aSearchStart|..|aSearchEnd|.

Returns |PR_TRUE| if a match was found, and adjusts |aSearchStart| and |aSearchEnd| to point to the match. If no match was found, returns |PR_FALSE| and makes |aSearchStart == aSearchEnd|.

Currently, this is equivalent to the O(m*n) implementation previously on |ns[C]String|. If we need something faster, then we can implement that later.

Definition at line 870 of file nsReadableUtils.cpp.

  {
    return FindInReadable_Impl(aPattern, aSearchStart, aSearchEnd, aComparator);
  }

Here is the call graph for this function:

NS_COM PRBool FindInReadable ( const nsACString &  aPattern,
nsACString::const_iterator &  aSearchStart,
nsACString::const_iterator &  aSearchEnd,
const nsCStringComparator &  aComparator 
)

Definition at line 877 of file nsReadableUtils.cpp.

  {
    return FindInReadable_Impl(aPattern, aSearchStart, aSearchEnd, aComparator);
  }

Here is the call graph for this function:

template<class StringT , class IteratorT , class Comparator >
PRBool FindInReadable_Impl ( const StringT &  aPattern,
IteratorT &  aSearchStart,
IteratorT &  aSearchEnd,
const Comparator &  compare 
)

Definition at line 802 of file nsReadableUtils.cpp.

  {
    PRBool found_it = PR_FALSE;

      // only bother searching at all if we're given a non-empty range to search
    if ( aSearchStart != aSearchEnd )
      {
        IteratorT 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
            IteratorT testPattern(aPatternStart);
            IteratorT 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:

NS_COM PRBool IsASCII ( const nsAString &  aString)

Returns |PR_TRUE| if |aString| contains only ASCII characters, that is, characters in the range (0x00, 0x7F).

Parameters:
aStringa 16-bit wide string to scan

Definition at line 465 of file nsReadableUtils.cpp.

  {
    static const PRUnichar NOT_ASCII = PRUnichar(~0x007F);


    // Don't want to use |copy_string| for this task, since we can stop at the first non-ASCII character

    nsAString::const_iterator done_reading;
    aString.EndReading(done_reading);

      // for each chunk of |aString|...
    PRUint32 fragmentLength = 0;
    nsAString::const_iterator iter;
    for ( aString.BeginReading(iter); iter != done_reading; iter.advance( PRInt32(fragmentLength) ) )
      {
        fragmentLength = PRUint32(iter.size_forward());
        const PRUnichar* c = iter.get();
        const PRUnichar* fragmentEnd = c + fragmentLength;

          // for each character in this chunk...
        while ( c < fragmentEnd )
          if ( *c++ & NOT_ASCII )
            return PR_FALSE;
      }

    return PR_TRUE;
  }

Here is the caller graph for this function:

NS_COM PRBool IsASCII ( const nsACString &  aString)

Returns |PR_TRUE| if |aString| contains only ASCII characters, that is, characters in the range (0x00, 0x7F).

Parameters:
aStringa 8-bit wide string to scan

Definition at line 495 of file nsReadableUtils.cpp.

  {
    static const char NOT_ASCII = char(~0x7F);


    // Don't want to use |copy_string| for this task, since we can stop at the first non-ASCII character

    nsACString::const_iterator done_reading;
    aString.EndReading(done_reading);

      // for each chunk of |aString|...
    PRUint32 fragmentLength = 0;
    nsACString::const_iterator iter;
    for ( aString.BeginReading(iter); iter != done_reading; iter.advance( PRInt32(fragmentLength) ) )
      {
        fragmentLength = PRUint32(iter.size_forward());
        const char* c = iter.get();
        const char* fragmentEnd = c + fragmentLength;

          // for each character in this chunk...
        while ( c < fragmentEnd )
          if ( *c++ & NOT_ASCII )
            return PR_FALSE;
      }

    return PR_TRUE;
  }
NS_COM PRBool IsUTF8 ( const nsACString &  aString)

Returns |PR_TRUE| if |aString| is a valid UTF-8 string.

XXX This is not bullet-proof and nor an all-purpose UTF-8 validator. It is mainly written to replace and roughly equivalent to

str.Equals(NS_ConvertUTF16toUTF8(NS_ConvertUTF8toUTF16(str)))

(see bug 191541) As such, it does not check for non-UTF-8 7bit encodings such as ISO-2022-JP and HZ. However, it filters out UTF-8 representation of surrogate codepoints and non-characters ( 0xFFFE and 0xFFFF in planes 0 through 16.) as well as overlong UTF-8 sequences. Also note that it regards UTF-8 sequences corresponding to codepoints above 0x10FFFF as invalid in accordance with http://www.ietf.org/internet-drafts/draft-yergeau-rfc2279bis-04.txt

Parameters:
aStringan 8-bit wide string to scan

Definition at line 525 of file nsReadableUtils.cpp.

  {
    nsReadingIterator<char> done_reading;
    aString.EndReading(done_reading);

    PRInt32 state = 0;
    PRBool overlong = PR_FALSE;
    PRBool surrogate = PR_FALSE;
    PRBool nonchar = PR_FALSE;
    PRUint16 olupper = 0; // overlong byte upper bound.
    PRUint16 slower = 0;  // surrogate byte lower bound.

      // for each chunk of |aString|...
    PRUint32 fragmentLength = 0;
    nsReadingIterator<char> iter;

    for ( aString.BeginReading(iter); iter != done_reading; iter.advance( PRInt32(fragmentLength) ) )
      {
        fragmentLength = PRUint32(iter.size_forward());
        const char* ptr = iter.get();
        const char* fragmentEnd = ptr + fragmentLength;

          // for each character in this chunk...
        while ( ptr < fragmentEnd )
          {
            PRUint8 c;
            
            if (0 == state)
              {
                c = *ptr++;

                if ( UTF8traits::isASCII(c) ) 
                  continue;

                if ( c <= 0xC1 ) // [80-BF] where not expected, [C0-C1] for overlong.
                  return PR_FALSE;
                else if ( UTF8traits::is2byte(c) ) 
                    state = 1;
                else if ( UTF8traits::is3byte(c) ) 
                  {
                    state = 2;
                    if ( c == 0xE0 ) // to exclude E0[80-9F][80-BF] 
                      {
                        overlong = PR_TRUE;
                        olupper = 0x9F;
                      }
                    else if ( c == 0xED ) // ED[A0-BF][80-BF] : surrogate codepoint
                      {
                        surrogate = PR_TRUE;
                        slower = 0xA0;
                      }
                    else if ( c == 0xEF ) // EF BF [BE-BF] : non-character
                      nonchar = PR_TRUE;
                  }
                else if ( c <= 0xF4 ) // XXX replace /w UTF8traits::is4byte when it's updated to exclude [F5-F7].(bug 199090)
                  {
                    state = 3;
                    nonchar = PR_TRUE;
                    if ( c == 0xF0 ) // to exclude F0[80-8F][80-BF]{2}
                      {
                        overlong = PR_TRUE;
                        olupper = 0x8F;
                      }
                    else if ( c == 0xF4 ) // to exclude F4[90-BF][80-BF] 
                      {
                        // actually not surrogates but codepoints beyond 0x10FFFF
                        surrogate = PR_TRUE;
                        slower = 0x90;
                      }
                  }
                else
                  return PR_FALSE; // Not UTF-8 string
              }
              
              while (ptr < fragmentEnd && state)
                {
                  c = *ptr++;
                  --state;

                  // non-character : EF BF [BE-BF] or F[0-7] [89AB]F BF [BE-BF]
                  if ( nonchar &&  ( !state &&  c < 0xBE ||
                       state == 1 && c != 0xBF  ||
                       state == 2 && 0x0F != (0x0F & c) ))
                     nonchar = PR_FALSE;

                  if ( !UTF8traits::isInSeq(c) || overlong && c <= olupper || 
                       surrogate && slower <= c || nonchar && !state )
                    return PR_FALSE; // Not UTF-8 string
                  overlong = surrogate = PR_FALSE;
                }
            }
        }
    return !state; // state != 0 at the end indicates an invalid UTF-8 seq. 
  }

Here is the call graph for this function:

Here is the caller graph for this function:

NS_COM void LossyAppendUTF16toASCII ( const nsAString &  aSource,
nsACString &  aDest 
)

Definition at line 114 of file nsReadableUtils.cpp.

  {
    PRUint32 old_dest_length = aDest.Length();
    aDest.SetLength(old_dest_length + aSource.Length());

    nsAString::const_iterator fromBegin, fromEnd;

    nsACString::iterator dest;
    aDest.BeginWriting(dest);

    dest.advance(old_dest_length);

      // right now, this won't work on multi-fragment destinations
    LossyConvertEncoding<PRUnichar, char> converter(dest.get());
    
    copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd), converter);
  }

Here is the call graph for this function:

Here is the caller graph for this function:

NS_COM void LossyAppendUTF16toASCII ( const PRUnichar aSource,
nsACString &  aDest 
)

Definition at line 154 of file nsReadableUtils.cpp.

  {
    if (aSource) {
      LossyAppendUTF16toASCII(nsDependentString(aSource), aDest);
    }
  }

Here is the call graph for this function:

NS_COM void LossyCopyUTF16toASCII ( const nsAString &  aSource,
nsACString &  aDest 
)

Definition at line 46 of file nsReadableUtils.cpp.

  {
    aDest.Truncate();
    LossyAppendUTF16toASCII(aSource, aDest);
  }

Here is the call graph for this function:

Here is the caller graph for this function:

NS_COM void LossyCopyUTF16toASCII ( const PRUnichar aSource,
nsACString &  aDest 
)

Definition at line 62 of file nsReadableUtils.cpp.

  {
    aDest.Truncate();
    if (aSource) {
      LossyAppendUTF16toASCII(nsDependentString(aSource), aDest);
    }
  }

Here is the call graph for this function:

NS_COM PRBool RFindInReadable ( const nsAString &  aPattern,
nsAString::const_iterator &  aSearchStart,
nsAString::const_iterator &  aSearchEnd,
const nsStringComparator &  aComparator 
)

This implementation is simple, but does too much work.

Finds the rightmost occurance of |aPattern| Returns |PR_TRUE| if a match was found, and adjusts |aSearchStart| and |aSearchEnd| to point to the match.

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 896 of file nsReadableUtils.cpp.

  {
    PRBool found_it = PR_FALSE;

    nsAString::const_iterator savedSearchEnd(aSearchEnd);
    nsAString::const_iterator 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:

NS_COM PRBool RFindInReadable ( const nsACString &  aPattern,
nsACString::const_iterator &  aSearchStart,
nsACString::const_iterator &  aSearchEnd,
const nsCStringComparator &  aComparator 
)

Definition at line 929 of file nsReadableUtils.cpp.

  {
    PRBool found_it = PR_FALSE;

    nsACString::const_iterator savedSearchEnd(aSearchEnd);
    nsACString::const_iterator 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:

NS_COM PRBool StringBeginsWith ( const nsAString &  aSource,
const nsAString &  aSubstring,
const nsStringComparator &  aComparator 
)

Definition at line 1035 of file nsReadableUtils.cpp.

  {
    nsAString::size_type src_len = aSource.Length(),
                         sub_len = aSubstring.Length();
    if (sub_len > src_len)
      return PR_FALSE;
    return Substring(aSource, 0, sub_len).Equals(aSubstring, aComparator);
  }

Here is the call graph for this function:

NS_COM PRBool StringBeginsWith ( const nsACString &  aSource,
const nsACString &  aSubstring,
const nsCStringComparator &  aComparator 
)

Definition at line 1046 of file nsReadableUtils.cpp.

  {
    nsACString::size_type src_len = aSource.Length(),
                          sub_len = aSubstring.Length();
    if (sub_len > src_len)
      return PR_FALSE;
    return Substring(aSource, 0, sub_len).Equals(aSubstring, aComparator);
  }

Here is the call graph for this function:

NS_COM PRBool StringEndsWith ( const nsAString &  aSource,
const nsAString &  aSubstring,
const nsStringComparator &  aComparator 
)

Definition at line 1057 of file nsReadableUtils.cpp.

  {
    nsAString::size_type src_len = aSource.Length(),
                         sub_len = aSubstring.Length();
    if (sub_len > src_len)
      return PR_FALSE;
    return Substring(aSource, src_len - sub_len, sub_len).Equals(aSubstring,
                                                                 aComparator);
  }

Here is the call graph for this function:

Here is the caller graph for this function:

NS_COM PRBool StringEndsWith ( const nsACString &  aSource,
const nsACString &  aSubstring,
const nsCStringComparator &  aComparator 
)

Definition at line 1069 of file nsReadableUtils.cpp.

  {
    nsACString::size_type src_len = aSource.Length(),
                          sub_len = aSubstring.Length();
    if (sub_len > src_len)
      return PR_FALSE;
    return Substring(aSource, src_len - sub_len, sub_len).Equals(aSubstring,
                                                                 aComparator);
  }

Here is the call graph for this function:

Definition at line 746 of file nsReadableUtils.cpp.

  {
    ConvertToLowerCase converter;
    char* start;
    converter.write(aCString.BeginWriting(start), aCString.Length());
  }

Here is the call graph for this function:

NS_COM void ToLowerCase ( const nsACString &  aSource,
nsACString &  aDest 
)

Definition at line 791 of file nsReadableUtils.cpp.

  {
    nsACString::const_iterator fromBegin, fromEnd;
    nsACString::iterator toBegin;
    aDest.SetLength(aSource.Length());
    CopyToLowerCase converter(aDest.BeginWriting(toBegin));
    copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd), converter);
  }

Here is the call graph for this function:

NS_COM char* ToNewCString ( const nsAString &  aSource)

Returns a new |char| buffer containing a zero-terminated copy of |aSource|.

Allocates and returns a new |char| buffer which you must free with |nsMemory::Free|. Performs a lossy encoding conversion by chopping 16-bit wide characters down to 8-bits wide while copying |aSource| to your new buffer. This conversion is not well defined; but it reproduces legacy string behavior. The new buffer is zero-terminated, but that may not help you if |aSource| contains embedded nulls.

Parameters:
aSourcea 16-bit wide string
Returns:
a new |char| buffer you must free with |nsMemory::Free|.

Definition at line 316 of file nsReadableUtils.cpp.

  {
    char* result = AllocateStringCopy(aSource, (char*)0);
    if (!result)
      return nsnull;

    nsAString::const_iterator fromBegin, fromEnd;
    LossyConvertEncoding<PRUnichar, char> converter(result);
    copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd), converter).write_terminator();
    return result;
  }

Here is the call graph for this function:

NS_COM char* ToNewCString ( const nsACString &  aSource)

Returns a new |char| buffer containing a zero-terminated copy of |aSource|.

Allocates and returns a new |char| buffer which you must free with |nsMemory::Free|. The new buffer is zero-terminated, but that may not help you if |aSource| contains embedded nulls.

Parameters:
aSourcean 8-bit wide string
Returns:
a new |char| buffer you must free with |nsMemory::Free|.

Definition at line 355 of file nsReadableUtils.cpp.

  {
    // no conversion needed, just allocate a buffer of the correct length and copy into it

    char* result = AllocateStringCopy(aSource, (char*)0);
    if (!result)
      return nsnull;

    nsACString::const_iterator fromBegin, fromEnd;
    char* toBegin = result;
    *copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd), toBegin) = char(0);
    return result;
  }

Here is the call graph for this function:

NS_COM PRUnichar* ToNewUnicode ( const nsAString &  aSource)

Returns a new |PRUnichar| buffer containing a zero-terminated copy of |aSource|.

Allocates and returns a new |PRUnichar| buffer which you must free with |nsMemory::Free|. The new buffer is zero-terminated, but that may not help you if |aSource| contains embedded nulls.

Parameters:
aSourcea UTF-16 string
Returns:
a new |PRUnichar| buffer you must free with |nsMemory::Free|.

Definition at line 371 of file nsReadableUtils.cpp.

  {
    // no conversion needed, just allocate a buffer of the correct length and copy into it

    PRUnichar* result = AllocateStringCopy(aSource, (PRUnichar*)0);
    if (!result)
      return nsnull;

    nsAString::const_iterator fromBegin, fromEnd;
    PRUnichar* toBegin = result;
    *copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd), toBegin) = PRUnichar(0);
    return result;
  }

Here is the call graph for this function:

NS_COM PRUnichar* ToNewUnicode ( const nsACString &  aSource)

Returns a new |PRUnichar| buffer containing a zero-terminated copy of |aSource|.

Allocates and returns a new |PRUnichar| buffer which you must free with |nsMemory::Free|. Performs an encoding conversion by 0-padding 8-bit wide characters up to 16-bits wide while copying |aSource| to your new buffer. This conversion is not well defined; but it reproduces legacy string behavior. The new buffer is zero-terminated, but that may not help you if |aSource| contains embedded nulls.

Parameters:
aSourcean 8-bit wide string (a C-string, NOT UTF-8)
Returns:
a new |PRUnichar| buffer you must free with |nsMemory::Free|.

Definition at line 387 of file nsReadableUtils.cpp.

  {
    PRUnichar* result = AllocateStringCopy(aSource, (PRUnichar*)0);
    if (!result)
      return nsnull;

    nsACString::const_iterator fromBegin, fromEnd;
    LossyConvertEncoding<char, PRUnichar> converter(result);
    copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd), converter).write_terminator();
    return result;
  }

Here is the call graph for this function:

NS_COM char* ToNewUTF8String ( const nsAString &  aSource,
PRUint32 aUTF8Count = nsnull 
)

Returns a new |char| buffer containing a zero-terminated copy of |aSource|.

Allocates and returns a new |char| buffer which you must free with |nsMemory::Free|. Performs an encoding conversion from a UTF-16 string to a UTF-8 string copying |aSource| to your new buffer. The new buffer is zero-terminated, but that may not help you if |aSource| contains embedded nulls.

Parameters:
aSourcea UTF-16 string (made of PRUnichar's)
aUTF8Countthe number of 8-bit units that was returned
Returns:
a new |char| buffer you must free with |nsMemory::Free|.

Definition at line 330 of file nsReadableUtils.cpp.

  {
    nsAString::const_iterator start, end;
    CalculateUTF8Size calculator;
    copy_string(aSource.BeginReading(start), aSource.EndReading(end),
                calculator);

    if (aUTF8Count)
      *aUTF8Count = calculator.Size();

    char *result = NS_STATIC_CAST(char*,
        nsMemory::Alloc(calculator.Size() + 1));
    if (!result)
      return nsnull;

    ConvertUTF16toUTF8 converter(result);
    copy_string(aSource.BeginReading(start), aSource.EndReading(end),
                converter).write_terminator();
    NS_ASSERTION(calculator.Size() == converter.Size(), "length mismatch");

    return result;
  }

Here is the call graph for this function:

Converts case in place in the argument string.

Definition at line 656 of file nsReadableUtils.cpp.

  {
    ConvertToUpperCase converter;
    char* start;
    converter.write(aCString.BeginWriting(start), aCString.Length());
  }

Here is the call graph for this function:

NS_COM void ToUpperCase ( const nsACString &  aSource,
nsACString &  aDest 
)

Converts case from string aSource to aDest.

Definition at line 701 of file nsReadableUtils.cpp.

  {
    nsACString::const_iterator fromBegin, fromEnd;
    nsACString::iterator toBegin;
    aDest.SetLength(aSource.Length());
    CopyToUpperCase converter(aDest.BeginWriting(toBegin));
    copy_string(aSource.BeginReading(fromBegin), aSource.EndReading(fromEnd), converter);
  }

Here is the call graph for this function:

NS_COM PRUnichar* UTF8ToNewUnicode ( const nsACString &  aSource,
PRUint32 aUTF16Count = nsnull 
)

Returns a new |PRUnichar| buffer containing a zero-terminated copy of |aSource|.

Allocates and returns a new |char| buffer which you must free with |nsMemory::Free|. Performs an encoding conversion from UTF-8 to UTF-16 while copying |aSource| to your new buffer. This conversion is well defined for a valid UTF-8 string. The new buffer is zero-terminated, but that may not help you if |aSource| contains embedded nulls.

Parameters:
aSourcean 8-bit wide string, UTF-8 encoded
aUTF16Countthe number of 16-bit units that was returned
Returns:
a new |PRUnichar| buffer you must free with |nsMemory::Free|. (UTF-16 encoded)

Definition at line 401 of file nsReadableUtils.cpp.

  {
    nsACString::const_iterator start, end;
    CalculateUTF8Length calculator;
    copy_string(aSource.BeginReading(start), aSource.EndReading(end),
                calculator);

    if (aUTF16Count)
      *aUTF16Count = calculator.Length();

    PRUnichar *result = NS_STATIC_CAST(PRUnichar*,
        nsMemory::Alloc(sizeof(PRUnichar) * (calculator.Length() + 1)));
    if (!result)
      return nsnull;

    ConvertUTF8toUTF16 converter(result);
    copy_string(aSource.BeginReading(start), aSource.EndReading(end),
                converter).write_terminator();
    NS_ASSERTION(calculator.Length() == converter.Length(), "length mismatch");

    return result;
  }

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

const PRUnichar empty_buffer[1] = { '\0' } [static]

Definition at line 1082 of file nsReadableUtils.cpp.