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