2000-09-09 00:02:48 +00: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 nsSlidingString_h___
|
|
|
|
#define nsSlidingString_h___
|
|
|
|
|
2001-04-02 19:40:52 +00:00
|
|
|
#ifndef nsAString_h___
|
|
|
|
#include "nsAString.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifndef nsSharedBufferList_h___
|
2000-09-08 23:54:23 +00:00
|
|
|
#include "nsSharedBufferList.h"
|
2001-04-02 19:40:52 +00:00
|
|
|
#endif
|
2000-09-08 23:54:23 +00:00
|
|
|
|
2002-03-02 21:22:41 +00:00
|
|
|
/*
|
|
|
|
* nsFreeProc : A pointer to the free function used by nsSlidingString
|
|
|
|
*/
|
|
|
|
typedef void nsFreeProc(void *ptr, void *clientdata);
|
|
|
|
|
2000-09-08 23:54:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Maintains the sequence from the prev-most referenced buffer to the last buffer.
|
|
|
|
* As prev-most buffers become un-referenced, they are unlinked from the list
|
|
|
|
* and destroyed.
|
|
|
|
*
|
|
|
|
* One or more |nsSlidingSubstring|s may reference into the list. Each |nsSlidingSubstring|
|
|
|
|
* holds a reference into the prev-most buffer intersecting the
|
|
|
|
* substring it describes. The destructor of a |nsSlidingSubstring| releases this
|
|
|
|
* reference, allowing the buffer list to destroy the contiguous prefix of
|
|
|
|
* unreferenced buffers.
|
|
|
|
*
|
|
|
|
* A single instance of |nsSlidingString| may reference this list.
|
|
|
|
* Through that interface, new data can be appended onto the next-most end
|
|
|
|
* of the list. |nsSlidingString| also the client to advance its starting point.
|
|
|
|
*
|
|
|
|
*/
|
2000-12-12 21:58:14 +00:00
|
|
|
class NS_COM nsSlidingSharedBufferList
|
2000-09-08 23:54:23 +00:00
|
|
|
: public nsSharedBufferList
|
|
|
|
{
|
|
|
|
public:
|
2002-03-02 21:22:41 +00:00
|
|
|
nsSlidingSharedBufferList( Buffer* aBuffer )
|
|
|
|
: nsSharedBufferList(aBuffer), mRefCount(0), mFreeProc(nsnull), mClientData(nsnull) { }
|
|
|
|
nsSlidingSharedBufferList( Buffer* aBuffer, nsFreeProc *aFreeproc, void *aClientData = NULL )
|
|
|
|
: nsSharedBufferList(aBuffer), mRefCount(0), mFreeProc(aFreeproc), mClientData(aClientData) { }
|
2000-09-08 23:54:23 +00:00
|
|
|
|
|
|
|
void AcquireReference() { ++mRefCount; }
|
|
|
|
void ReleaseReference() { if ( !--mRefCount ) delete this; }
|
|
|
|
|
|
|
|
void DiscardUnreferencedPrefix( Buffer* );
|
|
|
|
|
|
|
|
private:
|
|
|
|
PRUint32 mRefCount;
|
2002-03-02 21:22:41 +00:00
|
|
|
nsFreeProc *mFreeProc;
|
|
|
|
void *mClientData;
|
2000-09-08 23:54:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
2000-12-12 21:58:14 +00:00
|
|
|
class nsSlidingString;
|
2000-09-08 23:54:23 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* a substring over a buffer list, this
|
|
|
|
*/
|
2000-12-12 21:58:14 +00:00
|
|
|
class NS_COM nsSlidingSubstring
|
2001-04-02 19:40:52 +00:00
|
|
|
: virtual public nsAPromiseString
|
2000-09-08 23:54:23 +00:00
|
|
|
{
|
2000-12-12 21:58:14 +00:00
|
|
|
friend class nsSlidingString;
|
|
|
|
|
2000-09-08 23:54:23 +00:00
|
|
|
public:
|
2000-09-11 05:24:49 +00:00
|
|
|
typedef nsSlidingSharedBufferList::Buffer Buffer;
|
|
|
|
typedef nsSlidingSharedBufferList::Position Position;
|
2000-09-08 23:54:23 +00:00
|
|
|
|
2000-12-12 21:58:14 +00:00
|
|
|
nsSlidingSubstring()
|
|
|
|
: mStart(0,0),
|
|
|
|
mEnd(0,0),
|
|
|
|
mBufferList(0),
|
|
|
|
mLength(0)
|
|
|
|
{
|
|
|
|
// nothing else to do here
|
|
|
|
}
|
|
|
|
|
2000-09-11 05:24:49 +00:00
|
|
|
nsSlidingSubstring( const nsSlidingSubstring& ); // copy-constructor
|
2001-09-25 09:35:50 +00:00
|
|
|
nsSlidingSubstring( const nsSlidingSubstring& aString, const nsAString::const_iterator& aStart, const nsAString::const_iterator& aEnd );
|
2000-12-12 21:58:14 +00:00
|
|
|
nsSlidingSubstring( const nsSlidingString& );
|
2001-09-25 09:35:50 +00:00
|
|
|
nsSlidingSubstring( const nsSlidingString& aString, const nsAString::const_iterator& aStart, const nsAString::const_iterator& aEnd );
|
2001-04-02 19:40:52 +00:00
|
|
|
explicit nsSlidingSubstring( const nsAString& );
|
2000-12-12 21:58:14 +00:00
|
|
|
// copy the supplied string into a new buffer ... there will be no modifying instance over this buffer list
|
|
|
|
|
|
|
|
void Rebind( const nsSlidingSubstring& );
|
2001-09-25 09:35:50 +00:00
|
|
|
void Rebind( const nsSlidingSubstring&, const nsAString::const_iterator&, const nsAString::const_iterator& );
|
2000-12-12 21:58:14 +00:00
|
|
|
void Rebind( const nsSlidingString& );
|
2001-09-25 09:35:50 +00:00
|
|
|
void Rebind( const nsSlidingString&, const nsAString::const_iterator&, const nsAString::const_iterator& );
|
2001-04-02 19:40:52 +00:00
|
|
|
void Rebind( const nsAString& );
|
2000-12-12 21:58:14 +00:00
|
|
|
|
2000-09-11 05:24:49 +00:00
|
|
|
~nsSlidingSubstring();
|
2000-09-08 23:54:23 +00:00
|
|
|
|
|
|
|
virtual PRUint32 Length() const { return mLength; }
|
|
|
|
|
|
|
|
protected:
|
2000-12-12 21:58:14 +00:00
|
|
|
nsSlidingSubstring( nsSlidingSharedBufferList* aBufferList );
|
2000-09-09 06:35:16 +00:00
|
|
|
virtual const PRUnichar* GetReadableFragment( nsReadableFragment<PRUnichar>&, nsFragmentRequest, PRUint32 ) const;
|
2001-04-02 19:40:52 +00:00
|
|
|
virtual PRUnichar* GetWritableFragment( nsWritableFragment<PRUnichar>&, nsFragmentRequest, PRUint32 ) { return 0; }
|
2000-09-11 05:24:49 +00:00
|
|
|
|
|
|
|
private:
|
|
|
|
// can't assign into me, I'm a read-only reference
|
|
|
|
void operator=( const nsSlidingSubstring& ); // NOT TO BE IMPLEMENTED
|
|
|
|
|
2000-12-12 21:58:14 +00:00
|
|
|
void
|
|
|
|
init_range_from_buffer_list()
|
|
|
|
// used only from constructors
|
|
|
|
{
|
|
|
|
mStart.PointBefore(mBufferList->GetFirstBuffer());
|
|
|
|
mEnd.PointAfter(mBufferList->GetLastBuffer());
|
|
|
|
mLength = PRUint32(Position::Distance(mStart, mEnd));
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
acquire_ownership_of_buffer_list() const
|
|
|
|
// used only from constructors and |Rebind|, requires |mStart| already be initialized
|
|
|
|
{
|
|
|
|
mBufferList->AcquireReference();
|
|
|
|
mStart.mBuffer->AcquireNonOwningReference();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
release_ownership_of_buffer_list()
|
|
|
|
{
|
|
|
|
if ( mBufferList )
|
|
|
|
{
|
|
|
|
mStart.mBuffer->ReleaseNonOwningReference();
|
|
|
|
mBufferList->DiscardUnreferencedPrefix(mStart.mBuffer);
|
|
|
|
mBufferList->ReleaseReference();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2000-09-08 23:54:23 +00:00
|
|
|
protected:
|
2000-09-11 05:24:49 +00:00
|
|
|
Position mStart;
|
|
|
|
Position mEnd;
|
2000-12-12 21:58:14 +00:00
|
|
|
nsSlidingSharedBufferList* mBufferList;
|
2000-09-11 05:24:49 +00:00
|
|
|
PRUint32 mLength;
|
2000-09-08 23:54:23 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An |nsSlidingSharedBufferList| may be modified by zero or one instances of this class.
|
|
|
|
*
|
|
|
|
*/
|
2000-12-12 21:58:14 +00:00
|
|
|
class NS_COM nsSlidingString
|
2001-04-02 19:40:52 +00:00
|
|
|
: virtual public nsAPromiseString,
|
2000-12-12 21:58:14 +00:00
|
|
|
private nsSlidingSubstring
|
2000-09-08 23:54:23 +00:00
|
|
|
{
|
2000-12-12 21:58:14 +00:00
|
|
|
friend class nsSlidingSubstring;
|
|
|
|
|
2000-09-08 23:54:23 +00:00
|
|
|
public:
|
2000-09-09 11:31:04 +00:00
|
|
|
nsSlidingString( PRUnichar* aStorageStart, PRUnichar* aDataEnd, PRUnichar* aStorageEnd );
|
2000-09-11 05:24:49 +00:00
|
|
|
// ...created by consuming ownership of a buffer ... |aStorageStart| must point to something
|
2002-03-02 21:22:41 +00:00
|
|
|
// that it will be OK for the sliding string to call |nsMemory::Free| on
|
|
|
|
|
|
|
|
nsSlidingString( PRUnichar* aStorageStart, PRUnichar* aDataEnd, PRUnichar* aStorageEnd,
|
|
|
|
nsFreeProc *aFreeProc, void *aClientData = NULL);
|
|
|
|
// ...created by consuming ownership of a buffer ... |aStorageStart| must point to something
|
|
|
|
// that it will be OK for the sliding string to call (*aFreeProc)(ptr, aClientData) on
|
2000-09-08 23:54:23 +00:00
|
|
|
|
2000-12-12 21:58:14 +00:00
|
|
|
virtual PRUint32 Length() const { return mLength; }
|
|
|
|
|
2000-09-11 05:24:49 +00:00
|
|
|
// you are giving ownership to the string, it takes and keeps your buffer, deleting it (with |nsMemory::Free|) when done
|
2000-09-08 23:54:23 +00:00
|
|
|
void AppendBuffer( PRUnichar* aStorageStart, PRUnichar* aDataEnd, PRUnichar* aStorageEnd );
|
2000-09-11 05:24:49 +00:00
|
|
|
|
2000-09-08 23:54:23 +00:00
|
|
|
// void Append( ... ); do you want some |Append|s that copy the supplied data?
|
|
|
|
|
2001-09-25 09:35:50 +00:00
|
|
|
void DiscardPrefix( const nsAString::const_iterator& );
|
2000-09-08 23:54:23 +00:00
|
|
|
// any other way you want to do this?
|
2000-09-11 05:24:49 +00:00
|
|
|
|
2000-12-12 21:58:14 +00:00
|
|
|
protected:
|
|
|
|
virtual const PRUnichar* GetReadableFragment( nsReadableFragment<PRUnichar>&, nsFragmentRequest, PRUint32 ) const;
|
2001-04-02 19:40:52 +00:00
|
|
|
virtual PRUnichar* GetWritableFragment( nsWritableFragment<PRUnichar>&, nsFragmentRequest, PRUint32 ) { return 0; }
|
2000-12-12 21:58:14 +00:00
|
|
|
|
2001-09-25 09:35:50 +00:00
|
|
|
void InsertReadable( const nsAString&, const nsAString::const_iterator& ); // ...to implement |nsScannerString::UngetReadable|
|
2001-02-10 01:05:43 +00:00
|
|
|
|
2000-09-11 05:24:49 +00:00
|
|
|
private:
|
2000-12-12 21:58:14 +00:00
|
|
|
|
|
|
|
void
|
|
|
|
acquire_ownership_of_buffer_list() const
|
|
|
|
// used only from constructors and |Rebind|, requires |mStart| already be initialized
|
|
|
|
{
|
|
|
|
mBufferList->AcquireReference();
|
|
|
|
mStart.mBuffer->AcquireNonOwningReference();
|
|
|
|
}
|
|
|
|
|
2000-09-11 05:24:49 +00:00
|
|
|
nsSlidingString( const nsSlidingString& ); // NOT TO BE IMPLEMENTED
|
|
|
|
void operator=( const nsSlidingString& ); // NOT TO BE IMPLEMENTED
|
2000-09-08 23:54:23 +00:00
|
|
|
};
|
|
|
|
|
2000-09-09 00:02:48 +00:00
|
|
|
#endif // !defined(nsSlidingString_h___)
|