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.

Implementation

Mercurial (5350524bb654)

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 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
/* -*- 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/. */

#ifndef GFX_SKIP_CHARS_H
#define GFX_SKIP_CHARS_H

#include "nsTArray.h"

/*
 * gfxSkipChars is a data structure representing a list of characters that
 * have been skipped. The initial string is called the "original string"
 * and after skipping some characters, the result is called the "skipped string".
 * gfxSkipChars provides efficient ways to translate between offsets in the
 * original string and the skipped string. It is used by textrun code to keep
 * track of offsets before and after text transformations such as whitespace
 * compression and control code deletion.
 */

/**
 * The gfxSkipChars is represented as a sorted array of skipped ranges.
 *
 * A freshly-created gfxSkipChars means "all chars kept".
 */
class gfxSkipChars
{
    friend struct SkippedRangeStartComparator;
    friend struct SkippedRangeOffsetComparator;

private:
    class SkippedRange
    {
    public:
        SkippedRange(uint32_t aOffset, uint32_t aLength, uint32_t aDelta)
            : mOffset(aOffset), mLength(aLength), mDelta(aDelta)
        { }

        uint32_t Start() const
        {
            return mOffset;
        }

        uint32_t End() const
        {
            return mOffset + mLength;
        }

        uint32_t Length() const
        {
            return mLength;
        }

        uint32_t SkippedOffset() const
        {
            return mOffset - mDelta;
        }

        uint32_t Delta() const
        {
            return mDelta;
        }

        uint32_t NextDelta() const
        {
            return mDelta + mLength;
        }

        void Extend(uint32_t aChars)
        {
            mLength += aChars;
        }

    private:
        uint32_t mOffset; // original-string offset at which we want to skip
        uint32_t mLength; // number of skipped chars at this offset
        uint32_t mDelta;  // sum of lengths of preceding skipped-ranges
    };

public:
    gfxSkipChars()
        : mCharCount(0)
    { }

    void SkipChars(uint32_t aChars)
    {
        NS_ASSERTION(mCharCount + aChars > mCharCount,
                     "Character count overflow");
        uint32_t rangeCount = mRanges.Length();
        uint32_t delta = 0;
        if (rangeCount > 0) {
            SkippedRange& lastRange = mRanges[rangeCount - 1];
            if (lastRange.End() == mCharCount) {
                lastRange.Extend(aChars);
                mCharCount += aChars;
                return;
            }
            delta = lastRange.NextDelta();
        }
        mRanges.AppendElement(SkippedRange(mCharCount, aChars, delta));
        mCharCount += aChars;
    }

    void KeepChars(uint32_t aChars)
    {
        NS_ASSERTION(mCharCount + aChars > mCharCount,
                     "Character count overflow");
        mCharCount += aChars;
    }

    void SkipChar()
    {
        SkipChars(1);
    }

    void KeepChar()
    {
        KeepChars(1);
    }

    void TakeFrom(gfxSkipChars* aSkipChars)
    {
        mRanges.SwapElements(aSkipChars->mRanges);
        mCharCount = aSkipChars->mCharCount;
        aSkipChars->mCharCount = 0;
    }

    int32_t GetOriginalCharCount() const
    {
        return mCharCount;
    }

    const SkippedRange& LastRange() const
    {
        // this is only valid if mRanges is non-empty; no assertion here
        // because nsTArray will already assert if we abuse it
        return mRanges[mRanges.Length() - 1];
    }

    friend class gfxSkipCharsIterator;

private:
    nsTArray<SkippedRange> mRanges;
    uint32_t               mCharCount;
};

/**
 * A gfxSkipCharsIterator represents a position in the original string. It lets you
 * map efficiently to and from positions in the string after skipped characters
 * have been removed. You can also specify an offset that is added to all
 * incoming original string offsets and subtracted from all outgoing original
 * string offsets --- useful when the gfxSkipChars corresponds to something
 * offset from the original DOM coordinates, which it often does for gfxTextRuns.
 *
 * The current positions (in both the original and skipped strings) are
 * always constrained to be >= 0 and <= the string length. When the position
 * is equal to the string length, it is at the end of the string. The current
 * positions do not include any aOriginalStringToSkipCharsOffset.
 *
 * When the position in the original string corresponds to a skipped character,
 * the skipped-characters offset is the offset of the next unskipped character,
 * or the skipped-characters string length if there is no next unskipped character.
 */
class gfxSkipCharsIterator
{
public:
    /**
     * @param aOriginalStringToSkipCharsOffset add this to all incoming and
     * outgoing original string offsets
     */
    gfxSkipCharsIterator(const gfxSkipChars& aSkipChars,
                         int32_t aOriginalStringToSkipCharsOffset,
                         int32_t aOriginalStringOffset)
        : mSkipChars(&aSkipChars),
          mOriginalStringOffset(0),
          mSkippedStringOffset(0),
          mCurrentRangeIndex(-1),
          mOriginalStringToSkipCharsOffset(aOriginalStringToSkipCharsOffset)
    {
        SetOriginalOffset(aOriginalStringOffset);
    }

    explicit gfxSkipCharsIterator(const gfxSkipChars& aSkipChars,
                                  int32_t aOriginalStringToSkipCharsOffset = 0)
        : mSkipChars(&aSkipChars),
          mOriginalStringOffset(0),
          mSkippedStringOffset(0),
          mOriginalStringToSkipCharsOffset(aOriginalStringToSkipCharsOffset)
    {
        mCurrentRangeIndex =
            mSkipChars->mRanges.IsEmpty() ||
            mSkipChars->mRanges[0].Start() > 0 ? -1 : 0;
    }

    gfxSkipCharsIterator(const gfxSkipCharsIterator& aIterator)
        : mSkipChars(aIterator.mSkipChars),
          mOriginalStringOffset(aIterator.mOriginalStringOffset),
          mSkippedStringOffset(aIterator.mSkippedStringOffset),
          mCurrentRangeIndex(aIterator.mCurrentRangeIndex),
          mOriginalStringToSkipCharsOffset(aIterator.mOriginalStringToSkipCharsOffset)
    { }

    /**
     * The empty constructor creates an object that is useless until it is assigned.
     */
    gfxSkipCharsIterator()
        : mSkipChars(nullptr)
    { }

    /**
     * Return true if this iterator is properly initialized and usable.
     */
    bool IsInitialized()
    {
        return mSkipChars != nullptr;
    }

    /**
     * Set the iterator to aOriginalStringOffset in the original string.
     * This can efficiently move forward or backward from the current position.
     * aOriginalStringOffset is clamped to [0,originalStringLength].
     */
    void SetOriginalOffset(int32_t aOriginalStringOffset);

    /**
     * Set the iterator to aSkippedStringOffset in the skipped string.
     * This can efficiently move forward or backward from the current position.
     * aSkippedStringOffset is clamped to [0,skippedStringLength].
     */
    void SetSkippedOffset(uint32_t aSkippedStringOffset);

    uint32_t ConvertOriginalToSkipped(int32_t aOriginalStringOffset)
    {
        SetOriginalOffset(aOriginalStringOffset);
        return GetSkippedOffset();
    }

    int32_t ConvertSkippedToOriginal(uint32_t aSkippedStringOffset)
    {
        SetSkippedOffset(aSkippedStringOffset);
        return GetOriginalOffset();
    }

    /**
     * Test if the character at the current position in the original string
     * is skipped or not. If aRunLength is non-null, then *aRunLength is set
     * to a number of characters all of which are either skipped or not, starting
     * at this character. When the current position is at the end of the original
     * string, we return true and *aRunLength is set to zero.
     */
    bool IsOriginalCharSkipped(int32_t* aRunLength = nullptr) const;

    void AdvanceOriginal(int32_t aDelta)
    {
        SetOriginalOffset(GetOriginalOffset() + aDelta);
    }

    void AdvanceSkipped(int32_t aDelta)
    {
        SetSkippedOffset(GetSkippedOffset() + aDelta);
    }

    /**
     * @return the offset within the original string
     */
    int32_t GetOriginalOffset() const
    {
        return mOriginalStringOffset - mOriginalStringToSkipCharsOffset;
    }

    /**
     * @return the offset within the skipped string corresponding to the
     * current position in the original string. If the current position
     * in the original string is a character that is skipped, then we return
     * the position corresponding to the first non-skipped character in the
     * original string after the current position, or the length of the skipped
     * string if there is no such character.
     */
    uint32_t GetSkippedOffset() const
    {
        return mSkippedStringOffset;
    }

    int32_t GetOriginalEnd() const
    {
        return mSkipChars->GetOriginalCharCount() -
            mOriginalStringToSkipCharsOffset;
    }

private:
    const gfxSkipChars* mSkipChars;

    // Current position
    int32_t mOriginalStringOffset;
    uint32_t mSkippedStringOffset;

    // Index of the last skippedRange that precedes or contains the current
    // position in the original string.
    // If index == -1 then we are before the first skipped char.
    int32_t mCurrentRangeIndex;

    // This offset is added to map from "skipped+unskipped characters in
    // the original DOM string" character space to "skipped+unskipped
    // characters in the textrun's gfxSkipChars" character space
    int32_t mOriginalStringToSkipCharsOffset;
};

#endif /*GFX_SKIP_CHARS_H*/