Reduce string copying and allocation in the parser by only copying the scanner buffer when we need to mutate the string. Allow a nsDependentString / nsDependentSubstring to be created without being bound to anything. Move StripChar() onto nsSubstring from nsString. Bug 269853, r=jst, sr=darin.

git-svn-id: svn://10.0.0.236/trunk@165773 18797224-902f-48f8-a5cc-f745e15eee43
This commit is contained in:
bryner%brianryner.com
2004-11-25 07:03:20 +00:00
parent 2e6bee01c4
commit 7a461c49a4
17 changed files with 458 additions and 138 deletions

View File

@@ -265,7 +265,7 @@ public:
virtual const nsSubstring& GetStringValue(void);
protected:
nsString mTextValue;
nsScannerSharedSubstring mTextValue;
};
/**
@@ -363,7 +363,7 @@ public:
virtual void SetKey(const nsAString& aKey);
virtual void BindKey(nsScanner* aScanner, nsScannerIterator& aStart,
nsScannerIterator& aEnd);
const nsString& GetValue(void) {return mTextValue;}
const nsSubstring& GetValue(void) {return mTextValue.str();}
virtual void SanitizeKey();
virtual const nsSubstring& GetStringValue(void);
virtual void GetSource(nsString& anOutputString);
@@ -374,7 +374,7 @@ protected:
#ifdef DEBUG
PRPackedBool mLastAttribute;
#endif
nsAutoString mTextValue;
nsScannerSharedSubstring mTextValue;
nsScannerSubstring mTextKey;
};

View File

@@ -273,8 +273,10 @@ class nsScannerSubstring
size_type mLength;
// these fields are used to implement AsString
nsString mFlattenedRep;
nsDependentSubstring mFlattenedRep;
PRBool mIsDirty;
friend class nsScannerSharedSubstring;
};
@@ -301,6 +303,52 @@ class nsScannerString : public nsScannerSubstring
};
/**
* nsScannerSharedSubstring implements copy-on-write semantics for
* nsScannerSubstring. When you call .writable(), it will copy the data
* and return a mutable string object. This class also manages releasing
* the reference to the scanner buffer when it is no longer needed.
*/
class nsScannerSharedSubstring
{
public:
nsScannerSharedSubstring()
: mBuffer(nsnull), mBufferList(nsnull) { }
~nsScannerSharedSubstring()
{
if (mBufferList)
ReleaseBuffer();
}
// Acquire a copy-on-write reference to the given substring.
NS_HIDDEN_(void) Rebind(const nsScannerIterator& aStart,
const nsScannerIterator& aEnd);
// Get a mutable reference to this string
nsSubstring& writable()
{
if (mBufferList)
MakeMutable();
return mString;
}
// Get a const reference to this string
const nsSubstring& str() const { return mString; }
private:
typedef nsScannerBufferList::Buffer Buffer;
NS_HIDDEN_(void) ReleaseBuffer();
NS_HIDDEN_(void) MakeMutable();
nsDependentSubstring mString;
Buffer *mBuffer;
nsScannerBufferList *mBufferList;
};
/**
* nsScannerIterator works just like nsReadingIterator<CharT> except that
* it knows how to iterate over a list of scanner buffers.
@@ -322,6 +370,7 @@ class nsScannerIterator
const nsScannerSubstring* mOwner;
friend class nsScannerSubstring;
friend class nsScannerSharedSubstring;
public:
nsScannerIterator() {}
@@ -553,6 +602,11 @@ AppendUnicodeTo( const nsScannerSubstring& aSrc, nsAString& aDest )
AppendUnicodeTo(aSrc.BeginReading(begin), aSrc.EndReading(end), aDest);
}
void
AppendUnicodeTo( const nsScannerIterator& aSrcStart,
const nsScannerIterator& aSrcEnd,
nsScannerSharedSubstring& aDest );
PRBool
FindCharInReadable( PRUnichar aChar,
nsScannerIterator& aStart,