2004-02-19 05:44:03 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim:set ts=2 sw=2 sts=2 et cindent: */
|
2012-05-21 15:12:37 +04:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include "nsScannerString.h"
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nsScannerBufferList
|
|
|
|
*/
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
#define MAX_CAPACITY ((UINT32_MAX / sizeof(char16_t)) - \
|
|
|
|
(sizeof(Buffer) + sizeof(char16_t)))
|
2011-09-30 22:27:22 +04:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
nsScannerBufferList::Buffer*
|
|
|
|
nsScannerBufferList::AllocBufferFromString( const nsAString& aString )
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = aString.Length();
|
2013-07-31 19:35:25 +04:00
|
|
|
Buffer* buf = AllocBuffer(len);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
if (buf)
|
|
|
|
{
|
|
|
|
nsAString::const_iterator source;
|
|
|
|
aString.BeginReading(source);
|
2014-01-04 19:02:17 +04:00
|
|
|
nsCharTraits<char16_t>::copy(buf->DataStart(), source.get(), len);
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsScannerBufferList::Buffer*
|
2012-08-22 19:56:38 +04:00
|
|
|
nsScannerBufferList::AllocBuffer( uint32_t capacity )
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
2011-09-30 22:27:22 +04:00
|
|
|
if (capacity > MAX_CAPACITY)
|
2012-07-30 18:20:58 +04:00
|
|
|
return nullptr;
|
2011-09-30 22:27:22 +04:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
void* ptr = malloc(sizeof(Buffer) + (capacity + 1) * sizeof(char16_t));
|
2013-07-31 19:35:25 +04:00
|
|
|
if (!ptr)
|
|
|
|
return nullptr;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2013-07-31 19:35:25 +04:00
|
|
|
Buffer* buf = new (ptr) Buffer();
|
2012-10-19 20:23:15 +04:00
|
|
|
|
2013-07-31 19:35:25 +04:00
|
|
|
buf->mUsageCount = 0;
|
|
|
|
buf->mDataEnd = buf->DataStart() + capacity;
|
|
|
|
|
|
|
|
// XXX null terminate. this shouldn't be required, but we do it because
|
|
|
|
// nsScanner erroneously thinks it can dereference DataEnd :-(
|
2014-01-04 19:02:17 +04:00
|
|
|
*buf->mDataEnd = char16_t(0);
|
2004-02-19 05:44:03 +03:00
|
|
|
return buf;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScannerBufferList::ReleaseAll()
|
|
|
|
{
|
2013-07-31 19:35:25 +04:00
|
|
|
while (!mBuffers.isEmpty())
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
2013-07-31 19:35:25 +04:00
|
|
|
Buffer* node = mBuffers.popFirst();
|
2004-02-19 05:44:03 +03:00
|
|
|
//printf(">>> freeing buffer @%p\n", node);
|
2013-07-31 19:35:25 +04:00
|
|
|
free(node);
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScannerBufferList::SplitBuffer( const Position& pos )
|
|
|
|
{
|
|
|
|
// splitting to the right keeps the work string and any extant token
|
|
|
|
// pointing to and holding a reference count on the same buffer.
|
|
|
|
|
|
|
|
Buffer* bufferToSplit = pos.mBuffer;
|
|
|
|
NS_ASSERTION(bufferToSplit, "null pointer");
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t splitOffset = pos.mPosition - bufferToSplit->DataStart();
|
2004-02-19 05:44:03 +03:00
|
|
|
NS_ASSERTION(pos.mPosition >= bufferToSplit->DataStart() &&
|
|
|
|
splitOffset <= bufferToSplit->DataLength(),
|
|
|
|
"split offset is outside buffer");
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t len = bufferToSplit->DataLength() - splitOffset;
|
2004-02-19 05:44:03 +03:00
|
|
|
Buffer* new_buffer = AllocBuffer(len);
|
|
|
|
if (new_buffer)
|
|
|
|
{
|
2014-01-04 19:02:17 +04:00
|
|
|
nsCharTraits<char16_t>::copy(new_buffer->DataStart(),
|
2004-02-19 05:44:03 +03:00
|
|
|
bufferToSplit->DataStart() + splitOffset,
|
|
|
|
len);
|
|
|
|
InsertAfter(new_buffer, bufferToSplit);
|
|
|
|
bufferToSplit->SetDataLength(splitOffset);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScannerBufferList::DiscardUnreferencedPrefix( Buffer* aBuf )
|
|
|
|
{
|
|
|
|
if (aBuf == Head())
|
|
|
|
{
|
2013-07-31 19:35:25 +04:00
|
|
|
while (!mBuffers.isEmpty() && !Head()->IsInUse())
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
|
|
|
Buffer* buffer = Head();
|
2013-07-31 19:35:25 +04:00
|
|
|
buffer->remove();
|
2004-02-19 05:44:03 +03:00
|
|
|
free(buffer);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
nsScannerBufferList::Position::Distance( const Position& aStart, const Position& aEnd )
|
|
|
|
{
|
|
|
|
size_t result = 0;
|
|
|
|
if (aStart.mBuffer == aEnd.mBuffer)
|
|
|
|
{
|
|
|
|
result = aEnd.mPosition - aStart.mPosition;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
result = aStart.mBuffer->DataEnd() - aStart.mPosition;
|
|
|
|
for (Buffer* b = aStart.mBuffer->Next(); b != aEnd.mBuffer; b = b->Next())
|
|
|
|
result += b->DataLength();
|
|
|
|
result += aEnd.mPosition - aEnd.mBuffer->DataStart();
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nsScannerSubstring
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsScannerSubstring::nsScannerSubstring()
|
2012-07-30 18:20:58 +04:00
|
|
|
: mStart(nullptr, nullptr)
|
|
|
|
, mEnd(nullptr, nullptr)
|
|
|
|
, mBufferList(nullptr)
|
2004-02-19 05:44:03 +03:00
|
|
|
, mLength(0)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mIsDirty(true)
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
nsScannerSubstring::nsScannerSubstring( const nsAString& s )
|
2012-07-30 18:20:58 +04:00
|
|
|
: mBufferList(nullptr)
|
2011-10-17 18:59:28 +04:00
|
|
|
, mIsDirty(true)
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
|
|
|
Rebind(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsScannerSubstring::~nsScannerSubstring()
|
|
|
|
{
|
|
|
|
release_ownership_of_buffer_list();
|
|
|
|
}
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t
|
2014-01-04 19:02:17 +04:00
|
|
|
nsScannerSubstring::CountChar( char16_t c ) const
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
|
|
|
/*
|
|
|
|
re-write this to use a counting sink
|
|
|
|
*/
|
|
|
|
|
|
|
|
size_type result = 0;
|
|
|
|
size_type lengthToExamine = Length();
|
|
|
|
|
|
|
|
nsScannerIterator iter;
|
|
|
|
for ( BeginReading(iter); ; )
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t lengthToExamineInThisFragment = iter.size_forward();
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* fromBegin = iter.get();
|
2004-02-19 05:44:03 +03:00
|
|
|
result += size_type(NS_COUNT(fromBegin, fromBegin+lengthToExamineInThisFragment, c));
|
|
|
|
if ( !(lengthToExamine -= lengthToExamineInThisFragment) )
|
|
|
|
return result;
|
|
|
|
iter.advance(lengthToExamineInThisFragment);
|
|
|
|
}
|
|
|
|
// never reached; quiets warnings
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScannerSubstring::Rebind( const nsScannerSubstring& aString,
|
|
|
|
const nsScannerIterator& aStart,
|
|
|
|
const nsScannerIterator& aEnd )
|
|
|
|
{
|
|
|
|
// allow for the case where &aString == this
|
|
|
|
|
|
|
|
aString.acquire_ownership_of_buffer_list();
|
|
|
|
release_ownership_of_buffer_list();
|
|
|
|
|
|
|
|
mStart = aStart;
|
|
|
|
mEnd = aEnd;
|
|
|
|
mBufferList = aString.mBufferList;
|
|
|
|
mLength = Distance(aStart, aEnd);
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDirty = true;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScannerSubstring::Rebind( const nsAString& aString )
|
|
|
|
{
|
|
|
|
release_ownership_of_buffer_list();
|
|
|
|
|
|
|
|
mBufferList = new nsScannerBufferList(AllocBufferFromString(aString));
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDirty = true;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
init_range_from_buffer_list();
|
|
|
|
acquire_ownership_of_buffer_list();
|
|
|
|
}
|
|
|
|
|
2004-11-11 06:46:51 +03:00
|
|
|
const nsSubstring&
|
2004-02-19 05:44:03 +03:00
|
|
|
nsScannerSubstring::AsString() const
|
|
|
|
{
|
|
|
|
if (mIsDirty)
|
|
|
|
{
|
2007-07-08 11:08:04 +04:00
|
|
|
nsScannerSubstring* mutable_this = const_cast<nsScannerSubstring*>(this);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2004-11-11 06:46:51 +03:00
|
|
|
if (mStart.mBuffer == mEnd.mBuffer) {
|
|
|
|
// We only have a single fragment to deal with, so just return it
|
2004-11-25 10:03:20 +03:00
|
|
|
// as a substring.
|
|
|
|
mutable_this->mFlattenedRep.Rebind(mStart.mPosition, mEnd.mPosition);
|
2004-11-11 06:46:51 +03:00
|
|
|
} else {
|
|
|
|
// Otherwise, we need to copy the data into a flattened buffer.
|
|
|
|
nsScannerIterator start, end;
|
|
|
|
CopyUnicodeTo(BeginReading(start), EndReading(end), mutable_this->mFlattenedRep);
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mutable_this->mIsDirty = false;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
2004-11-11 06:46:51 +03:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
return mFlattenedRep;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsScannerIterator&
|
|
|
|
nsScannerSubstring::BeginReading( nsScannerIterator& iter ) const
|
|
|
|
{
|
|
|
|
iter.mOwner = this;
|
|
|
|
|
|
|
|
iter.mFragment.mBuffer = mStart.mBuffer;
|
|
|
|
iter.mFragment.mFragmentStart = mStart.mPosition;
|
|
|
|
if (mStart.mBuffer == mEnd.mBuffer)
|
|
|
|
iter.mFragment.mFragmentEnd = mEnd.mPosition;
|
|
|
|
else
|
|
|
|
iter.mFragment.mFragmentEnd = mStart.mBuffer->DataEnd();
|
|
|
|
|
|
|
|
iter.mPosition = mStart.mPosition;
|
|
|
|
iter.normalize_forward();
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsScannerIterator&
|
|
|
|
nsScannerSubstring::EndReading( nsScannerIterator& iter ) const
|
|
|
|
{
|
|
|
|
iter.mOwner = this;
|
|
|
|
|
|
|
|
iter.mFragment.mBuffer = mEnd.mBuffer;
|
|
|
|
iter.mFragment.mFragmentEnd = mEnd.mPosition;
|
|
|
|
if (mStart.mBuffer == mEnd.mBuffer)
|
|
|
|
iter.mFragment.mFragmentStart = mStart.mPosition;
|
|
|
|
else
|
|
|
|
iter.mFragment.mFragmentStart = mEnd.mBuffer->DataStart();
|
|
|
|
|
|
|
|
iter.mPosition = mEnd.mPosition;
|
|
|
|
// must not |normalize_backward| as that would likely invalidate tests like |while ( first != last )|
|
|
|
|
return iter;
|
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2004-02-19 05:44:03 +03:00
|
|
|
nsScannerSubstring::GetNextFragment( nsScannerFragment& frag ) const
|
|
|
|
{
|
|
|
|
// check to see if we are at the end of the buffer list
|
|
|
|
if (frag.mBuffer == mEnd.mBuffer)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2013-07-31 19:35:25 +04:00
|
|
|
frag.mBuffer = frag.mBuffer->getNext();
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
if (frag.mBuffer == mStart.mBuffer)
|
|
|
|
frag.mFragmentStart = mStart.mPosition;
|
|
|
|
else
|
|
|
|
frag.mFragmentStart = frag.mBuffer->DataStart();
|
|
|
|
|
|
|
|
if (frag.mBuffer == mEnd.mBuffer)
|
|
|
|
frag.mFragmentEnd = mEnd.mPosition;
|
|
|
|
else
|
|
|
|
frag.mFragmentEnd = frag.mBuffer->DataEnd();
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2004-02-19 05:44:03 +03:00
|
|
|
nsScannerSubstring::GetPrevFragment( nsScannerFragment& frag ) const
|
|
|
|
{
|
|
|
|
// check to see if we are at the beginning of the buffer list
|
|
|
|
if (frag.mBuffer == mStart.mBuffer)
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2013-07-31 19:35:25 +04:00
|
|
|
frag.mBuffer = frag.mBuffer->getPrevious();
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
if (frag.mBuffer == mStart.mBuffer)
|
|
|
|
frag.mFragmentStart = mStart.mPosition;
|
|
|
|
else
|
|
|
|
frag.mFragmentStart = frag.mBuffer->DataStart();
|
|
|
|
|
|
|
|
if (frag.mBuffer == mEnd.mBuffer)
|
|
|
|
frag.mFragmentEnd = mEnd.mPosition;
|
|
|
|
else
|
|
|
|
frag.mFragmentEnd = frag.mBuffer->DataEnd();
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nsScannerString
|
|
|
|
*/
|
|
|
|
|
|
|
|
nsScannerString::nsScannerString( Buffer* aBuf )
|
|
|
|
{
|
|
|
|
mBufferList = new nsScannerBufferList(aBuf);
|
|
|
|
|
|
|
|
init_range_from_buffer_list();
|
|
|
|
acquire_ownership_of_buffer_list();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScannerString::AppendBuffer( Buffer* aBuf )
|
|
|
|
{
|
|
|
|
mBufferList->Append(aBuf);
|
|
|
|
mLength += aBuf->DataLength();
|
|
|
|
|
|
|
|
mEnd.mBuffer = aBuf;
|
|
|
|
mEnd.mPosition = aBuf->DataEnd();
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDirty = true;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScannerString::DiscardPrefix( const nsScannerIterator& aIter )
|
|
|
|
{
|
|
|
|
Position old_start(mStart);
|
|
|
|
mStart = aIter;
|
|
|
|
mLength -= Position::Distance(old_start, mStart);
|
|
|
|
|
|
|
|
mStart.mBuffer->IncrementUsageCount();
|
|
|
|
old_start.mBuffer->DecrementUsageCount();
|
|
|
|
|
|
|
|
mBufferList->DiscardUnreferencedPrefix(old_start.mBuffer);
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDirty = true;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScannerString::UngetReadable( const nsAString& aReadable, const nsScannerIterator& aInsertPoint )
|
|
|
|
/*
|
|
|
|
* Warning: this routine manipulates the shared buffer list in an unexpected way.
|
|
|
|
* The original design did not really allow for insertions, but this call promises
|
|
|
|
* that if called for a point after the end of all extant token strings, that no token string
|
|
|
|
* or the work string will be invalidated.
|
|
|
|
*
|
|
|
|
* This routine is protected because it is the responsibility of the derived class to keep those promises.
|
|
|
|
*/
|
|
|
|
{
|
|
|
|
Position insertPos(aInsertPoint);
|
|
|
|
|
|
|
|
mBufferList->SplitBuffer(insertPos);
|
|
|
|
// splitting to the right keeps the work string and any extant token pointing to and
|
|
|
|
// holding a reference count on the same buffer
|
|
|
|
|
|
|
|
Buffer* new_buffer = AllocBufferFromString(aReadable);
|
|
|
|
// make a new buffer with all the data to insert...
|
|
|
|
// BULLSHIT ALERT: we may have empty space to re-use in the split buffer, measure the cost
|
|
|
|
// of this and decide if we should do the work to fill it
|
|
|
|
|
|
|
|
Buffer* buffer_to_split = insertPos.mBuffer;
|
|
|
|
mBufferList->InsertAfter(new_buffer, buffer_to_split);
|
|
|
|
mLength += aReadable.Length();
|
|
|
|
|
|
|
|
mEnd.mBuffer = mBufferList->Tail();
|
|
|
|
mEnd.mPosition = mEnd.mBuffer->DataEnd();
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDirty = true;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2014-01-04 19:02:17 +04:00
|
|
|
nsScannerString::ReplaceCharacter(nsScannerIterator& aPosition, char16_t aChar)
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
|
|
|
// XXX Casting a const to non-const. Unless the base class
|
|
|
|
// provides support for writing iterators, this is the best
|
|
|
|
// that can be done.
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* pos = const_cast<char16_t*>(aPosition.get());
|
2004-02-19 05:44:03 +03:00
|
|
|
*pos = aChar;
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
mIsDirty = true;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-11-25 10:03:20 +03:00
|
|
|
/**
|
|
|
|
* nsScannerSharedSubstring
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScannerSharedSubstring::Rebind(const nsScannerIterator &aStart,
|
|
|
|
const nsScannerIterator &aEnd)
|
|
|
|
{
|
|
|
|
// If the start and end positions are inside the same buffer, we must
|
|
|
|
// acquire ownership of the buffer. If not, we can optimize by not holding
|
|
|
|
// onto it.
|
|
|
|
|
2007-07-08 11:08:04 +04:00
|
|
|
Buffer *buffer = const_cast<Buffer*>(aStart.buffer());
|
2011-09-29 10:19:26 +04:00
|
|
|
bool sameBuffer = buffer == aEnd.buffer();
|
2004-11-25 10:03:20 +03:00
|
|
|
|
|
|
|
nsScannerBufferList *bufferList;
|
|
|
|
|
|
|
|
if (sameBuffer) {
|
|
|
|
bufferList = aStart.mOwner->mBufferList;
|
|
|
|
bufferList->AddRef();
|
|
|
|
buffer->IncrementUsageCount();
|
|
|
|
}
|
|
|
|
|
|
|
|
if (mBufferList)
|
|
|
|
ReleaseBuffer();
|
|
|
|
|
|
|
|
if (sameBuffer) {
|
|
|
|
mBuffer = buffer;
|
|
|
|
mBufferList = bufferList;
|
|
|
|
mString.Rebind(aStart.mPosition, aEnd.mPosition);
|
|
|
|
} else {
|
2012-07-30 18:20:58 +04:00
|
|
|
mBuffer = nullptr;
|
|
|
|
mBufferList = nullptr;
|
2004-11-25 10:03:20 +03:00
|
|
|
CopyUnicodeTo(aStart, aEnd, mString);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScannerSharedSubstring::ReleaseBuffer()
|
|
|
|
{
|
|
|
|
NS_ASSERTION(mBufferList, "Should only be called with non-null mBufferList");
|
|
|
|
mBuffer->DecrementUsageCount();
|
|
|
|
mBufferList->DiscardUnreferencedPrefix(mBuffer);
|
|
|
|
mBufferList->Release();
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
nsScannerSharedSubstring::MakeMutable()
|
|
|
|
{
|
|
|
|
nsString temp(mString); // this will force a copy of the data
|
|
|
|
mString.Assign(temp); // mString will now share the just-allocated buffer
|
|
|
|
|
|
|
|
ReleaseBuffer();
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
mBuffer = nullptr;
|
|
|
|
mBufferList = nullptr;
|
2004-11-25 10:03:20 +03:00
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
/**
|
|
|
|
* utils -- based on code from nsReadableUtils.cpp
|
|
|
|
*/
|
|
|
|
|
2008-01-04 03:07:06 +03:00
|
|
|
// private helper function
|
|
|
|
static inline
|
|
|
|
nsAString::iterator&
|
|
|
|
copy_multifragment_string( nsScannerIterator& first, const nsScannerIterator& last, nsAString::iterator& result )
|
|
|
|
{
|
|
|
|
typedef nsCharSourceTraits<nsScannerIterator> source_traits;
|
|
|
|
typedef nsCharSinkTraits<nsAString::iterator> sink_traits;
|
|
|
|
|
|
|
|
while ( first != last )
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t distance = source_traits::readable_distance(first, last);
|
2008-01-04 03:07:06 +03:00
|
|
|
sink_traits::write(result, source_traits::read(first), distance);
|
|
|
|
NS_ASSERTION(distance > 0, "|copy_multifragment_string| will never terminate");
|
|
|
|
source_traits::advance(first, distance);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
void
|
|
|
|
CopyUnicodeTo( const nsScannerIterator& aSrcStart,
|
|
|
|
const nsScannerIterator& aSrcEnd,
|
|
|
|
nsAString& aDest )
|
|
|
|
{
|
|
|
|
nsAString::iterator writer;
|
2015-01-28 12:00:40 +03:00
|
|
|
if (!aDest.SetLength(Distance(aSrcStart, aSrcEnd), mozilla::fallible)) {
|
2006-06-22 23:07:30 +04:00
|
|
|
aDest.Truncate();
|
|
|
|
return; // out of memory
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
aDest.BeginWriting(writer);
|
|
|
|
nsScannerIterator fromBegin(aSrcStart);
|
|
|
|
|
2008-01-04 03:07:06 +03:00
|
|
|
copy_multifragment_string(fromBegin, aSrcEnd, writer);
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
2004-11-25 10:03:20 +03:00
|
|
|
void
|
|
|
|
AppendUnicodeTo( const nsScannerIterator& aSrcStart,
|
|
|
|
const nsScannerIterator& aSrcEnd,
|
|
|
|
nsScannerSharedSubstring& aDest )
|
|
|
|
{
|
|
|
|
// Check whether we can just create a dependent string.
|
|
|
|
if (aDest.str().IsEmpty()) {
|
|
|
|
// We can just make |aDest| point to the buffer.
|
|
|
|
// This will take care of copying if the buffer spans fragments.
|
|
|
|
aDest.Rebind(aSrcStart, aSrcEnd);
|
|
|
|
} else {
|
|
|
|
// The dest string is not empty, so it can't be a dependent substring.
|
|
|
|
AppendUnicodeTo(aSrcStart, aSrcEnd, aDest.writable());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
void
|
|
|
|
AppendUnicodeTo( const nsScannerIterator& aSrcStart,
|
|
|
|
const nsScannerIterator& aSrcEnd,
|
|
|
|
nsAString& aDest )
|
|
|
|
{
|
|
|
|
nsAString::iterator writer;
|
2012-08-22 19:56:38 +04:00
|
|
|
uint32_t oldLength = aDest.Length();
|
2015-01-28 12:00:40 +03:00
|
|
|
if (!aDest.SetLength(oldLength + Distance(aSrcStart, aSrcEnd), mozilla::fallible))
|
2006-06-22 23:07:30 +04:00
|
|
|
return; // out of memory
|
2004-02-19 05:44:03 +03:00
|
|
|
aDest.BeginWriting(writer).advance(oldLength);
|
|
|
|
nsScannerIterator fromBegin(aSrcStart);
|
|
|
|
|
2008-01-04 03:07:06 +03:00
|
|
|
copy_multifragment_string(fromBegin, aSrcEnd, writer);
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2014-01-04 19:02:17 +04:00
|
|
|
FindCharInReadable( char16_t aChar,
|
2004-02-19 05:44:03 +03:00
|
|
|
nsScannerIterator& aSearchStart,
|
|
|
|
const nsScannerIterator& aSearchEnd )
|
|
|
|
{
|
|
|
|
while ( aSearchStart != aSearchEnd )
|
|
|
|
{
|
2012-08-22 19:56:38 +04:00
|
|
|
int32_t fragmentLength;
|
2004-02-19 05:44:03 +03:00
|
|
|
if ( SameFragment(aSearchStart, aSearchEnd) )
|
|
|
|
fragmentLength = aSearchEnd.get() - aSearchStart.get();
|
|
|
|
else
|
|
|
|
fragmentLength = aSearchStart.size_forward();
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* charFoundAt = nsCharTraits<char16_t>::find(aSearchStart.get(), fragmentLength, aChar);
|
2004-02-19 05:44:03 +03:00
|
|
|
if ( charFoundAt ) {
|
|
|
|
aSearchStart.advance( charFoundAt - aSearchStart.get() );
|
2011-10-17 18:59:28 +04:00
|
|
|
return true;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
aSearchStart.advance(fragmentLength);
|
|
|
|
}
|
|
|
|
|
2011-10-17 18:59:28 +04:00
|
|
|
return false;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2004-02-19 05:44:03 +03:00
|
|
|
FindInReadable( const nsAString& aPattern,
|
|
|
|
nsScannerIterator& aSearchStart,
|
|
|
|
nsScannerIterator& aSearchEnd,
|
|
|
|
const nsStringComparator& compare )
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool found_it = false;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
// only bother searching at all if we're given a non-empty range to search
|
|
|
|
if ( aSearchStart != aSearchEnd )
|
|
|
|
{
|
|
|
|
nsAString::const_iterator aPatternStart, aPatternEnd;
|
|
|
|
aPattern.BeginReading(aPatternStart);
|
|
|
|
aPattern.EndReading(aPatternEnd);
|
|
|
|
|
|
|
|
// outer loop keeps searching till we find it or run out of string to search
|
|
|
|
while ( !found_it )
|
|
|
|
{
|
|
|
|
// fast inner loop (that's what it's called, not what it is) looks for a potential match
|
|
|
|
while ( aSearchStart != aSearchEnd &&
|
2010-09-01 05:03:40 +04:00
|
|
|
compare(aPatternStart.get(), aSearchStart.get(), 1, 1) )
|
2004-02-19 05:44:03 +03:00
|
|
|
++aSearchStart;
|
|
|
|
|
|
|
|
// if we broke out of the `fast' loop because we're out of string ... we're done: no match
|
|
|
|
if ( aSearchStart == aSearchEnd )
|
|
|
|
break;
|
|
|
|
|
|
|
|
// otherwise, we're at a potential match, let's see if we really hit one
|
|
|
|
nsAString::const_iterator testPattern(aPatternStart);
|
|
|
|
nsScannerIterator testSearch(aSearchStart);
|
|
|
|
|
|
|
|
// slow inner loop verifies the potential match (found by the `fast' loop) at the current position
|
|
|
|
for(;;)
|
|
|
|
{
|
|
|
|
// we already compared the first character in the outer loop,
|
|
|
|
// so we'll advance before the next comparison
|
|
|
|
++testPattern;
|
|
|
|
++testSearch;
|
|
|
|
|
|
|
|
// if we verified all the way to the end of the pattern, then we found it!
|
|
|
|
if ( testPattern == aPatternEnd )
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
found_it = true;
|
2004-02-19 05:44:03 +03:00
|
|
|
aSearchEnd = testSearch; // return the exact found range through the parameters
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we got to end of the string we're searching before we hit the end of the
|
|
|
|
// pattern, we'll never find what we're looking for
|
|
|
|
if ( testSearch == aSearchEnd )
|
|
|
|
{
|
|
|
|
aSearchStart = aSearchEnd;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// else if we mismatched ... it's time to advance to the next search position
|
|
|
|
// and get back into the `fast' loop
|
2010-09-01 05:03:40 +04:00
|
|
|
if ( compare(testPattern.get(), testSearch.get(), 1, 1) )
|
2004-02-19 05:44:03 +03:00
|
|
|
{
|
|
|
|
++aSearchStart;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return found_it;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This implementation is simple, but does too much work.
|
|
|
|
* It searches the entire string from left to right, and returns the last match found, if any.
|
|
|
|
* This implementation will be replaced when I get |reverse_iterator|s working.
|
|
|
|
*/
|
2011-09-29 10:19:26 +04:00
|
|
|
bool
|
2004-02-19 05:44:03 +03:00
|
|
|
RFindInReadable( const nsAString& aPattern,
|
|
|
|
nsScannerIterator& aSearchStart,
|
|
|
|
nsScannerIterator& aSearchEnd,
|
|
|
|
const nsStringComparator& aComparator )
|
|
|
|
{
|
2011-09-29 10:19:26 +04:00
|
|
|
bool found_it = false;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
nsScannerIterator savedSearchEnd(aSearchEnd);
|
|
|
|
nsScannerIterator searchStart(aSearchStart), searchEnd(aSearchEnd);
|
|
|
|
|
|
|
|
while ( searchStart != searchEnd )
|
|
|
|
{
|
|
|
|
if ( FindInReadable(aPattern, searchStart, searchEnd, aComparator) )
|
|
|
|
{
|
2011-10-17 18:59:28 +04:00
|
|
|
found_it = true;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
// this is the best match so far, so remember it
|
|
|
|
aSearchStart = searchStart;
|
|
|
|
aSearchEnd = searchEnd;
|
|
|
|
|
|
|
|
// ...and get ready to search some more
|
|
|
|
// (it's tempting to set |searchStart=searchEnd| ... but that misses overlapping patterns)
|
|
|
|
++searchStart;
|
|
|
|
searchEnd = savedSearchEnd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// if we never found it, return an empty range
|
|
|
|
if ( !found_it )
|
|
|
|
aSearchStart = aSearchEnd;
|
|
|
|
|
|
|
|
return found_it;
|
|
|
|
}
|