DXR is a code search and navigation tool aimed at making sense of large projects. It supports full-text and regex searches as well as structural queries.

Header

Mercurial (777e60ca8853)

VCS Links

Line Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
/* -*- Mode: C++; tab-width: 20; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 * 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/. */

#include "gfxSkipChars.h"

void
gfxSkipCharsIterator::SetOriginalOffset(int32_t aOffset)
{
    aOffset += mOriginalStringToSkipCharsOffset;
    NS_ASSERTION(uint32_t(aOffset) <= mSkipChars->mCharCount,
                 "Invalid offset");

    mOriginalStringOffset = aOffset;

    uint32_t rangeCount = mSkipChars->mRanges.Length();
    if (rangeCount == 0) {
        mSkippedStringOffset = aOffset;
        return;
    }

    // at start of string?
    if (aOffset == 0) {
        mSkippedStringOffset = 0;
        mCurrentRangeIndex =
            rangeCount && mSkipChars->mRanges[0].Start() == 0 ? 0 : -1;
        return;
    }

    // find the range that includes or precedes aOffset
    uint32_t lo = 0, hi = rangeCount;
    const gfxSkipChars::SkippedRange* ranges = mSkipChars->mRanges.Elements();
    while (lo < hi) {
        uint32_t mid = (lo + hi) / 2;
        if (uint32_t(aOffset) < ranges[mid].Start()) {
            hi = mid;
        } else {
            lo = mid + 1;
        }
    }

    if (lo == rangeCount) {
        mCurrentRangeIndex = rangeCount - 1;
    } else if (uint32_t(aOffset) < ranges[lo].Start()) {
        mCurrentRangeIndex = lo - 1;
        if (mCurrentRangeIndex == -1) {
            mSkippedStringOffset = aOffset;
            return;
        }
    } else {
        mCurrentRangeIndex = lo;
    }

    const gfxSkipChars::SkippedRange& r = ranges[mCurrentRangeIndex];
    if (uint32_t(aOffset) < r.End()) {
        mSkippedStringOffset = r.SkippedOffset();
        return;
    }

    mSkippedStringOffset = aOffset - r.NextDelta();
}

void
gfxSkipCharsIterator::SetSkippedOffset(uint32_t aOffset)
{
    NS_ASSERTION((mSkipChars->mRanges.IsEmpty() &&
                  aOffset <= mSkipChars->mCharCount) ||
                 (aOffset <= mSkipChars->LastRange().SkippedOffset() +
                                 mSkipChars->mCharCount -
                                 mSkipChars->LastRange().End()),
                 "Invalid skipped offset");
    mSkippedStringOffset = aOffset;

    uint32_t rangeCount = mSkipChars->mRanges.Length();
    if (rangeCount == 0) {
        mOriginalStringOffset = aOffset;
        return;
    }

    uint32_t lo = 0, hi = rangeCount;
    const gfxSkipChars::SkippedRange* ranges = mSkipChars->mRanges.Elements();
    while (lo < hi) {
        uint32_t mid = (lo + hi) / 2;
        if (aOffset < ranges[mid].SkippedOffset()) {
            hi = mid;
        } else {
            lo = mid + 1;
        }
    }

    if (lo == rangeCount) {
        mCurrentRangeIndex = rangeCount - 1;
    } else if (aOffset < ranges[lo].SkippedOffset()) {
        mCurrentRangeIndex = lo - 1;
        if (mCurrentRangeIndex == -1) {
            mOriginalStringOffset = aOffset;
            return;
        }
    } else {
        mCurrentRangeIndex = lo;
    }

    const gfxSkipChars::SkippedRange& r = ranges[mCurrentRangeIndex];
    mOriginalStringOffset = r.End() + aOffset - r.SkippedOffset();
}

bool
gfxSkipCharsIterator::IsOriginalCharSkipped(int32_t* aRunLength) const
{
    if (mCurrentRangeIndex == -1) {
        // we're before the first skipped range (if any)
        if (aRunLength) {
            uint32_t end = mSkipChars->mRanges.IsEmpty() ?
                mSkipChars->mCharCount : mSkipChars->mRanges[0].Start();
            *aRunLength = end - mOriginalStringOffset;
        }
        return mSkipChars->mCharCount == uint32_t(mOriginalStringOffset);
    }

    const gfxSkipChars::SkippedRange& range =
        mSkipChars->mRanges[mCurrentRangeIndex];

    if (uint32_t(mOriginalStringOffset) < range.End()) {
        if (aRunLength) {
            *aRunLength = range.End() - mOriginalStringOffset;
        }
        return true;
    }

    if (aRunLength) {
        uint32_t end =
            uint32_t(mCurrentRangeIndex) + 1 < mSkipChars->mRanges.Length() ?
                mSkipChars->mRanges[mCurrentRangeIndex + 1].Start() :
                mSkipChars->mCharCount;
        *aRunLength = end - mOriginalStringOffset;
    }

    return mSkipChars->mCharCount == uint32_t(mOriginalStringOffset);
}