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