1 | /*
|
---|
2 | * Copyright (C) 1999-2001, 2004 Harri Porten ([email protected])
|
---|
3 | * Copyright (c) 2007, 2008 Apple Inc. All rights reserved.
|
---|
4 | *
|
---|
5 | * This library is free software; you can redistribute it and/or
|
---|
6 | * modify it under the terms of the GNU Lesser General Public
|
---|
7 | * License as published by the Free Software Foundation; either
|
---|
8 | * version 2 of the License, or (at your option) any later version.
|
---|
9 | *
|
---|
10 | * This library is distributed in the hope that it will be useful,
|
---|
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
13 | * Lesser General Public License for more details.
|
---|
14 | *
|
---|
15 | * You should have received a copy of the GNU Lesser General Public
|
---|
16 | * License along with this library; if not, write to the Free Software
|
---|
17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
18 | *
|
---|
19 | */
|
---|
20 |
|
---|
21 | #include "config.h"
|
---|
22 | #include "regexp.h"
|
---|
23 |
|
---|
24 | #include "CTI.h"
|
---|
25 | #include "lexer.h"
|
---|
26 | #include <pcre/pcre.h>
|
---|
27 | #include <stdio.h>
|
---|
28 | #include <stdlib.h>
|
---|
29 | #include <string.h>
|
---|
30 | #include <wtf/Assertions.h>
|
---|
31 | #include <wtf/OwnArrayPtr.h>
|
---|
32 |
|
---|
33 | namespace JSC {
|
---|
34 |
|
---|
35 |
|
---|
36 |
|
---|
37 | inline RegExp::RegExp(ExecState* exec, const UString& pattern)
|
---|
38 | : m_pattern(pattern)
|
---|
39 | , m_flagBits(0)
|
---|
40 | , m_regExp(0)
|
---|
41 | , m_constructionError(0)
|
---|
42 | , m_numSubpatterns(0)
|
---|
43 | {
|
---|
44 | #if ENABLE(WREC)
|
---|
45 | if (!(m_wrecFunction = (WRECFunction)CTI::compileRegExp(exec, pattern, &m_numSubpatterns, &m_constructionError)))
|
---|
46 | #else
|
---|
47 | UNUSED_PARAM(exec);
|
---|
48 | #endif
|
---|
49 | {
|
---|
50 | m_regExp = jsRegExpCompile(reinterpret_cast<const UChar*>(pattern.data()), pattern.size(),
|
---|
51 | JSRegExpDoNotIgnoreCase, JSRegExpSingleLine, &m_numSubpatterns, &m_constructionError);
|
---|
52 | }
|
---|
53 | }
|
---|
54 |
|
---|
55 | PassRefPtr<RegExp> RegExp::create(ExecState* exec, const UString& pattern)
|
---|
56 | {
|
---|
57 | return adoptRef(new RegExp(exec, pattern));
|
---|
58 | }
|
---|
59 |
|
---|
60 | inline RegExp::RegExp(ExecState* exec, const UString& pattern, const UString& flags)
|
---|
61 | : m_pattern(pattern)
|
---|
62 | , m_flags(flags)
|
---|
63 | , m_flagBits(0)
|
---|
64 | , m_regExp(0)
|
---|
65 | , m_constructionError(0)
|
---|
66 | , m_numSubpatterns(0)
|
---|
67 | {
|
---|
68 | // NOTE: The global flag is handled on a case-by-case basis by functions like
|
---|
69 | // String::match and RegExpObject::match.
|
---|
70 | if (flags.find('g') != -1)
|
---|
71 | m_flagBits |= Global;
|
---|
72 |
|
---|
73 | // FIXME: Eliminate duplication by adding a way ask a JSRegExp what its flags are?
|
---|
74 | JSRegExpIgnoreCaseOption ignoreCaseOption = JSRegExpDoNotIgnoreCase;
|
---|
75 | if (flags.find('i') != -1) {
|
---|
76 | m_flagBits |= IgnoreCase;
|
---|
77 | ignoreCaseOption = JSRegExpIgnoreCase;
|
---|
78 | }
|
---|
79 |
|
---|
80 | JSRegExpMultilineOption multilineOption = JSRegExpSingleLine;
|
---|
81 | if (flags.find('m') != -1) {
|
---|
82 | m_flagBits |= Multiline;
|
---|
83 | multilineOption = JSRegExpMultiline;
|
---|
84 | }
|
---|
85 |
|
---|
86 | #if ENABLE(WREC)
|
---|
87 | if (!(m_wrecFunction = (WRECFunction)CTI::compileRegExp(exec, pattern, &m_numSubpatterns, &m_constructionError, (m_flagBits & IgnoreCase), (m_flagBits & Multiline))))
|
---|
88 | #else
|
---|
89 | UNUSED_PARAM(exec);
|
---|
90 | #endif
|
---|
91 | {
|
---|
92 | m_regExp = jsRegExpCompile(reinterpret_cast<const UChar*>(pattern.data()), pattern.size(),
|
---|
93 | ignoreCaseOption, multilineOption, &m_numSubpatterns, &m_constructionError);
|
---|
94 | }
|
---|
95 | }
|
---|
96 |
|
---|
97 | PassRefPtr<RegExp> RegExp::create(ExecState* exec, const UString& pattern, const UString& flags)
|
---|
98 | {
|
---|
99 | return adoptRef(new RegExp(exec, pattern, flags));
|
---|
100 | }
|
---|
101 |
|
---|
102 | RegExp::~RegExp()
|
---|
103 | {
|
---|
104 | jsRegExpFree(m_regExp);
|
---|
105 | #if ENABLE(WREC)
|
---|
106 | if (m_wrecFunction)
|
---|
107 | fastFree(reinterpret_cast<void*>(m_wrecFunction));
|
---|
108 | #endif
|
---|
109 | }
|
---|
110 |
|
---|
111 | int RegExp::match(const UString& s, int i, OwnArrayPtr<int>* ovector)
|
---|
112 | {
|
---|
113 | if (i < 0)
|
---|
114 | i = 0;
|
---|
115 | if (ovector)
|
---|
116 | ovector->clear();
|
---|
117 |
|
---|
118 | if (i > s.size() || s.isNull())
|
---|
119 | return -1;
|
---|
120 |
|
---|
121 | #if ENABLE(WREC)
|
---|
122 | if (m_wrecFunction) {
|
---|
123 | int offsetVectorSize = (m_numSubpatterns + 1) * 2;
|
---|
124 | int* offsetVector = new int [offsetVectorSize];
|
---|
125 | for (int j = 0; j < offsetVectorSize; ++j)
|
---|
126 | offsetVector[j] = -1;
|
---|
127 |
|
---|
128 | OwnArrayPtr<int> nonReturnedOvector;
|
---|
129 | if (!ovector)
|
---|
130 | nonReturnedOvector.set(offsetVector);
|
---|
131 | else
|
---|
132 | ovector->set(offsetVector);
|
---|
133 |
|
---|
134 | int result = m_wrecFunction(s.data(), i, s.size(), offsetVector);
|
---|
135 |
|
---|
136 | if (result < 0) {
|
---|
137 | #ifndef NDEBUG
|
---|
138 | // TODO: define up a symbol, rather than magic -1
|
---|
139 | if (result != -1)
|
---|
140 | fprintf(stderr, "jsRegExpExecute failed with result %d\n", result);
|
---|
141 | #endif
|
---|
142 | if (ovector)
|
---|
143 | ovector->clear();
|
---|
144 | }
|
---|
145 | return result;
|
---|
146 | } else
|
---|
147 | #endif
|
---|
148 | if (m_regExp) {
|
---|
149 | // Set up the offset vector for the result.
|
---|
150 | // First 2/3 used for result, the last third used by PCRE.
|
---|
151 | int* offsetVector;
|
---|
152 | int offsetVectorSize;
|
---|
153 | int fixedSizeOffsetVector[3];
|
---|
154 | if (!ovector) {
|
---|
155 | offsetVectorSize = 3;
|
---|
156 | offsetVector = fixedSizeOffsetVector;
|
---|
157 | } else {
|
---|
158 | offsetVectorSize = (m_numSubpatterns + 1) * 3;
|
---|
159 | offsetVector = new int [offsetVectorSize];
|
---|
160 | ovector->set(offsetVector);
|
---|
161 | }
|
---|
162 |
|
---|
163 | int numMatches = jsRegExpExecute(m_regExp, reinterpret_cast<const UChar*>(s.data()), s.size(), i, offsetVector, offsetVectorSize);
|
---|
164 |
|
---|
165 | if (numMatches < 0) {
|
---|
166 | #ifndef NDEBUG
|
---|
167 | if (numMatches != JSRegExpErrorNoMatch)
|
---|
168 | fprintf(stderr, "jsRegExpExecute failed with result %d\n", numMatches);
|
---|
169 | #endif
|
---|
170 | if (ovector)
|
---|
171 | ovector->clear();
|
---|
172 | return -1;
|
---|
173 | }
|
---|
174 |
|
---|
175 | return offsetVector[0];
|
---|
176 | }
|
---|
177 |
|
---|
178 | return -1;
|
---|
179 | }
|
---|
180 |
|
---|
181 | } // namespace JSC
|
---|