1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : * vim: set ts=8 sw=4 et tw=99 ft=cpp:
3 : *
4 : * ***** BEGIN LICENSE BLOCK *****
5 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 : *
7 : * The contents of this file are subject to the Mozilla Public License Version
8 : * 1.1 (the "License"); you may not use this file except in compliance with
9 : * the License. You may obtain a copy of the License at
10 : * http://www.mozilla.org/MPL/
11 : *
12 : * Software distributed under the License is distributed on an "AS IS" basis,
13 : * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
14 : * for the specific language governing rights and limitations under the
15 : * License.
16 : *
17 : * The Original Code is Mozilla SpiderMonkey JavaScript code.
18 : *
19 : * The Initial Developer of the Original Code is
20 : * the Mozilla Foundation.
21 : * Portions created by the Initial Developer are Copyright (C) 2011
22 : * the Initial Developer. All Rights Reserved.
23 : *
24 : * Contributor(s):
25 : * Chris Leary <cdleary@mozilla.com>
26 : *
27 : * Alternatively, the contents of this file may be used under the terms of
28 : * either the GNU General Public License Version 2 or later (the "GPL"), or
29 : * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
30 : * in which case the provisions of the GPL or the LGPL are applicable instead
31 : * of those above. If you wish to allow use of your version of this file only
32 : * under the terms of either the GPL or the LGPL, and not to allow others to
33 : * use your version of this file under the terms of the MPL, indicate your
34 : * decision by deleting the provisions above and replace them with the notice
35 : * and other provisions required by the GPL or the LGPL. If you do not delete
36 : * the provisions above, a recipient may use your version of this file under
37 : * the terms of any one of the MPL, the GPL or the LGPL.
38 : *
39 : * ***** END LICENSE BLOCK ***** */
40 :
41 : #ifndef MatchPairs_h__
42 : #define MatchPairs_h__
43 :
44 : /*
45 : * RegExp match results are succinctly represented by pairs of integer
46 : * indices delimiting (start, limit] segments of the input string.
47 : *
48 : * The pair count for a given RegExp match is the capturing parentheses
49 : * count plus one for the "0 capturing paren" whole text match.
50 : */
51 :
52 : namespace js {
53 :
54 : struct MatchPair
55 : {
56 : int start;
57 : int limit;
58 :
59 19735744 : MatchPair(int start, int limit) : start(start), limit(limit) {}
60 :
61 651195 : size_t length() const {
62 651195 : JS_ASSERT(!isUndefined());
63 651195 : return limit - start;
64 : }
65 :
66 10620632 : bool isUndefined() const {
67 10620632 : return start == -1;
68 : }
69 :
70 4418083 : void check() const {
71 4418083 : JS_ASSERT(limit >= start);
72 4418083 : JS_ASSERT_IF(!isUndefined(), start >= 0);
73 4418083 : }
74 : };
75 :
76 : class MatchPairs
77 : {
78 : size_t pairCount_;
79 : int buffer_[1];
80 :
81 3528974 : explicit MatchPairs(size_t pairCount) : pairCount_(pairCount) {
82 3528974 : initPairValues();
83 3528974 : }
84 :
85 3528974 : void initPairValues() {
86 14605612 : for (int *it = buffer_; it < buffer_ + 2 * pairCount_; ++it)
87 11076638 : *it = -1;
88 3528974 : }
89 :
90 3528974 : static size_t calculateSize(size_t backingPairCount) {
91 3528974 : return sizeof(MatchPairs) - sizeof(int) + sizeof(int) * backingPairCount;
92 : }
93 :
94 3528974 : int *buffer() { return buffer_; }
95 :
96 : friend class RegExpShared;
97 :
98 : public:
99 : /*
100 : * |backingPairCount| is necessary because PCRE uses extra space
101 : * after the actual results in the buffer.
102 : */
103 : static MatchPairs *create(LifoAlloc &alloc, size_t pairCount, size_t backingPairCount);
104 :
105 37620224 : size_t pairCount() const { return pairCount_; }
106 :
107 19735744 : MatchPair pair(size_t i) {
108 19735744 : JS_ASSERT(i < pairCount());
109 19735744 : return MatchPair(buffer_[2 * i], buffer_[2 * i + 1]);
110 : }
111 :
112 2566819 : void displace(size_t amount) {
113 2566819 : if (!amount)
114 2566801 : return;
115 :
116 90 : for (int *it = buffer_; it < buffer_ + 2 * pairCount_; ++it)
117 72 : *it = (*it < 0) ? -1 : *it + amount;
118 : }
119 :
120 : inline void checkAgainst(size_t length);
121 : };
122 :
123 : } /* namespace js */
124 :
125 : #endif
|