mirror of
https://github.com/mozilla/gecko-dev.git
synced 2024-11-26 22:32:46 +00:00
Incremental changes. These files are not yet part of the build.
This commit is contained in:
parent
383a02c1ba
commit
b0b8c5e18f
@ -71,6 +71,10 @@ template <class CharT> class basic_nsAWritableString;
|
||||
template <class CharT> class basic_nsLiteralString;
|
||||
// ...because we sometimes use them as in params to force the conversion of |CharT*|s
|
||||
|
||||
template <class CharT> class nsPromiseConcatenation;
|
||||
template <class CharT> class nsPromiseSubstring;
|
||||
template <class CharT> PRBool SameImplementation( const basic_nsAReadableString<CharT>&, const basic_nsAReadableString<CharT>& );
|
||||
|
||||
|
||||
enum nsFragmentRequest { kPrevFragment, kFirstFragment, kLastFragment, kNextFragment, kFragmentAt };
|
||||
|
||||
@ -221,21 +225,6 @@ class nsReadingIterator
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
// Damn again! Problems with templates made me implement comparisons as members.
|
||||
|
||||
PRBool
|
||||
operator==( const nsReadingIterator<CharT>& rhs ) const
|
||||
{
|
||||
return mPosition == rhs.mPosition;
|
||||
}
|
||||
|
||||
PRBool
|
||||
operator!=( const nsReadingIterator<CharT>& rhs ) const
|
||||
{
|
||||
return mPosition != rhs.mPosition;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -249,20 +238,21 @@ class basic_nsAReadableString
|
||||
...
|
||||
*/
|
||||
{
|
||||
// friend class nsReadingIterator<CharT>;
|
||||
protected:
|
||||
|
||||
friend PRBool SameImplementation<CharT>( const basic_nsAReadableString<CharT>&, const basic_nsAReadableString<CharT>& );
|
||||
virtual const void* Implementation() const;
|
||||
|
||||
friend class nsReadingIterator<CharT>;
|
||||
friend class nsPromiseConcatenation<CharT>;
|
||||
friend class nsPromiseSubstring<CharT>;
|
||||
virtual const CharT* GetReadableFragment( nsReadableFragment<CharT>&, nsFragmentRequest, PRUint32 = 0 ) const = 0;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
typedef nsReadingIterator<CharT> ConstIterator;
|
||||
|
||||
public:
|
||||
|
||||
virtual const void* Implementation() const;
|
||||
|
||||
// Damn! Had to make |GetReadableFragment| public because the compilers suck. Should be protected.
|
||||
virtual const CharT* GetReadableFragment( nsReadableFragment<CharT>&, nsFragmentRequest, PRUint32 = 0 ) const = 0;
|
||||
|
||||
|
||||
nsReadingIterator<CharT>
|
||||
BeginReading( PRUint32 aOffset = 0 ) const
|
||||
{
|
||||
@ -400,6 +390,24 @@ nsReadingIterator<CharT>::normalize_backward()
|
||||
mPosition = mFragment.mEnd;
|
||||
}
|
||||
|
||||
|
||||
template <class CharT>
|
||||
inline
|
||||
PRBool
|
||||
operator==( const nsReadingIterator<CharT>& lhs, const nsReadingIterator<CharT>& rhs )
|
||||
{
|
||||
return lhs.operator->() == rhs.operator->();
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
inline
|
||||
PRBool
|
||||
operator!=( const nsReadingIterator<CharT>& lhs, const nsReadingIterator<CharT>& rhs )
|
||||
{
|
||||
return lhs.operator->() != rhs.operator->();
|
||||
}
|
||||
|
||||
|
||||
#define NS_DEF_1_STRING_COMPARISON_OPERATOR(comp, T1, T2) \
|
||||
inline \
|
||||
PRBool \
|
||||
@ -890,6 +898,57 @@ nsPromiseSubstring<CharT>::GetReadableFragment( nsReadableFragment<CharT>& aFrag
|
||||
// Global functions
|
||||
//
|
||||
|
||||
template <class InputIterator, class OutputIterator>
|
||||
OutputIterator
|
||||
string_copy( InputIterator first, InputIterator last, OutputIterator result )
|
||||
{
|
||||
while ( first != last )
|
||||
{
|
||||
PRUint32 lengthToCopy = PRUint32( NS_MIN(first.size_forward(), result.size_forward()) );
|
||||
if ( first.fragment().mStart == last.fragment().mStart )
|
||||
lengthToCopy = NS_MIN(lengthToCopy, PRUint32(last.operator->() - first.operator->()));
|
||||
|
||||
// assert(lengthToCopy > 0);
|
||||
|
||||
nsCharTraits<InputIterator::value_type>::copy(result.operator->(), first.operator->(), lengthToCopy);
|
||||
|
||||
first += PRInt32(lengthToCopy);
|
||||
result += PRInt32(lengthToCopy);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class InputIterator, class CharT>
|
||||
CharT*
|
||||
string_copy( InputIterator first, InputIterator last, CharT* result )
|
||||
{
|
||||
while ( first != last )
|
||||
{
|
||||
PRUint32 lengthToCopy = PRUint32(first.size_forward());
|
||||
if ( first.fragment().mStart == last.fragment().mStart )
|
||||
lengthToCopy = NS_MIN(lengthToCopy, PRUint32(last.operator->() - first.operator->()));
|
||||
|
||||
// assert(lengthToCopy > 0);
|
||||
|
||||
nsCharTraits<CharT>::copy(result, first.operator->(), lengthToCopy);
|
||||
|
||||
first += PRInt32(lengthToCopy);
|
||||
result += PRInt32(lengthToCopy);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
inline
|
||||
PRBool
|
||||
SameImplementation( const basic_nsAReadableString<CharT>& lhs, const basic_nsAReadableString<CharT>& rhs )
|
||||
{
|
||||
const void* imp_tag = lhs.Implementation();
|
||||
return imp_tag && (imp_tag==rhs.Implementation());
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
nsPromiseSubstring<CharT>
|
||||
Substring( const basic_nsAReadableString<CharT>& aString, PRUint32 aStartPos, PRUint32 aSubstringLength )
|
||||
|
@ -323,7 +323,7 @@ inline
|
||||
PRBool
|
||||
operator==( const nsWritingIterator<CharT>& lhs, const nsWritingIterator<CharT>& rhs )
|
||||
{
|
||||
return lhs.mPosition == rhs.mPosition;
|
||||
return lhs.operator->() == rhs.operator->();
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
@ -331,29 +331,9 @@ inline
|
||||
PRBool
|
||||
operator!=( const nsWritingIterator<CharT>& lhs, const nsWritingIterator<CharT>& rhs )
|
||||
{
|
||||
return lhs.mPosition != rhs.mPosition;
|
||||
return lhs.operator->() != rhs.operator->();
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
nsWritingIterator<CharT>
|
||||
copy_chunky( nsReadingIterator<CharT> first, nsReadingIterator<CharT> last, nsWritingIterator<CharT> result )
|
||||
{
|
||||
while ( first != last )
|
||||
{
|
||||
PRUint32 lengthToCopy = PRUint32( NS_MIN(first.size_forward(), result.size_forward()) );
|
||||
if ( first.fragment().mStart == last.fragment().mStart )
|
||||
lengthToCopy = NS_MIN(lengthToCopy, PRUint32(last.operator->() - first.operator->()));
|
||||
|
||||
// assert(lengthToCopy > 0);
|
||||
|
||||
nsCharTraits<CharT>::copy(result.operator->(), first.operator->(), lengthToCopy);
|
||||
|
||||
first += PRInt32(lengthToCopy);
|
||||
result += PRInt32(lengthToCopy);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
nsWritingIterator<CharT>
|
||||
@ -382,7 +362,7 @@ void
|
||||
basic_nsAWritableString<CharT>::Assign( const basic_nsAReadableString<CharT>& rhs )
|
||||
{
|
||||
SetLength(rhs.Length());
|
||||
copy_chunky<CharT>(rhs.BeginReading(), rhs.EndReading(), BeginWriting());
|
||||
string_copy(rhs.BeginReading(), rhs.EndReading(), BeginWriting());
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
@ -391,7 +371,7 @@ basic_nsAWritableString<CharT>::Append( const basic_nsAReadableString<CharT>& rh
|
||||
{
|
||||
PRUint32 oldLength = Length();
|
||||
SetLength(oldLength + rhs.Length());
|
||||
copy_chunky<CharT>(rhs.BeginReading(), rhs.EndReading(), BeginWriting(oldLength));
|
||||
string_copy(rhs.BeginReading(), rhs.EndReading(), BeginWriting(oldLength));
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
@ -412,14 +392,14 @@ basic_nsAWritableString<CharT>::Insert( const basic_nsAReadableString<CharT>& aR
|
||||
copy_backward_chunky<CharT>(BeginReading(aPosition), BeginReading(oldLength), EndWriting());
|
||||
else
|
||||
aPosition = oldLength;
|
||||
copy_chunky<CharT>(aReadable.BeginReading(), aReadable.EndReading(), BeginWriting(aPosition));
|
||||
string_copy(aReadable.BeginReading(), aReadable.EndReading(), BeginWriting(aPosition));
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
void
|
||||
basic_nsAWritableString<CharT>::Cut( PRUint32 cutStart, PRUint32 cutLength )
|
||||
{
|
||||
copy_chunky<CharT>(BeginReading(cutStart+cutLength), EndReading(), BeginWriting(cutStart));
|
||||
string_copy(BeginReading(cutStart+cutLength), EndReading(), BeginWriting(cutStart));
|
||||
SetLength(Length()-cutLength);
|
||||
}
|
||||
|
||||
@ -439,12 +419,12 @@ basic_nsAWritableString<CharT>::Replace( PRUint32 cutStart, PRUint32 cutLength,
|
||||
PRUint32 newLength = oldLength - cutLength + replacementLength;
|
||||
|
||||
if ( cutLength > replacementLength )
|
||||
copy_chunky<CharT>(BeginReading(cutEnd), EndReading(), BeginWriting(replacementEnd));
|
||||
string_copy(BeginReading(cutEnd), EndReading(), BeginWriting(replacementEnd));
|
||||
SetLength(newLength);
|
||||
if ( cutLength < replacementLength )
|
||||
copy_backward_chunky<CharT>(BeginReading(cutEnd), BeginReading(oldLength), BeginWriting(replacementEnd));
|
||||
|
||||
copy_chunky<CharT>(aReplacement.BeginReading(), aReplacement.EndReading(), BeginWriting(cutStart));
|
||||
string_copy(aReplacement.BeginReading(), aReplacement.EndReading(), BeginWriting(cutStart));
|
||||
}
|
||||
|
||||
// operator>>
|
||||
|
@ -52,10 +52,10 @@ class basic_nsSharedString
|
||||
return mLength;
|
||||
}
|
||||
|
||||
basic_nsSharedString( const basic_nsAReadableString<CharT>& aReadable )
|
||||
basic_nsSharedString( const CharT* data, size_t length )
|
||||
: mRefCount(0), mData(data), mLength(length)
|
||||
{
|
||||
mLength = aReadable.Length();
|
||||
copy(aReadable.BeginReading(), aReadable.EndReading(), mData+0);
|
||||
// nothing else to do here
|
||||
}
|
||||
|
||||
nsrefcnt
|
||||
@ -75,8 +75,8 @@ class basic_nsSharedString
|
||||
|
||||
private:
|
||||
mutable nsrefcnt mRefCount;
|
||||
const CharT* mData;
|
||||
size_t mLength;
|
||||
CharT mData[1];
|
||||
};
|
||||
|
||||
NS_DEF_STRING_COMPARISONS(basic_nsSharedString<CharT>)
|
||||
@ -117,8 +117,20 @@ template <class CharT>
|
||||
basic_nsSharedString<CharT>*
|
||||
new_nsSharedString( const basic_nsAReadableString<CharT>& aReadable )
|
||||
{
|
||||
void* in_buffer = operator new( sizeof(basic_nsSharedString<CharT>) + aReadable.Length()*sizeof(CharT) );
|
||||
return new (in_buffer) basic_nsSharedString<CharT>(aReadable);
|
||||
size_t object_size = ((sizeof(basic_nsSharedString<CharT>) + sizeof(CharT) - 1) / sizeof(CharT)) * sizeof(CharT);
|
||||
size_t string_length = aReadable.Length();
|
||||
size_t string_size = string_length * sizeof(CharT);
|
||||
|
||||
void* object_ptr = operator new(object_size + string_size);
|
||||
if ( object_ptr )
|
||||
{
|
||||
typedef CharT* CharT_ptr;
|
||||
CharT* string_ptr = CharT_ptr(NS_STATIC_CAST(unsigned char*, object_ptr) + object_size);
|
||||
string_copy(aReadable.BeginReading(), aReadable.EndReading(), string_ptr);
|
||||
return new (object_ptr) basic_nsSharedString<CharT>(string_ptr, string_length);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
@ -71,6 +71,10 @@ template <class CharT> class basic_nsAWritableString;
|
||||
template <class CharT> class basic_nsLiteralString;
|
||||
// ...because we sometimes use them as in params to force the conversion of |CharT*|s
|
||||
|
||||
template <class CharT> class nsPromiseConcatenation;
|
||||
template <class CharT> class nsPromiseSubstring;
|
||||
template <class CharT> PRBool SameImplementation( const basic_nsAReadableString<CharT>&, const basic_nsAReadableString<CharT>& );
|
||||
|
||||
|
||||
enum nsFragmentRequest { kPrevFragment, kFirstFragment, kLastFragment, kNextFragment, kFragmentAt };
|
||||
|
||||
@ -221,21 +225,6 @@ class nsReadingIterator
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
// Damn again! Problems with templates made me implement comparisons as members.
|
||||
|
||||
PRBool
|
||||
operator==( const nsReadingIterator<CharT>& rhs ) const
|
||||
{
|
||||
return mPosition == rhs.mPosition;
|
||||
}
|
||||
|
||||
PRBool
|
||||
operator!=( const nsReadingIterator<CharT>& rhs ) const
|
||||
{
|
||||
return mPosition != rhs.mPosition;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -249,20 +238,21 @@ class basic_nsAReadableString
|
||||
...
|
||||
*/
|
||||
{
|
||||
// friend class nsReadingIterator<CharT>;
|
||||
protected:
|
||||
|
||||
friend PRBool SameImplementation<CharT>( const basic_nsAReadableString<CharT>&, const basic_nsAReadableString<CharT>& );
|
||||
virtual const void* Implementation() const;
|
||||
|
||||
friend class nsReadingIterator<CharT>;
|
||||
friend class nsPromiseConcatenation<CharT>;
|
||||
friend class nsPromiseSubstring<CharT>;
|
||||
virtual const CharT* GetReadableFragment( nsReadableFragment<CharT>&, nsFragmentRequest, PRUint32 = 0 ) const = 0;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
typedef nsReadingIterator<CharT> ConstIterator;
|
||||
|
||||
public:
|
||||
|
||||
virtual const void* Implementation() const;
|
||||
|
||||
// Damn! Had to make |GetReadableFragment| public because the compilers suck. Should be protected.
|
||||
virtual const CharT* GetReadableFragment( nsReadableFragment<CharT>&, nsFragmentRequest, PRUint32 = 0 ) const = 0;
|
||||
|
||||
|
||||
nsReadingIterator<CharT>
|
||||
BeginReading( PRUint32 aOffset = 0 ) const
|
||||
{
|
||||
@ -400,6 +390,24 @@ nsReadingIterator<CharT>::normalize_backward()
|
||||
mPosition = mFragment.mEnd;
|
||||
}
|
||||
|
||||
|
||||
template <class CharT>
|
||||
inline
|
||||
PRBool
|
||||
operator==( const nsReadingIterator<CharT>& lhs, const nsReadingIterator<CharT>& rhs )
|
||||
{
|
||||
return lhs.operator->() == rhs.operator->();
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
inline
|
||||
PRBool
|
||||
operator!=( const nsReadingIterator<CharT>& lhs, const nsReadingIterator<CharT>& rhs )
|
||||
{
|
||||
return lhs.operator->() != rhs.operator->();
|
||||
}
|
||||
|
||||
|
||||
#define NS_DEF_1_STRING_COMPARISON_OPERATOR(comp, T1, T2) \
|
||||
inline \
|
||||
PRBool \
|
||||
@ -890,6 +898,57 @@ nsPromiseSubstring<CharT>::GetReadableFragment( nsReadableFragment<CharT>& aFrag
|
||||
// Global functions
|
||||
//
|
||||
|
||||
template <class InputIterator, class OutputIterator>
|
||||
OutputIterator
|
||||
string_copy( InputIterator first, InputIterator last, OutputIterator result )
|
||||
{
|
||||
while ( first != last )
|
||||
{
|
||||
PRUint32 lengthToCopy = PRUint32( NS_MIN(first.size_forward(), result.size_forward()) );
|
||||
if ( first.fragment().mStart == last.fragment().mStart )
|
||||
lengthToCopy = NS_MIN(lengthToCopy, PRUint32(last.operator->() - first.operator->()));
|
||||
|
||||
// assert(lengthToCopy > 0);
|
||||
|
||||
nsCharTraits<InputIterator::value_type>::copy(result.operator->(), first.operator->(), lengthToCopy);
|
||||
|
||||
first += PRInt32(lengthToCopy);
|
||||
result += PRInt32(lengthToCopy);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class InputIterator, class CharT>
|
||||
CharT*
|
||||
string_copy( InputIterator first, InputIterator last, CharT* result )
|
||||
{
|
||||
while ( first != last )
|
||||
{
|
||||
PRUint32 lengthToCopy = PRUint32(first.size_forward());
|
||||
if ( first.fragment().mStart == last.fragment().mStart )
|
||||
lengthToCopy = NS_MIN(lengthToCopy, PRUint32(last.operator->() - first.operator->()));
|
||||
|
||||
// assert(lengthToCopy > 0);
|
||||
|
||||
nsCharTraits<CharT>::copy(result, first.operator->(), lengthToCopy);
|
||||
|
||||
first += PRInt32(lengthToCopy);
|
||||
result += PRInt32(lengthToCopy);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
inline
|
||||
PRBool
|
||||
SameImplementation( const basic_nsAReadableString<CharT>& lhs, const basic_nsAReadableString<CharT>& rhs )
|
||||
{
|
||||
const void* imp_tag = lhs.Implementation();
|
||||
return imp_tag && (imp_tag==rhs.Implementation());
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
nsPromiseSubstring<CharT>
|
||||
Substring( const basic_nsAReadableString<CharT>& aString, PRUint32 aStartPos, PRUint32 aSubstringLength )
|
||||
|
@ -323,7 +323,7 @@ inline
|
||||
PRBool
|
||||
operator==( const nsWritingIterator<CharT>& lhs, const nsWritingIterator<CharT>& rhs )
|
||||
{
|
||||
return lhs.mPosition == rhs.mPosition;
|
||||
return lhs.operator->() == rhs.operator->();
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
@ -331,29 +331,9 @@ inline
|
||||
PRBool
|
||||
operator!=( const nsWritingIterator<CharT>& lhs, const nsWritingIterator<CharT>& rhs )
|
||||
{
|
||||
return lhs.mPosition != rhs.mPosition;
|
||||
return lhs.operator->() != rhs.operator->();
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
nsWritingIterator<CharT>
|
||||
copy_chunky( nsReadingIterator<CharT> first, nsReadingIterator<CharT> last, nsWritingIterator<CharT> result )
|
||||
{
|
||||
while ( first != last )
|
||||
{
|
||||
PRUint32 lengthToCopy = PRUint32( NS_MIN(first.size_forward(), result.size_forward()) );
|
||||
if ( first.fragment().mStart == last.fragment().mStart )
|
||||
lengthToCopy = NS_MIN(lengthToCopy, PRUint32(last.operator->() - first.operator->()));
|
||||
|
||||
// assert(lengthToCopy > 0);
|
||||
|
||||
nsCharTraits<CharT>::copy(result.operator->(), first.operator->(), lengthToCopy);
|
||||
|
||||
first += PRInt32(lengthToCopy);
|
||||
result += PRInt32(lengthToCopy);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
nsWritingIterator<CharT>
|
||||
@ -382,7 +362,7 @@ void
|
||||
basic_nsAWritableString<CharT>::Assign( const basic_nsAReadableString<CharT>& rhs )
|
||||
{
|
||||
SetLength(rhs.Length());
|
||||
copy_chunky<CharT>(rhs.BeginReading(), rhs.EndReading(), BeginWriting());
|
||||
string_copy(rhs.BeginReading(), rhs.EndReading(), BeginWriting());
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
@ -391,7 +371,7 @@ basic_nsAWritableString<CharT>::Append( const basic_nsAReadableString<CharT>& rh
|
||||
{
|
||||
PRUint32 oldLength = Length();
|
||||
SetLength(oldLength + rhs.Length());
|
||||
copy_chunky<CharT>(rhs.BeginReading(), rhs.EndReading(), BeginWriting(oldLength));
|
||||
string_copy(rhs.BeginReading(), rhs.EndReading(), BeginWriting(oldLength));
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
@ -412,14 +392,14 @@ basic_nsAWritableString<CharT>::Insert( const basic_nsAReadableString<CharT>& aR
|
||||
copy_backward_chunky<CharT>(BeginReading(aPosition), BeginReading(oldLength), EndWriting());
|
||||
else
|
||||
aPosition = oldLength;
|
||||
copy_chunky<CharT>(aReadable.BeginReading(), aReadable.EndReading(), BeginWriting(aPosition));
|
||||
string_copy(aReadable.BeginReading(), aReadable.EndReading(), BeginWriting(aPosition));
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
void
|
||||
basic_nsAWritableString<CharT>::Cut( PRUint32 cutStart, PRUint32 cutLength )
|
||||
{
|
||||
copy_chunky<CharT>(BeginReading(cutStart+cutLength), EndReading(), BeginWriting(cutStart));
|
||||
string_copy(BeginReading(cutStart+cutLength), EndReading(), BeginWriting(cutStart));
|
||||
SetLength(Length()-cutLength);
|
||||
}
|
||||
|
||||
@ -439,12 +419,12 @@ basic_nsAWritableString<CharT>::Replace( PRUint32 cutStart, PRUint32 cutLength,
|
||||
PRUint32 newLength = oldLength - cutLength + replacementLength;
|
||||
|
||||
if ( cutLength > replacementLength )
|
||||
copy_chunky<CharT>(BeginReading(cutEnd), EndReading(), BeginWriting(replacementEnd));
|
||||
string_copy(BeginReading(cutEnd), EndReading(), BeginWriting(replacementEnd));
|
||||
SetLength(newLength);
|
||||
if ( cutLength < replacementLength )
|
||||
copy_backward_chunky<CharT>(BeginReading(cutEnd), BeginReading(oldLength), BeginWriting(replacementEnd));
|
||||
|
||||
copy_chunky<CharT>(aReplacement.BeginReading(), aReplacement.EndReading(), BeginWriting(cutStart));
|
||||
string_copy(aReplacement.BeginReading(), aReplacement.EndReading(), BeginWriting(cutStart));
|
||||
}
|
||||
|
||||
// operator>>
|
||||
|
@ -52,10 +52,10 @@ class basic_nsSharedString
|
||||
return mLength;
|
||||
}
|
||||
|
||||
basic_nsSharedString( const basic_nsAReadableString<CharT>& aReadable )
|
||||
basic_nsSharedString( const CharT* data, size_t length )
|
||||
: mRefCount(0), mData(data), mLength(length)
|
||||
{
|
||||
mLength = aReadable.Length();
|
||||
copy(aReadable.BeginReading(), aReadable.EndReading(), mData+0);
|
||||
// nothing else to do here
|
||||
}
|
||||
|
||||
nsrefcnt
|
||||
@ -75,8 +75,8 @@ class basic_nsSharedString
|
||||
|
||||
private:
|
||||
mutable nsrefcnt mRefCount;
|
||||
const CharT* mData;
|
||||
size_t mLength;
|
||||
CharT mData[1];
|
||||
};
|
||||
|
||||
NS_DEF_STRING_COMPARISONS(basic_nsSharedString<CharT>)
|
||||
@ -117,8 +117,20 @@ template <class CharT>
|
||||
basic_nsSharedString<CharT>*
|
||||
new_nsSharedString( const basic_nsAReadableString<CharT>& aReadable )
|
||||
{
|
||||
void* in_buffer = operator new( sizeof(basic_nsSharedString<CharT>) + aReadable.Length()*sizeof(CharT) );
|
||||
return new (in_buffer) basic_nsSharedString<CharT>(aReadable);
|
||||
size_t object_size = ((sizeof(basic_nsSharedString<CharT>) + sizeof(CharT) - 1) / sizeof(CharT)) * sizeof(CharT);
|
||||
size_t string_length = aReadable.Length();
|
||||
size_t string_size = string_length * sizeof(CharT);
|
||||
|
||||
void* object_ptr = operator new(object_size + string_size);
|
||||
if ( object_ptr )
|
||||
{
|
||||
typedef CharT* CharT_ptr;
|
||||
CharT* string_ptr = CharT_ptr(NS_STATIC_CAST(unsigned char*, object_ptr) + object_size);
|
||||
string_copy(aReadable.BeginReading(), aReadable.EndReading(), string_ptr);
|
||||
return new (object_ptr) basic_nsSharedString<CharT>(string_ptr, string_length);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
@ -71,6 +71,10 @@ template <class CharT> class basic_nsAWritableString;
|
||||
template <class CharT> class basic_nsLiteralString;
|
||||
// ...because we sometimes use them as in params to force the conversion of |CharT*|s
|
||||
|
||||
template <class CharT> class nsPromiseConcatenation;
|
||||
template <class CharT> class nsPromiseSubstring;
|
||||
template <class CharT> PRBool SameImplementation( const basic_nsAReadableString<CharT>&, const basic_nsAReadableString<CharT>& );
|
||||
|
||||
|
||||
enum nsFragmentRequest { kPrevFragment, kFirstFragment, kLastFragment, kNextFragment, kFragmentAt };
|
||||
|
||||
@ -221,21 +225,6 @@ class nsReadingIterator
|
||||
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
// Damn again! Problems with templates made me implement comparisons as members.
|
||||
|
||||
PRBool
|
||||
operator==( const nsReadingIterator<CharT>& rhs ) const
|
||||
{
|
||||
return mPosition == rhs.mPosition;
|
||||
}
|
||||
|
||||
PRBool
|
||||
operator!=( const nsReadingIterator<CharT>& rhs ) const
|
||||
{
|
||||
return mPosition != rhs.mPosition;
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@ -249,20 +238,21 @@ class basic_nsAReadableString
|
||||
...
|
||||
*/
|
||||
{
|
||||
// friend class nsReadingIterator<CharT>;
|
||||
protected:
|
||||
|
||||
friend PRBool SameImplementation<CharT>( const basic_nsAReadableString<CharT>&, const basic_nsAReadableString<CharT>& );
|
||||
virtual const void* Implementation() const;
|
||||
|
||||
friend class nsReadingIterator<CharT>;
|
||||
friend class nsPromiseConcatenation<CharT>;
|
||||
friend class nsPromiseSubstring<CharT>;
|
||||
virtual const CharT* GetReadableFragment( nsReadableFragment<CharT>&, nsFragmentRequest, PRUint32 = 0 ) const = 0;
|
||||
|
||||
|
||||
public:
|
||||
|
||||
typedef nsReadingIterator<CharT> ConstIterator;
|
||||
|
||||
public:
|
||||
|
||||
virtual const void* Implementation() const;
|
||||
|
||||
// Damn! Had to make |GetReadableFragment| public because the compilers suck. Should be protected.
|
||||
virtual const CharT* GetReadableFragment( nsReadableFragment<CharT>&, nsFragmentRequest, PRUint32 = 0 ) const = 0;
|
||||
|
||||
|
||||
nsReadingIterator<CharT>
|
||||
BeginReading( PRUint32 aOffset = 0 ) const
|
||||
{
|
||||
@ -400,6 +390,24 @@ nsReadingIterator<CharT>::normalize_backward()
|
||||
mPosition = mFragment.mEnd;
|
||||
}
|
||||
|
||||
|
||||
template <class CharT>
|
||||
inline
|
||||
PRBool
|
||||
operator==( const nsReadingIterator<CharT>& lhs, const nsReadingIterator<CharT>& rhs )
|
||||
{
|
||||
return lhs.operator->() == rhs.operator->();
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
inline
|
||||
PRBool
|
||||
operator!=( const nsReadingIterator<CharT>& lhs, const nsReadingIterator<CharT>& rhs )
|
||||
{
|
||||
return lhs.operator->() != rhs.operator->();
|
||||
}
|
||||
|
||||
|
||||
#define NS_DEF_1_STRING_COMPARISON_OPERATOR(comp, T1, T2) \
|
||||
inline \
|
||||
PRBool \
|
||||
@ -890,6 +898,57 @@ nsPromiseSubstring<CharT>::GetReadableFragment( nsReadableFragment<CharT>& aFrag
|
||||
// Global functions
|
||||
//
|
||||
|
||||
template <class InputIterator, class OutputIterator>
|
||||
OutputIterator
|
||||
string_copy( InputIterator first, InputIterator last, OutputIterator result )
|
||||
{
|
||||
while ( first != last )
|
||||
{
|
||||
PRUint32 lengthToCopy = PRUint32( NS_MIN(first.size_forward(), result.size_forward()) );
|
||||
if ( first.fragment().mStart == last.fragment().mStart )
|
||||
lengthToCopy = NS_MIN(lengthToCopy, PRUint32(last.operator->() - first.operator->()));
|
||||
|
||||
// assert(lengthToCopy > 0);
|
||||
|
||||
nsCharTraits<InputIterator::value_type>::copy(result.operator->(), first.operator->(), lengthToCopy);
|
||||
|
||||
first += PRInt32(lengthToCopy);
|
||||
result += PRInt32(lengthToCopy);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class InputIterator, class CharT>
|
||||
CharT*
|
||||
string_copy( InputIterator first, InputIterator last, CharT* result )
|
||||
{
|
||||
while ( first != last )
|
||||
{
|
||||
PRUint32 lengthToCopy = PRUint32(first.size_forward());
|
||||
if ( first.fragment().mStart == last.fragment().mStart )
|
||||
lengthToCopy = NS_MIN(lengthToCopy, PRUint32(last.operator->() - first.operator->()));
|
||||
|
||||
// assert(lengthToCopy > 0);
|
||||
|
||||
nsCharTraits<CharT>::copy(result, first.operator->(), lengthToCopy);
|
||||
|
||||
first += PRInt32(lengthToCopy);
|
||||
result += PRInt32(lengthToCopy);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
inline
|
||||
PRBool
|
||||
SameImplementation( const basic_nsAReadableString<CharT>& lhs, const basic_nsAReadableString<CharT>& rhs )
|
||||
{
|
||||
const void* imp_tag = lhs.Implementation();
|
||||
return imp_tag && (imp_tag==rhs.Implementation());
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
nsPromiseSubstring<CharT>
|
||||
Substring( const basic_nsAReadableString<CharT>& aString, PRUint32 aStartPos, PRUint32 aSubstringLength )
|
||||
|
@ -323,7 +323,7 @@ inline
|
||||
PRBool
|
||||
operator==( const nsWritingIterator<CharT>& lhs, const nsWritingIterator<CharT>& rhs )
|
||||
{
|
||||
return lhs.mPosition == rhs.mPosition;
|
||||
return lhs.operator->() == rhs.operator->();
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
@ -331,29 +331,9 @@ inline
|
||||
PRBool
|
||||
operator!=( const nsWritingIterator<CharT>& lhs, const nsWritingIterator<CharT>& rhs )
|
||||
{
|
||||
return lhs.mPosition != rhs.mPosition;
|
||||
return lhs.operator->() != rhs.operator->();
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
nsWritingIterator<CharT>
|
||||
copy_chunky( nsReadingIterator<CharT> first, nsReadingIterator<CharT> last, nsWritingIterator<CharT> result )
|
||||
{
|
||||
while ( first != last )
|
||||
{
|
||||
PRUint32 lengthToCopy = PRUint32( NS_MIN(first.size_forward(), result.size_forward()) );
|
||||
if ( first.fragment().mStart == last.fragment().mStart )
|
||||
lengthToCopy = NS_MIN(lengthToCopy, PRUint32(last.operator->() - first.operator->()));
|
||||
|
||||
// assert(lengthToCopy > 0);
|
||||
|
||||
nsCharTraits<CharT>::copy(result.operator->(), first.operator->(), lengthToCopy);
|
||||
|
||||
first += PRInt32(lengthToCopy);
|
||||
result += PRInt32(lengthToCopy);
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
nsWritingIterator<CharT>
|
||||
@ -382,7 +362,7 @@ void
|
||||
basic_nsAWritableString<CharT>::Assign( const basic_nsAReadableString<CharT>& rhs )
|
||||
{
|
||||
SetLength(rhs.Length());
|
||||
copy_chunky<CharT>(rhs.BeginReading(), rhs.EndReading(), BeginWriting());
|
||||
string_copy(rhs.BeginReading(), rhs.EndReading(), BeginWriting());
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
@ -391,7 +371,7 @@ basic_nsAWritableString<CharT>::Append( const basic_nsAReadableString<CharT>& rh
|
||||
{
|
||||
PRUint32 oldLength = Length();
|
||||
SetLength(oldLength + rhs.Length());
|
||||
copy_chunky<CharT>(rhs.BeginReading(), rhs.EndReading(), BeginWriting(oldLength));
|
||||
string_copy(rhs.BeginReading(), rhs.EndReading(), BeginWriting(oldLength));
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
@ -412,14 +392,14 @@ basic_nsAWritableString<CharT>::Insert( const basic_nsAReadableString<CharT>& aR
|
||||
copy_backward_chunky<CharT>(BeginReading(aPosition), BeginReading(oldLength), EndWriting());
|
||||
else
|
||||
aPosition = oldLength;
|
||||
copy_chunky<CharT>(aReadable.BeginReading(), aReadable.EndReading(), BeginWriting(aPosition));
|
||||
string_copy(aReadable.BeginReading(), aReadable.EndReading(), BeginWriting(aPosition));
|
||||
}
|
||||
|
||||
template <class CharT>
|
||||
void
|
||||
basic_nsAWritableString<CharT>::Cut( PRUint32 cutStart, PRUint32 cutLength )
|
||||
{
|
||||
copy_chunky<CharT>(BeginReading(cutStart+cutLength), EndReading(), BeginWriting(cutStart));
|
||||
string_copy(BeginReading(cutStart+cutLength), EndReading(), BeginWriting(cutStart));
|
||||
SetLength(Length()-cutLength);
|
||||
}
|
||||
|
||||
@ -439,12 +419,12 @@ basic_nsAWritableString<CharT>::Replace( PRUint32 cutStart, PRUint32 cutLength,
|
||||
PRUint32 newLength = oldLength - cutLength + replacementLength;
|
||||
|
||||
if ( cutLength > replacementLength )
|
||||
copy_chunky<CharT>(BeginReading(cutEnd), EndReading(), BeginWriting(replacementEnd));
|
||||
string_copy(BeginReading(cutEnd), EndReading(), BeginWriting(replacementEnd));
|
||||
SetLength(newLength);
|
||||
if ( cutLength < replacementLength )
|
||||
copy_backward_chunky<CharT>(BeginReading(cutEnd), BeginReading(oldLength), BeginWriting(replacementEnd));
|
||||
|
||||
copy_chunky<CharT>(aReplacement.BeginReading(), aReplacement.EndReading(), BeginWriting(cutStart));
|
||||
string_copy(aReplacement.BeginReading(), aReplacement.EndReading(), BeginWriting(cutStart));
|
||||
}
|
||||
|
||||
// operator>>
|
||||
|
@ -52,10 +52,10 @@ class basic_nsSharedString
|
||||
return mLength;
|
||||
}
|
||||
|
||||
basic_nsSharedString( const basic_nsAReadableString<CharT>& aReadable )
|
||||
basic_nsSharedString( const CharT* data, size_t length )
|
||||
: mRefCount(0), mData(data), mLength(length)
|
||||
{
|
||||
mLength = aReadable.Length();
|
||||
copy(aReadable.BeginReading(), aReadable.EndReading(), mData+0);
|
||||
// nothing else to do here
|
||||
}
|
||||
|
||||
nsrefcnt
|
||||
@ -75,8 +75,8 @@ class basic_nsSharedString
|
||||
|
||||
private:
|
||||
mutable nsrefcnt mRefCount;
|
||||
const CharT* mData;
|
||||
size_t mLength;
|
||||
CharT mData[1];
|
||||
};
|
||||
|
||||
NS_DEF_STRING_COMPARISONS(basic_nsSharedString<CharT>)
|
||||
@ -117,8 +117,20 @@ template <class CharT>
|
||||
basic_nsSharedString<CharT>*
|
||||
new_nsSharedString( const basic_nsAReadableString<CharT>& aReadable )
|
||||
{
|
||||
void* in_buffer = operator new( sizeof(basic_nsSharedString<CharT>) + aReadable.Length()*sizeof(CharT) );
|
||||
return new (in_buffer) basic_nsSharedString<CharT>(aReadable);
|
||||
size_t object_size = ((sizeof(basic_nsSharedString<CharT>) + sizeof(CharT) - 1) / sizeof(CharT)) * sizeof(CharT);
|
||||
size_t string_length = aReadable.Length();
|
||||
size_t string_size = string_length * sizeof(CharT);
|
||||
|
||||
void* object_ptr = operator new(object_size + string_size);
|
||||
if ( object_ptr )
|
||||
{
|
||||
typedef CharT* CharT_ptr;
|
||||
CharT* string_ptr = CharT_ptr(NS_STATIC_CAST(unsigned char*, object_ptr) + object_size);
|
||||
string_copy(aReadable.BeginReading(), aReadable.EndReading(), string_ptr);
|
||||
return new (object_ptr) basic_nsSharedString<CharT>(string_ptr, string_length);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user