diff --git a/string/public/nsAWritableString.h b/string/public/nsAWritableString.h index 27b023d089f..3dbc52b1b52 100644 --- a/string/public/nsAWritableString.h +++ b/string/public/nsAWritableString.h @@ -233,16 +233,18 @@ class basic_nsAWritableString - virtual void Assign( const basic_nsAReadableString& rhs ) = 0; + virtual void Assign( const basic_nsAReadableString& rhs ); // virtual void AssignChar( CharT ) = 0; - virtual void Append( const basic_nsAReadableString& ) = 0; - virtual void AppendChar( CharT ) = 0; + virtual void Append( const basic_nsAReadableString& ); + virtual void AppendChar( CharT ); - virtual void Insert( const basic_nsAReadableString&, PRUint32 atPosition ) = 0; + virtual void Insert( const basic_nsAReadableString&, PRUint32 atPosition ); // virtual void InsertChar( CharT, PRUint32 atPosition ) = 0; - virtual void Cut( PRUint32 cutStart, PRUint32 cutEnd ) = 0; + virtual void Cut( PRUint32 cutStart, PRUint32 cutLength ); + + virtual void Replace( PRUint32 cutStart, PRUint32 cutLength, const basic_nsAReadableString& ); basic_nsAWritableString& @@ -276,6 +278,75 @@ class basic_nsAWritableString NS_DEF_STRING_COMPARISONS(basic_nsAWritableString) +template +void +basic_nsAWritableString::Assign( const basic_nsAReadableString& rhs ) + { + SetLength(rhs.Length()); + std::copy(rhs.Begin(), rhs.End(), Begin()); + } + +template +void +basic_nsAWritableString::Append( const basic_nsAReadableString& rhs ) + { + PRUint32 oldLength = Length(); + SetLength(oldLength + rhs.Length()); + std::copy(rhs.Begin(), rhs.End(), Begin(oldLength)); + } + +template +void +basic_nsAWritableString::AppendChar( CharT aChar ) + { + SetLength(Length()+1); + *End(1) = aChar; + } + +template +void +basic_nsAWritableString::Insert( const basic_nsAReadableString& aReadable, PRUint32 aPosition ) + { + PRUint32 oldLength = Length(); + SetLength(oldLength + aReadable.Length()); + if ( aPosition < oldLength ) + std::copy_backward(Begin(aPosition), Begin(oldLength), End()); + else + aPosition = oldLength; + std::copy(aReadable.Begin(), aReadable.End(), Begin(aPosition)); + } + +template +void +basic_nsAWritableString::Cut( PRUint32 cutStart, PRUint32 cutLength ) + { + std::copy(Begin(cutStart+cutLength), End(), Begin(cutStart)); + SetLength(Length()-cutLength); + } + +template +void +basic_nsAWritableString::Replace( PRUint32 cutStart, PRUint32 cutLength, const basic_nsAReadableString& aReplacement ) + { + PRUint32 oldLength = Length(); + + cutStart = min(cutStart, oldLength); + cutLength = min(cutLength, oldLength-cutStart); + PRUint32 cutEnd = cutStart + cutLength; + + PRUint32 replacementLength = aReplacement.Length(); + PRUint32 replacementEnd = cutStart + replacementLength; + + PRUint32 newLength = oldLength - cutLength + replacementLength; + + if ( cutLength > replacementLength ) + std::copy(Begin(cutEnd), End(), Begin(replacementEnd)); + SetLength(newLength); + if ( cutLength < replacementLength ) + std::copy_backward(Begin(cutEnd), Begin(oldLength), Begin(replacementEnd)); + + std::copy(aReplacement.Begin(), aReplacement.End(), Begin(cutStart)); + } // operator>> // getline (maybe) diff --git a/xpcom/ds/nsAWritableString.h b/xpcom/ds/nsAWritableString.h index 27b023d089f..3dbc52b1b52 100644 --- a/xpcom/ds/nsAWritableString.h +++ b/xpcom/ds/nsAWritableString.h @@ -233,16 +233,18 @@ class basic_nsAWritableString - virtual void Assign( const basic_nsAReadableString& rhs ) = 0; + virtual void Assign( const basic_nsAReadableString& rhs ); // virtual void AssignChar( CharT ) = 0; - virtual void Append( const basic_nsAReadableString& ) = 0; - virtual void AppendChar( CharT ) = 0; + virtual void Append( const basic_nsAReadableString& ); + virtual void AppendChar( CharT ); - virtual void Insert( const basic_nsAReadableString&, PRUint32 atPosition ) = 0; + virtual void Insert( const basic_nsAReadableString&, PRUint32 atPosition ); // virtual void InsertChar( CharT, PRUint32 atPosition ) = 0; - virtual void Cut( PRUint32 cutStart, PRUint32 cutEnd ) = 0; + virtual void Cut( PRUint32 cutStart, PRUint32 cutLength ); + + virtual void Replace( PRUint32 cutStart, PRUint32 cutLength, const basic_nsAReadableString& ); basic_nsAWritableString& @@ -276,6 +278,75 @@ class basic_nsAWritableString NS_DEF_STRING_COMPARISONS(basic_nsAWritableString) +template +void +basic_nsAWritableString::Assign( const basic_nsAReadableString& rhs ) + { + SetLength(rhs.Length()); + std::copy(rhs.Begin(), rhs.End(), Begin()); + } + +template +void +basic_nsAWritableString::Append( const basic_nsAReadableString& rhs ) + { + PRUint32 oldLength = Length(); + SetLength(oldLength + rhs.Length()); + std::copy(rhs.Begin(), rhs.End(), Begin(oldLength)); + } + +template +void +basic_nsAWritableString::AppendChar( CharT aChar ) + { + SetLength(Length()+1); + *End(1) = aChar; + } + +template +void +basic_nsAWritableString::Insert( const basic_nsAReadableString& aReadable, PRUint32 aPosition ) + { + PRUint32 oldLength = Length(); + SetLength(oldLength + aReadable.Length()); + if ( aPosition < oldLength ) + std::copy_backward(Begin(aPosition), Begin(oldLength), End()); + else + aPosition = oldLength; + std::copy(aReadable.Begin(), aReadable.End(), Begin(aPosition)); + } + +template +void +basic_nsAWritableString::Cut( PRUint32 cutStart, PRUint32 cutLength ) + { + std::copy(Begin(cutStart+cutLength), End(), Begin(cutStart)); + SetLength(Length()-cutLength); + } + +template +void +basic_nsAWritableString::Replace( PRUint32 cutStart, PRUint32 cutLength, const basic_nsAReadableString& aReplacement ) + { + PRUint32 oldLength = Length(); + + cutStart = min(cutStart, oldLength); + cutLength = min(cutLength, oldLength-cutStart); + PRUint32 cutEnd = cutStart + cutLength; + + PRUint32 replacementLength = aReplacement.Length(); + PRUint32 replacementEnd = cutStart + replacementLength; + + PRUint32 newLength = oldLength - cutLength + replacementLength; + + if ( cutLength > replacementLength ) + std::copy(Begin(cutEnd), End(), Begin(replacementEnd)); + SetLength(newLength); + if ( cutLength < replacementLength ) + std::copy_backward(Begin(cutEnd), Begin(oldLength), Begin(replacementEnd)); + + std::copy(aReplacement.Begin(), aReplacement.End(), Begin(cutStart)); + } // operator>> // getline (maybe) diff --git a/xpcom/string/public/nsAWritableString.h b/xpcom/string/public/nsAWritableString.h index 27b023d089f..3dbc52b1b52 100644 --- a/xpcom/string/public/nsAWritableString.h +++ b/xpcom/string/public/nsAWritableString.h @@ -233,16 +233,18 @@ class basic_nsAWritableString - virtual void Assign( const basic_nsAReadableString& rhs ) = 0; + virtual void Assign( const basic_nsAReadableString& rhs ); // virtual void AssignChar( CharT ) = 0; - virtual void Append( const basic_nsAReadableString& ) = 0; - virtual void AppendChar( CharT ) = 0; + virtual void Append( const basic_nsAReadableString& ); + virtual void AppendChar( CharT ); - virtual void Insert( const basic_nsAReadableString&, PRUint32 atPosition ) = 0; + virtual void Insert( const basic_nsAReadableString&, PRUint32 atPosition ); // virtual void InsertChar( CharT, PRUint32 atPosition ) = 0; - virtual void Cut( PRUint32 cutStart, PRUint32 cutEnd ) = 0; + virtual void Cut( PRUint32 cutStart, PRUint32 cutLength ); + + virtual void Replace( PRUint32 cutStart, PRUint32 cutLength, const basic_nsAReadableString& ); basic_nsAWritableString& @@ -276,6 +278,75 @@ class basic_nsAWritableString NS_DEF_STRING_COMPARISONS(basic_nsAWritableString) +template +void +basic_nsAWritableString::Assign( const basic_nsAReadableString& rhs ) + { + SetLength(rhs.Length()); + std::copy(rhs.Begin(), rhs.End(), Begin()); + } + +template +void +basic_nsAWritableString::Append( const basic_nsAReadableString& rhs ) + { + PRUint32 oldLength = Length(); + SetLength(oldLength + rhs.Length()); + std::copy(rhs.Begin(), rhs.End(), Begin(oldLength)); + } + +template +void +basic_nsAWritableString::AppendChar( CharT aChar ) + { + SetLength(Length()+1); + *End(1) = aChar; + } + +template +void +basic_nsAWritableString::Insert( const basic_nsAReadableString& aReadable, PRUint32 aPosition ) + { + PRUint32 oldLength = Length(); + SetLength(oldLength + aReadable.Length()); + if ( aPosition < oldLength ) + std::copy_backward(Begin(aPosition), Begin(oldLength), End()); + else + aPosition = oldLength; + std::copy(aReadable.Begin(), aReadable.End(), Begin(aPosition)); + } + +template +void +basic_nsAWritableString::Cut( PRUint32 cutStart, PRUint32 cutLength ) + { + std::copy(Begin(cutStart+cutLength), End(), Begin(cutStart)); + SetLength(Length()-cutLength); + } + +template +void +basic_nsAWritableString::Replace( PRUint32 cutStart, PRUint32 cutLength, const basic_nsAReadableString& aReplacement ) + { + PRUint32 oldLength = Length(); + + cutStart = min(cutStart, oldLength); + cutLength = min(cutLength, oldLength-cutStart); + PRUint32 cutEnd = cutStart + cutLength; + + PRUint32 replacementLength = aReplacement.Length(); + PRUint32 replacementEnd = cutStart + replacementLength; + + PRUint32 newLength = oldLength - cutLength + replacementLength; + + if ( cutLength > replacementLength ) + std::copy(Begin(cutEnd), End(), Begin(replacementEnd)); + SetLength(newLength); + if ( cutLength < replacementLength ) + std::copy_backward(Begin(cutEnd), Begin(oldLength), Begin(replacementEnd)); + + std::copy(aReplacement.Begin(), aReplacement.End(), Begin(cutStart)); + } // operator>> // getline (maybe)