js/src/vm/MatchPairs.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*-
michael@0 2 * vim: set ts=8 sts=4 et sw=4 tw=99:
michael@0 3 * This Source Code Form is subject to the terms of the Mozilla Public
michael@0 4 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 6
michael@0 7 #ifndef vm_MatchPairs_h
michael@0 8 #define vm_MatchPairs_h
michael@0 9
michael@0 10 #include "jsalloc.h"
michael@0 11
michael@0 12 #include "ds/LifoAlloc.h"
michael@0 13 #include "js/Vector.h"
michael@0 14
michael@0 15 /*
michael@0 16 * RegExp match results are succinctly represented by pairs of integer
michael@0 17 * indices delimiting (start, limit] segments of the input string.
michael@0 18 *
michael@0 19 * The pair count for a given RegExp match is the capturing parentheses
michael@0 20 * count plus one for the "0 capturing paren" whole text match.
michael@0 21 */
michael@0 22
michael@0 23 namespace js {
michael@0 24
michael@0 25 struct MatchPair
michael@0 26 {
michael@0 27 int start;
michael@0 28 int limit;
michael@0 29
michael@0 30 MatchPair()
michael@0 31 : start(-1), limit(-1)
michael@0 32 { }
michael@0 33
michael@0 34 MatchPair(int start, int limit)
michael@0 35 : start(start), limit(limit)
michael@0 36 { }
michael@0 37
michael@0 38 size_t length() const { JS_ASSERT(!isUndefined()); return limit - start; }
michael@0 39 bool isEmpty() const { return length() == 0; }
michael@0 40 bool isUndefined() const { return start < 0; }
michael@0 41
michael@0 42 void displace(size_t amount) {
michael@0 43 start += (start < 0) ? 0 : amount;
michael@0 44 limit += (limit < 0) ? 0 : amount;
michael@0 45 }
michael@0 46
michael@0 47 inline bool check() const {
michael@0 48 JS_ASSERT(limit >= start);
michael@0 49 JS_ASSERT_IF(start < 0, start == -1);
michael@0 50 JS_ASSERT_IF(limit < 0, limit == -1);
michael@0 51 return true;
michael@0 52 }
michael@0 53 };
michael@0 54
michael@0 55 /* Base class for RegExp execution output. */
michael@0 56 class MatchPairs
michael@0 57 {
michael@0 58 protected:
michael@0 59 size_t pairCount_; /* Length of pairs_. */
michael@0 60 MatchPair *pairs_; /* Raw pointer into an allocated MatchPair buffer. */
michael@0 61
michael@0 62 protected:
michael@0 63 /* Not used directly: use ScopedMatchPairs or VectorMatchPairs. */
michael@0 64 MatchPairs()
michael@0 65 : pairCount_(0), pairs_(nullptr)
michael@0 66 { }
michael@0 67
michael@0 68 protected:
michael@0 69 /* Functions used by friend classes. */
michael@0 70 friend class RegExpShared;
michael@0 71 friend class RegExpStatics;
michael@0 72
michael@0 73 /* MatchPair buffer allocator: set pairs_ and pairCount_. */
michael@0 74 virtual bool allocOrExpandArray(size_t pairCount) = 0;
michael@0 75
michael@0 76 bool initArray(size_t pairCount);
michael@0 77 bool initArrayFrom(MatchPairs &copyFrom);
michael@0 78 void forgetArray() { pairs_ = nullptr; }
michael@0 79
michael@0 80 void displace(size_t disp);
michael@0 81 void checkAgainst(size_t inputLength) {
michael@0 82 #ifdef DEBUG
michael@0 83 for (size_t i = 0; i < pairCount_; i++) {
michael@0 84 const MatchPair &p = pair(i);
michael@0 85 JS_ASSERT(p.check());
michael@0 86 if (p.isUndefined())
michael@0 87 continue;
michael@0 88 JS_ASSERT(size_t(p.limit) <= inputLength);
michael@0 89 }
michael@0 90 #endif
michael@0 91 }
michael@0 92
michael@0 93 public:
michael@0 94 /* Querying functions in the style of RegExpStatics. */
michael@0 95 bool empty() const { return pairCount_ == 0; }
michael@0 96 size_t pairCount() const { JS_ASSERT(pairCount_ > 0); return pairCount_; }
michael@0 97 size_t parenCount() const { return pairCount_ - 1; }
michael@0 98
michael@0 99 public:
michael@0 100 unsigned *rawBuf() const { return reinterpret_cast<unsigned *>(pairs_); }
michael@0 101 size_t length() const { return pairCount_; }
michael@0 102
michael@0 103 /* Pair accessors. */
michael@0 104 const MatchPair &pair(size_t i) const {
michael@0 105 JS_ASSERT(pairCount_ && i < pairCount_);
michael@0 106 JS_ASSERT(pairs_);
michael@0 107 return pairs_[i];
michael@0 108 }
michael@0 109
michael@0 110 const MatchPair &operator[](size_t i) const { return pair(i); }
michael@0 111 };
michael@0 112
michael@0 113 /* MatchPairs allocated into temporary storage, removed when out of scope. */
michael@0 114 class ScopedMatchPairs : public MatchPairs
michael@0 115 {
michael@0 116 LifoAllocScope lifoScope_;
michael@0 117
michael@0 118 public:
michael@0 119 /* Constructs an implicit LifoAllocScope. */
michael@0 120 ScopedMatchPairs(LifoAlloc *lifoAlloc)
michael@0 121 : lifoScope_(lifoAlloc)
michael@0 122 { }
michael@0 123
michael@0 124 const MatchPair &operator[](size_t i) const { return pair(i); }
michael@0 125
michael@0 126 protected:
michael@0 127 bool allocOrExpandArray(size_t pairCount);
michael@0 128 };
michael@0 129
michael@0 130 /*
michael@0 131 * MatchPairs allocated into permanent storage, for RegExpStatics.
michael@0 132 * The Vector of MatchPairs is reusable by Vector expansion.
michael@0 133 */
michael@0 134 class VectorMatchPairs : public MatchPairs
michael@0 135 {
michael@0 136 Vector<MatchPair, 10, SystemAllocPolicy> vec_;
michael@0 137
michael@0 138 public:
michael@0 139 VectorMatchPairs() {
michael@0 140 vec_.clear();
michael@0 141 }
michael@0 142
michael@0 143 const MatchPair &operator[](size_t i) const { return pair(i); }
michael@0 144
michael@0 145 protected:
michael@0 146 friend class RegExpStatics;
michael@0 147 bool allocOrExpandArray(size_t pairCount);
michael@0 148 };
michael@0 149
michael@0 150 /*
michael@0 151 * Passes either MatchPair or MatchPairs through ExecuteRegExp()
michael@0 152 * to avoid duplication of generic code.
michael@0 153 */
michael@0 154 struct MatchConduit
michael@0 155 {
michael@0 156 union {
michael@0 157 MatchPair *pair;
michael@0 158 MatchPairs *pairs;
michael@0 159 } u;
michael@0 160 bool isPair;
michael@0 161
michael@0 162 explicit MatchConduit(MatchPair *pair) {
michael@0 163 isPair = true;
michael@0 164 u.pair = pair;
michael@0 165 }
michael@0 166 explicit MatchConduit(MatchPairs *pairs) {
michael@0 167 isPair = false;
michael@0 168 u.pairs = pairs;
michael@0 169 }
michael@0 170 };
michael@0 171
michael@0 172 } /* namespace js */
michael@0 173
michael@0 174 #endif /* vm_MatchPairs_h */

mercurial