diff --git a/string/public/nsAReadableString.h b/string/public/nsAReadableString.h index ed9f52da307b..211cbe8f37e8 100644 --- a/string/public/nsAReadableString.h +++ b/string/public/nsAReadableString.h @@ -220,6 +220,14 @@ class nsReadingIterator } }; +template +inline +PRBool +SameFragment( const Iterator& lhs, const Iterator& rhs ) + { + return lhs.fragment().mStart == rhs.fragment().mStart; + } + // // nsAReadable[C]String @@ -963,7 +971,14 @@ nsPromiseSubstring::GetReadableFragment( nsReadableFragment& aFrag else if ( aRequest == kFragmentAt ) aPosition += mStartPos; - return mString.GetReadableFragment(aFragment, aRequest, aPosition); + const CharT* position_ptr = mString.GetReadableFragment(aFragment, aRequest, aPosition); + + // if there's more physical data in the returned fragment than I logically have left + size_t logical_size_forward = mLength - aPosition; + if ( aFragment.mEnd - position_ptr > logical_size_forward ) + aFragment.mEnd = position_ptr + logical_size_forward; + + return position_ptr; } @@ -975,44 +990,24 @@ nsPromiseSubstring::GetReadableFragment( nsReadableFragment& aFrag // Global functions // +template +inline +PRBool +SameImplementation( const basic_nsAReadableString& lhs, const basic_nsAReadableString& rhs ) + { + const void* imp_tag = lhs.Implementation(); + return imp_tag && (imp_tag==rhs.Implementation()); + } + template OutputIterator copy_string( InputIterator first, InputIterator last, OutputIterator result ) { + typedef nsCharSourceTraits source_traits; + typedef nsCharSinkTraits sink_traits; + while ( first != last ) - { - PRUint32 lengthToCopy = PRUint32( NS_MIN(first.size_forward(), result.size_forward()) ); - if ( first.fragment().mStart == last.fragment().mStart ) - lengthToCopy = NS_MIN(lengthToCopy, PRUint32(last.operator->() - first.operator->())); - - NS_ASSERTION(lengthToCopy, "|copy_string| will never terminate"); - - nsCharTraits::copy(result.operator->(), first.operator->(), lengthToCopy); - - first += PRInt32(lengthToCopy); - result += PRInt32(lengthToCopy); - } - - return result; - } - -template -CharT* -copy_string( InputIterator first, InputIterator last, CharT* result ) - { - while ( first != last ) - { - PRUint32 lengthToCopy = PRUint32(first.size_forward()); - if ( first.fragment().mStart == last.fragment().mStart ) - lengthToCopy = NS_MIN(lengthToCopy, PRUint32(last.operator->() - first.operator->())); - - NS_ASSERTION(lengthToCopy, "|copy_string| will never terminate"); - - nsCharTraits::copy(result, first.operator->(), lengthToCopy); - - first += PRInt32(lengthToCopy); - result += PRInt32(lengthToCopy); - } + first += PRInt32(sink_traits::write(result, source_traits::read(first), source_traits::readable_size(first, last))); return result; } @@ -1029,7 +1024,7 @@ copy_string_backward( InputIterator first, InputIterator last, OutputIterator re NS_ASSERTION(lengthToCopy, "|copy_string_backward| will never terminate"); - nsCharTraits::move(result.operator->()-lengthToCopy, last.operator->()-lengthToCopy, lengthToCopy); + nsCharTraits::move(result.operator->()-lengthToCopy, last.operator->()-lengthToCopy, lengthToCopy); last -= PRInt32(lengthToCopy); result -= PRInt32(lengthToCopy); @@ -1038,15 +1033,6 @@ copy_string_backward( InputIterator first, InputIterator last, OutputIterator re return result; } -template -inline -PRBool -SameImplementation( const basic_nsAReadableString& lhs, const basic_nsAReadableString& rhs ) - { - const void* imp_tag = lhs.Implementation(); - return imp_tag && (imp_tag==rhs.Implementation()); - } - template nsPromiseSubstring Substring( const basic_nsAReadableString& aString, PRUint32 aStartPos, PRUint32 aSubstringLength ) @@ -1165,18 +1151,6 @@ operator+( const basic_nsLiteralString& lhs, const basic_nsLiteralString< -#if 0 -#ifdef STANDALONE_STRING_TESTS -template -basic_ostream& -operator<<( basic_ostream& os, const basic_nsAReadableString& s ) - { - copy(s.BeginReading(), s.EndReading(), ostream_iterator(os)); - return os; - } -#endif -#endif - typedef basic_nsAReadableString nsAReadableString; typedef basic_nsAReadableString nsAReadableCString; diff --git a/xpcom/ds/nsAReadableString.h b/xpcom/ds/nsAReadableString.h index ed9f52da307b..211cbe8f37e8 100644 --- a/xpcom/ds/nsAReadableString.h +++ b/xpcom/ds/nsAReadableString.h @@ -220,6 +220,14 @@ class nsReadingIterator } }; +template +inline +PRBool +SameFragment( const Iterator& lhs, const Iterator& rhs ) + { + return lhs.fragment().mStart == rhs.fragment().mStart; + } + // // nsAReadable[C]String @@ -963,7 +971,14 @@ nsPromiseSubstring::GetReadableFragment( nsReadableFragment& aFrag else if ( aRequest == kFragmentAt ) aPosition += mStartPos; - return mString.GetReadableFragment(aFragment, aRequest, aPosition); + const CharT* position_ptr = mString.GetReadableFragment(aFragment, aRequest, aPosition); + + // if there's more physical data in the returned fragment than I logically have left + size_t logical_size_forward = mLength - aPosition; + if ( aFragment.mEnd - position_ptr > logical_size_forward ) + aFragment.mEnd = position_ptr + logical_size_forward; + + return position_ptr; } @@ -975,44 +990,24 @@ nsPromiseSubstring::GetReadableFragment( nsReadableFragment& aFrag // Global functions // +template +inline +PRBool +SameImplementation( const basic_nsAReadableString& lhs, const basic_nsAReadableString& rhs ) + { + const void* imp_tag = lhs.Implementation(); + return imp_tag && (imp_tag==rhs.Implementation()); + } + template OutputIterator copy_string( InputIterator first, InputIterator last, OutputIterator result ) { + typedef nsCharSourceTraits source_traits; + typedef nsCharSinkTraits sink_traits; + while ( first != last ) - { - PRUint32 lengthToCopy = PRUint32( NS_MIN(first.size_forward(), result.size_forward()) ); - if ( first.fragment().mStart == last.fragment().mStart ) - lengthToCopy = NS_MIN(lengthToCopy, PRUint32(last.operator->() - first.operator->())); - - NS_ASSERTION(lengthToCopy, "|copy_string| will never terminate"); - - nsCharTraits::copy(result.operator->(), first.operator->(), lengthToCopy); - - first += PRInt32(lengthToCopy); - result += PRInt32(lengthToCopy); - } - - return result; - } - -template -CharT* -copy_string( InputIterator first, InputIterator last, CharT* result ) - { - while ( first != last ) - { - PRUint32 lengthToCopy = PRUint32(first.size_forward()); - if ( first.fragment().mStart == last.fragment().mStart ) - lengthToCopy = NS_MIN(lengthToCopy, PRUint32(last.operator->() - first.operator->())); - - NS_ASSERTION(lengthToCopy, "|copy_string| will never terminate"); - - nsCharTraits::copy(result, first.operator->(), lengthToCopy); - - first += PRInt32(lengthToCopy); - result += PRInt32(lengthToCopy); - } + first += PRInt32(sink_traits::write(result, source_traits::read(first), source_traits::readable_size(first, last))); return result; } @@ -1029,7 +1024,7 @@ copy_string_backward( InputIterator first, InputIterator last, OutputIterator re NS_ASSERTION(lengthToCopy, "|copy_string_backward| will never terminate"); - nsCharTraits::move(result.operator->()-lengthToCopy, last.operator->()-lengthToCopy, lengthToCopy); + nsCharTraits::move(result.operator->()-lengthToCopy, last.operator->()-lengthToCopy, lengthToCopy); last -= PRInt32(lengthToCopy); result -= PRInt32(lengthToCopy); @@ -1038,15 +1033,6 @@ copy_string_backward( InputIterator first, InputIterator last, OutputIterator re return result; } -template -inline -PRBool -SameImplementation( const basic_nsAReadableString& lhs, const basic_nsAReadableString& rhs ) - { - const void* imp_tag = lhs.Implementation(); - return imp_tag && (imp_tag==rhs.Implementation()); - } - template nsPromiseSubstring Substring( const basic_nsAReadableString& aString, PRUint32 aStartPos, PRUint32 aSubstringLength ) @@ -1165,18 +1151,6 @@ operator+( const basic_nsLiteralString& lhs, const basic_nsLiteralString< -#if 0 -#ifdef STANDALONE_STRING_TESTS -template -basic_ostream& -operator<<( basic_ostream& os, const basic_nsAReadableString& s ) - { - copy(s.BeginReading(), s.EndReading(), ostream_iterator(os)); - return os; - } -#endif -#endif - typedef basic_nsAReadableString nsAReadableString; typedef basic_nsAReadableString nsAReadableCString; diff --git a/xpcom/string/public/nsAReadableString.h b/xpcom/string/public/nsAReadableString.h index ed9f52da307b..211cbe8f37e8 100644 --- a/xpcom/string/public/nsAReadableString.h +++ b/xpcom/string/public/nsAReadableString.h @@ -220,6 +220,14 @@ class nsReadingIterator } }; +template +inline +PRBool +SameFragment( const Iterator& lhs, const Iterator& rhs ) + { + return lhs.fragment().mStart == rhs.fragment().mStart; + } + // // nsAReadable[C]String @@ -963,7 +971,14 @@ nsPromiseSubstring::GetReadableFragment( nsReadableFragment& aFrag else if ( aRequest == kFragmentAt ) aPosition += mStartPos; - return mString.GetReadableFragment(aFragment, aRequest, aPosition); + const CharT* position_ptr = mString.GetReadableFragment(aFragment, aRequest, aPosition); + + // if there's more physical data in the returned fragment than I logically have left + size_t logical_size_forward = mLength - aPosition; + if ( aFragment.mEnd - position_ptr > logical_size_forward ) + aFragment.mEnd = position_ptr + logical_size_forward; + + return position_ptr; } @@ -975,44 +990,24 @@ nsPromiseSubstring::GetReadableFragment( nsReadableFragment& aFrag // Global functions // +template +inline +PRBool +SameImplementation( const basic_nsAReadableString& lhs, const basic_nsAReadableString& rhs ) + { + const void* imp_tag = lhs.Implementation(); + return imp_tag && (imp_tag==rhs.Implementation()); + } + template OutputIterator copy_string( InputIterator first, InputIterator last, OutputIterator result ) { + typedef nsCharSourceTraits source_traits; + typedef nsCharSinkTraits sink_traits; + while ( first != last ) - { - PRUint32 lengthToCopy = PRUint32( NS_MIN(first.size_forward(), result.size_forward()) ); - if ( first.fragment().mStart == last.fragment().mStart ) - lengthToCopy = NS_MIN(lengthToCopy, PRUint32(last.operator->() - first.operator->())); - - NS_ASSERTION(lengthToCopy, "|copy_string| will never terminate"); - - nsCharTraits::copy(result.operator->(), first.operator->(), lengthToCopy); - - first += PRInt32(lengthToCopy); - result += PRInt32(lengthToCopy); - } - - return result; - } - -template -CharT* -copy_string( InputIterator first, InputIterator last, CharT* result ) - { - while ( first != last ) - { - PRUint32 lengthToCopy = PRUint32(first.size_forward()); - if ( first.fragment().mStart == last.fragment().mStart ) - lengthToCopy = NS_MIN(lengthToCopy, PRUint32(last.operator->() - first.operator->())); - - NS_ASSERTION(lengthToCopy, "|copy_string| will never terminate"); - - nsCharTraits::copy(result, first.operator->(), lengthToCopy); - - first += PRInt32(lengthToCopy); - result += PRInt32(lengthToCopy); - } + first += PRInt32(sink_traits::write(result, source_traits::read(first), source_traits::readable_size(first, last))); return result; } @@ -1029,7 +1024,7 @@ copy_string_backward( InputIterator first, InputIterator last, OutputIterator re NS_ASSERTION(lengthToCopy, "|copy_string_backward| will never terminate"); - nsCharTraits::move(result.operator->()-lengthToCopy, last.operator->()-lengthToCopy, lengthToCopy); + nsCharTraits::move(result.operator->()-lengthToCopy, last.operator->()-lengthToCopy, lengthToCopy); last -= PRInt32(lengthToCopy); result -= PRInt32(lengthToCopy); @@ -1038,15 +1033,6 @@ copy_string_backward( InputIterator first, InputIterator last, OutputIterator re return result; } -template -inline -PRBool -SameImplementation( const basic_nsAReadableString& lhs, const basic_nsAReadableString& rhs ) - { - const void* imp_tag = lhs.Implementation(); - return imp_tag && (imp_tag==rhs.Implementation()); - } - template nsPromiseSubstring Substring( const basic_nsAReadableString& aString, PRUint32 aStartPos, PRUint32 aSubstringLength ) @@ -1165,18 +1151,6 @@ operator+( const basic_nsLiteralString& lhs, const basic_nsLiteralString< -#if 0 -#ifdef STANDALONE_STRING_TESTS -template -basic_ostream& -operator<<( basic_ostream& os, const basic_nsAReadableString& s ) - { - copy(s.BeginReading(), s.EndReading(), ostream_iterator(os)); - return os; - } -#endif -#endif - typedef basic_nsAReadableString nsAReadableString; typedef basic_nsAReadableString nsAReadableCString;