/src/mozilla-central/js/src/vm/MatchPairs.h
Line | Count | Source (jump to first uncovered line) |
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 "ds/LifoAlloc.h" |
11 | | #include "js/AllocPolicy.h" |
12 | | #include "js/Vector.h" |
13 | | |
14 | | /* |
15 | | * RegExp match results are succinctly represented by pairs of integer |
16 | | * indices delimiting (start, limit] segments of the input string. |
17 | | * |
18 | | * The pair count for a given RegExp match is the capturing parentheses |
19 | | * count plus one for the "0 capturing paren" whole text match. |
20 | | */ |
21 | | |
22 | | namespace js { |
23 | | |
24 | | struct MatchPair final |
25 | | { |
26 | | int32_t start; |
27 | | int32_t limit; |
28 | | |
29 | | static constexpr int32_t NoMatch = -1; |
30 | | |
31 | | MatchPair() |
32 | | : start(NoMatch), limit(NoMatch) |
33 | 0 | { } |
34 | | |
35 | | MatchPair(int32_t start, int32_t limit) |
36 | | : start(start), limit(limit) |
37 | 0 | { } |
38 | | |
39 | 0 | size_t length() const { MOZ_ASSERT(!isUndefined()); return limit - start; } |
40 | 0 | bool isUndefined() const { return start < 0; } |
41 | | |
42 | 0 | inline bool check() const { |
43 | 0 | MOZ_ASSERT(limit >= start); |
44 | 0 | MOZ_ASSERT_IF(start < 0, start == NoMatch); |
45 | 0 | MOZ_ASSERT_IF(limit < 0, limit == NoMatch); |
46 | 0 | return true; |
47 | 0 | } |
48 | | }; |
49 | | |
50 | | // MachPairs is used as base class for VectorMatchPairs but can also be |
51 | | // stack-allocated (without a Vector) in JIT code. |
52 | | class MatchPairs |
53 | | { |
54 | | protected: |
55 | | /* Length of pairs_. */ |
56 | | uint32_t pairCount_; |
57 | | |
58 | | /* Raw pointer into an allocated MatchPair buffer. */ |
59 | | MatchPair* pairs_; |
60 | | |
61 | | protected: |
62 | | /* Not used directly: use VectorMatchPairs. */ |
63 | | MatchPairs() |
64 | | : pairCount_(0), pairs_(nullptr) |
65 | 0 | { } |
66 | | |
67 | | protected: |
68 | | /* Functions used by friend classes. */ |
69 | | friend class RegExpShared; |
70 | | friend class RegExpStatics; |
71 | | |
72 | 0 | void forgetArray() { pairs_ = nullptr; } |
73 | | |
74 | 0 | void checkAgainst(size_t inputLength) { |
75 | | #ifdef DEBUG |
76 | | for (size_t i = 0; i < pairCount_; i++) { |
77 | | const MatchPair& p = (*this)[i]; |
78 | | MOZ_ASSERT(p.check()); |
79 | | if (p.isUndefined()) { |
80 | | continue; |
81 | | } |
82 | | MOZ_ASSERT(size_t(p.limit) <= inputLength); |
83 | | } |
84 | | #endif |
85 | | } |
86 | | |
87 | | public: |
88 | | /* Querying functions in the style of RegExpStatics. */ |
89 | 0 | bool empty() const { return pairCount_ == 0; } |
90 | 0 | size_t pairCount() const { MOZ_ASSERT(pairCount_ > 0); return pairCount_; } |
91 | | |
92 | 0 | static size_t offsetOfPairs() { return offsetof(MatchPairs, pairs_); } |
93 | 0 | static size_t offsetOfPairCount() { return offsetof(MatchPairs, pairCount_); } |
94 | | |
95 | 0 | int32_t* pairsRaw() { return reinterpret_cast<int32_t*>(pairs_); } |
96 | | |
97 | | public: |
98 | 0 | size_t length() const { return pairCount_; } |
99 | | |
100 | 0 | const MatchPair& operator[](size_t i) const { |
101 | 0 | MOZ_ASSERT(i < pairCount_); |
102 | 0 | return pairs_[i]; |
103 | 0 | } |
104 | 0 | MatchPair& operator[](size_t i) { |
105 | 0 | MOZ_ASSERT(i < pairCount_); |
106 | 0 | return pairs_[i]; |
107 | 0 | } |
108 | | }; |
109 | | |
110 | | class VectorMatchPairs : public MatchPairs |
111 | | { |
112 | | Vector<MatchPair, 10, SystemAllocPolicy> vec_; |
113 | | |
114 | | protected: |
115 | | friend class RegExpShared; |
116 | | friend class RegExpStatics; |
117 | | |
118 | | /* MatchPair buffer allocator: set pairs_ and pairCount_. */ |
119 | | bool allocOrExpandArray(size_t pairCount); |
120 | | |
121 | | bool initArrayFrom(VectorMatchPairs& copyFrom); |
122 | | }; |
123 | | |
124 | | } /* namespace js */ |
125 | | |
126 | | #endif /* vm_MatchPairs_h */ |