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
|
|
|
|
|
|
|
#ifndef nsScannerString_h___
|
|
|
|
#define nsScannerString_h___
|
|
|
|
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsUnicharUtils.h" // for nsCaseInsensitiveStringComparator
|
2013-07-31 19:35:25 +04:00
|
|
|
#include "mozilla/LinkedList.h"
|
2013-01-15 16:22:03 +04:00
|
|
|
#include <algorithm>
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* NOTE: nsScannerString (and the other classes defined in this file) are
|
|
|
|
* not related to nsAString or any of the other xpcom/string classes.
|
|
|
|
*
|
|
|
|
* nsScannerString is based on the nsSlidingString implementation that used
|
|
|
|
* to live in xpcom/string. Now that nsAString is limited to representing
|
|
|
|
* only single fragment strings, nsSlidingString can no longer be used.
|
|
|
|
*
|
2017-10-26 17:50:26 +03:00
|
|
|
* An advantage to this design is that it does not employ any virtual
|
2004-02-19 05:44:03 +03:00
|
|
|
* functions.
|
|
|
|
*
|
|
|
|
* This file uses SCC-style indenting in deference to the nsSlidingString
|
|
|
|
* code from which this code is derived ;-)
|
|
|
|
*/
|
|
|
|
|
|
|
|
class nsScannerIterator;
|
|
|
|
class nsScannerSubstring;
|
|
|
|
class nsScannerString;
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
/**
|
2004-02-19 05:44:03 +03:00
|
|
|
* nsScannerBufferList
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2004-02-19 05:44:03 +03:00
|
|
|
* This class maintains a list of heap-allocated Buffer objects. The buffers
|
|
|
|
* are maintained in a circular linked list. Each buffer has a usage count
|
|
|
|
* that is decremented by the owning nsScannerSubstring.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2004-02-19 05:44:03 +03:00
|
|
|
* The buffer list itself is reference counted. This allows the buffer list
|
|
|
|
* to be shared by multiple nsScannerSubstring objects. The reference
|
|
|
|
* counting is not threadsafe, which is not at all a requirement.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2004-02-19 05:44:03 +03:00
|
|
|
* When a nsScannerSubstring releases its reference to a buffer list, it
|
|
|
|
* decrements the usage count of the first buffer in the buffer list that it
|
|
|
|
* was referencing. It informs the buffer list that it can discard buffers
|
|
|
|
* starting at that prefix. The buffer list will do so if the usage count of
|
|
|
|
* that buffer is 0 and if it is the first buffer in the list. It will
|
|
|
|
* continue to prune buffers starting from the front of the buffer list until
|
|
|
|
* it finds a buffer that has a usage count that is non-zero.
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
class nsScannerBufferList {
|
2018-11-30 13:46:48 +03:00
|
|
|
public:
|
2004-02-19 05:44:03 +03:00
|
|
|
/**
|
|
|
|
* Buffer objects are directly followed by a data segment. The start
|
|
|
|
* of the data segment is determined by increment the |this| pointer
|
|
|
|
* by 1 unit.
|
|
|
|
*/
|
2013-07-31 19:35:25 +04:00
|
|
|
class Buffer : public mozilla::LinkedListElement<Buffer> {
|
|
|
|
public:
|
2012-08-22 19:56:38 +04:00
|
|
|
void IncrementUsageCount() { ++mUsageCount; }
|
2004-02-19 05:44:03 +03:00
|
|
|
void DecrementUsageCount() { --mUsageCount; }
|
2017-10-26 17:50:26 +03:00
|
|
|
|
2014-09-02 02:04:52 +04:00
|
|
|
bool IsInUse() const { return mUsageCount != 0; }
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
const char16_t* DataStart() const { return (const char16_t*)(this + 1); }
|
|
|
|
char16_t* DataStart() { return (char16_t*)(this + 1); }
|
|
|
|
|
|
|
|
const char16_t* DataEnd() const { return mDataEnd; }
|
2014-01-04 19:02:17 +04:00
|
|
|
char16_t* DataEnd() { return mDataEnd; }
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
const Buffer* Next() const { return getNext(); }
|
|
|
|
Buffer* Next() { return getNext(); }
|
|
|
|
|
|
|
|
const Buffer* Prev() const { return getPrevious(); }
|
2012-08-22 19:56:38 +04:00
|
|
|
Buffer* Prev() { return getPrevious(); }
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2013-07-31 19:35:25 +04:00
|
|
|
uint32_t DataLength() const { return mDataEnd - DataStart(); }
|
2004-02-19 05:44:03 +03:00
|
|
|
void SetDataLength(uint32_t len) { mDataEnd = DataStart() + len; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
friend class nsScannerBufferList;
|
|
|
|
|
2017-10-26 17:50:26 +03:00
|
|
|
int32_t mUsageCount;
|
|
|
|
char16_t* mDataEnd;
|
2013-07-31 19:35:25 +04:00
|
|
|
};
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
/**
|
2013-07-31 19:35:25 +04:00
|
|
|
* Position objects serve as lightweight pointers into a buffer list.
|
|
|
|
* The mPosition member must be contained with mBuffer->DataStart()
|
|
|
|
* and mBuffer->DataEnd().
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2013-07-31 19:35:25 +04:00
|
|
|
class Position {
|
2018-11-30 13:46:48 +03:00
|
|
|
public:
|
2013-07-31 19:35:25 +04:00
|
|
|
Position() : mBuffer(nullptr), mPosition(nullptr) {}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2013-07-31 19:35:25 +04:00
|
|
|
Position(Buffer* buffer, char16_t* position)
|
|
|
|
: mBuffer(buffer), mPosition(position) {}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
inline explicit Position(const nsScannerIterator& aIter);
|
|
|
|
|
|
|
|
inline Position& operator=(const nsScannerIterator& aIter);
|
|
|
|
|
|
|
|
static size_t Distance(const Position& p1, const Position& p2);
|
|
|
|
|
2013-07-31 19:35:25 +04:00
|
|
|
Buffer* mBuffer;
|
|
|
|
char16_t* mPosition;
|
2004-02-19 05:44:03 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
static Buffer* AllocBufferFromString(const nsAString&);
|
2012-08-22 19:56:38 +04:00
|
|
|
static Buffer* AllocBuffer(uint32_t capacity); // capacity = number of chars
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
explicit nsScannerBufferList(Buffer* buf) : mRefCnt(0) {
|
|
|
|
mBuffers.insertBack(buf);
|
|
|
|
}
|
|
|
|
|
|
|
|
void AddRef() { ++mRefCnt; }
|
|
|
|
void Release() {
|
2012-08-22 19:56:38 +04:00
|
|
|
if (--mRefCnt == 0) delete this;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
void Append(Buffer* buf) { mBuffers.insertBack(buf); }
|
2014-09-02 02:04:52 +04:00
|
|
|
void InsertAfter(Buffer* buf, Buffer* prev) { prev->setNext(buf); }
|
|
|
|
void SplitBuffer(const Position&);
|
|
|
|
void DiscardUnreferencedPrefix(Buffer*);
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2013-07-31 19:35:25 +04:00
|
|
|
Buffer* Head() { return mBuffers.getFirst(); }
|
2004-02-19 05:44:03 +03:00
|
|
|
const Buffer* Head() const { return mBuffers.getFirst(); }
|
|
|
|
|
|
|
|
Buffer* Tail() { return mBuffers.getLast(); }
|
|
|
|
const Buffer* Tail() const { return mBuffers.getLast(); }
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
private:
|
2004-02-19 05:44:03 +03:00
|
|
|
friend class nsScannerSubstring;
|
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
~nsScannerBufferList() { ReleaseAll(); }
|
2004-02-19 05:44:03 +03:00
|
|
|
void ReleaseAll();
|
|
|
|
|
|
|
|
int32_t mRefCnt;
|
|
|
|
mozilla::LinkedList<Buffer> mBuffers;
|
|
|
|
};
|
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
/**
|
2017-06-20 12:19:52 +03:00
|
|
|
* nsScannerFragment represents a "slice" of a Buffer object.
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2017-06-20 12:19:52 +03:00
|
|
|
struct nsScannerFragment {
|
|
|
|
typedef nsScannerBufferList::Buffer Buffer;
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2011-09-29 10:19:26 +04:00
|
|
|
const Buffer* mBuffer;
|
|
|
|
const char16_t* mFragmentStart;
|
|
|
|
const char16_t* mFragmentEnd;
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2018-11-30 13:46:48 +03:00
|
|
|
/**
|
2004-02-19 05:44:03 +03:00
|
|
|
* nsScannerSubstring is the base class for nsScannerString. It provides
|
|
|
|
* access to iterators and methods to bind the substring to another
|
|
|
|
* substring or nsAString instance.
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2004-02-19 05:44:03 +03:00
|
|
|
* This class owns the buffer list.
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
class nsScannerSubstring {
|
|
|
|
public:
|
|
|
|
typedef nsScannerBufferList::Buffer Buffer;
|
|
|
|
typedef nsScannerBufferList::Position Position;
|
|
|
|
typedef uint32_t size_type;
|
|
|
|
|
2004-11-25 10:03:20 +03:00
|
|
|
nsScannerSubstring();
|
2004-02-19 05:44:03 +03:00
|
|
|
explicit nsScannerSubstring(const nsAString& s);
|
|
|
|
|
|
|
|
~nsScannerSubstring();
|
|
|
|
|
|
|
|
nsScannerIterator& BeginReading(nsScannerIterator& iter) const;
|
|
|
|
nsScannerIterator& EndReading(nsScannerIterator& iter) const;
|
2017-10-26 17:50:26 +03:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
size_type Length() const { return mLength; }
|
|
|
|
|
|
|
|
int32_t CountChar(char16_t) const;
|
|
|
|
|
|
|
|
void Rebind(const nsScannerSubstring&, const nsScannerIterator&,
|
|
|
|
const nsScannerIterator&);
|
|
|
|
void Rebind(const nsAString&);
|
|
|
|
|
|
|
|
const nsAString& AsString() const;
|
|
|
|
|
|
|
|
bool GetNextFragment(nsScannerFragment&) const;
|
2004-11-25 10:03:20 +03:00
|
|
|
bool GetPrevFragment(nsScannerFragment&) const;
|
|
|
|
|
|
|
|
static inline Buffer* AllocBufferFromString(const nsAString& aStr) {
|
|
|
|
return nsScannerBufferList::AllocBufferFromString(aStr);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2012-08-22 19:56:38 +04:00
|
|
|
static inline Buffer* AllocBuffer(size_type aCapacity) {
|
2004-02-19 05:44:03 +03:00
|
|
|
return nsScannerBufferList::AllocBuffer(aCapacity);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void acquire_ownership_of_buffer_list() const {
|
|
|
|
mBufferList->AddRef();
|
|
|
|
mStart.mBuffer->IncrementUsageCount();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
void release_ownership_of_buffer_list() {
|
|
|
|
if (mBufferList) {
|
|
|
|
mStart.mBuffer->DecrementUsageCount();
|
|
|
|
mBufferList->DiscardUnreferencedPrefix(mStart.mBuffer);
|
|
|
|
mBufferList->Release();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-09-02 02:04:52 +04:00
|
|
|
void init_range_from_buffer_list() {
|
|
|
|
mStart.mBuffer = mBufferList->Head();
|
2004-02-19 05:44:03 +03:00
|
|
|
mStart.mPosition = mStart.mBuffer->DataStart();
|
|
|
|
|
|
|
|
mEnd.mBuffer = mBufferList->Tail();
|
|
|
|
mEnd.mPosition = mEnd.mBuffer->DataEnd();
|
|
|
|
|
|
|
|
mLength = Position::Distance(mStart, mEnd);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
Position mStart;
|
|
|
|
Position mEnd;
|
2004-11-25 10:03:20 +03:00
|
|
|
nsScannerBufferList* mBufferList;
|
2004-02-19 05:44:03 +03:00
|
|
|
size_type mLength;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
// these fields are used to implement AsString
|
2004-11-25 10:03:20 +03:00
|
|
|
nsDependentSubstring mFlattenedRep;
|
2011-09-29 10:19:26 +04:00
|
|
|
bool mIsDirty;
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
friend class nsScannerSharedSubstring;
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2004-02-19 05:44:03 +03:00
|
|
|
* nsScannerString provides methods to grow and modify a buffer list.
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2004-02-19 05:44:03 +03:00
|
|
|
class nsScannerString : public nsScannerSubstring {
|
2018-11-30 13:46:48 +03:00
|
|
|
public:
|
2014-09-02 02:04:52 +04:00
|
|
|
explicit nsScannerString(Buffer*);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
// you are giving ownership to the string, it takes and keeps your
|
|
|
|
// buffer, deleting it when done.
|
|
|
|
// Use AllocBuffer or AllocBufferFromString to create a Buffer object
|
|
|
|
// for use with this function.
|
2017-10-26 17:50:26 +03:00
|
|
|
void AppendBuffer(Buffer*);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
void DiscardPrefix(const nsScannerIterator&);
|
|
|
|
// any other way you want to do this?
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
void UngetReadable(const nsAString& aReadable,
|
|
|
|
const nsScannerIterator& aCurrentPosition);
|
|
|
|
};
|
2018-11-30 13:46:48 +03:00
|
|
|
|
|
|
|
/**
|
2004-11-25 10:03:20 +03:00
|
|
|
* nsScannerSharedSubstring implements copy-on-write semantics for
|
|
|
|
* nsScannerSubstring. When you call .writable(), it will copy the data
|
|
|
|
* and return a mutable string object. This class also manages releasing
|
|
|
|
* the reference to the scanner buffer when it is no longer needed.
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
|
|
|
|
2004-11-25 10:03:20 +03:00
|
|
|
class nsScannerSharedSubstring {
|
2018-11-30 13:46:48 +03:00
|
|
|
public:
|
2012-07-30 18:20:58 +04:00
|
|
|
nsScannerSharedSubstring() : mBuffer(nullptr), mBufferList(nullptr) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-11-25 10:03:20 +03:00
|
|
|
~nsScannerSharedSubstring() {
|
2004-02-19 05:44:03 +03:00
|
|
|
if (mBufferList) ReleaseBuffer();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2004-11-25 10:03:20 +03:00
|
|
|
// Acquire a copy-on-write reference to the given substring.
|
2004-02-19 05:44:03 +03:00
|
|
|
void Rebind(const nsScannerIterator& aStart, const nsScannerIterator& aEnd);
|
|
|
|
|
2004-11-25 10:03:20 +03:00
|
|
|
// Get a mutable reference to this string
|
|
|
|
nsAString& writable() {
|
|
|
|
if (mBufferList) MakeMutable();
|
|
|
|
|
|
|
|
return mString;
|
2012-07-30 18:20:58 +04:00
|
|
|
}
|
2004-11-25 10:03:20 +03:00
|
|
|
|
|
|
|
// Get a const reference to this string
|
|
|
|
const nsAString& str() const { return mString; }
|
|
|
|
|
|
|
|
private:
|
|
|
|
typedef nsScannerBufferList::Buffer Buffer;
|
|
|
|
|
|
|
|
void ReleaseBuffer();
|
|
|
|
void MakeMutable();
|
|
|
|
|
|
|
|
nsDependentSubstring mString;
|
2013-07-31 19:35:25 +04:00
|
|
|
Buffer* mBuffer;
|
2004-11-25 10:03:20 +03:00
|
|
|
nsScannerBufferList* mBufferList;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* nsScannerIterator works just like nsReadingIterator<CharT> except that
|
|
|
|
* it knows how to iterate over a list of scanner buffers.
|
|
|
|
*/
|
2017-06-20 12:19:52 +03:00
|
|
|
class nsScannerIterator {
|
2018-11-30 13:46:48 +03:00
|
|
|
public:
|
2017-06-20 12:19:52 +03:00
|
|
|
typedef nsScannerIterator self_type;
|
|
|
|
typedef ptrdiff_t difference_type;
|
2014-01-04 19:02:17 +04:00
|
|
|
typedef char16_t value_type;
|
2017-06-20 12:19:52 +03:00
|
|
|
typedef const char16_t* pointer;
|
|
|
|
typedef const char16_t& reference;
|
|
|
|
typedef nsScannerSubstring::Buffer Buffer;
|
2004-11-25 10:03:20 +03:00
|
|
|
|
|
|
|
protected:
|
|
|
|
nsScannerFragment mFragment;
|
2014-01-04 19:02:17 +04:00
|
|
|
const char16_t* mPosition;
|
2004-11-25 10:03:20 +03:00
|
|
|
const nsScannerSubstring* mOwner;
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
friend class nsScannerSubstring;
|
2014-06-02 16:08:21 +04:00
|
|
|
friend class nsScannerSharedSubstring;
|
2004-11-25 10:03:20 +03:00
|
|
|
|
|
|
|
public:
|
|
|
|
// nsScannerIterator(); // auto-generate
|
|
|
|
// default constructor is OK nsScannerIterator( const nsScannerIterator& ); //
|
2004-02-19 05:44:03 +03:00
|
|
|
// auto-generated copy-constructor OK nsScannerIterator& operator=( const
|
2004-11-25 10:03:20 +03:00
|
|
|
// nsScannerIterator& ); // auto-generated copy-assignment operator OK
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
inline void normalize_forward();
|
|
|
|
inline void normalize_backward();
|
|
|
|
|
2013-07-31 19:35:25 +04:00
|
|
|
pointer get() const { return mPosition; }
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
char16_t operator*() const { return *get(); }
|
|
|
|
|
|
|
|
const nsScannerFragment& fragment() const { return mFragment; }
|
|
|
|
|
2012-08-22 19:56:38 +04:00
|
|
|
const Buffer* buffer() const { return mFragment.mBuffer; }
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
self_type& operator++() {
|
|
|
|
++mPosition;
|
|
|
|
normalize_forward();
|
|
|
|
return *this;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
self_type operator++(int) {
|
|
|
|
self_type result(*this);
|
|
|
|
++mPosition;
|
|
|
|
normalize_forward();
|
|
|
|
return result;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
self_type& operator--() {
|
|
|
|
normalize_backward();
|
|
|
|
--mPosition;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
self_type operator--(int) {
|
|
|
|
self_type result(*this);
|
|
|
|
normalize_backward();
|
|
|
|
--mPosition;
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
difference_type size_forward() const {
|
|
|
|
return mFragment.mFragmentEnd - mPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
difference_type size_backward() const {
|
|
|
|
return mPosition - mFragment.mFragmentStart;
|
|
|
|
}
|
|
|
|
|
|
|
|
self_type& advance(difference_type n) {
|
|
|
|
while (n > 0) {
|
2013-01-15 16:22:03 +04:00
|
|
|
difference_type one_hop = std::min(n, size_forward());
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
NS_ASSERTION(one_hop > 0,
|
|
|
|
"Infinite loop: can't advance a reading iterator beyond the "
|
|
|
|
"end of a string");
|
2014-01-04 19:02:17 +04:00
|
|
|
// perhaps I should |break| if |!one_hop|?
|
2004-02-19 05:44:03 +03:00
|
|
|
|
2014-01-04 19:02:17 +04:00
|
|
|
mPosition += one_hop;
|
|
|
|
normalize_forward();
|
2004-02-19 05:44:03 +03:00
|
|
|
n -= one_hop;
|
|
|
|
}
|
|
|
|
|
|
|
|
while (n < 0) {
|
|
|
|
normalize_backward();
|
2013-01-15 16:22:03 +04:00
|
|
|
difference_type one_hop = std::max(n, -size_backward());
|
2004-02-19 05:44:03 +03:00
|
|
|
|
|
|
|
NS_ASSERTION(one_hop < 0,
|
|
|
|
"Infinite loop: can't advance (backward) a reading iterator "
|
|
|
|
"beyond the end of a string");
|
|
|
|
// perhaps I should |break| if |!one_hop|?
|
|
|
|
|
|
|
|
mPosition += one_hop;
|
|
|
|
n -= one_hop;
|
|
|
|
}
|
|
|
|
|
2016-02-09 13:45:59 +03:00
|
|
|
return *this;
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
inline bool SameFragment(const nsScannerIterator& a,
|
|
|
|
const nsScannerIterator& b) {
|
|
|
|
return a.fragment().mFragmentStart == b.fragment().mFragmentStart;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-02-19 05:44:03 +03:00
|
|
|
* this class is needed in order to make use of the methods in nsAlgorithm.h
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
2011-12-13 18:17:59 +04:00
|
|
|
template <>
|
2004-02-19 05:44:03 +03:00
|
|
|
struct nsCharSourceTraits<nsScannerIterator> {
|
|
|
|
typedef nsScannerIterator::difference_type difference_type;
|
|
|
|
|
|
|
|
static uint32_t readable_distance(const nsScannerIterator& first,
|
|
|
|
const nsScannerIterator& last) {
|
2016-02-09 13:45:59 +03:00
|
|
|
return uint32_t(SameFragment(first, last) ? last.get() - first.get()
|
|
|
|
: first.size_forward());
|
2004-02-19 05:44:03 +03:00
|
|
|
}
|
|
|
|
|
2004-11-25 10:03:20 +03:00
|
|
|
static const nsScannerIterator::value_type* read(
|
2004-02-19 05:44:03 +03:00
|
|
|
const nsScannerIterator& iter) {
|
|
|
|
return iter.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void advance(nsScannerIterator& s, difference_type n) { s.advance(n); }
|
2018-11-30 13:46:48 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
2004-02-19 05:44:03 +03:00
|
|
|
* inline methods follow
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
inline void nsScannerIterator::normalize_forward() {
|
|
|
|
while (mPosition == mFragment.mFragmentEnd &&
|
|
|
|
mOwner->GetNextFragment(mFragment))
|
|
|
|
mPosition = mFragment.mFragmentStart;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
inline void nsScannerIterator::normalize_backward() {
|
|
|
|
while (mPosition == mFragment.mFragmentStart &&
|
|
|
|
mOwner->GetPrevFragment(mFragment))
|
|
|
|
mPosition = mFragment.mFragmentEnd;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
inline bool operator==(const nsScannerIterator& lhs,
|
|
|
|
const nsScannerIterator& rhs) {
|
|
|
|
return lhs.get() == rhs.get();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
inline bool operator!=(const nsScannerIterator& lhs,
|
|
|
|
const nsScannerIterator& rhs) {
|
|
|
|
return lhs.get() != rhs.get();
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
inline nsScannerBufferList::Position::Position(const nsScannerIterator& aIter)
|
2007-07-08 11:08:04 +04:00
|
|
|
: mBuffer(const_cast<Buffer*>(aIter.buffer())),
|
2014-01-04 19:02:17 +04:00
|
|
|
mPosition(const_cast<char16_t*>(aIter.get())) {}
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
inline nsScannerBufferList::Position& nsScannerBufferList::Position::operator=(
|
|
|
|
const nsScannerIterator& aIter) {
|
2007-07-08 11:08:04 +04:00
|
|
|
mBuffer = const_cast<Buffer*>(aIter.buffer());
|
2014-01-04 19:02:17 +04:00
|
|
|
mPosition = const_cast<char16_t*>(aIter.get());
|
2004-02-19 05:44:03 +03:00
|
|
|
return *this;
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2004-11-25 10:03:20 +03:00
|
|
|
* scanner string utils
|
2018-11-30 13:46:48 +03:00
|
|
|
*
|
2004-02-19 05:44:03 +03:00
|
|
|
* These methods mimic the API provided by nsReadableUtils in xpcom/string.
|
|
|
|
* Here we provide only the methods that the htmlparser module needs.
|
2018-11-30 13:46:48 +03:00
|
|
|
*/
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
inline size_t Distance(const nsScannerIterator& aStart,
|
|
|
|
const nsScannerIterator& aEnd) {
|
|
|
|
typedef nsScannerBufferList::Position Position;
|
|
|
|
return Position::Distance(Position(aStart), Position(aEnd));
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
bool CopyUnicodeTo(const nsScannerIterator& aSrcStart,
|
|
|
|
const nsScannerIterator& aSrcEnd, nsAString& aDest);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
inline bool CopyUnicodeTo(const nsScannerSubstring& aSrc, nsAString& aDest) {
|
|
|
|
nsScannerIterator begin, end;
|
2016-02-09 13:45:59 +03:00
|
|
|
return CopyUnicodeTo(aSrc.BeginReading(begin), aSrc.EndReading(end), aDest);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
bool AppendUnicodeTo(const nsScannerIterator& aSrcStart,
|
|
|
|
const nsScannerIterator& aSrcEnd, nsAString& aDest);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
inline bool AppendUnicodeTo(const nsScannerSubstring& aSrc, nsAString& aDest) {
|
|
|
|
nsScannerIterator begin, end;
|
2016-02-09 13:45:59 +03:00
|
|
|
return AppendUnicodeTo(aSrc.BeginReading(begin), aSrc.EndReading(end), aDest);
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
bool AppendUnicodeTo(const nsScannerIterator& aSrcStart,
|
|
|
|
const nsScannerIterator& aSrcEnd,
|
2004-11-25 10:03:20 +03:00
|
|
|
nsScannerSharedSubstring& aDest);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
bool FindCharInReadable(char16_t aChar, nsScannerIterator& aStart,
|
|
|
|
const nsScannerIterator& aEnd);
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
bool FindInReadable(const nsAString& aPattern, nsScannerIterator& aStart,
|
|
|
|
nsScannerIterator& aEnd,
|
|
|
|
const nsStringComparator& = nsDefaultStringComparator());
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
bool RFindInReadable(const nsAString& aPattern, nsScannerIterator& aStart,
|
|
|
|
nsScannerIterator& aEnd,
|
|
|
|
const nsStringComparator& = nsDefaultStringComparator());
|
2018-11-30 13:46:48 +03:00
|
|
|
|
2017-10-26 17:50:26 +03:00
|
|
|
inline bool CaseInsensitiveFindInReadable(const nsAString& aPattern,
|
2004-02-19 05:44:03 +03:00
|
|
|
nsScannerIterator& aStart,
|
|
|
|
nsScannerIterator& aEnd) {
|
|
|
|
return FindInReadable(aPattern, aStart, aEnd,
|
|
|
|
nsCaseInsensitiveStringComparator());
|
2018-11-30 13:46:48 +03:00
|
|
|
}
|
|
|
|
|
2004-02-19 05:44:03 +03:00
|
|
|
#endif // !defined(nsScannerString_h___)
|