Debian Codegraph @

js/src/vm/MatchPairs.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef vm_MatchPairs_h
8 #define vm_MatchPairs_h
9
10 #include "js/AllocPolicy.h"
11 #include "js/Vector.h"
12
13 /*
14 * RegExp match results are succinctly represented by pairs of integer
15 * indices delimiting (start, limit] segments of the input string.
16 *
17 * The pair count for a given RegExp match is the capturing parentheses
18 * count plus one for the "0 capturing paren" whole text match.
19 */
20
21 namespace js {
22
23 struct MatchPair final {
24 int32_t start;
25 int32_t limit;
26
27 static constexpr int32_t NoMatch = -1;
28
29 MatchPair() : start(NoMatch), limit(NoMatch) {}
30
31 MatchPair(int32_t start, int32_t limit) : start(start), limit(limit) {}
32
33 size_t length() const {
34 MOZ_ASSERT(!isUndefined());
35 return limit - start;
36 }
37 bool isUndefined() const { return start < 0; }
38
39 inline bool check() const {
40 MOZ_ASSERT(limit >= start);
41 MOZ_ASSERT_IF(start < 0, start == NoMatch);
42 MOZ_ASSERT_IF(limit < 0, limit == NoMatch);
43 return true;
44 }
45
46 // Note: return int32_t instead of size_t to prevent signed => unsigned
47 // conversions in caller functions.
48 static constexpr int32_t offsetOfStart() {
49 return int32_t(offsetof(MatchPair, start));
50 }
51 static constexpr int32_t offsetOfLimit() {
52 return int32_t(offsetof(MatchPair, limit));
53 }
54 };
55
56 // MachPairs is used as base class for VectorMatchPairs but can also be
57 // stack-allocated (without a Vector) in JIT code.
58 class MatchPairs {
59 protected:
60 /* Length of pairs_. */
61 uint32_t pairCount_;
62
63 /* Raw pointer into an allocated MatchPair buffer. */
64 MatchPair* pairs_;
65
66 protected:
67 /* Not used directly: use VectorMatchPairs. */
68 MatchPairs() : pairCount_(0), pairs_(nullptr) {}
69
70 protected:
71 /* Functions used by friend classes. */
72 friend class RegExpShared;
73 friend class RegExpStatics;
74
75 void forgetArray() {
76 pairs_ = nullptr;
77 pairCount_ = 0;
78 }
79
80 public:
81 void checkAgainst(size_t inputLength) {
82 #ifdef DEBUG
83 for (size_t i = 0; i < pairCount_; i++) {
84 const MatchPair& p = (*this)[i];
85 MOZ_ASSERT(p.check());
86 if (p.isUndefined()) {
87 continue;
88 }
89 MOZ_ASSERT(size_t(p.limit) <= inputLength);
90 }
91 #endif
92 }
93
94 /* Querying functions in the style of RegExpStatics. */
95 bool empty() const { return pairCount_ == 0; }
96 size_t pairCount() const {
97 MOZ_ASSERT(pairCount_ > 0);
98 return pairCount_;
99 }
100
101 // Note: return int32_t instead of size_t to prevent signed => unsigned
102 // conversions in caller functions.
103 static constexpr int32_t offsetOfPairs() {
104 return int32_t(offsetof(MatchPairs, pairs_));
105 }
106 static constexpr int32_t offsetOfPairCount() {
107 return int32_t(offsetof(MatchPairs, pairCount_));
108 }
109
110 int32_t* pairsRaw() { return reinterpret_cast<int32_t*>(pairs_); }
111
112 public:
113 size_t length() const { return pairCount_; }
114
115 const MatchPair& operator[](size_t i) const {
116 MOZ_ASSERT(i < pairCount_);
117 return pairs_[i];
118 }
119 MatchPair& operator[](size_t i) {
120 MOZ_ASSERT(i < pairCount_);
121 return pairs_[i];
122 }
123 };
124
125 class VectorMatchPairs : public MatchPairs {
126 Vector<MatchPair, 10, SystemAllocPolicy> vec_;
127
128 protected:
129 friend class RegExpShared;
130 friend class RegExpStatics;
131
132 /* MatchPair buffer allocator: set pairs_ and pairCount_. */
133 bool allocOrExpandArray(size_t pairCount);
134
135 bool initArrayFrom(VectorMatchPairs& copyFrom);
136
137 size_t sizeOfExcludingThis(mozilla::MallocSizeOf mallocSizeOf) const {
138 return vec_.sizeOfExcludingThis(mallocSizeOf);
139 }
140 };
141
142 } /* namespace js */
143
144 #endif /* vm_MatchPairs_h */