diff --git a/mozilla/string/README.html b/mozilla/string/README.html index c042892a33e..eb54a3df194 100644 --- a/mozilla/string/README.html +++ b/mozilla/string/README.html @@ -21,11 +21,9 @@ - Scott Collins (original author) --> -

string code will be moving here soon

+

managing sequences of characters

- The string code will be moving here soon; - in the meantime, you can find the bulk of it in - mozilla/xpcom/ds. +

diff --git a/mozilla/string/doc/README.html b/mozilla/string/doc/README.html index af9eee88be5..e9a28f949ae 100644 --- a/mozilla/string/doc/README.html +++ b/mozilla/string/doc/README.html @@ -21,7 +21,7 @@ - Scott Collins (original author) --> -

string doc will be moving here soon

+

documentation aimed at programmers who are clients of the string library

diff --git a/mozilla/string/macbuild/string.mcp b/mozilla/string/macbuild/string.mcp index b37d99192d8..17264396504 100644 Binary files a/mozilla/string/macbuild/string.mcp and b/mozilla/string/macbuild/string.mcp differ diff --git a/mozilla/string/obsolete/nsString.cpp b/mozilla/string/obsolete/nsString.cpp index ade2c679bd6..057ffcea00c 100644 --- a/mozilla/string/obsolete/nsString.cpp +++ b/mozilla/string/obsolete/nsString.cpp @@ -792,8 +792,8 @@ void nsCString::AssignWithConversion( const nsAReadableString& aString ) { temp.mLength=fraglen; StrAppend(*this,temp,0,fraglen); - - start += fraglen; + + start.advance(fraglen); } } } @@ -816,7 +816,7 @@ void nsCString::AppendWithConversion( const nsAReadableString& aString ) { StrAppend(*this,temp,0,fraglen); - start += fraglen; + start.advance(fraglen); } } } @@ -1390,7 +1390,7 @@ NS_ConvertUCS2toUTF8::NS_ConvertUCS2toUTF8( const nsAReadableString& aString ) while (start != end) { nsReadableFragment frag(start.fragment()); Append(frag.mStart, frag.mEnd - frag.mStart); - start += start.size_forward(); + start.advance(start.size_forward()); } } diff --git a/mozilla/string/obsolete/nsString.h b/mozilla/string/obsolete/nsString.h index bf6d0fe2268..5c90e1e12f3 100644 --- a/mozilla/string/obsolete/nsString.h +++ b/mozilla/string/obsolete/nsString.h @@ -51,6 +51,7 @@ #include "nsIAtom.h" #include "nsAWritableString.h" +#include "nsLiteralString.h" class NS_COM nsSubsumeCStr; diff --git a/mozilla/string/obsolete/nsString2.h b/mozilla/string/obsolete/nsString2.h index bb9740a0b42..be4a9698aaa 100644 --- a/mozilla/string/obsolete/nsString2.h +++ b/mozilla/string/obsolete/nsString2.h @@ -53,6 +53,7 @@ #include "nsCRT.h" #include "nsAWritableString.h" +#include "nsLiteralString.h" #ifdef STANDALONE_MI_STRING_TESTS class nsAReadableString { public: virtual ~nsAReadableString() { } }; diff --git a/mozilla/string/public/MANIFEST b/mozilla/string/public/MANIFEST index 6a48cc1b8c0..6742f976d7e 100644 --- a/mozilla/string/public/MANIFEST +++ b/mozilla/string/public/MANIFEST @@ -22,14 +22,20 @@ nsAlgorithm.h +nsAString.h nsAReadableString.h nsAWritableString.h nsBufferHandle.h nsBufferHandleUtils.h nsCharTraits.h nsFragmentedString.h +nsLiteralString.h +nsLocalString.h nsPrintfCString.h nsPrivateSharableString.h nsReadableUtils.h nsSharedBufferList.h nsSlidingString.h +nsStringFragment.h +nsStringIterator.h +nsStringIteratorUtils.h diff --git a/mozilla/string/public/Makefile.in b/mozilla/string/public/Makefile.in index 3fd4a987aaf..394a6c8043b 100644 --- a/mozilla/string/public/Makefile.in +++ b/mozilla/string/public/Makefile.in @@ -18,6 +18,7 @@ # # Contributor(s): # Johnny Stenback (original author) +# Scott Collins # DEPTH = ../.. @@ -31,6 +32,7 @@ MODULE = string EXPORTS = \ nsAlgorithm.h \ + nsAString.h \ nsBufferHandle.h \ nsBufferHandleUtils.h \ nsPrivateSharableString.h \ @@ -41,7 +43,12 @@ EXPORTS = \ nsSharedBufferList.h \ nsSlidingString.h \ nsFragmentedString.h \ + nsLiteralString.h \ + nsLocalString.h \ nsPrintfCString.h \ + nsStringFragment.h \ + nsStringIterator.h \ + nsStringIteratorUtils.h \ $(NULL) EXPORTS := $(addprefix $(srcdir)/, $(EXPORTS)) diff --git a/mozilla/string/public/makefile.win b/mozilla/string/public/makefile.win index 3d819cedb8d..ae1d7a2e561 100644 --- a/mozilla/string/public/makefile.win +++ b/mozilla/string/public/makefile.win @@ -19,23 +19,30 @@ # # Contributor(s): # Johnny Stenback (original author) +# Scott Collins # DEPTH=..\.. EXPORTS = \ nsAlgorithm.h \ + nsAString.h \ nsAReadableString.h \ nsAWritableString.h \ nsBufferHandle.h \ nsBufferHandleUtils.h \ nsCharTraits.h \ nsFragmentedString.h \ + nsLiteralString.h \ + nsLocalString.h \ nsPrintfCString.h \ nsPrivateSharableString.h \ nsReadableUtils.h \ nsSharedBufferList.h \ nsSlidingString.h \ + nsStringFragment.h \ + nsStringIterator.h \ + nsStringIteratorUtils.h \ $(NULL) include <$(DEPTH)\config\rules.mak> diff --git a/mozilla/string/public/nsAReadableString.h b/mozilla/string/public/nsAReadableString.h index b36279dbd06..3b76ccce3f5 100644 --- a/mozilla/string/public/nsAReadableString.h +++ b/mozilla/string/public/nsAReadableString.h @@ -33,15 +33,14 @@ #include "nsCharTraits.h" #endif -#ifndef nsAlgorithm_h___ -#include "nsAlgorithm.h" - // for |NS_MIN|, |NS_MAX|, and |NS_COUNT|... -#endif - #ifndef nsPrivateSharableString_h___ #include "nsPrivateSharableString.h" #endif +#ifndef nsStringIterator_h___ +#include "nsStringIterator.h" +#endif + #include "nsMemory.h" /* @@ -61,22 +60,6 @@ 'C') is a string of |char|s. */ -enum nsFragmentRequest { kPrevFragment, kFirstFragment, kLastFragment, kNextFragment, kFragmentAt }; - -template -struct nsReadableFragment - { - const CharT* mStart; - const CharT* mEnd; - const void* mFragmentIdentifier; - - nsReadableFragment() - : mStart(0), mEnd(0), mFragmentIdentifier(0) - { - // nothing else to do here - } - }; - template class basic_nsAReadableString; template class basic_nsAWritableString; @@ -90,219 +73,6 @@ template class basic_nsAWritableString; #pragma warning( disable: 4284 ) #endif -template -class nsReadingIterator -// : public bidirectional_iterator_tag - { - public: - typedef ptrdiff_t difference_type; - typedef CharT value_type; - typedef const CharT* pointer; - typedef const CharT& reference; -// typedef bidirectional_iterator_tag iterator_category; - - private: - friend class basic_nsAReadableString; - - nsReadableFragment mFragment; - const CharT* mPosition; - const basic_nsAReadableString* mOwningString; - - nsReadingIterator( const nsReadableFragment& aFragment, - const CharT* aStartingPosition, - const basic_nsAReadableString& aOwningString ) - : mFragment(aFragment), - mPosition(aStartingPosition), - mOwningString(&aOwningString) - { - // nothing else to do here - } - - public: - nsReadingIterator() { } - // nsReadingIterator( const nsReadingIterator& ); // auto-generated copy-constructor OK - // nsReadingIterator& operator=( const nsReadingIterator& ); // auto-generated copy-assignment operator OK - - inline void normalize_forward(); - inline void normalize_backward(); - - pointer - get() const - { - return mPosition; - } - - CharT - operator*() const - { - return *get(); - } - -#if 0 - // An iterator really deserves this, but some compilers (notably IBM VisualAge for OS/2) - // don't like this when |CharT| is a type without members. - pointer - operator->() const - { - return get(); - } -#endif - - nsReadingIterator& - operator++() - { - ++mPosition; - normalize_forward(); - return *this; - } - - nsReadingIterator - operator++( int ) - { - nsReadingIterator result(*this); - ++mPosition; - normalize_forward(); - return result; - } - - nsReadingIterator& - operator--() - { - normalize_backward(); - --mPosition; - return *this; - } - - nsReadingIterator - operator--( int ) - { - nsReadingIterator result(*this); - normalize_backward(); - --mPosition; - return result; - } - - const nsReadableFragment& - fragment() const - { - return mFragment; - } - - const basic_nsAReadableString& - string() const - { - NS_ASSERTION(mOwningString, "iterator not attached to a string (|mOwningString| == 0)"); - return *mOwningString; - } - - difference_type - size_forward() const - { - return mFragment.mEnd - mPosition; - } - - difference_type - size_backward() const - { - return mPosition - mFragment.mStart; - } - - nsReadingIterator& - advance( difference_type n ) - { - while ( n > 0 ) - { - difference_type one_hop = NS_MIN(n, size_forward()); - - NS_ASSERTION(one_hop>0, "Infinite loop: can't advance a reading iterator beyond the end of a string"); - // perhaps I should |break| if |!one_hop|? - - mPosition += one_hop; - normalize_forward(); - n -= one_hop; - } - - while ( n < 0 ) - { - normalize_backward(); - difference_type one_hop = NS_MAX(n, -size_backward()); - - NS_ASSERTION(one_hop<0, "Infinite loop: can't advance (backward) a reading iterator beyond the end of a string"); - // perhaps I should |break| if |!one_hop|? - - mPosition += one_hop; - n -= one_hop; - } - - return *this; - } - - /** - * Really don't want to call these two operations |+=| and |-=|. - * Would prefer a single function, e.g., |advance|, which doesn't imply a constant time operation. - * - * We'll get rid of these as soon as we can. - */ - nsReadingIterator& - operator+=( difference_type n ) // deprecated - { - return advance(n); - } - - nsReadingIterator& - operator-=( difference_type n ) // deprecated - { - return advance(-n); - } - }; - -#if 0 -template -nsReadingIterator& -nsReadingIterator::advance( difference_type n ) - { - while ( n > 0 ) - { - difference_type one_hop = NS_MIN(n, size_forward()); - - NS_ASSERTION(one_hop>0, "Infinite loop: can't advance a reading iterator beyond the end of a string"); - // perhaps I should |break| if |!one_hop|? - - mPosition += one_hop; - normalize_forward(); - n -= one_hop; - } - - while ( n < 0 ) - { - normalize_backward(); - difference_type one_hop = NS_MAX(n, -size_backward()); - - NS_ASSERTION(one_hop<0, "Infinite loop: can't advance (backward) a reading iterator beyond the end of a string"); - // perhaps I should |break| if |!one_hop|? - - mPosition += one_hop; - n -= one_hop; - } - - return *this; - } -#endif - - // NOTE: need to break iterators out into their own file (as with many classes here), need - // these routines, but can't currently |#include "nsReadableUtils.h"|, this hack is bad - // but we need it to get OS2 building again. Fix by splitting things into different files. -NS_COM size_t Distance( const nsReadingIterator&, const nsReadingIterator& ); -NS_COM size_t Distance( const nsReadingIterator&, const nsReadingIterator& ); - -template -inline -PRBool -SameFragment( const Iterator& lhs, const Iterator& rhs ) - { - return lhs.fragment().mStart == rhs.fragment().mStart; - } - // // nsAReadable[C]String @@ -313,9 +83,9 @@ class basic_nsAReadableString : public nsPrivateSharableString { public: - typedef CharT char_type; - typedef PRUint32 size_type; - typedef PRUint32 index_type; +// typedef CharT char_type; +// typedef PRUint32 size_type; +// typedef PRUint32 index_type; typedef nsReadingIterator const_iterator; @@ -462,26 +232,6 @@ class basic_nsAReadableString #define NS_READABLE_CAST(CharT, expr) (expr) #endif -template -inline -void -nsReadingIterator::normalize_forward() - { - while ( mPosition == mFragment.mEnd - && mOwningString->GetReadableFragment(mFragment, kNextFragment) ) - mPosition = mFragment.mStart; - } - -template -inline -void -nsReadingIterator::normalize_backward() - { - while ( mPosition == mFragment.mStart - && mOwningString->GetReadableFragment(mFragment, kPrevFragment) ) - mPosition = mFragment.mEnd; - } - /** * Note: measure -- should the |BeginReading| and |EndReading| be |inline|? */ @@ -735,105 +485,6 @@ basic_nsAReadableString::FindChar( CharT aChar, PRUint32 aOffset ) const - // - // nsLiteral[C]String - // - -template -class basic_nsLiteralString - : public basic_nsAReadableString - /* - ...this class wraps a constant literal string and lets it act like an |nsAReadable...|. - - Use it like this: - - SomeFunctionTakingACString( nsLiteralCString("Hello, World!") ); - - With some tweaking, I think I can make this work as well... - - SomeStringFunc( nsLiteralString( L"Hello, World!" ) ); - - This class just holds a pointer. If you don't supply the length, it must calculate it. - No copying or allocations are performed. - - |const basic_nsLiteralString&| appears frequently in interfaces because it - allows the automatic conversion of a |CharT*|. - */ - { - protected: - virtual const CharT* GetReadableFragment( nsReadableFragment&, nsFragmentRequest, PRUint32 ) const; - - public: - - explicit - basic_nsLiteralString( const CharT* aLiteral ) - : mStart(aLiteral), - mEnd(mStart ? (mStart + nsCharTraits::length(mStart)) : mStart) - { - // nothing else to do here - } - - basic_nsLiteralString( const CharT* aLiteral, PRUint32 aLength ) - : mStart(aLiteral), - mEnd(mStart + aLength) - { - // This is an annoying hack. Callers should be fixed to use the other - // constructor if they don't really know the length. - if ( aLength == PRUint32(-1) ) - { -// NS_WARNING("Tell scc: Caller constructing a string doesn't know the real length. Please use the other constructor."); - mEnd = mStart ? (mStart + nsCharTraits::length(mStart)) : mStart; - } - } - - // basic_nsLiteralString( const basic_nsLiteralString& ); // auto-generated copy-constructor OK - // ~basic_nsLiteralString(); // auto-generated destructor OK - - private: - // NOT TO BE IMPLEMENTED - void operator=( const basic_nsLiteralString& ); // we're immutable - - public: - - virtual PRUint32 Length() const; - - - const CharT* get() const { return mStart; } - operator const CharT*() const { return get(); } // to be deprecated, prefer |get()| - - private: - const CharT* mStart; - const CharT* mEnd; - }; - -// NS_DEF_TEMPLATE_STRING_COMPARISON_OPERATORS(basic_nsLiteralString, CharT) - -template -const CharT* -basic_nsLiteralString::GetReadableFragment( nsReadableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const - { - switch ( aRequest ) - { - case kFirstFragment: - case kLastFragment: - case kFragmentAt: - aFragment.mStart = mStart; - aFragment.mEnd = mEnd; - return mStart + aOffset; - - case kPrevFragment: - case kNextFragment: - default: - return 0; - } - } - -template -PRUint32 -basic_nsLiteralString::Length() const - { - return PRUint32(mEnd - mStart); - } #if 0 @@ -842,7 +493,7 @@ inline PRBool basic_nsAReadableString::Equals( const CharT* rhs, PRUint32 rhs_length ) const { - return Compare(basic_nsLiteralString(rhs, rhs_length)) == 0; + return Compare(literal_string(rhs, rhs_length)) == 0; } #endif @@ -852,74 +503,11 @@ inline int basic_nsAReadableString::Compare( const CharT* rhs, PRUint32 rhs_length ) const { - return ::Compare(*this, NS_READABLE_CAST(CharT, basic_nsLiteralString(rhs, rhs_length))); + return ::Compare(*this, NS_READABLE_CAST(CharT, literal_string(rhs, rhs_length))); } #endif - // - // nsLiteralChar, nsLiteralPRUnichar - // - -template -class basic_nsLiteralChar - : public basic_nsAReadableString - { - protected: - virtual const CharT* GetReadableFragment( nsReadableFragment&, nsFragmentRequest, PRUint32 ) const; - - public: - - explicit - basic_nsLiteralChar( CharT aChar ) - : mChar(aChar) - { - // nothing else to do here - } - - // basic_nsLiteralChar( const basic_nsLiteralString& ); // auto-generated copy-constructor OK - // ~basic_nsLiteralChar(); // auto-generated destructor OK - - private: - // NOT TO BE IMPLEMENTED - void operator=( const basic_nsLiteralChar& ); // we're immutable - - public: - - virtual - PRUint32 - Length() const - { - return 1; - } - - private: - CharT mChar; - }; - -template -const CharT* -basic_nsLiteralChar::GetReadableFragment( nsReadableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const - { - switch ( aRequest ) - { - case kFirstFragment: - case kLastFragment: - case kFragmentAt: - aFragment.mEnd = (aFragment.mStart = &mChar) + 1; - return aFragment.mStart + aOffset; - - case kPrevFragment: - case kNextFragment: - default: - return 0; - } - } - - - - - // // nsPromiseConcatenation @@ -1335,7 +923,7 @@ inline int Compare( const basic_nsAReadableString& lhs, const CharT* rhs ) { - return Compare(lhs, NS_READABLE_CAST(CharT, basic_nsLiteralString(rhs))); + return Compare(lhs, NS_READABLE_CAST(CharT, literal_string(rhs))); } template @@ -1343,7 +931,7 @@ inline int Compare( const CharT* lhs, const basic_nsAReadableString& rhs ) { - return Compare(NS_READABLE_CAST(CharT, basic_nsLiteralString(lhs)), rhs); + return Compare(NS_READABLE_CAST(CharT, literal_string(lhs)), rhs); } // XXX Note that these are located here because some compilers are @@ -1361,7 +949,7 @@ inline PRBool basic_nsAReadableString::Equals( const CharT* rhs ) const { - return Compare(basic_nsLiteralString(rhs)) == 0; + return Compare(literal_string(rhs)) == 0; } template @@ -1369,7 +957,7 @@ inline int basic_nsAReadableString::Compare( const CharT* rhs ) const { - return ::Compare(*this, NS_READABLE_CAST(CharT, basic_nsLiteralString(rhs))); + return ::Compare(*this, NS_READABLE_CAST(CharT, literal_string(rhs))); } @@ -1484,7 +1072,7 @@ basic_nsPromiseFlatString::basic_nsPromiseFlatString( const basic_nsARead while ( start != end ) { buffer_count++; - start += start.size_forward(); + start.advance(start.size_forward()); } // Now figure out what we want to do with the string @@ -1539,31 +1127,8 @@ basic_nsPromiseFlatString::GetReadableFragment( nsReadableFragment typedef basic_nsAReadableString nsAReadableString; typedef basic_nsAReadableString nsAReadableCString; -typedef basic_nsLiteralString nsLiteralString; -typedef basic_nsLiteralString nsLiteralCString; - typedef basic_nsPromiseFlatString nsPromiseFlatString; typedef basic_nsPromiseFlatString nsPromiseFlatCString; -#ifdef HAVE_CPP_2BYTE_WCHAR_T - #define NS_L(s) L##s - #define NS_MULTILINE_LITERAL_STRING(s) nsLiteralString(s, (sizeof(s)/sizeof(wchar_t))-1) - #define NS_NAMED_MULTILINE_LITERAL_STRING(n,s) nsLiteralString n(s, (sizeof(s)/sizeof(wchar_t))-1) -#else - #define NS_L(s) s - #define NS_MULTILINE_LITERAL_STRING(s) NS_ConvertASCIItoUCS2(s, sizeof(s)-1) - #define NS_NAMED_MULTILINE_LITERAL_STRING(n,s) NS_ConvertASCIItoUCS2 n(s, sizeof(s)-1) -#endif - -#define NS_LITERAL_STRING(s) NS_MULTILINE_LITERAL_STRING(NS_L(s)) -#define NS_NAMED_LITERAL_STRING(n,s) NS_NAMED_MULTILINE_LITERAL_STRING(n,NS_L(s)) - -#define NS_LITERAL_CSTRING(s) nsLiteralCString(s, sizeof(s)-1) -#define NS_NAMED_LITERAL_CSTRING(n,s) nsLiteralCString n(s, sizeof(s)-1) - -typedef basic_nsLiteralChar nsLiteralChar; -typedef basic_nsLiteralChar nsLiteralPRUnichar; - - #endif // !defined(nsAReadableString_h___) diff --git a/mozilla/string/public/nsAString.h b/mozilla/string/public/nsAString.h new file mode 100644 index 00000000000..c9e909d007b --- /dev/null +++ b/mozilla/string/public/nsAString.h @@ -0,0 +1,96 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +#ifndef nsAString_h___ +#define nsAString_h___ + +#ifndef nsPrivateSharableString_h___ +#include "nsPrivateSharableString.h" +#endif + +#ifndef nsCharTraits_h___ +#include "nsCharTraits.h" +#endif + + + /** + * + */ + +class nsAString + : public nsPrivateSharableString + { + public: + typedef PRUint32 size_type; + + typedef nsReadingIterator const_iterator; + typedef nsWritingIterator iterator; + + // nsAString(); // auto-generated default constructor OK (we're abstract anyway) + // nsAString( const nsAString& ); // auto-generated copy-constructor OK (again, only because we're abstract) + virtual ~nsAString() { } // ...yes, I expect to be sub-classed + + const_iterator& BeginReading( const_iterator& ) const; + const_iterator& EndReading( const_iterator& ) const; + + iterator& BeginWriting( iterator& ); + iterator& EndWriting( iterator& ); + + virtual size_type Length() const = 0; + PRBool IsEmpty() const; + + + // ... + }; + + + /** + * + */ + +class nsACString + : public nsPrivateSharableString + { + public: + typedef PRUint32 size_type; + + typedef nsReadingIterator const_iterator; + typedef nsWritingIterator iterator; + + // nsACString(); // auto-generated default constructor OK (we're abstract anyway) + // nsACString( const nsACString& ); // auto-generated copy-constructor OK (again, only because we're abstract) + virtual ~nsACString() { } // ...yes, I expect to be sub-classed + + const_iterator& BeginReading( const_iterator& ) const; + const_iterator& EndReading( const_iterator& ) const; + + iterator& BeginWriting( iterator& ); + iterator& EndWriting( iterator& ); + + virtual size_type Length() const = 0; + PRBool IsEmpty() const; + + + // ... + }; + +#endif // !defined(nsAString_h___) diff --git a/mozilla/string/public/nsAWritableString.h b/mozilla/string/public/nsAWritableString.h index 209e4ff8766..b123dce5dbc 100644 --- a/mozilla/string/public/nsAWritableString.h +++ b/mozilla/string/public/nsAWritableString.h @@ -29,246 +29,13 @@ #include "nsAReadableString.h" #endif +#ifndef nsLiteralString_h___ +#include "nsLiteralString.h" +#endif -template -struct nsWritableFragment - { - CharT* mStart; - CharT* mEnd; - void* mFragmentIdentifier; - - nsWritableFragment() - : mStart(0), mEnd(0), mFragmentIdentifier(0) - { - // nothing else to do here - } - }; template class basic_nsAWritableString; -template -class nsWritingIterator -// : public bidirectional_iterator_tag - { - public: - typedef ptrdiff_t difference_type; - typedef CharT value_type; - typedef CharT* pointer; - typedef CharT& reference; -// typedef bidirectional_iterator_tag iterator_category; - - private: - friend class basic_nsAWritableString; - - nsWritableFragment mFragment; - CharT* mPosition; - basic_nsAWritableString* mOwningString; - - nsWritingIterator( nsWritableFragment& aFragment, - CharT* aStartingPosition, - basic_nsAWritableString& aOwningString ) - : mFragment(aFragment), - mPosition(aStartingPosition), - mOwningString(&aOwningString) - { - // nothing else to do here - } - - public: - nsWritingIterator() { } - // nsWritingIterator( const nsWritingIterator& ); // auto-generated copy-constructor OK - // nsWritingIterator& operator=( const nsWritingIterator& ); // auto-generated copy-assignment operator OK - - inline void normalize_forward(); - inline void normalize_backward(); - - pointer - get() const - { - return mPosition; - } - - reference - operator*() const - { - return *get(); - } - -#if 0 - // An iterator really deserves this, but some compilers (notably IBM VisualAge for OS/2) - // don't like this when |CharT| is a type without members. - pointer - operator->() const - { - return get(); - } -#endif - - nsWritingIterator& - operator++() - { - ++mPosition; - normalize_forward(); - return *this; - } - - nsWritingIterator - operator++( int ) - { - nsWritingIterator result(*this); - ++mPosition; - normalize_forward(); - return result; - } - - nsWritingIterator& - operator--() - { - normalize_backward(); - --mPosition; - return *this; - } - - nsWritingIterator - operator--( int ) - { - nsWritingIterator result(*this); - normalize_backward(); - --mPosition; - return result; - } - - const nsWritableFragment& - fragment() const - { - return mFragment; - } - - nsWritableFragment& - fragment() - { - return mFragment; - } - - const basic_nsAWritableString& - string() const - { - NS_ASSERTION(mOwningString, "iterator not attached to a string (|mOwningString| == 0)"); - return *mOwningString; - } - - basic_nsAWritableString& - string() - { - NS_ASSERTION(mOwningString, "iterator not attached to a string (|mOwningString| == 0)"); - return *mOwningString; - } - - difference_type - size_forward() const - { - return mFragment.mEnd - mPosition; - } - - difference_type - size_backward() const - { - return mPosition - mFragment.mStart; - } - - nsWritingIterator& - advance( difference_type n ) - { - while ( n > 0 ) - { - difference_type one_hop = NS_MIN(n, size_forward()); - - NS_ASSERTION(one_hop>0, "Infinite loop: can't advance a writing iterator beyond the end of a string"); - // perhaps I should |break| if |!one_hop|? - - mPosition += one_hop; - normalize_forward(); - n -= one_hop; - } - - while ( n < 0 ) - { - normalize_backward(); - difference_type one_hop = NS_MAX(n, -size_backward()); - - NS_ASSERTION(one_hop<0, "Infinite loop: can't advance (backward) a writing iterator beyond the end of a string"); - // perhaps I should |break| if |!one_hop|? - - mPosition += one_hop; - n -= one_hop; - } - - return *this; - } - - /** - * Really don't want to call these two operations |+=| and |-=|. - * Would prefer a single function, e.g., |advance|, which doesn't imply a constant time operation. - * - * We'll get rid of these as soon as we can. - */ - nsWritingIterator& - operator+=( difference_type n ) // deprecated - { - return advance(n); - } - - nsWritingIterator& - operator-=( difference_type n ) // deprecated - { - return advance(-n); - } - - PRUint32 - write( const value_type* s, PRUint32 n ) - { - NS_ASSERTION(size_forward() > 0, "You can't |write| into an |nsWritingIterator| with no space!"); - - n = NS_MIN(n, PRUint32(size_forward())); - nsCharTraits::move(mPosition, s, n); - advance( difference_type(n) ); - return n; - } - }; - -#if 0 -template -nsWritingIterator& -nsWritingIterator::advance( difference_type n ) - { - while ( n > 0 ) - { - difference_type one_hop = NS_MIN(n, size_forward()); - - NS_ASSERTION(one_hop>0, "Infinite loop: can't advance a writing iterator beyond the end of a string"); - // perhaps I should |break| if |!one_hop|? - - mPosition += one_hop; - normalize_forward(); - n -= one_hop; - } - - while ( n < 0 ) - { - normalize_backward(); - difference_type one_hop = NS_MAX(n, -size_backward()); - - NS_ASSERTION(one_hop<0, "Infinite loop: can't advance (backward) a writing iterator beyond the end of a string"); - // perhaps I should |break| if |!one_hop|? - - mPosition += one_hop; - n -= one_hop; - } - - return *this; - } -#endif - /* This file defines the abstract interfaces |nsAWritableString| and |nsAWritableCString|. @@ -477,46 +244,6 @@ class basic_nsAWritableString - // - // |nsWritingIterator|s - // - -template -inline -void -nsWritingIterator::normalize_forward() - { - while ( mPosition == mFragment.mEnd - && mOwningString->GetWritableFragment(mFragment, kNextFragment) ) - mPosition = mFragment.mStart; - } - -template -inline -void -nsWritingIterator::normalize_backward() - { - while ( mPosition == mFragment.mStart - && mOwningString->GetWritableFragment(mFragment, kPrevFragment) ) - mPosition = mFragment.mEnd; - } - -template -inline -PRBool -operator==( const nsWritingIterator& lhs, const nsWritingIterator& rhs ) - { - return lhs.get() == rhs.get(); - } - -template -inline -PRBool -operator!=( const nsWritingIterator& lhs, const nsWritingIterator& rhs ) - { - return lhs.get() != rhs.get(); - } - // @@ -594,21 +321,21 @@ template void basic_nsAWritableString::do_AssignFromElementPtr( const CharT* aPtr ) { - do_AssignFromReadable(basic_nsLiteralString(aPtr)); + do_AssignFromReadable(literal_string(aPtr)); } template void basic_nsAWritableString::do_AssignFromElementPtrLength( const CharT* aPtr, PRUint32 aLength ) { - do_AssignFromReadable(basic_nsLiteralString(aPtr, aLength)); + do_AssignFromReadable(literal_string(aPtr, aLength)); } template void basic_nsAWritableString::do_AssignFromElement( CharT aChar ) { - do_AssignFromReadable(basic_nsLiteralChar(aChar)); + do_AssignFromReadable(literal_string(&aChar, 1)); } @@ -665,21 +392,21 @@ template void basic_nsAWritableString::do_AppendFromElementPtr( const CharT* aChar ) { - do_AppendFromReadable(basic_nsLiteralString(aChar)); + do_AppendFromReadable(literal_string(aChar)); } template void basic_nsAWritableString::do_AppendFromElementPtrLength( const CharT* aChar, PRUint32 aLength ) { - do_AppendFromReadable(basic_nsLiteralString(aChar, aLength)); + do_AppendFromReadable(literal_string(aChar, aLength)); } template void basic_nsAWritableString::do_AppendFromElement( CharT aChar ) { - do_AppendFromReadable(basic_nsLiteralChar(aChar)); + do_AppendFromReadable(literal_string(&aChar, 1)); } @@ -740,21 +467,21 @@ template void basic_nsAWritableString::do_InsertFromElementPtr( const CharT* aPtr, PRUint32 atPosition ) { - do_InsertFromReadable(basic_nsLiteralString(aPtr), atPosition); + do_InsertFromReadable(literal_string(aPtr), atPosition); } template void basic_nsAWritableString::do_InsertFromElementPtrLength( const CharT* aPtr, PRUint32 atPosition, PRUint32 aLength ) { - do_InsertFromReadable(basic_nsLiteralString(aPtr, aLength), atPosition); + do_InsertFromReadable(literal_string(aPtr, aLength), atPosition); } template void basic_nsAWritableString::do_InsertFromElement( CharT aChar, PRUint32 atPosition ) { - do_InsertFromReadable(basic_nsLiteralChar(aChar), atPosition); + do_InsertFromReadable(literal_string(&aChar, 1), atPosition); } @@ -809,7 +536,7 @@ basic_nsAWritableString::ReplaceFromPromise( PRUint32 cutStart, PRUint32 nsReadingIterator fromBegin, fromEnd; CharT* toBegin = buffer; copy_string(aReadable.BeginReading(fromBegin), aReadable.EndReading(fromEnd), toBegin); - do_ReplaceFromReadable(cutStart, cutLength, basic_nsLiteralString(buffer, length)); + do_ReplaceFromReadable(cutStart, cutLength, literal_string(buffer, length)); delete buffer; } // else assert? diff --git a/mozilla/string/public/nsCharTraits.h b/mozilla/string/public/nsCharTraits.h index dc4b681a1f5..964c6be8646 100644 --- a/mozilla/string/public/nsCharTraits.h +++ b/mozilla/string/public/nsCharTraits.h @@ -35,6 +35,10 @@ // for |PRUnichar| #endif +#ifndef nsStringIteratorUtils_h___ +#include "nsStringIteratorUtils.h" +#endif + #ifdef HAVE_CPP_BOOL typedef bool nsCharTraits_bool; diff --git a/mozilla/string/public/nsDependentString.h b/mozilla/string/public/nsDependentString.h new file mode 100644 index 00000000000..6e44379e262 --- /dev/null +++ b/mozilla/string/public/nsDependentString.h @@ -0,0 +1,148 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +#ifndef nsLocalString_h___ +#define nsLocalString_h___ + +#ifndef nsAReadableString_h___ +#include "nsAReadableString.h" +#endif + + /* + ...this class wraps a constant literal string and lets it act like an |nsAReadable...|. + + Use it like this: + + SomeFunctionTakingACString( nsLiteralCString("Hello, World!") ); + + With some tweaking, I think I can make this work as well... + + SomeStringFunc( nsLiteralString( L"Hello, World!" ) ); + + This class just holds a pointer. If you don't supply the length, it must calculate it. + No copying or allocations are performed. + + |const nsLocalString&| appears frequently in interfaces because it + allows the automatic conversion of a |PRUnichar*|. + */ + +class NS_COM nsLocalString + : public basic_nsAReadableString + { + protected: + virtual const PRUnichar* GetReadableFragment( nsReadableFragment&, nsFragmentRequest, PRUint32 ) const; + + public: + + explicit + nsLocalString( const PRUnichar* aLiteral ) + : mStart(aLiteral), + mEnd(mStart ? (mStart + nsCharTraits::length(mStart)) : mStart) + { + // nothing else to do here + } + + nsLocalString( const PRUnichar* aLiteral, PRUint32 aLength ) + : mStart(aLiteral), + mEnd(mStart + aLength) + { + // This is an annoying hack. Callers should be fixed to use the other + // constructor if they don't really know the length. + if ( aLength == PRUint32(-1) ) + { +// NS_WARNING("Tell scc: Caller constructing a string doesn't know the real length. Please use the other constructor."); + mEnd = mStart ? (mStart + nsCharTraits::length(mStart)) : mStart; + } + } + + // nsLocalString( const nsLocalString& ); // auto-generated copy-constructor OK + // ~nsLocalString(); // auto-generated destructor OK + + private: + // NOT TO BE IMPLEMENTED + void operator=( const nsLocalString& ); // we're immutable + + public: + + virtual PRUint32 Length() const; + + + const PRUnichar* get() const { return mStart; } + operator const PRUnichar*() const { return get(); } // to be deprecated, prefer |get()| + + private: + const PRUnichar* mStart; + const PRUnichar* mEnd; + }; + + + +class NS_COM nsLocalCString + : public basic_nsAReadableString + { + protected: + virtual const char* GetReadableFragment( nsReadableFragment&, nsFragmentRequest, PRUint32 ) const; + + public: + + explicit + nsLocalCString( const char* aLiteral ) + : mStart(aLiteral), + mEnd(mStart ? (mStart + nsCharTraits::length(mStart)) : mStart) + { + // nothing else to do here + } + + nsLocalCString( const char* aLiteral, PRUint32 aLength ) + : mStart(aLiteral), + mEnd(mStart + aLength) + { + // This is an annoying hack. Callers should be fixed to use the other + // constructor if they don't really know the length. + if ( aLength == PRUint32(-1) ) + { +// NS_WARNING("Tell scc: Caller constructing a string doesn't know the real length. Please use the other constructor."); + mEnd = mStart ? (mStart + nsCharTraits::length(mStart)) : mStart; + } + } + + // nsLocalCString( const nsLocalCString& ); // auto-generated copy-constructor OK + // ~nsLocalCString(); // auto-generated destructor OK + + private: + // NOT TO BE IMPLEMENTED + void operator=( const nsLocalCString& ); // we're immutable + + public: + + virtual PRUint32 Length() const; + + + const char* get() const { return mStart; } + operator const char*() const { return get(); } // to be deprecated, prefer |get()| + + private: + const char* mStart; + const char* mEnd; + }; + +#endif /* !defined(nsLocalString_h___) */ diff --git a/mozilla/string/public/nsLiteralString.h b/mozilla/string/public/nsLiteralString.h new file mode 100644 index 00000000000..53a735ba4f1 --- /dev/null +++ b/mozilla/string/public/nsLiteralString.h @@ -0,0 +1,86 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +#ifndef nsLiteralString_h___ +#define nsLiteralString_h___ + +#ifndef nscore_h___ +#include "nscore.h" +#endif + +#ifndef nsLocalString_h___ +#include "nsLocalString.h" +#endif + +typedef const nsLocalString nsLiteralString; +typedef const nsLocalCString nsLiteralCString; + +template +struct nsLiteralStringTraits + { + }; + +NS_SPECIALIZE_TEMPLATE +struct nsLiteralStringTraits + { + typedef nsLiteralString literal_string_type; + }; + +NS_SPECIALIZE_TEMPLATE +struct nsLiteralStringTraits + { + typedef nsLiteralCString literal_string_type; + }; + +template +inline +const typename nsLiteralStringTraits::literal_string_type +literal_string( const CharT* aPtr ) + { + return nsLiteralStringTraits::literal_string_type(aPtr); + } + +template +inline +const typename nsLiteralStringTraits::literal_string_type +literal_string( const CharT* aPtr, PRUint32 aLength ) + { + return nsLiteralStringTraits::literal_string_type(aPtr, aLength); + } + +#ifdef HAVE_CPP_2BYTE_WCHAR_T + #define NS_L(s) L##s + #define NS_MULTILINE_LITERAL_STRING(s) nsLiteralString(s, (sizeof(s)/sizeof(wchar_t))-1) + #define NS_NAMED_MULTILINE_LITERAL_STRING(n,s) nsLiteralString n(s, (sizeof(s)/sizeof(wchar_t))-1) +#else + #define NS_L(s) s + #define NS_MULTILINE_LITERAL_STRING(s) NS_ConvertASCIItoUCS2(s, sizeof(s)-1) + #define NS_NAMED_MULTILINE_LITERAL_STRING(n,s) NS_ConvertASCIItoUCS2 n(s, sizeof(s)-1) +#endif + +#define NS_LITERAL_STRING(s) NS_MULTILINE_LITERAL_STRING(NS_L(s)) +#define NS_NAMED_LITERAL_STRING(n,s) NS_NAMED_MULTILINE_LITERAL_STRING(n,NS_L(s)) + +#define NS_LITERAL_CSTRING(s) nsLiteralCString(s, sizeof(s)-1) +#define NS_NAMED_LITERAL_CSTRING(n,s) nsLiteralCString n(s, sizeof(s)-1) + +#endif /* !defined(nsLiteralString_h___) */ \ No newline at end of file diff --git a/mozilla/string/public/nsLocalString.h b/mozilla/string/public/nsLocalString.h new file mode 100644 index 00000000000..6e44379e262 --- /dev/null +++ b/mozilla/string/public/nsLocalString.h @@ -0,0 +1,148 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +#ifndef nsLocalString_h___ +#define nsLocalString_h___ + +#ifndef nsAReadableString_h___ +#include "nsAReadableString.h" +#endif + + /* + ...this class wraps a constant literal string and lets it act like an |nsAReadable...|. + + Use it like this: + + SomeFunctionTakingACString( nsLiteralCString("Hello, World!") ); + + With some tweaking, I think I can make this work as well... + + SomeStringFunc( nsLiteralString( L"Hello, World!" ) ); + + This class just holds a pointer. If you don't supply the length, it must calculate it. + No copying or allocations are performed. + + |const nsLocalString&| appears frequently in interfaces because it + allows the automatic conversion of a |PRUnichar*|. + */ + +class NS_COM nsLocalString + : public basic_nsAReadableString + { + protected: + virtual const PRUnichar* GetReadableFragment( nsReadableFragment&, nsFragmentRequest, PRUint32 ) const; + + public: + + explicit + nsLocalString( const PRUnichar* aLiteral ) + : mStart(aLiteral), + mEnd(mStart ? (mStart + nsCharTraits::length(mStart)) : mStart) + { + // nothing else to do here + } + + nsLocalString( const PRUnichar* aLiteral, PRUint32 aLength ) + : mStart(aLiteral), + mEnd(mStart + aLength) + { + // This is an annoying hack. Callers should be fixed to use the other + // constructor if they don't really know the length. + if ( aLength == PRUint32(-1) ) + { +// NS_WARNING("Tell scc: Caller constructing a string doesn't know the real length. Please use the other constructor."); + mEnd = mStart ? (mStart + nsCharTraits::length(mStart)) : mStart; + } + } + + // nsLocalString( const nsLocalString& ); // auto-generated copy-constructor OK + // ~nsLocalString(); // auto-generated destructor OK + + private: + // NOT TO BE IMPLEMENTED + void operator=( const nsLocalString& ); // we're immutable + + public: + + virtual PRUint32 Length() const; + + + const PRUnichar* get() const { return mStart; } + operator const PRUnichar*() const { return get(); } // to be deprecated, prefer |get()| + + private: + const PRUnichar* mStart; + const PRUnichar* mEnd; + }; + + + +class NS_COM nsLocalCString + : public basic_nsAReadableString + { + protected: + virtual const char* GetReadableFragment( nsReadableFragment&, nsFragmentRequest, PRUint32 ) const; + + public: + + explicit + nsLocalCString( const char* aLiteral ) + : mStart(aLiteral), + mEnd(mStart ? (mStart + nsCharTraits::length(mStart)) : mStart) + { + // nothing else to do here + } + + nsLocalCString( const char* aLiteral, PRUint32 aLength ) + : mStart(aLiteral), + mEnd(mStart + aLength) + { + // This is an annoying hack. Callers should be fixed to use the other + // constructor if they don't really know the length. + if ( aLength == PRUint32(-1) ) + { +// NS_WARNING("Tell scc: Caller constructing a string doesn't know the real length. Please use the other constructor."); + mEnd = mStart ? (mStart + nsCharTraits::length(mStart)) : mStart; + } + } + + // nsLocalCString( const nsLocalCString& ); // auto-generated copy-constructor OK + // ~nsLocalCString(); // auto-generated destructor OK + + private: + // NOT TO BE IMPLEMENTED + void operator=( const nsLocalCString& ); // we're immutable + + public: + + virtual PRUint32 Length() const; + + + const char* get() const { return mStart; } + operator const char*() const { return get(); } // to be deprecated, prefer |get()| + + private: + const char* mStart; + const char* mEnd; + }; + +#endif /* !defined(nsLocalString_h___) */ diff --git a/mozilla/string/public/nsSharedBufferList.h b/mozilla/string/public/nsSharedBufferList.h index a97c2af1ec6..5a5ad9330c5 100755 --- a/mozilla/string/public/nsSharedBufferList.h +++ b/mozilla/string/public/nsSharedBufferList.h @@ -40,6 +40,10 @@ // for |nsReadingIterator| #endif +#ifndef nsLiteralString_h___ +#include "nsLiteralString.h" +#endif + #ifndef nsBufferHandleUtils_h___ #include "nsBufferHandleUtils.h" // for |NS_AllocateContiguousHandleWithData| @@ -180,7 +184,7 @@ class NS_COM nsSharedBufferList NewSingleAllocationBuffer( const PRUnichar* aData, PRUint32 aDataLength, PRUint32 aAdditionalCapacity = 1 ) { typedef Buffer* Buffer_ptr; - return NS_AllocateContiguousHandleWithData(Buffer_ptr(0), NS_READABLE_CAST(PRUnichar, nsLiteralString(aData, aDataLength)), aAdditionalCapacity); + return NS_AllocateContiguousHandleWithData(Buffer_ptr(0), NS_READABLE_CAST(PRUnichar, literal_string(aData, aDataLength)), aAdditionalCapacity); } static diff --git a/mozilla/string/public/nsStringFragment.h b/mozilla/string/public/nsStringFragment.h new file mode 100644 index 00000000000..b1296c291fd --- /dev/null +++ b/mozilla/string/public/nsStringFragment.h @@ -0,0 +1,93 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +/* nsStringFragment.h --- machinery that makes string iterators work */ + +#ifndef nsStringFragment_h___ +#define nsStringFragment_h___ + + + + /** + * An |nsFragmentRequest| is used to tell |GetReadableFragment| and + * |GetWritableFragment| what to do. + * + * @see GetReadableFragment + */ + +enum nsFragmentRequest { kPrevFragment, kFirstFragment, kLastFragment, kNextFragment, kFragmentAt }; + + + /** + * A |nsReadableFragment| provides |const| access to a contiguous hunk of + * string of homogenous units, e.g., bytes (|char|). This doesn't mean it + * represents a flat hunk. It could be a variable length encoding, for + * instance UTF-8. And the fragment itself need not be zero-terminated. + * + * An |nsReadableFragment| is the underlying machinery that lets + * |nsReadingIterator|s work. + * + * @see nsReadingIterator + */ + +template +struct nsReadableFragment + { + const CharT* mStart; + const CharT* mEnd; + const void* mFragmentIdentifier; + + nsReadableFragment() + : mStart(0), mEnd(0), mFragmentIdentifier(0) + { + // nothing else to do here + } + }; + + + /** + * A |nsWritableFragment| provides non-|const| access to a contiguous hunk of + * string of homogenous units, e.g., bytes (|char|). This doesn't mean it + * represents a flat hunk. It could be a variable length encoding, for + * instance UTF-8. And the fragment itself need not be zero-terminated. + * + * An |nsWritableFragment| is the underlying machinery that lets + * |nsWritingIterator|s work. + * + * @see nsWritingIterator + */ + +template +struct nsWritableFragment + { + CharT* mStart; + CharT* mEnd; + void* mFragmentIdentifier; + + nsWritableFragment() + : mStart(0), mEnd(0), mFragmentIdentifier(0) + { + // nothing else to do here + } + }; + +#endif /* !defined(nsStringFragment_h___) */ diff --git a/mozilla/string/public/nsStringIterator.h b/mozilla/string/public/nsStringIterator.h new file mode 100644 index 00000000000..316bd540465 --- /dev/null +++ b/mozilla/string/public/nsStringIterator.h @@ -0,0 +1,408 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +#ifndef nsStringIterator_h___ +#define nsStringIterator_h___ + +#ifndef nsStringFragment_h___ +#include "nsStringFragment.h" +#endif + +#ifndef nsAlgorithm_h___ +#include "nsAlgorithm.h" + // for |NS_MIN|, |NS_MAX|, and |NS_COUNT|... +#endif + + +template class basic_nsAReadableString; +template class basic_nsAWritableString; + + + + /** + * + * @see nsReadableFragment + * @see basic_nsAReadableString + */ + +template +class nsReadingIterator +// : public bidirectional_iterator_tag + { + public: + typedef ptrdiff_t difference_type; + typedef CharT value_type; + typedef const CharT* pointer; + typedef const CharT& reference; +// typedef bidirectional_iterator_tag iterator_category; + + private: + friend class basic_nsAReadableString; + + nsReadableFragment mFragment; + const CharT* mPosition; + const basic_nsAReadableString* mOwningString; + + public: + nsReadingIterator() { } + // nsReadingIterator( const nsReadingIterator& ); // auto-generated copy-constructor OK + // nsReadingIterator& operator=( const nsReadingIterator& ); // auto-generated copy-assignment operator OK + + inline void normalize_forward(); + inline void normalize_backward(); + + pointer + get() const + { + return mPosition; + } + + CharT + operator*() const + { + return *get(); + } + +#if 0 + // An iterator really deserves this, but some compilers (notably IBM VisualAge for OS/2) + // don't like this when |CharT| is a type without members. + pointer + operator->() const + { + return get(); + } +#endif + + nsReadingIterator& + operator++() + { + ++mPosition; + normalize_forward(); + return *this; + } + + nsReadingIterator + operator++( int ) + { + nsReadingIterator result(*this); + ++mPosition; + normalize_forward(); + return result; + } + + nsReadingIterator& + operator--() + { + normalize_backward(); + --mPosition; + return *this; + } + + nsReadingIterator + operator--( int ) + { + nsReadingIterator result(*this); + normalize_backward(); + --mPosition; + return result; + } + + const nsReadableFragment& + fragment() const + { + return mFragment; + } + + const basic_nsAReadableString& + string() const + { + NS_ASSERTION(mOwningString, "iterator not attached to a string (|mOwningString| == 0)"); + return *mOwningString; + } + + difference_type + size_forward() const + { + return mFragment.mEnd - mPosition; + } + + difference_type + size_backward() const + { + return mPosition - mFragment.mStart; + } + + nsReadingIterator& + advance( difference_type n ) + { + while ( n > 0 ) + { + difference_type one_hop = NS_MIN(n, size_forward()); + + NS_ASSERTION(one_hop>0, "Infinite loop: can't advance a reading iterator beyond the end of a string"); + // perhaps I should |break| if |!one_hop|? + + mPosition += one_hop; + normalize_forward(); + n -= one_hop; + } + + while ( n < 0 ) + { + normalize_backward(); + difference_type one_hop = NS_MAX(n, -size_backward()); + + NS_ASSERTION(one_hop<0, "Infinite loop: can't advance (backward) a reading iterator beyond the end of a string"); + // perhaps I should |break| if |!one_hop|? + + mPosition += one_hop; + n -= one_hop; + } + + return *this; + } + }; + +template +class nsWritingIterator +// : public nsReadingIterator + { + public: + typedef ptrdiff_t difference_type; + typedef CharT value_type; + typedef CharT* pointer; + typedef CharT& reference; +// typedef bidirectional_iterator_tag iterator_category; + + private: + friend class basic_nsAWritableString; + + nsWritableFragment mFragment; + CharT* mPosition; + basic_nsAWritableString* mOwningString; + + public: + nsWritingIterator() { } + // nsWritingIterator( const nsWritingIterator& ); // auto-generated copy-constructor OK + // nsWritingIterator& operator=( const nsWritingIterator& ); // auto-generated copy-assignment operator OK + + inline void normalize_forward(); + inline void normalize_backward(); + + pointer + get() const + { + return mPosition; + } + + reference + operator*() const + { + return *get(); + } + +#if 0 + // An iterator really deserves this, but some compilers (notably IBM VisualAge for OS/2) + // don't like this when |CharT| is a type without members. + pointer + operator->() const + { + return get(); + } +#endif + + nsWritingIterator& + operator++() + { + ++mPosition; + normalize_forward(); + return *this; + } + + nsWritingIterator + operator++( int ) + { + nsWritingIterator result(*this); + ++mPosition; + normalize_forward(); + return result; + } + + nsWritingIterator& + operator--() + { + normalize_backward(); + --mPosition; + return *this; + } + + nsWritingIterator + operator--( int ) + { + nsWritingIterator result(*this); + normalize_backward(); + --mPosition; + return result; + } + + const nsWritableFragment& + fragment() const + { + return mFragment; + } + + nsWritableFragment& + fragment() + { + return mFragment; + } + + const basic_nsAWritableString& + string() const + { + NS_ASSERTION(mOwningString, "iterator not attached to a string (|mOwningString| == 0)"); + return *mOwningString; + } + + basic_nsAWritableString& + string() + { + NS_ASSERTION(mOwningString, "iterator not attached to a string (|mOwningString| == 0)"); + return *mOwningString; + } + + difference_type + size_forward() const + { + return mFragment.mEnd - mPosition; + } + + difference_type + size_backward() const + { + return mPosition - mFragment.mStart; + } + + nsWritingIterator& + advance( difference_type n ) + { + while ( n > 0 ) + { + difference_type one_hop = NS_MIN(n, size_forward()); + + NS_ASSERTION(one_hop>0, "Infinite loop: can't advance a writing iterator beyond the end of a string"); + // perhaps I should |break| if |!one_hop|? + + mPosition += one_hop; + normalize_forward(); + n -= one_hop; + } + + while ( n < 0 ) + { + normalize_backward(); + difference_type one_hop = NS_MAX(n, -size_backward()); + + NS_ASSERTION(one_hop<0, "Infinite loop: can't advance (backward) a writing iterator beyond the end of a string"); + // perhaps I should |break| if |!one_hop|? + + mPosition += one_hop; + n -= one_hop; + } + + return *this; + } + + PRUint32 + write( const value_type* s, PRUint32 n ) + { + NS_ASSERTION(size_forward() > 0, "You can't |write| into an |nsWritingIterator| with no space!"); + + n = NS_MIN(n, PRUint32(size_forward())); + nsCharTraits::move(mPosition, s, n); + advance( difference_type(n) ); + return n; + } + }; + +template +inline +void +nsReadingIterator::normalize_forward() + { + while ( mPosition == mFragment.mEnd + && mOwningString->GetReadableFragment(mFragment, kNextFragment) ) + mPosition = mFragment.mStart; + } + +template +inline +void +nsReadingIterator::normalize_backward() + { + while ( mPosition == mFragment.mStart + && mOwningString->GetReadableFragment(mFragment, kPrevFragment) ) + mPosition = mFragment.mEnd; + } + + + // + // |nsWritingIterator|s + // + +template +inline +void +nsWritingIterator::normalize_forward() + { + while ( mPosition == mFragment.mEnd + && mOwningString->GetWritableFragment(mFragment, kNextFragment) ) + mPosition = mFragment.mStart; + } + +template +inline +void +nsWritingIterator::normalize_backward() + { + while ( mPosition == mFragment.mStart + && mOwningString->GetWritableFragment(mFragment, kPrevFragment) ) + mPosition = mFragment.mEnd; + } + +template +inline +PRBool +operator==( const nsWritingIterator& lhs, const nsWritingIterator& rhs ) + { + return lhs.get() == rhs.get(); + } + +template +inline +PRBool +operator!=( const nsWritingIterator& lhs, const nsWritingIterator& rhs ) + { + return lhs.get() != rhs.get(); + } + +#endif /* !defined(nsStringIterator_h___) */ diff --git a/mozilla/string/public/nsStringIteratorUtils.h b/mozilla/string/public/nsStringIteratorUtils.h new file mode 100644 index 00000000000..1bed6be81f3 --- /dev/null +++ b/mozilla/string/public/nsStringIteratorUtils.h @@ -0,0 +1,46 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +#ifndef nsStringIteratorUtils_h___ +#define nsStringIteratorUtils_h___ + +#ifndef nsStringIterator_h___ +#include "nsStringIterator.h" +#endif + +template +inline +PRBool +SameFragment( const Iterator& lhs, const Iterator& rhs ) + { + return lhs.fragment().mStart == rhs.fragment().mStart; + } + + + // NOTE: need to break iterators out into their own file (as with many classes here), need + // these routines, but can't currently |#include "nsReadableUtils.h"|, this hack is bad + // but we need it to get OS2 building again. Fix by splitting things into different files. +NS_COM size_t Distance( const nsReadingIterator&, const nsReadingIterator& ); +NS_COM size_t Distance( const nsReadingIterator&, const nsReadingIterator& ); + + +#endif /* !defined(nsStringIteratorUtils_h___) */ diff --git a/mozilla/string/src/Makefile.in b/mozilla/string/src/Makefile.in index fdec9c55559..16189711271 100644 --- a/mozilla/string/src/Makefile.in +++ b/mozilla/string/src/Makefile.in @@ -18,6 +18,7 @@ # # Contributor(s): # Johnny Stenback (original author) +# Scott Collins # DEPTH = ../.. @@ -33,6 +34,7 @@ LIBRARY_NAME = string_s REQUIRES = xpcom CPPSRCS = \ + nsLocalString.cpp \ nsFragmentedString.cpp \ nsPrintfCString.cpp \ nsReadableUtils.cpp \ diff --git a/mozilla/string/src/makefile.win b/mozilla/string/src/makefile.win index 4442c23ef74..d4cd1a950d1 100644 --- a/mozilla/string/src/makefile.win +++ b/mozilla/string/src/makefile.win @@ -19,6 +19,7 @@ # # Contributor(s): # Johnny Stenback (original author) +# Scott Collins # DEPTH=..\.. @@ -28,6 +29,7 @@ LIBRARY_NAME=string_s LCFLAGS = -D_IMPL_NS_COM -D_IMPL_NS_BASE -DWIN32_LEAN_AND_MEAN CPP_OBJS = \ + .\$(OBJDIR)\nsLocalString.obj \ .\$(OBJDIR)\nsFragmentedString.obj \ .\$(OBJDIR)\nsPrintfCString.obj \ .\$(OBJDIR)\nsReadableUtils.obj \ diff --git a/mozilla/string/src/nsDependentString.cpp b/mozilla/string/src/nsDependentString.cpp new file mode 100644 index 00000000000..3a077a67251 --- /dev/null +++ b/mozilla/string/src/nsDependentString.cpp @@ -0,0 +1,74 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +#include "nsLocalString.h" + + +const PRUnichar* +nsLocalString::GetReadableFragment( nsReadableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const + { + switch ( aRequest ) + { + case kFirstFragment: + case kLastFragment: + case kFragmentAt: + aFragment.mStart = mStart; + aFragment.mEnd = mEnd; + return mStart + aOffset; + + case kPrevFragment: + case kNextFragment: + default: + return 0; + } + } + +PRUint32 +nsLocalString::Length() const + { + return PRUint32(mEnd - mStart); + } + +const char* +nsLocalCString::GetReadableFragment( nsReadableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const + { + switch ( aRequest ) + { + case kFirstFragment: + case kLastFragment: + case kFragmentAt: + aFragment.mStart = mStart; + aFragment.mEnd = mEnd; + return mStart + aOffset; + + case kPrevFragment: + case kNextFragment: + default: + return 0; + } + } + +PRUint32 +nsLocalCString::Length() const + { + return PRUint32(mEnd - mStart); + } diff --git a/mozilla/string/src/nsLocalString.cpp b/mozilla/string/src/nsLocalString.cpp new file mode 100644 index 00000000000..3a077a67251 --- /dev/null +++ b/mozilla/string/src/nsLocalString.cpp @@ -0,0 +1,74 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +#include "nsLocalString.h" + + +const PRUnichar* +nsLocalString::GetReadableFragment( nsReadableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const + { + switch ( aRequest ) + { + case kFirstFragment: + case kLastFragment: + case kFragmentAt: + aFragment.mStart = mStart; + aFragment.mEnd = mEnd; + return mStart + aOffset; + + case kPrevFragment: + case kNextFragment: + default: + return 0; + } + } + +PRUint32 +nsLocalString::Length() const + { + return PRUint32(mEnd - mStart); + } + +const char* +nsLocalCString::GetReadableFragment( nsReadableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const + { + switch ( aRequest ) + { + case kFirstFragment: + case kLastFragment: + case kFragmentAt: + aFragment.mStart = mStart; + aFragment.mEnd = mEnd; + return mStart + aOffset; + + case kPrevFragment: + case kNextFragment: + default: + return 0; + } + } + +PRUint32 +nsLocalCString::Length() const + { + return PRUint32(mEnd - mStart); + } diff --git a/mozilla/xpcom/string/README.html b/mozilla/xpcom/string/README.html index c042892a33e..eb54a3df194 100644 --- a/mozilla/xpcom/string/README.html +++ b/mozilla/xpcom/string/README.html @@ -21,11 +21,9 @@ - Scott Collins (original author) --> -

string code will be moving here soon

+

managing sequences of characters

- The string code will be moving here soon; - in the meantime, you can find the bulk of it in - mozilla/xpcom/ds. +

diff --git a/mozilla/xpcom/string/doc/README.html b/mozilla/xpcom/string/doc/README.html index af9eee88be5..e9a28f949ae 100644 --- a/mozilla/xpcom/string/doc/README.html +++ b/mozilla/xpcom/string/doc/README.html @@ -21,7 +21,7 @@ - Scott Collins (original author) --> -

string doc will be moving here soon

+

documentation aimed at programmers who are clients of the string library

diff --git a/mozilla/xpcom/string/macbuild/string.mcp b/mozilla/xpcom/string/macbuild/string.mcp index b37d99192d8..17264396504 100644 Binary files a/mozilla/xpcom/string/macbuild/string.mcp and b/mozilla/xpcom/string/macbuild/string.mcp differ diff --git a/mozilla/xpcom/string/obsolete/nsString.cpp b/mozilla/xpcom/string/obsolete/nsString.cpp index ade2c679bd6..057ffcea00c 100644 --- a/mozilla/xpcom/string/obsolete/nsString.cpp +++ b/mozilla/xpcom/string/obsolete/nsString.cpp @@ -792,8 +792,8 @@ void nsCString::AssignWithConversion( const nsAReadableString& aString ) { temp.mLength=fraglen; StrAppend(*this,temp,0,fraglen); - - start += fraglen; + + start.advance(fraglen); } } } @@ -816,7 +816,7 @@ void nsCString::AppendWithConversion( const nsAReadableString& aString ) { StrAppend(*this,temp,0,fraglen); - start += fraglen; + start.advance(fraglen); } } } @@ -1390,7 +1390,7 @@ NS_ConvertUCS2toUTF8::NS_ConvertUCS2toUTF8( const nsAReadableString& aString ) while (start != end) { nsReadableFragment frag(start.fragment()); Append(frag.mStart, frag.mEnd - frag.mStart); - start += start.size_forward(); + start.advance(start.size_forward()); } } diff --git a/mozilla/xpcom/string/obsolete/nsString.h b/mozilla/xpcom/string/obsolete/nsString.h index bf6d0fe2268..5c90e1e12f3 100644 --- a/mozilla/xpcom/string/obsolete/nsString.h +++ b/mozilla/xpcom/string/obsolete/nsString.h @@ -51,6 +51,7 @@ #include "nsIAtom.h" #include "nsAWritableString.h" +#include "nsLiteralString.h" class NS_COM nsSubsumeCStr; diff --git a/mozilla/xpcom/string/obsolete/nsString2.h b/mozilla/xpcom/string/obsolete/nsString2.h index bb9740a0b42..be4a9698aaa 100644 --- a/mozilla/xpcom/string/obsolete/nsString2.h +++ b/mozilla/xpcom/string/obsolete/nsString2.h @@ -53,6 +53,7 @@ #include "nsCRT.h" #include "nsAWritableString.h" +#include "nsLiteralString.h" #ifdef STANDALONE_MI_STRING_TESTS class nsAReadableString { public: virtual ~nsAReadableString() { } }; diff --git a/mozilla/xpcom/string/public/MANIFEST b/mozilla/xpcom/string/public/MANIFEST index 6a48cc1b8c0..6742f976d7e 100644 --- a/mozilla/xpcom/string/public/MANIFEST +++ b/mozilla/xpcom/string/public/MANIFEST @@ -22,14 +22,20 @@ nsAlgorithm.h +nsAString.h nsAReadableString.h nsAWritableString.h nsBufferHandle.h nsBufferHandleUtils.h nsCharTraits.h nsFragmentedString.h +nsLiteralString.h +nsLocalString.h nsPrintfCString.h nsPrivateSharableString.h nsReadableUtils.h nsSharedBufferList.h nsSlidingString.h +nsStringFragment.h +nsStringIterator.h +nsStringIteratorUtils.h diff --git a/mozilla/xpcom/string/public/Makefile.in b/mozilla/xpcom/string/public/Makefile.in index 3fd4a987aaf..394a6c8043b 100644 --- a/mozilla/xpcom/string/public/Makefile.in +++ b/mozilla/xpcom/string/public/Makefile.in @@ -18,6 +18,7 @@ # # Contributor(s): # Johnny Stenback (original author) +# Scott Collins # DEPTH = ../.. @@ -31,6 +32,7 @@ MODULE = string EXPORTS = \ nsAlgorithm.h \ + nsAString.h \ nsBufferHandle.h \ nsBufferHandleUtils.h \ nsPrivateSharableString.h \ @@ -41,7 +43,12 @@ EXPORTS = \ nsSharedBufferList.h \ nsSlidingString.h \ nsFragmentedString.h \ + nsLiteralString.h \ + nsLocalString.h \ nsPrintfCString.h \ + nsStringFragment.h \ + nsStringIterator.h \ + nsStringIteratorUtils.h \ $(NULL) EXPORTS := $(addprefix $(srcdir)/, $(EXPORTS)) diff --git a/mozilla/xpcom/string/public/makefile.win b/mozilla/xpcom/string/public/makefile.win index 3d819cedb8d..ae1d7a2e561 100644 --- a/mozilla/xpcom/string/public/makefile.win +++ b/mozilla/xpcom/string/public/makefile.win @@ -19,23 +19,30 @@ # # Contributor(s): # Johnny Stenback (original author) +# Scott Collins # DEPTH=..\.. EXPORTS = \ nsAlgorithm.h \ + nsAString.h \ nsAReadableString.h \ nsAWritableString.h \ nsBufferHandle.h \ nsBufferHandleUtils.h \ nsCharTraits.h \ nsFragmentedString.h \ + nsLiteralString.h \ + nsLocalString.h \ nsPrintfCString.h \ nsPrivateSharableString.h \ nsReadableUtils.h \ nsSharedBufferList.h \ nsSlidingString.h \ + nsStringFragment.h \ + nsStringIterator.h \ + nsStringIteratorUtils.h \ $(NULL) include <$(DEPTH)\config\rules.mak> diff --git a/mozilla/xpcom/string/public/nsAReadableString.h b/mozilla/xpcom/string/public/nsAReadableString.h index b36279dbd06..3b76ccce3f5 100644 --- a/mozilla/xpcom/string/public/nsAReadableString.h +++ b/mozilla/xpcom/string/public/nsAReadableString.h @@ -33,15 +33,14 @@ #include "nsCharTraits.h" #endif -#ifndef nsAlgorithm_h___ -#include "nsAlgorithm.h" - // for |NS_MIN|, |NS_MAX|, and |NS_COUNT|... -#endif - #ifndef nsPrivateSharableString_h___ #include "nsPrivateSharableString.h" #endif +#ifndef nsStringIterator_h___ +#include "nsStringIterator.h" +#endif + #include "nsMemory.h" /* @@ -61,22 +60,6 @@ 'C') is a string of |char|s. */ -enum nsFragmentRequest { kPrevFragment, kFirstFragment, kLastFragment, kNextFragment, kFragmentAt }; - -template -struct nsReadableFragment - { - const CharT* mStart; - const CharT* mEnd; - const void* mFragmentIdentifier; - - nsReadableFragment() - : mStart(0), mEnd(0), mFragmentIdentifier(0) - { - // nothing else to do here - } - }; - template class basic_nsAReadableString; template class basic_nsAWritableString; @@ -90,219 +73,6 @@ template class basic_nsAWritableString; #pragma warning( disable: 4284 ) #endif -template -class nsReadingIterator -// : public bidirectional_iterator_tag - { - public: - typedef ptrdiff_t difference_type; - typedef CharT value_type; - typedef const CharT* pointer; - typedef const CharT& reference; -// typedef bidirectional_iterator_tag iterator_category; - - private: - friend class basic_nsAReadableString; - - nsReadableFragment mFragment; - const CharT* mPosition; - const basic_nsAReadableString* mOwningString; - - nsReadingIterator( const nsReadableFragment& aFragment, - const CharT* aStartingPosition, - const basic_nsAReadableString& aOwningString ) - : mFragment(aFragment), - mPosition(aStartingPosition), - mOwningString(&aOwningString) - { - // nothing else to do here - } - - public: - nsReadingIterator() { } - // nsReadingIterator( const nsReadingIterator& ); // auto-generated copy-constructor OK - // nsReadingIterator& operator=( const nsReadingIterator& ); // auto-generated copy-assignment operator OK - - inline void normalize_forward(); - inline void normalize_backward(); - - pointer - get() const - { - return mPosition; - } - - CharT - operator*() const - { - return *get(); - } - -#if 0 - // An iterator really deserves this, but some compilers (notably IBM VisualAge for OS/2) - // don't like this when |CharT| is a type without members. - pointer - operator->() const - { - return get(); - } -#endif - - nsReadingIterator& - operator++() - { - ++mPosition; - normalize_forward(); - return *this; - } - - nsReadingIterator - operator++( int ) - { - nsReadingIterator result(*this); - ++mPosition; - normalize_forward(); - return result; - } - - nsReadingIterator& - operator--() - { - normalize_backward(); - --mPosition; - return *this; - } - - nsReadingIterator - operator--( int ) - { - nsReadingIterator result(*this); - normalize_backward(); - --mPosition; - return result; - } - - const nsReadableFragment& - fragment() const - { - return mFragment; - } - - const basic_nsAReadableString& - string() const - { - NS_ASSERTION(mOwningString, "iterator not attached to a string (|mOwningString| == 0)"); - return *mOwningString; - } - - difference_type - size_forward() const - { - return mFragment.mEnd - mPosition; - } - - difference_type - size_backward() const - { - return mPosition - mFragment.mStart; - } - - nsReadingIterator& - advance( difference_type n ) - { - while ( n > 0 ) - { - difference_type one_hop = NS_MIN(n, size_forward()); - - NS_ASSERTION(one_hop>0, "Infinite loop: can't advance a reading iterator beyond the end of a string"); - // perhaps I should |break| if |!one_hop|? - - mPosition += one_hop; - normalize_forward(); - n -= one_hop; - } - - while ( n < 0 ) - { - normalize_backward(); - difference_type one_hop = NS_MAX(n, -size_backward()); - - NS_ASSERTION(one_hop<0, "Infinite loop: can't advance (backward) a reading iterator beyond the end of a string"); - // perhaps I should |break| if |!one_hop|? - - mPosition += one_hop; - n -= one_hop; - } - - return *this; - } - - /** - * Really don't want to call these two operations |+=| and |-=|. - * Would prefer a single function, e.g., |advance|, which doesn't imply a constant time operation. - * - * We'll get rid of these as soon as we can. - */ - nsReadingIterator& - operator+=( difference_type n ) // deprecated - { - return advance(n); - } - - nsReadingIterator& - operator-=( difference_type n ) // deprecated - { - return advance(-n); - } - }; - -#if 0 -template -nsReadingIterator& -nsReadingIterator::advance( difference_type n ) - { - while ( n > 0 ) - { - difference_type one_hop = NS_MIN(n, size_forward()); - - NS_ASSERTION(one_hop>0, "Infinite loop: can't advance a reading iterator beyond the end of a string"); - // perhaps I should |break| if |!one_hop|? - - mPosition += one_hop; - normalize_forward(); - n -= one_hop; - } - - while ( n < 0 ) - { - normalize_backward(); - difference_type one_hop = NS_MAX(n, -size_backward()); - - NS_ASSERTION(one_hop<0, "Infinite loop: can't advance (backward) a reading iterator beyond the end of a string"); - // perhaps I should |break| if |!one_hop|? - - mPosition += one_hop; - n -= one_hop; - } - - return *this; - } -#endif - - // NOTE: need to break iterators out into their own file (as with many classes here), need - // these routines, but can't currently |#include "nsReadableUtils.h"|, this hack is bad - // but we need it to get OS2 building again. Fix by splitting things into different files. -NS_COM size_t Distance( const nsReadingIterator&, const nsReadingIterator& ); -NS_COM size_t Distance( const nsReadingIterator&, const nsReadingIterator& ); - -template -inline -PRBool -SameFragment( const Iterator& lhs, const Iterator& rhs ) - { - return lhs.fragment().mStart == rhs.fragment().mStart; - } - // // nsAReadable[C]String @@ -313,9 +83,9 @@ class basic_nsAReadableString : public nsPrivateSharableString { public: - typedef CharT char_type; - typedef PRUint32 size_type; - typedef PRUint32 index_type; +// typedef CharT char_type; +// typedef PRUint32 size_type; +// typedef PRUint32 index_type; typedef nsReadingIterator const_iterator; @@ -462,26 +232,6 @@ class basic_nsAReadableString #define NS_READABLE_CAST(CharT, expr) (expr) #endif -template -inline -void -nsReadingIterator::normalize_forward() - { - while ( mPosition == mFragment.mEnd - && mOwningString->GetReadableFragment(mFragment, kNextFragment) ) - mPosition = mFragment.mStart; - } - -template -inline -void -nsReadingIterator::normalize_backward() - { - while ( mPosition == mFragment.mStart - && mOwningString->GetReadableFragment(mFragment, kPrevFragment) ) - mPosition = mFragment.mEnd; - } - /** * Note: measure -- should the |BeginReading| and |EndReading| be |inline|? */ @@ -735,105 +485,6 @@ basic_nsAReadableString::FindChar( CharT aChar, PRUint32 aOffset ) const - // - // nsLiteral[C]String - // - -template -class basic_nsLiteralString - : public basic_nsAReadableString - /* - ...this class wraps a constant literal string and lets it act like an |nsAReadable...|. - - Use it like this: - - SomeFunctionTakingACString( nsLiteralCString("Hello, World!") ); - - With some tweaking, I think I can make this work as well... - - SomeStringFunc( nsLiteralString( L"Hello, World!" ) ); - - This class just holds a pointer. If you don't supply the length, it must calculate it. - No copying or allocations are performed. - - |const basic_nsLiteralString&| appears frequently in interfaces because it - allows the automatic conversion of a |CharT*|. - */ - { - protected: - virtual const CharT* GetReadableFragment( nsReadableFragment&, nsFragmentRequest, PRUint32 ) const; - - public: - - explicit - basic_nsLiteralString( const CharT* aLiteral ) - : mStart(aLiteral), - mEnd(mStart ? (mStart + nsCharTraits::length(mStart)) : mStart) - { - // nothing else to do here - } - - basic_nsLiteralString( const CharT* aLiteral, PRUint32 aLength ) - : mStart(aLiteral), - mEnd(mStart + aLength) - { - // This is an annoying hack. Callers should be fixed to use the other - // constructor if they don't really know the length. - if ( aLength == PRUint32(-1) ) - { -// NS_WARNING("Tell scc: Caller constructing a string doesn't know the real length. Please use the other constructor."); - mEnd = mStart ? (mStart + nsCharTraits::length(mStart)) : mStart; - } - } - - // basic_nsLiteralString( const basic_nsLiteralString& ); // auto-generated copy-constructor OK - // ~basic_nsLiteralString(); // auto-generated destructor OK - - private: - // NOT TO BE IMPLEMENTED - void operator=( const basic_nsLiteralString& ); // we're immutable - - public: - - virtual PRUint32 Length() const; - - - const CharT* get() const { return mStart; } - operator const CharT*() const { return get(); } // to be deprecated, prefer |get()| - - private: - const CharT* mStart; - const CharT* mEnd; - }; - -// NS_DEF_TEMPLATE_STRING_COMPARISON_OPERATORS(basic_nsLiteralString, CharT) - -template -const CharT* -basic_nsLiteralString::GetReadableFragment( nsReadableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const - { - switch ( aRequest ) - { - case kFirstFragment: - case kLastFragment: - case kFragmentAt: - aFragment.mStart = mStart; - aFragment.mEnd = mEnd; - return mStart + aOffset; - - case kPrevFragment: - case kNextFragment: - default: - return 0; - } - } - -template -PRUint32 -basic_nsLiteralString::Length() const - { - return PRUint32(mEnd - mStart); - } #if 0 @@ -842,7 +493,7 @@ inline PRBool basic_nsAReadableString::Equals( const CharT* rhs, PRUint32 rhs_length ) const { - return Compare(basic_nsLiteralString(rhs, rhs_length)) == 0; + return Compare(literal_string(rhs, rhs_length)) == 0; } #endif @@ -852,74 +503,11 @@ inline int basic_nsAReadableString::Compare( const CharT* rhs, PRUint32 rhs_length ) const { - return ::Compare(*this, NS_READABLE_CAST(CharT, basic_nsLiteralString(rhs, rhs_length))); + return ::Compare(*this, NS_READABLE_CAST(CharT, literal_string(rhs, rhs_length))); } #endif - // - // nsLiteralChar, nsLiteralPRUnichar - // - -template -class basic_nsLiteralChar - : public basic_nsAReadableString - { - protected: - virtual const CharT* GetReadableFragment( nsReadableFragment&, nsFragmentRequest, PRUint32 ) const; - - public: - - explicit - basic_nsLiteralChar( CharT aChar ) - : mChar(aChar) - { - // nothing else to do here - } - - // basic_nsLiteralChar( const basic_nsLiteralString& ); // auto-generated copy-constructor OK - // ~basic_nsLiteralChar(); // auto-generated destructor OK - - private: - // NOT TO BE IMPLEMENTED - void operator=( const basic_nsLiteralChar& ); // we're immutable - - public: - - virtual - PRUint32 - Length() const - { - return 1; - } - - private: - CharT mChar; - }; - -template -const CharT* -basic_nsLiteralChar::GetReadableFragment( nsReadableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const - { - switch ( aRequest ) - { - case kFirstFragment: - case kLastFragment: - case kFragmentAt: - aFragment.mEnd = (aFragment.mStart = &mChar) + 1; - return aFragment.mStart + aOffset; - - case kPrevFragment: - case kNextFragment: - default: - return 0; - } - } - - - - - // // nsPromiseConcatenation @@ -1335,7 +923,7 @@ inline int Compare( const basic_nsAReadableString& lhs, const CharT* rhs ) { - return Compare(lhs, NS_READABLE_CAST(CharT, basic_nsLiteralString(rhs))); + return Compare(lhs, NS_READABLE_CAST(CharT, literal_string(rhs))); } template @@ -1343,7 +931,7 @@ inline int Compare( const CharT* lhs, const basic_nsAReadableString& rhs ) { - return Compare(NS_READABLE_CAST(CharT, basic_nsLiteralString(lhs)), rhs); + return Compare(NS_READABLE_CAST(CharT, literal_string(lhs)), rhs); } // XXX Note that these are located here because some compilers are @@ -1361,7 +949,7 @@ inline PRBool basic_nsAReadableString::Equals( const CharT* rhs ) const { - return Compare(basic_nsLiteralString(rhs)) == 0; + return Compare(literal_string(rhs)) == 0; } template @@ -1369,7 +957,7 @@ inline int basic_nsAReadableString::Compare( const CharT* rhs ) const { - return ::Compare(*this, NS_READABLE_CAST(CharT, basic_nsLiteralString(rhs))); + return ::Compare(*this, NS_READABLE_CAST(CharT, literal_string(rhs))); } @@ -1484,7 +1072,7 @@ basic_nsPromiseFlatString::basic_nsPromiseFlatString( const basic_nsARead while ( start != end ) { buffer_count++; - start += start.size_forward(); + start.advance(start.size_forward()); } // Now figure out what we want to do with the string @@ -1539,31 +1127,8 @@ basic_nsPromiseFlatString::GetReadableFragment( nsReadableFragment typedef basic_nsAReadableString nsAReadableString; typedef basic_nsAReadableString nsAReadableCString; -typedef basic_nsLiteralString nsLiteralString; -typedef basic_nsLiteralString nsLiteralCString; - typedef basic_nsPromiseFlatString nsPromiseFlatString; typedef basic_nsPromiseFlatString nsPromiseFlatCString; -#ifdef HAVE_CPP_2BYTE_WCHAR_T - #define NS_L(s) L##s - #define NS_MULTILINE_LITERAL_STRING(s) nsLiteralString(s, (sizeof(s)/sizeof(wchar_t))-1) - #define NS_NAMED_MULTILINE_LITERAL_STRING(n,s) nsLiteralString n(s, (sizeof(s)/sizeof(wchar_t))-1) -#else - #define NS_L(s) s - #define NS_MULTILINE_LITERAL_STRING(s) NS_ConvertASCIItoUCS2(s, sizeof(s)-1) - #define NS_NAMED_MULTILINE_LITERAL_STRING(n,s) NS_ConvertASCIItoUCS2 n(s, sizeof(s)-1) -#endif - -#define NS_LITERAL_STRING(s) NS_MULTILINE_LITERAL_STRING(NS_L(s)) -#define NS_NAMED_LITERAL_STRING(n,s) NS_NAMED_MULTILINE_LITERAL_STRING(n,NS_L(s)) - -#define NS_LITERAL_CSTRING(s) nsLiteralCString(s, sizeof(s)-1) -#define NS_NAMED_LITERAL_CSTRING(n,s) nsLiteralCString n(s, sizeof(s)-1) - -typedef basic_nsLiteralChar nsLiteralChar; -typedef basic_nsLiteralChar nsLiteralPRUnichar; - - #endif // !defined(nsAReadableString_h___) diff --git a/mozilla/xpcom/string/public/nsAString.h b/mozilla/xpcom/string/public/nsAString.h new file mode 100644 index 00000000000..c9e909d007b --- /dev/null +++ b/mozilla/xpcom/string/public/nsAString.h @@ -0,0 +1,96 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +#ifndef nsAString_h___ +#define nsAString_h___ + +#ifndef nsPrivateSharableString_h___ +#include "nsPrivateSharableString.h" +#endif + +#ifndef nsCharTraits_h___ +#include "nsCharTraits.h" +#endif + + + /** + * + */ + +class nsAString + : public nsPrivateSharableString + { + public: + typedef PRUint32 size_type; + + typedef nsReadingIterator const_iterator; + typedef nsWritingIterator iterator; + + // nsAString(); // auto-generated default constructor OK (we're abstract anyway) + // nsAString( const nsAString& ); // auto-generated copy-constructor OK (again, only because we're abstract) + virtual ~nsAString() { } // ...yes, I expect to be sub-classed + + const_iterator& BeginReading( const_iterator& ) const; + const_iterator& EndReading( const_iterator& ) const; + + iterator& BeginWriting( iterator& ); + iterator& EndWriting( iterator& ); + + virtual size_type Length() const = 0; + PRBool IsEmpty() const; + + + // ... + }; + + + /** + * + */ + +class nsACString + : public nsPrivateSharableString + { + public: + typedef PRUint32 size_type; + + typedef nsReadingIterator const_iterator; + typedef nsWritingIterator iterator; + + // nsACString(); // auto-generated default constructor OK (we're abstract anyway) + // nsACString( const nsACString& ); // auto-generated copy-constructor OK (again, only because we're abstract) + virtual ~nsACString() { } // ...yes, I expect to be sub-classed + + const_iterator& BeginReading( const_iterator& ) const; + const_iterator& EndReading( const_iterator& ) const; + + iterator& BeginWriting( iterator& ); + iterator& EndWriting( iterator& ); + + virtual size_type Length() const = 0; + PRBool IsEmpty() const; + + + // ... + }; + +#endif // !defined(nsAString_h___) diff --git a/mozilla/xpcom/string/public/nsAWritableString.h b/mozilla/xpcom/string/public/nsAWritableString.h index 209e4ff8766..b123dce5dbc 100644 --- a/mozilla/xpcom/string/public/nsAWritableString.h +++ b/mozilla/xpcom/string/public/nsAWritableString.h @@ -29,246 +29,13 @@ #include "nsAReadableString.h" #endif +#ifndef nsLiteralString_h___ +#include "nsLiteralString.h" +#endif -template -struct nsWritableFragment - { - CharT* mStart; - CharT* mEnd; - void* mFragmentIdentifier; - - nsWritableFragment() - : mStart(0), mEnd(0), mFragmentIdentifier(0) - { - // nothing else to do here - } - }; template class basic_nsAWritableString; -template -class nsWritingIterator -// : public bidirectional_iterator_tag - { - public: - typedef ptrdiff_t difference_type; - typedef CharT value_type; - typedef CharT* pointer; - typedef CharT& reference; -// typedef bidirectional_iterator_tag iterator_category; - - private: - friend class basic_nsAWritableString; - - nsWritableFragment mFragment; - CharT* mPosition; - basic_nsAWritableString* mOwningString; - - nsWritingIterator( nsWritableFragment& aFragment, - CharT* aStartingPosition, - basic_nsAWritableString& aOwningString ) - : mFragment(aFragment), - mPosition(aStartingPosition), - mOwningString(&aOwningString) - { - // nothing else to do here - } - - public: - nsWritingIterator() { } - // nsWritingIterator( const nsWritingIterator& ); // auto-generated copy-constructor OK - // nsWritingIterator& operator=( const nsWritingIterator& ); // auto-generated copy-assignment operator OK - - inline void normalize_forward(); - inline void normalize_backward(); - - pointer - get() const - { - return mPosition; - } - - reference - operator*() const - { - return *get(); - } - -#if 0 - // An iterator really deserves this, but some compilers (notably IBM VisualAge for OS/2) - // don't like this when |CharT| is a type without members. - pointer - operator->() const - { - return get(); - } -#endif - - nsWritingIterator& - operator++() - { - ++mPosition; - normalize_forward(); - return *this; - } - - nsWritingIterator - operator++( int ) - { - nsWritingIterator result(*this); - ++mPosition; - normalize_forward(); - return result; - } - - nsWritingIterator& - operator--() - { - normalize_backward(); - --mPosition; - return *this; - } - - nsWritingIterator - operator--( int ) - { - nsWritingIterator result(*this); - normalize_backward(); - --mPosition; - return result; - } - - const nsWritableFragment& - fragment() const - { - return mFragment; - } - - nsWritableFragment& - fragment() - { - return mFragment; - } - - const basic_nsAWritableString& - string() const - { - NS_ASSERTION(mOwningString, "iterator not attached to a string (|mOwningString| == 0)"); - return *mOwningString; - } - - basic_nsAWritableString& - string() - { - NS_ASSERTION(mOwningString, "iterator not attached to a string (|mOwningString| == 0)"); - return *mOwningString; - } - - difference_type - size_forward() const - { - return mFragment.mEnd - mPosition; - } - - difference_type - size_backward() const - { - return mPosition - mFragment.mStart; - } - - nsWritingIterator& - advance( difference_type n ) - { - while ( n > 0 ) - { - difference_type one_hop = NS_MIN(n, size_forward()); - - NS_ASSERTION(one_hop>0, "Infinite loop: can't advance a writing iterator beyond the end of a string"); - // perhaps I should |break| if |!one_hop|? - - mPosition += one_hop; - normalize_forward(); - n -= one_hop; - } - - while ( n < 0 ) - { - normalize_backward(); - difference_type one_hop = NS_MAX(n, -size_backward()); - - NS_ASSERTION(one_hop<0, "Infinite loop: can't advance (backward) a writing iterator beyond the end of a string"); - // perhaps I should |break| if |!one_hop|? - - mPosition += one_hop; - n -= one_hop; - } - - return *this; - } - - /** - * Really don't want to call these two operations |+=| and |-=|. - * Would prefer a single function, e.g., |advance|, which doesn't imply a constant time operation. - * - * We'll get rid of these as soon as we can. - */ - nsWritingIterator& - operator+=( difference_type n ) // deprecated - { - return advance(n); - } - - nsWritingIterator& - operator-=( difference_type n ) // deprecated - { - return advance(-n); - } - - PRUint32 - write( const value_type* s, PRUint32 n ) - { - NS_ASSERTION(size_forward() > 0, "You can't |write| into an |nsWritingIterator| with no space!"); - - n = NS_MIN(n, PRUint32(size_forward())); - nsCharTraits::move(mPosition, s, n); - advance( difference_type(n) ); - return n; - } - }; - -#if 0 -template -nsWritingIterator& -nsWritingIterator::advance( difference_type n ) - { - while ( n > 0 ) - { - difference_type one_hop = NS_MIN(n, size_forward()); - - NS_ASSERTION(one_hop>0, "Infinite loop: can't advance a writing iterator beyond the end of a string"); - // perhaps I should |break| if |!one_hop|? - - mPosition += one_hop; - normalize_forward(); - n -= one_hop; - } - - while ( n < 0 ) - { - normalize_backward(); - difference_type one_hop = NS_MAX(n, -size_backward()); - - NS_ASSERTION(one_hop<0, "Infinite loop: can't advance (backward) a writing iterator beyond the end of a string"); - // perhaps I should |break| if |!one_hop|? - - mPosition += one_hop; - n -= one_hop; - } - - return *this; - } -#endif - /* This file defines the abstract interfaces |nsAWritableString| and |nsAWritableCString|. @@ -477,46 +244,6 @@ class basic_nsAWritableString - // - // |nsWritingIterator|s - // - -template -inline -void -nsWritingIterator::normalize_forward() - { - while ( mPosition == mFragment.mEnd - && mOwningString->GetWritableFragment(mFragment, kNextFragment) ) - mPosition = mFragment.mStart; - } - -template -inline -void -nsWritingIterator::normalize_backward() - { - while ( mPosition == mFragment.mStart - && mOwningString->GetWritableFragment(mFragment, kPrevFragment) ) - mPosition = mFragment.mEnd; - } - -template -inline -PRBool -operator==( const nsWritingIterator& lhs, const nsWritingIterator& rhs ) - { - return lhs.get() == rhs.get(); - } - -template -inline -PRBool -operator!=( const nsWritingIterator& lhs, const nsWritingIterator& rhs ) - { - return lhs.get() != rhs.get(); - } - // @@ -594,21 +321,21 @@ template void basic_nsAWritableString::do_AssignFromElementPtr( const CharT* aPtr ) { - do_AssignFromReadable(basic_nsLiteralString(aPtr)); + do_AssignFromReadable(literal_string(aPtr)); } template void basic_nsAWritableString::do_AssignFromElementPtrLength( const CharT* aPtr, PRUint32 aLength ) { - do_AssignFromReadable(basic_nsLiteralString(aPtr, aLength)); + do_AssignFromReadable(literal_string(aPtr, aLength)); } template void basic_nsAWritableString::do_AssignFromElement( CharT aChar ) { - do_AssignFromReadable(basic_nsLiteralChar(aChar)); + do_AssignFromReadable(literal_string(&aChar, 1)); } @@ -665,21 +392,21 @@ template void basic_nsAWritableString::do_AppendFromElementPtr( const CharT* aChar ) { - do_AppendFromReadable(basic_nsLiteralString(aChar)); + do_AppendFromReadable(literal_string(aChar)); } template void basic_nsAWritableString::do_AppendFromElementPtrLength( const CharT* aChar, PRUint32 aLength ) { - do_AppendFromReadable(basic_nsLiteralString(aChar, aLength)); + do_AppendFromReadable(literal_string(aChar, aLength)); } template void basic_nsAWritableString::do_AppendFromElement( CharT aChar ) { - do_AppendFromReadable(basic_nsLiteralChar(aChar)); + do_AppendFromReadable(literal_string(&aChar, 1)); } @@ -740,21 +467,21 @@ template void basic_nsAWritableString::do_InsertFromElementPtr( const CharT* aPtr, PRUint32 atPosition ) { - do_InsertFromReadable(basic_nsLiteralString(aPtr), atPosition); + do_InsertFromReadable(literal_string(aPtr), atPosition); } template void basic_nsAWritableString::do_InsertFromElementPtrLength( const CharT* aPtr, PRUint32 atPosition, PRUint32 aLength ) { - do_InsertFromReadable(basic_nsLiteralString(aPtr, aLength), atPosition); + do_InsertFromReadable(literal_string(aPtr, aLength), atPosition); } template void basic_nsAWritableString::do_InsertFromElement( CharT aChar, PRUint32 atPosition ) { - do_InsertFromReadable(basic_nsLiteralChar(aChar), atPosition); + do_InsertFromReadable(literal_string(&aChar, 1), atPosition); } @@ -809,7 +536,7 @@ basic_nsAWritableString::ReplaceFromPromise( PRUint32 cutStart, PRUint32 nsReadingIterator fromBegin, fromEnd; CharT* toBegin = buffer; copy_string(aReadable.BeginReading(fromBegin), aReadable.EndReading(fromEnd), toBegin); - do_ReplaceFromReadable(cutStart, cutLength, basic_nsLiteralString(buffer, length)); + do_ReplaceFromReadable(cutStart, cutLength, literal_string(buffer, length)); delete buffer; } // else assert? diff --git a/mozilla/xpcom/string/public/nsCharTraits.h b/mozilla/xpcom/string/public/nsCharTraits.h index dc4b681a1f5..964c6be8646 100644 --- a/mozilla/xpcom/string/public/nsCharTraits.h +++ b/mozilla/xpcom/string/public/nsCharTraits.h @@ -35,6 +35,10 @@ // for |PRUnichar| #endif +#ifndef nsStringIteratorUtils_h___ +#include "nsStringIteratorUtils.h" +#endif + #ifdef HAVE_CPP_BOOL typedef bool nsCharTraits_bool; diff --git a/mozilla/xpcom/string/public/nsDependentString.h b/mozilla/xpcom/string/public/nsDependentString.h new file mode 100644 index 00000000000..6e44379e262 --- /dev/null +++ b/mozilla/xpcom/string/public/nsDependentString.h @@ -0,0 +1,148 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +#ifndef nsLocalString_h___ +#define nsLocalString_h___ + +#ifndef nsAReadableString_h___ +#include "nsAReadableString.h" +#endif + + /* + ...this class wraps a constant literal string and lets it act like an |nsAReadable...|. + + Use it like this: + + SomeFunctionTakingACString( nsLiteralCString("Hello, World!") ); + + With some tweaking, I think I can make this work as well... + + SomeStringFunc( nsLiteralString( L"Hello, World!" ) ); + + This class just holds a pointer. If you don't supply the length, it must calculate it. + No copying or allocations are performed. + + |const nsLocalString&| appears frequently in interfaces because it + allows the automatic conversion of a |PRUnichar*|. + */ + +class NS_COM nsLocalString + : public basic_nsAReadableString + { + protected: + virtual const PRUnichar* GetReadableFragment( nsReadableFragment&, nsFragmentRequest, PRUint32 ) const; + + public: + + explicit + nsLocalString( const PRUnichar* aLiteral ) + : mStart(aLiteral), + mEnd(mStart ? (mStart + nsCharTraits::length(mStart)) : mStart) + { + // nothing else to do here + } + + nsLocalString( const PRUnichar* aLiteral, PRUint32 aLength ) + : mStart(aLiteral), + mEnd(mStart + aLength) + { + // This is an annoying hack. Callers should be fixed to use the other + // constructor if they don't really know the length. + if ( aLength == PRUint32(-1) ) + { +// NS_WARNING("Tell scc: Caller constructing a string doesn't know the real length. Please use the other constructor."); + mEnd = mStart ? (mStart + nsCharTraits::length(mStart)) : mStart; + } + } + + // nsLocalString( const nsLocalString& ); // auto-generated copy-constructor OK + // ~nsLocalString(); // auto-generated destructor OK + + private: + // NOT TO BE IMPLEMENTED + void operator=( const nsLocalString& ); // we're immutable + + public: + + virtual PRUint32 Length() const; + + + const PRUnichar* get() const { return mStart; } + operator const PRUnichar*() const { return get(); } // to be deprecated, prefer |get()| + + private: + const PRUnichar* mStart; + const PRUnichar* mEnd; + }; + + + +class NS_COM nsLocalCString + : public basic_nsAReadableString + { + protected: + virtual const char* GetReadableFragment( nsReadableFragment&, nsFragmentRequest, PRUint32 ) const; + + public: + + explicit + nsLocalCString( const char* aLiteral ) + : mStart(aLiteral), + mEnd(mStart ? (mStart + nsCharTraits::length(mStart)) : mStart) + { + // nothing else to do here + } + + nsLocalCString( const char* aLiteral, PRUint32 aLength ) + : mStart(aLiteral), + mEnd(mStart + aLength) + { + // This is an annoying hack. Callers should be fixed to use the other + // constructor if they don't really know the length. + if ( aLength == PRUint32(-1) ) + { +// NS_WARNING("Tell scc: Caller constructing a string doesn't know the real length. Please use the other constructor."); + mEnd = mStart ? (mStart + nsCharTraits::length(mStart)) : mStart; + } + } + + // nsLocalCString( const nsLocalCString& ); // auto-generated copy-constructor OK + // ~nsLocalCString(); // auto-generated destructor OK + + private: + // NOT TO BE IMPLEMENTED + void operator=( const nsLocalCString& ); // we're immutable + + public: + + virtual PRUint32 Length() const; + + + const char* get() const { return mStart; } + operator const char*() const { return get(); } // to be deprecated, prefer |get()| + + private: + const char* mStart; + const char* mEnd; + }; + +#endif /* !defined(nsLocalString_h___) */ diff --git a/mozilla/xpcom/string/public/nsLiteralString.h b/mozilla/xpcom/string/public/nsLiteralString.h new file mode 100644 index 00000000000..53a735ba4f1 --- /dev/null +++ b/mozilla/xpcom/string/public/nsLiteralString.h @@ -0,0 +1,86 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +#ifndef nsLiteralString_h___ +#define nsLiteralString_h___ + +#ifndef nscore_h___ +#include "nscore.h" +#endif + +#ifndef nsLocalString_h___ +#include "nsLocalString.h" +#endif + +typedef const nsLocalString nsLiteralString; +typedef const nsLocalCString nsLiteralCString; + +template +struct nsLiteralStringTraits + { + }; + +NS_SPECIALIZE_TEMPLATE +struct nsLiteralStringTraits + { + typedef nsLiteralString literal_string_type; + }; + +NS_SPECIALIZE_TEMPLATE +struct nsLiteralStringTraits + { + typedef nsLiteralCString literal_string_type; + }; + +template +inline +const typename nsLiteralStringTraits::literal_string_type +literal_string( const CharT* aPtr ) + { + return nsLiteralStringTraits::literal_string_type(aPtr); + } + +template +inline +const typename nsLiteralStringTraits::literal_string_type +literal_string( const CharT* aPtr, PRUint32 aLength ) + { + return nsLiteralStringTraits::literal_string_type(aPtr, aLength); + } + +#ifdef HAVE_CPP_2BYTE_WCHAR_T + #define NS_L(s) L##s + #define NS_MULTILINE_LITERAL_STRING(s) nsLiteralString(s, (sizeof(s)/sizeof(wchar_t))-1) + #define NS_NAMED_MULTILINE_LITERAL_STRING(n,s) nsLiteralString n(s, (sizeof(s)/sizeof(wchar_t))-1) +#else + #define NS_L(s) s + #define NS_MULTILINE_LITERAL_STRING(s) NS_ConvertASCIItoUCS2(s, sizeof(s)-1) + #define NS_NAMED_MULTILINE_LITERAL_STRING(n,s) NS_ConvertASCIItoUCS2 n(s, sizeof(s)-1) +#endif + +#define NS_LITERAL_STRING(s) NS_MULTILINE_LITERAL_STRING(NS_L(s)) +#define NS_NAMED_LITERAL_STRING(n,s) NS_NAMED_MULTILINE_LITERAL_STRING(n,NS_L(s)) + +#define NS_LITERAL_CSTRING(s) nsLiteralCString(s, sizeof(s)-1) +#define NS_NAMED_LITERAL_CSTRING(n,s) nsLiteralCString n(s, sizeof(s)-1) + +#endif /* !defined(nsLiteralString_h___) */ \ No newline at end of file diff --git a/mozilla/xpcom/string/public/nsLocalString.h b/mozilla/xpcom/string/public/nsLocalString.h new file mode 100644 index 00000000000..6e44379e262 --- /dev/null +++ b/mozilla/xpcom/string/public/nsLocalString.h @@ -0,0 +1,148 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +#ifndef nsLocalString_h___ +#define nsLocalString_h___ + +#ifndef nsAReadableString_h___ +#include "nsAReadableString.h" +#endif + + /* + ...this class wraps a constant literal string and lets it act like an |nsAReadable...|. + + Use it like this: + + SomeFunctionTakingACString( nsLiteralCString("Hello, World!") ); + + With some tweaking, I think I can make this work as well... + + SomeStringFunc( nsLiteralString( L"Hello, World!" ) ); + + This class just holds a pointer. If you don't supply the length, it must calculate it. + No copying or allocations are performed. + + |const nsLocalString&| appears frequently in interfaces because it + allows the automatic conversion of a |PRUnichar*|. + */ + +class NS_COM nsLocalString + : public basic_nsAReadableString + { + protected: + virtual const PRUnichar* GetReadableFragment( nsReadableFragment&, nsFragmentRequest, PRUint32 ) const; + + public: + + explicit + nsLocalString( const PRUnichar* aLiteral ) + : mStart(aLiteral), + mEnd(mStart ? (mStart + nsCharTraits::length(mStart)) : mStart) + { + // nothing else to do here + } + + nsLocalString( const PRUnichar* aLiteral, PRUint32 aLength ) + : mStart(aLiteral), + mEnd(mStart + aLength) + { + // This is an annoying hack. Callers should be fixed to use the other + // constructor if they don't really know the length. + if ( aLength == PRUint32(-1) ) + { +// NS_WARNING("Tell scc: Caller constructing a string doesn't know the real length. Please use the other constructor."); + mEnd = mStart ? (mStart + nsCharTraits::length(mStart)) : mStart; + } + } + + // nsLocalString( const nsLocalString& ); // auto-generated copy-constructor OK + // ~nsLocalString(); // auto-generated destructor OK + + private: + // NOT TO BE IMPLEMENTED + void operator=( const nsLocalString& ); // we're immutable + + public: + + virtual PRUint32 Length() const; + + + const PRUnichar* get() const { return mStart; } + operator const PRUnichar*() const { return get(); } // to be deprecated, prefer |get()| + + private: + const PRUnichar* mStart; + const PRUnichar* mEnd; + }; + + + +class NS_COM nsLocalCString + : public basic_nsAReadableString + { + protected: + virtual const char* GetReadableFragment( nsReadableFragment&, nsFragmentRequest, PRUint32 ) const; + + public: + + explicit + nsLocalCString( const char* aLiteral ) + : mStart(aLiteral), + mEnd(mStart ? (mStart + nsCharTraits::length(mStart)) : mStart) + { + // nothing else to do here + } + + nsLocalCString( const char* aLiteral, PRUint32 aLength ) + : mStart(aLiteral), + mEnd(mStart + aLength) + { + // This is an annoying hack. Callers should be fixed to use the other + // constructor if they don't really know the length. + if ( aLength == PRUint32(-1) ) + { +// NS_WARNING("Tell scc: Caller constructing a string doesn't know the real length. Please use the other constructor."); + mEnd = mStart ? (mStart + nsCharTraits::length(mStart)) : mStart; + } + } + + // nsLocalCString( const nsLocalCString& ); // auto-generated copy-constructor OK + // ~nsLocalCString(); // auto-generated destructor OK + + private: + // NOT TO BE IMPLEMENTED + void operator=( const nsLocalCString& ); // we're immutable + + public: + + virtual PRUint32 Length() const; + + + const char* get() const { return mStart; } + operator const char*() const { return get(); } // to be deprecated, prefer |get()| + + private: + const char* mStart; + const char* mEnd; + }; + +#endif /* !defined(nsLocalString_h___) */ diff --git a/mozilla/xpcom/string/public/nsSharedBufferList.h b/mozilla/xpcom/string/public/nsSharedBufferList.h index a97c2af1ec6..5a5ad9330c5 100755 --- a/mozilla/xpcom/string/public/nsSharedBufferList.h +++ b/mozilla/xpcom/string/public/nsSharedBufferList.h @@ -40,6 +40,10 @@ // for |nsReadingIterator| #endif +#ifndef nsLiteralString_h___ +#include "nsLiteralString.h" +#endif + #ifndef nsBufferHandleUtils_h___ #include "nsBufferHandleUtils.h" // for |NS_AllocateContiguousHandleWithData| @@ -180,7 +184,7 @@ class NS_COM nsSharedBufferList NewSingleAllocationBuffer( const PRUnichar* aData, PRUint32 aDataLength, PRUint32 aAdditionalCapacity = 1 ) { typedef Buffer* Buffer_ptr; - return NS_AllocateContiguousHandleWithData(Buffer_ptr(0), NS_READABLE_CAST(PRUnichar, nsLiteralString(aData, aDataLength)), aAdditionalCapacity); + return NS_AllocateContiguousHandleWithData(Buffer_ptr(0), NS_READABLE_CAST(PRUnichar, literal_string(aData, aDataLength)), aAdditionalCapacity); } static diff --git a/mozilla/xpcom/string/public/nsStringFragment.h b/mozilla/xpcom/string/public/nsStringFragment.h new file mode 100644 index 00000000000..b1296c291fd --- /dev/null +++ b/mozilla/xpcom/string/public/nsStringFragment.h @@ -0,0 +1,93 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +/* nsStringFragment.h --- machinery that makes string iterators work */ + +#ifndef nsStringFragment_h___ +#define nsStringFragment_h___ + + + + /** + * An |nsFragmentRequest| is used to tell |GetReadableFragment| and + * |GetWritableFragment| what to do. + * + * @see GetReadableFragment + */ + +enum nsFragmentRequest { kPrevFragment, kFirstFragment, kLastFragment, kNextFragment, kFragmentAt }; + + + /** + * A |nsReadableFragment| provides |const| access to a contiguous hunk of + * string of homogenous units, e.g., bytes (|char|). This doesn't mean it + * represents a flat hunk. It could be a variable length encoding, for + * instance UTF-8. And the fragment itself need not be zero-terminated. + * + * An |nsReadableFragment| is the underlying machinery that lets + * |nsReadingIterator|s work. + * + * @see nsReadingIterator + */ + +template +struct nsReadableFragment + { + const CharT* mStart; + const CharT* mEnd; + const void* mFragmentIdentifier; + + nsReadableFragment() + : mStart(0), mEnd(0), mFragmentIdentifier(0) + { + // nothing else to do here + } + }; + + + /** + * A |nsWritableFragment| provides non-|const| access to a contiguous hunk of + * string of homogenous units, e.g., bytes (|char|). This doesn't mean it + * represents a flat hunk. It could be a variable length encoding, for + * instance UTF-8. And the fragment itself need not be zero-terminated. + * + * An |nsWritableFragment| is the underlying machinery that lets + * |nsWritingIterator|s work. + * + * @see nsWritingIterator + */ + +template +struct nsWritableFragment + { + CharT* mStart; + CharT* mEnd; + void* mFragmentIdentifier; + + nsWritableFragment() + : mStart(0), mEnd(0), mFragmentIdentifier(0) + { + // nothing else to do here + } + }; + +#endif /* !defined(nsStringFragment_h___) */ diff --git a/mozilla/xpcom/string/public/nsStringIterator.h b/mozilla/xpcom/string/public/nsStringIterator.h new file mode 100644 index 00000000000..316bd540465 --- /dev/null +++ b/mozilla/xpcom/string/public/nsStringIterator.h @@ -0,0 +1,408 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +#ifndef nsStringIterator_h___ +#define nsStringIterator_h___ + +#ifndef nsStringFragment_h___ +#include "nsStringFragment.h" +#endif + +#ifndef nsAlgorithm_h___ +#include "nsAlgorithm.h" + // for |NS_MIN|, |NS_MAX|, and |NS_COUNT|... +#endif + + +template class basic_nsAReadableString; +template class basic_nsAWritableString; + + + + /** + * + * @see nsReadableFragment + * @see basic_nsAReadableString + */ + +template +class nsReadingIterator +// : public bidirectional_iterator_tag + { + public: + typedef ptrdiff_t difference_type; + typedef CharT value_type; + typedef const CharT* pointer; + typedef const CharT& reference; +// typedef bidirectional_iterator_tag iterator_category; + + private: + friend class basic_nsAReadableString; + + nsReadableFragment mFragment; + const CharT* mPosition; + const basic_nsAReadableString* mOwningString; + + public: + nsReadingIterator() { } + // nsReadingIterator( const nsReadingIterator& ); // auto-generated copy-constructor OK + // nsReadingIterator& operator=( const nsReadingIterator& ); // auto-generated copy-assignment operator OK + + inline void normalize_forward(); + inline void normalize_backward(); + + pointer + get() const + { + return mPosition; + } + + CharT + operator*() const + { + return *get(); + } + +#if 0 + // An iterator really deserves this, but some compilers (notably IBM VisualAge for OS/2) + // don't like this when |CharT| is a type without members. + pointer + operator->() const + { + return get(); + } +#endif + + nsReadingIterator& + operator++() + { + ++mPosition; + normalize_forward(); + return *this; + } + + nsReadingIterator + operator++( int ) + { + nsReadingIterator result(*this); + ++mPosition; + normalize_forward(); + return result; + } + + nsReadingIterator& + operator--() + { + normalize_backward(); + --mPosition; + return *this; + } + + nsReadingIterator + operator--( int ) + { + nsReadingIterator result(*this); + normalize_backward(); + --mPosition; + return result; + } + + const nsReadableFragment& + fragment() const + { + return mFragment; + } + + const basic_nsAReadableString& + string() const + { + NS_ASSERTION(mOwningString, "iterator not attached to a string (|mOwningString| == 0)"); + return *mOwningString; + } + + difference_type + size_forward() const + { + return mFragment.mEnd - mPosition; + } + + difference_type + size_backward() const + { + return mPosition - mFragment.mStart; + } + + nsReadingIterator& + advance( difference_type n ) + { + while ( n > 0 ) + { + difference_type one_hop = NS_MIN(n, size_forward()); + + NS_ASSERTION(one_hop>0, "Infinite loop: can't advance a reading iterator beyond the end of a string"); + // perhaps I should |break| if |!one_hop|? + + mPosition += one_hop; + normalize_forward(); + n -= one_hop; + } + + while ( n < 0 ) + { + normalize_backward(); + difference_type one_hop = NS_MAX(n, -size_backward()); + + NS_ASSERTION(one_hop<0, "Infinite loop: can't advance (backward) a reading iterator beyond the end of a string"); + // perhaps I should |break| if |!one_hop|? + + mPosition += one_hop; + n -= one_hop; + } + + return *this; + } + }; + +template +class nsWritingIterator +// : public nsReadingIterator + { + public: + typedef ptrdiff_t difference_type; + typedef CharT value_type; + typedef CharT* pointer; + typedef CharT& reference; +// typedef bidirectional_iterator_tag iterator_category; + + private: + friend class basic_nsAWritableString; + + nsWritableFragment mFragment; + CharT* mPosition; + basic_nsAWritableString* mOwningString; + + public: + nsWritingIterator() { } + // nsWritingIterator( const nsWritingIterator& ); // auto-generated copy-constructor OK + // nsWritingIterator& operator=( const nsWritingIterator& ); // auto-generated copy-assignment operator OK + + inline void normalize_forward(); + inline void normalize_backward(); + + pointer + get() const + { + return mPosition; + } + + reference + operator*() const + { + return *get(); + } + +#if 0 + // An iterator really deserves this, but some compilers (notably IBM VisualAge for OS/2) + // don't like this when |CharT| is a type without members. + pointer + operator->() const + { + return get(); + } +#endif + + nsWritingIterator& + operator++() + { + ++mPosition; + normalize_forward(); + return *this; + } + + nsWritingIterator + operator++( int ) + { + nsWritingIterator result(*this); + ++mPosition; + normalize_forward(); + return result; + } + + nsWritingIterator& + operator--() + { + normalize_backward(); + --mPosition; + return *this; + } + + nsWritingIterator + operator--( int ) + { + nsWritingIterator result(*this); + normalize_backward(); + --mPosition; + return result; + } + + const nsWritableFragment& + fragment() const + { + return mFragment; + } + + nsWritableFragment& + fragment() + { + return mFragment; + } + + const basic_nsAWritableString& + string() const + { + NS_ASSERTION(mOwningString, "iterator not attached to a string (|mOwningString| == 0)"); + return *mOwningString; + } + + basic_nsAWritableString& + string() + { + NS_ASSERTION(mOwningString, "iterator not attached to a string (|mOwningString| == 0)"); + return *mOwningString; + } + + difference_type + size_forward() const + { + return mFragment.mEnd - mPosition; + } + + difference_type + size_backward() const + { + return mPosition - mFragment.mStart; + } + + nsWritingIterator& + advance( difference_type n ) + { + while ( n > 0 ) + { + difference_type one_hop = NS_MIN(n, size_forward()); + + NS_ASSERTION(one_hop>0, "Infinite loop: can't advance a writing iterator beyond the end of a string"); + // perhaps I should |break| if |!one_hop|? + + mPosition += one_hop; + normalize_forward(); + n -= one_hop; + } + + while ( n < 0 ) + { + normalize_backward(); + difference_type one_hop = NS_MAX(n, -size_backward()); + + NS_ASSERTION(one_hop<0, "Infinite loop: can't advance (backward) a writing iterator beyond the end of a string"); + // perhaps I should |break| if |!one_hop|? + + mPosition += one_hop; + n -= one_hop; + } + + return *this; + } + + PRUint32 + write( const value_type* s, PRUint32 n ) + { + NS_ASSERTION(size_forward() > 0, "You can't |write| into an |nsWritingIterator| with no space!"); + + n = NS_MIN(n, PRUint32(size_forward())); + nsCharTraits::move(mPosition, s, n); + advance( difference_type(n) ); + return n; + } + }; + +template +inline +void +nsReadingIterator::normalize_forward() + { + while ( mPosition == mFragment.mEnd + && mOwningString->GetReadableFragment(mFragment, kNextFragment) ) + mPosition = mFragment.mStart; + } + +template +inline +void +nsReadingIterator::normalize_backward() + { + while ( mPosition == mFragment.mStart + && mOwningString->GetReadableFragment(mFragment, kPrevFragment) ) + mPosition = mFragment.mEnd; + } + + + // + // |nsWritingIterator|s + // + +template +inline +void +nsWritingIterator::normalize_forward() + { + while ( mPosition == mFragment.mEnd + && mOwningString->GetWritableFragment(mFragment, kNextFragment) ) + mPosition = mFragment.mStart; + } + +template +inline +void +nsWritingIterator::normalize_backward() + { + while ( mPosition == mFragment.mStart + && mOwningString->GetWritableFragment(mFragment, kPrevFragment) ) + mPosition = mFragment.mEnd; + } + +template +inline +PRBool +operator==( const nsWritingIterator& lhs, const nsWritingIterator& rhs ) + { + return lhs.get() == rhs.get(); + } + +template +inline +PRBool +operator!=( const nsWritingIterator& lhs, const nsWritingIterator& rhs ) + { + return lhs.get() != rhs.get(); + } + +#endif /* !defined(nsStringIterator_h___) */ diff --git a/mozilla/xpcom/string/public/nsStringIteratorUtils.h b/mozilla/xpcom/string/public/nsStringIteratorUtils.h new file mode 100644 index 00000000000..1bed6be81f3 --- /dev/null +++ b/mozilla/xpcom/string/public/nsStringIteratorUtils.h @@ -0,0 +1,46 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +#ifndef nsStringIteratorUtils_h___ +#define nsStringIteratorUtils_h___ + +#ifndef nsStringIterator_h___ +#include "nsStringIterator.h" +#endif + +template +inline +PRBool +SameFragment( const Iterator& lhs, const Iterator& rhs ) + { + return lhs.fragment().mStart == rhs.fragment().mStart; + } + + + // NOTE: need to break iterators out into their own file (as with many classes here), need + // these routines, but can't currently |#include "nsReadableUtils.h"|, this hack is bad + // but we need it to get OS2 building again. Fix by splitting things into different files. +NS_COM size_t Distance( const nsReadingIterator&, const nsReadingIterator& ); +NS_COM size_t Distance( const nsReadingIterator&, const nsReadingIterator& ); + + +#endif /* !defined(nsStringIteratorUtils_h___) */ diff --git a/mozilla/xpcom/string/src/Makefile.in b/mozilla/xpcom/string/src/Makefile.in index fdec9c55559..16189711271 100644 --- a/mozilla/xpcom/string/src/Makefile.in +++ b/mozilla/xpcom/string/src/Makefile.in @@ -18,6 +18,7 @@ # # Contributor(s): # Johnny Stenback (original author) +# Scott Collins # DEPTH = ../.. @@ -33,6 +34,7 @@ LIBRARY_NAME = string_s REQUIRES = xpcom CPPSRCS = \ + nsLocalString.cpp \ nsFragmentedString.cpp \ nsPrintfCString.cpp \ nsReadableUtils.cpp \ diff --git a/mozilla/xpcom/string/src/makefile.win b/mozilla/xpcom/string/src/makefile.win index 4442c23ef74..d4cd1a950d1 100644 --- a/mozilla/xpcom/string/src/makefile.win +++ b/mozilla/xpcom/string/src/makefile.win @@ -19,6 +19,7 @@ # # Contributor(s): # Johnny Stenback (original author) +# Scott Collins # DEPTH=..\.. @@ -28,6 +29,7 @@ LIBRARY_NAME=string_s LCFLAGS = -D_IMPL_NS_COM -D_IMPL_NS_BASE -DWIN32_LEAN_AND_MEAN CPP_OBJS = \ + .\$(OBJDIR)\nsLocalString.obj \ .\$(OBJDIR)\nsFragmentedString.obj \ .\$(OBJDIR)\nsPrintfCString.obj \ .\$(OBJDIR)\nsReadableUtils.obj \ diff --git a/mozilla/xpcom/string/src/nsDependentString.cpp b/mozilla/xpcom/string/src/nsDependentString.cpp new file mode 100644 index 00000000000..3a077a67251 --- /dev/null +++ b/mozilla/xpcom/string/src/nsDependentString.cpp @@ -0,0 +1,74 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +#include "nsLocalString.h" + + +const PRUnichar* +nsLocalString::GetReadableFragment( nsReadableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const + { + switch ( aRequest ) + { + case kFirstFragment: + case kLastFragment: + case kFragmentAt: + aFragment.mStart = mStart; + aFragment.mEnd = mEnd; + return mStart + aOffset; + + case kPrevFragment: + case kNextFragment: + default: + return 0; + } + } + +PRUint32 +nsLocalString::Length() const + { + return PRUint32(mEnd - mStart); + } + +const char* +nsLocalCString::GetReadableFragment( nsReadableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const + { + switch ( aRequest ) + { + case kFirstFragment: + case kLastFragment: + case kFragmentAt: + aFragment.mStart = mStart; + aFragment.mEnd = mEnd; + return mStart + aOffset; + + case kPrevFragment: + case kNextFragment: + default: + return 0; + } + } + +PRUint32 +nsLocalCString::Length() const + { + return PRUint32(mEnd - mStart); + } diff --git a/mozilla/xpcom/string/src/nsLocalString.cpp b/mozilla/xpcom/string/src/nsLocalString.cpp new file mode 100644 index 00000000000..3a077a67251 --- /dev/null +++ b/mozilla/xpcom/string/src/nsLocalString.cpp @@ -0,0 +1,74 @@ +/* + * The contents of this file are subject to the Mozilla Public + * License Version 1.1 (the "License"); you may not use this file + * except in compliance with the License. You may obtain a copy of + * the License at http://www.mozilla.org/MPL/ + * + * Software distributed under the License is distributed on an "AS + * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or + * implied. See the License for the specific language governing + * rights and limitations under the License. + * + * The Original Code is Mozilla. + * + * The Initial Developer of the Original Code is Netscape + * Communications. Portions created by Netscape Communications are + * Copyright (C) 2001 by Netscape Communications. All + * Rights Reserved. + * + * Contributor(s): + * Scott Collins (original author) + */ + +#include "nsLocalString.h" + + +const PRUnichar* +nsLocalString::GetReadableFragment( nsReadableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const + { + switch ( aRequest ) + { + case kFirstFragment: + case kLastFragment: + case kFragmentAt: + aFragment.mStart = mStart; + aFragment.mEnd = mEnd; + return mStart + aOffset; + + case kPrevFragment: + case kNextFragment: + default: + return 0; + } + } + +PRUint32 +nsLocalString::Length() const + { + return PRUint32(mEnd - mStart); + } + +const char* +nsLocalCString::GetReadableFragment( nsReadableFragment& aFragment, nsFragmentRequest aRequest, PRUint32 aOffset ) const + { + switch ( aRequest ) + { + case kFirstFragment: + case kLastFragment: + case kFragmentAt: + aFragment.mStart = mStart; + aFragment.mEnd = mEnd; + return mStart + aOffset; + + case kPrevFragment: + case kNextFragment: + default: + return 0; + } + } + +PRUint32 +nsLocalCString::Length() const + { + return PRUint32(mEnd - mStart); + }