2000-09-09 04:02:48 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/*
|
|
|
|
* 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 strings.
|
|
|
|
*
|
|
|
|
* The Initial Developer of the Original Code is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 2000 Netscape Communications Corporation. All
|
|
|
|
* Rights Reserved.
|
|
|
|
*
|
|
|
|
* Contributor(s):
|
|
|
|
* Scott Collins <scc@mozilla.org> (original author)
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef nsSharedBufferList_h___
|
|
|
|
#define nsSharedBufferList_h___
|
|
|
|
|
2000-09-11 09:24:49 +04:00
|
|
|
#ifndef nsBufferHandle_h___
|
2000-09-09 03:54:23 +04:00
|
|
|
#include "nsBufferHandle.h"
|
2000-09-09 08:42:19 +04:00
|
|
|
// for |nsSharedBufferHandle|
|
2000-09-11 09:24:49 +04:00
|
|
|
#endif
|
2000-09-09 08:42:19 +04:00
|
|
|
|
2000-09-11 09:24:49 +04:00
|
|
|
#ifndef nscore_h___
|
2000-09-09 08:42:19 +04:00
|
|
|
#include "nscore.h"
|
|
|
|
// for |PRUnichar|
|
2000-09-11 09:24:49 +04:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef nsAReadableString_h___
|
|
|
|
#include "nsAReadableString.h"
|
|
|
|
// for |nsReadingIterator|
|
|
|
|
#endif
|
2000-09-09 03:54:23 +04:00
|
|
|
|
2000-12-13 00:58:14 +03:00
|
|
|
#ifndef nsBufferHandleUtils_h___
|
|
|
|
#include "nsBufferHandleUtils.h"
|
|
|
|
// for |NS_AllocateContiguousHandleWithData|
|
|
|
|
#endif
|
|
|
|
|
|
|
|
|
2000-09-09 03:54:23 +04:00
|
|
|
/**
|
2000-09-09 08:42:19 +04:00
|
|
|
* This class forms the basis for several multi-fragment string classes, in
|
|
|
|
* particular: |nsFragmentedString| (though not yet), and |nsSlidingString|/|nsSlidingSubstring|.
|
2000-09-09 03:54:23 +04:00
|
|
|
*
|
2000-09-09 08:42:19 +04:00
|
|
|
* This class is not templated. It is provided only for |PRUnichar|-based strings.
|
|
|
|
* If we turn out to have a need for multi-fragment ASCII strings, then perhaps we'll templatize
|
|
|
|
* or else duplicate this class.
|
2000-09-09 03:54:23 +04:00
|
|
|
*/
|
|
|
|
class nsSharedBufferList
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
|
|
|
|
class Buffer
|
2000-09-13 06:27:11 +04:00
|
|
|
: public nsFlexBufferHandle<PRUnichar>
|
2000-09-09 03:54:23 +04:00
|
|
|
{
|
|
|
|
public:
|
2000-09-09 15:31:04 +04:00
|
|
|
Buffer( PRUnichar* aDataStart, PRUnichar* aDataEnd, PRUnichar* aStorageStart, PRUnichar* aStorageEnd, PRBool aIsSingleAllocation=PR_FALSE )
|
2000-09-13 06:27:11 +04:00
|
|
|
: nsFlexBufferHandle<PRUnichar>(aDataStart, aDataEnd, aStorageStart, aStorageEnd)
|
2000-09-09 15:31:04 +04:00
|
|
|
{
|
|
|
|
if ( aIsSingleAllocation )
|
2000-09-09 17:26:07 +04:00
|
|
|
this->mFlags |= this->kIsSingleAllocationWithBuffer;
|
2000-09-09 15:31:04 +04:00
|
|
|
}
|
2000-09-09 03:54:23 +04:00
|
|
|
|
2000-12-13 00:58:14 +03:00
|
|
|
/**
|
|
|
|
* These buffers will be `owned' by the list, and only the
|
|
|
|
* the list itself will be allowed to delete member |Buffer|s,
|
|
|
|
* therefore, we cannot use the inherited |AcquireReference|
|
|
|
|
* and |ReleaseReference|, as they use the model that the
|
|
|
|
* buffer manages its own lifetime.
|
|
|
|
*/
|
|
|
|
void
|
|
|
|
AcquireNonOwningReference() const
|
|
|
|
{
|
|
|
|
Buffer* mutable_this = NS_CONST_CAST(Buffer*, this);
|
|
|
|
mutable_this->set_refcount( get_refcount()+1 );
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
ReleaseNonOwningReference() const
|
|
|
|
{
|
|
|
|
Buffer* mutable_this = NS_CONST_CAST(Buffer*, this);
|
|
|
|
mutable_this->set_refcount( get_refcount()-1 );
|
|
|
|
}
|
|
|
|
|
2000-09-09 03:54:23 +04:00
|
|
|
Buffer* mPrev;
|
|
|
|
Buffer* mNext;
|
2000-09-11 09:24:49 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
// pass-by-value is explicitly denied
|
|
|
|
Buffer( const Buffer& ); // NOT TO BE IMPLEMENTED
|
|
|
|
void operator=( const Buffer& ); // NOT TO BE IMPLEMENTED
|
2000-09-09 03:54:23 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
struct Position
|
|
|
|
{
|
2000-09-09 13:39:44 +04:00
|
|
|
Buffer* mBuffer;
|
|
|
|
PRUnichar* mPosInBuffer;
|
2000-09-09 15:31:04 +04:00
|
|
|
|
|
|
|
Position() { }
|
|
|
|
Position( Buffer* aBuffer, PRUnichar* aPosInBuffer ) : mBuffer(aBuffer), mPosInBuffer(aPosInBuffer) { }
|
2000-09-11 09:24:49 +04:00
|
|
|
|
|
|
|
// Position( const Position& ); -- auto-generated copy-constructor OK
|
|
|
|
// Position& operator=( const Position& ); -- auto-generated copy-assignment OK
|
|
|
|
// ~Position(); -- auto-generated destructor OK
|
|
|
|
|
|
|
|
// iff |aIter| is a valid iterator into a |nsSharedBufferList|
|
|
|
|
explicit
|
|
|
|
Position( const nsReadingIterator<PRUnichar>& aIter )
|
|
|
|
: mBuffer( NS_CONST_CAST(Buffer*, NS_REINTERPRET_CAST(const Buffer*, aIter.fragment().mFragmentIdentifier)) ),
|
|
|
|
mPosInBuffer( NS_CONST_CAST(PRUnichar*, aIter.get()) )
|
|
|
|
{
|
|
|
|
// nothing else to do here
|
|
|
|
}
|
|
|
|
|
|
|
|
// iff |aIter| is a valid iterator into a |nsSharedBufferList|
|
|
|
|
Position&
|
|
|
|
operator=( const nsReadingIterator<PRUnichar>& aIter )
|
|
|
|
{
|
|
|
|
mBuffer = NS_CONST_CAST(Buffer*, NS_REINTERPRET_CAST(const Buffer*, aIter.fragment().mFragmentIdentifier));
|
|
|
|
mPosInBuffer = NS_CONST_CAST(PRUnichar*, aIter.get());
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
void PointTo( Buffer* aBuffer, PRUnichar* aPosInBuffer ) { mBuffer=aBuffer; mPosInBuffer=aPosInBuffer; }
|
|
|
|
void PointBefore( Buffer* aBuffer ) { PointTo(aBuffer, aBuffer->DataStart()); }
|
|
|
|
void PointAfter( Buffer* aBuffer ) { PointTo(aBuffer, aBuffer->DataEnd()); }
|
|
|
|
|
|
|
|
// Position( const Position& ); -- automatically generated copy-constructor is OK
|
|
|
|
// Position& operator=( const Position& ); -- automatically generated copy-assignment operator is OK
|
2000-12-13 00:58:14 +03:00
|
|
|
|
|
|
|
// don't want to provide this as |operator-|, since that might imply O(1)
|
|
|
|
static ptrdiff_t Distance( const Position&, const Position& );
|
2000-09-09 03:54:23 +04:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
public:
|
2000-09-09 15:31:04 +04:00
|
|
|
nsSharedBufferList( Buffer* aBuffer = 0 )
|
|
|
|
: mFirstBuffer(aBuffer),
|
|
|
|
mLastBuffer(aBuffer),
|
|
|
|
mTotalDataLength(0)
|
|
|
|
{
|
|
|
|
if ( aBuffer )
|
|
|
|
{
|
|
|
|
aBuffer->mPrev = aBuffer->mNext = 0;
|
|
|
|
mTotalDataLength = aBuffer->DataLength();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-09 03:54:23 +04:00
|
|
|
virtual ~nsSharedBufferList();
|
|
|
|
|
|
|
|
private:
|
2000-09-11 09:24:49 +04:00
|
|
|
// pass-by-value is explicitly denied
|
2000-09-09 03:54:23 +04:00
|
|
|
nsSharedBufferList( const nsSharedBufferList& ); // NOT TO BE IMPLEMENTED
|
|
|
|
void operator=( const nsSharedBufferList& ); // NOT TO BE IMPLEMENTED
|
|
|
|
|
|
|
|
public:
|
|
|
|
void LinkBuffer( Buffer*, Buffer*, Buffer* );
|
|
|
|
Buffer* UnlinkBuffer( Buffer* );
|
|
|
|
void SplitBuffer( const Position& );
|
|
|
|
|
2000-12-13 00:58:14 +03:00
|
|
|
static
|
|
|
|
Buffer*
|
|
|
|
NewSingleAllocationBuffer( const PRUnichar* aData, PRUint32 aDataLength, PRUint32 aAdditionalCapacity = 1 )
|
|
|
|
{
|
|
|
|
typedef Buffer* Buffer_ptr;
|
2000-12-13 02:02:01 +03:00
|
|
|
return NS_AllocateContiguousHandleWithData(Buffer_ptr(0), NS_READABLE_CAST(PRUnichar, nsLiteralString(aData, aDataLength)), aAdditionalCapacity);
|
2000-12-13 00:58:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
static
|
|
|
|
Buffer*
|
|
|
|
NewWrappingBuffer( PRUnichar* aDataStart, PRUnichar* aDataEnd, PRUnichar* aStorageEnd )
|
|
|
|
{
|
|
|
|
return new Buffer(aDataStart, aDataEnd, aDataStart, aStorageEnd);
|
|
|
|
}
|
2000-09-09 08:42:19 +04:00
|
|
|
|
|
|
|
void DiscardSuffix( PRUint32 );
|
|
|
|
// need other discards: prefix, and by iterator or pointer or something
|
|
|
|
|
|
|
|
Buffer* GetFirstBuffer() { return mFirstBuffer; }
|
|
|
|
const Buffer* GetFirstBuffer() const { return mFirstBuffer; }
|
|
|
|
|
|
|
|
Buffer* GetLastBuffer() { return mLastBuffer; }
|
|
|
|
const Buffer* GetLastBuffer() const { return mLastBuffer; }
|
|
|
|
|
|
|
|
ptrdiff_t GetDataLength() const { return mTotalDataLength; }
|
2000-09-09 03:54:23 +04:00
|
|
|
|
|
|
|
protected:
|
|
|
|
void DestroyBuffers();
|
|
|
|
|
|
|
|
protected:
|
|
|
|
Buffer* mFirstBuffer;
|
|
|
|
Buffer* mLastBuffer;
|
2000-09-09 08:42:19 +04:00
|
|
|
ptrdiff_t mTotalDataLength;
|
2000-09-09 03:54:23 +04:00
|
|
|
};
|
2000-09-09 04:02:48 +04:00
|
|
|
|
|
|
|
#endif // !defined(nsSharedBufferList_h___)
|