2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
2012-05-21 04:12:37 -07: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/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "gfxSkipChars.h"
|
2014-09-17 06:46:24 -07:00
|
|
|
#include "mozilla/BinarySearch.h"
|
|
|
|
|
|
|
|
struct SkippedRangeStartComparator
|
|
|
|
{
|
|
|
|
const uint32_t mOffset;
|
|
|
|
SkippedRangeStartComparator(const uint32_t aOffset) : mOffset(aOffset) {}
|
|
|
|
int operator()(const gfxSkipChars::SkippedRange& aRange) const {
|
|
|
|
return (mOffset < aRange.Start()) ? -1 : 1;
|
|
|
|
}
|
|
|
|
};
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2014-01-10 00:30:28 -08:00
|
|
|
void
|
2014-01-10 01:42:05 -08:00
|
|
|
gfxSkipCharsIterator::SetOriginalOffset(int32_t aOffset)
|
2014-01-10 00:30:28 -08:00
|
|
|
{
|
2014-01-10 01:42:05 -08:00
|
|
|
aOffset += mOriginalStringToSkipCharsOffset;
|
|
|
|
NS_ASSERTION(uint32_t(aOffset) <= mSkipChars->mCharCount,
|
|
|
|
"Invalid offset");
|
|
|
|
|
|
|
|
mOriginalStringOffset = aOffset;
|
|
|
|
|
2014-09-17 06:46:24 -07:00
|
|
|
const uint32_t rangeCount = mSkipChars->mRanges.Length();
|
2014-01-10 01:42:05 -08:00
|
|
|
if (rangeCount == 0) {
|
|
|
|
mSkippedStringOffset = aOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
2014-01-10 01:42:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// at start of string?
|
|
|
|
if (aOffset == 0) {
|
|
|
|
mSkippedStringOffset = 0;
|
|
|
|
mCurrentRangeIndex =
|
|
|
|
rangeCount && mSkipChars->mRanges[0].Start() == 0 ? 0 : -1;
|
2014-01-10 00:30:28 -08:00
|
|
|
return;
|
2014-01-10 01:42:05 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
// find the range that includes or precedes aOffset
|
2014-09-17 06:46:24 -07:00
|
|
|
const nsTArray<gfxSkipChars::SkippedRange>& ranges = mSkipChars->mRanges;
|
|
|
|
size_t idx;
|
|
|
|
mozilla::BinarySearchIf(ranges, 0, rangeCount,
|
|
|
|
SkippedRangeStartComparator(aOffset),
|
|
|
|
&idx);
|
2014-01-10 01:42:05 -08:00
|
|
|
|
2014-09-17 06:46:24 -07:00
|
|
|
if (idx == rangeCount) {
|
2014-01-10 01:42:05 -08:00
|
|
|
mCurrentRangeIndex = rangeCount - 1;
|
2014-09-17 06:46:24 -07:00
|
|
|
} else if (uint32_t(aOffset) < ranges[idx].Start()) {
|
|
|
|
mCurrentRangeIndex = idx - 1;
|
2014-01-10 01:42:05 -08:00
|
|
|
if (mCurrentRangeIndex == -1) {
|
|
|
|
mSkippedStringOffset = aOffset;
|
|
|
|
return;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-01-10 01:42:05 -08:00
|
|
|
} else {
|
2014-09-17 06:46:24 -07:00
|
|
|
mCurrentRangeIndex = idx;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-01-10 01:42:05 -08:00
|
|
|
|
|
|
|
const gfxSkipChars::SkippedRange& r = ranges[mCurrentRangeIndex];
|
|
|
|
if (uint32_t(aOffset) < r.End()) {
|
|
|
|
mSkippedStringOffset = r.SkippedOffset();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
mSkippedStringOffset = aOffset - r.NextDelta();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-09-17 06:46:24 -07:00
|
|
|
struct SkippedRangeOffsetComparator
|
|
|
|
{
|
|
|
|
const uint32_t mOffset;
|
|
|
|
SkippedRangeOffsetComparator(const uint32_t aOffset) : mOffset(aOffset) {}
|
|
|
|
int operator()(const gfxSkipChars::SkippedRange& aRange) const {
|
|
|
|
return (mOffset < aRange.SkippedOffset()) ? -1 : 1;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
void
|
2014-01-10 01:42:05 -08:00
|
|
|
gfxSkipCharsIterator::SetSkippedOffset(uint32_t aOffset)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-01-10 01:42:05 -08:00
|
|
|
NS_ASSERTION((mSkipChars->mRanges.IsEmpty() &&
|
|
|
|
aOffset <= mSkipChars->mCharCount) ||
|
|
|
|
(aOffset <= mSkipChars->LastRange().SkippedOffset() +
|
|
|
|
mSkipChars->mCharCount -
|
|
|
|
mSkipChars->LastRange().End()),
|
|
|
|
"Invalid skipped offset");
|
|
|
|
mSkippedStringOffset = aOffset;
|
2007-06-12 13:56:04 -07:00
|
|
|
|
2014-01-10 01:42:05 -08:00
|
|
|
uint32_t rangeCount = mSkipChars->mRanges.Length();
|
|
|
|
if (rangeCount == 0) {
|
|
|
|
mOriginalStringOffset = aOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-01-10 01:42:05 -08:00
|
|
|
|
2014-09-17 06:46:24 -07:00
|
|
|
const nsTArray<gfxSkipChars::SkippedRange>& ranges = mSkipChars->mRanges;
|
|
|
|
size_t idx;
|
|
|
|
mozilla::BinarySearchIf(ranges, 0, rangeCount,
|
|
|
|
SkippedRangeOffsetComparator(aOffset),
|
|
|
|
&idx);
|
2014-01-10 01:42:05 -08:00
|
|
|
|
2014-09-17 06:46:24 -07:00
|
|
|
if (idx == rangeCount) {
|
2014-01-10 01:42:05 -08:00
|
|
|
mCurrentRangeIndex = rangeCount - 1;
|
2014-09-17 06:46:24 -07:00
|
|
|
} else if (aOffset < ranges[idx].SkippedOffset()) {
|
|
|
|
mCurrentRangeIndex = idx - 1;
|
2014-01-10 01:42:05 -08:00
|
|
|
if (mCurrentRangeIndex == -1) {
|
|
|
|
mOriginalStringOffset = aOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
return;
|
|
|
|
}
|
2014-01-10 01:42:05 -08:00
|
|
|
} else {
|
2014-09-17 06:46:24 -07:00
|
|
|
mCurrentRangeIndex = idx;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-01-10 01:42:05 -08:00
|
|
|
|
|
|
|
const gfxSkipChars::SkippedRange& r = ranges[mCurrentRangeIndex];
|
|
|
|
mOriginalStringOffset = r.End() + aOffset - r.SkippedOffset();
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2011-09-28 23:19:26 -07:00
|
|
|
bool
|
2012-08-22 08:56:38 -07:00
|
|
|
gfxSkipCharsIterator::IsOriginalCharSkipped(int32_t* aRunLength) const
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
2014-01-10 01:42:05 -08:00
|
|
|
if (mCurrentRangeIndex == -1) {
|
|
|
|
// we're before the first skipped range (if any)
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aRunLength) {
|
2014-01-10 01:42:05 -08:00
|
|
|
uint32_t end = mSkipChars->mRanges.IsEmpty() ?
|
|
|
|
mSkipChars->mCharCount : mSkipChars->mRanges[0].Start();
|
|
|
|
*aRunLength = end - mOriginalStringOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2012-08-22 08:56:38 -07:00
|
|
|
return mSkipChars->mCharCount == uint32_t(mOriginalStringOffset);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-01-10 01:42:05 -08:00
|
|
|
|
|
|
|
const gfxSkipChars::SkippedRange& range =
|
|
|
|
mSkipChars->mRanges[mCurrentRangeIndex];
|
|
|
|
|
|
|
|
if (uint32_t(mOriginalStringOffset) < range.End()) {
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aRunLength) {
|
2014-01-10 01:42:05 -08:00
|
|
|
*aRunLength = range.End() - mOriginalStringOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2011-10-17 07:59:28 -07:00
|
|
|
return true;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
2014-01-10 01:42:05 -08:00
|
|
|
|
2007-03-22 10:30:00 -07:00
|
|
|
if (aRunLength) {
|
2014-01-10 01:42:05 -08:00
|
|
|
uint32_t end =
|
|
|
|
uint32_t(mCurrentRangeIndex) + 1 < mSkipChars->mRanges.Length() ?
|
|
|
|
mSkipChars->mRanges[mCurrentRangeIndex + 1].Start() :
|
|
|
|
mSkipChars->mCharCount;
|
|
|
|
*aRunLength = end - mOriginalStringOffset;
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|
|
|
|
|
2014-01-10 01:42:05 -08:00
|
|
|
return mSkipChars->mCharCount == uint32_t(mOriginalStringOffset);
|
2007-03-22 10:30:00 -07:00
|
|
|
}
|