2 * Copyright (C) 1999-2000 Harri Porten (porten@kde.org)
3 * Copyright (C) 2003, 2007, 2008 Apple Inc. All Rights Reserved.
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.
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.
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
21 #ifndef RegExpConstructor_h
22 #define RegExpConstructor_h
24 #include "InternalFunction.h"
26 #include <wtf/OwnPtr.h>
31 class RegExpPrototype;
32 struct RegExpConstructorPrivate;
34 struct RegExpConstructorPrivate {
35 WTF_MAKE_FAST_ALLOCATED;
37 // Global search cache / settings
38 RegExpConstructorPrivate()
39 : lastNumSubPatterns(0)
45 const Vector<int, 32>& lastOvector() const { return ovector[lastOvectorIndex]; }
46 Vector<int, 32>& lastOvector() { return ovector[lastOvectorIndex]; }
47 Vector<int, 32>& tempOvector() { return ovector[lastOvectorIndex ? 0 : 1]; }
48 void changeLastOvector() { lastOvectorIndex = lastOvectorIndex ? 0 : 1; }
52 Vector<int, 32> ovector[2];
53 unsigned lastNumSubPatterns : 30;
55 unsigned lastOvectorIndex : 1;
58 class RegExpConstructor : public InternalFunction {
60 typedef InternalFunction Base;
62 static RegExpConstructor* create(ExecState* exec, JSGlobalObject* globalObject, Structure* structure, RegExpPrototype* regExpPrototype)
64 RegExpConstructor* constructor = new (allocateCell<RegExpConstructor>(*exec->heap())) RegExpConstructor(globalObject, structure);
65 constructor->finishCreation(exec, regExpPrototype);
69 static Structure* createStructure(JSGlobalData& globalData, JSGlobalObject* globalObject, JSValue prototype)
71 return Structure::create(globalData, globalObject, prototype, TypeInfo(ObjectType, StructureFlags), &s_info);
74 static void put(JSCell*, ExecState*, const Identifier& propertyName, JSValue, PutPropertySlot&);
76 static bool getOwnPropertySlot(JSCell*, ExecState*, const Identifier& propertyName, PropertySlot&);
77 static bool getOwnPropertyDescriptor(JSObject*, ExecState*, const Identifier&, PropertyDescriptor&);
79 static const ClassInfo s_info;
81 void performMatch(JSGlobalData&, RegExp*, const UString&, int startOffset, int& position, int& length, int** ovector = 0);
82 JSObject* arrayOfMatches(ExecState*) const;
84 void setInput(const UString&);
85 const UString& input() const;
87 void setMultiline(bool);
88 bool multiline() const;
90 JSValue getBackref(ExecState*, unsigned) const;
91 JSValue getLastParen(ExecState*) const;
92 JSValue getLeftContext(ExecState*) const;
93 JSValue getRightContext(ExecState*) const;
96 void finishCreation(ExecState*, RegExpPrototype*);
97 static const unsigned StructureFlags = OverridesGetOwnPropertySlot | ImplementsHasInstance | InternalFunction::StructureFlags;
100 RegExpConstructor(JSGlobalObject*, Structure*);
101 static ConstructType getConstructData(JSCell*, ConstructData&);
102 static CallType getCallData(JSCell*, CallData&);
104 OwnPtr<RegExpConstructorPrivate> d;
107 RegExpConstructor* asRegExpConstructor(JSValue);
109 JSObject* constructRegExp(ExecState*, JSGlobalObject*, const ArgList&, bool callAsConstructor = false);
111 inline RegExpConstructor* asRegExpConstructor(JSValue value)
113 ASSERT(asObject(value)->inherits(&RegExpConstructor::s_info));
114 return static_cast<RegExpConstructor*>(asObject(value));
118 To facilitate result caching, exec(), test(), match(), search(), and replace() dipatch regular
119 expression matching through the performMatch function. We use cached results to calculate,
120 e.g., RegExp.lastMatch and RegExp.leftParen.
122 ALWAYS_INLINE void RegExpConstructor::performMatch(JSGlobalData& globalData, RegExp* r, const UString& s, int startOffset, int& position, int& length, int** ovector)
124 position = r->match(globalData, s, startOffset, &d->tempOvector());
127 *ovector = d->tempOvector().data();
129 if (position != -1) {
130 ASSERT(!d->tempOvector().isEmpty());
132 length = d->tempOvector()[1] - d->tempOvector()[0];
136 d->changeLastOvector();
137 d->lastNumSubPatterns = r->numSubpatterns();
143 #endif // RegExpConstructor_h