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
: FastAllocBase
{
35 // Global search cache / settings
36 RegExpConstructorPrivate()
37 : lastNumSubPatterns(0)
43 const Vector
<int, 32>& lastOvector() const { return ovector
[lastOvectorIndex
]; }
44 Vector
<int, 32>& lastOvector() { return ovector
[lastOvectorIndex
]; }
45 Vector
<int, 32>& tempOvector() { return ovector
[lastOvectorIndex
? 0 : 1]; }
46 void changeLastOvector() { lastOvectorIndex
= lastOvectorIndex
? 0 : 1; }
50 Vector
<int, 32> ovector
[2];
51 unsigned lastNumSubPatterns
: 30;
53 unsigned lastOvectorIndex
: 1;
56 class RegExpConstructor
: public InternalFunction
{
58 RegExpConstructor(ExecState
*, NonNullPassRefPtr
<Structure
>, RegExpPrototype
*);
60 static PassRefPtr
<Structure
> createStructure(JSValue prototype
)
62 return Structure::create(prototype
, TypeInfo(ObjectType
, StructureFlags
), AnonymousSlotCount
);
65 virtual void put(ExecState
*, const Identifier
& propertyName
, JSValue
, PutPropertySlot
&);
66 virtual bool getOwnPropertySlot(ExecState
*, const Identifier
& propertyName
, PropertySlot
&);
67 virtual bool getOwnPropertyDescriptor(ExecState
*, const Identifier
&, PropertyDescriptor
&);
69 static const ClassInfo info
;
71 void performMatch(RegExp
*, const UString
&, int startOffset
, int& position
, int& length
, int** ovector
= 0);
72 JSObject
* arrayOfMatches(ExecState
*) const;
74 void setInput(const UString
&);
75 const UString
& input() const;
77 void setMultiline(bool);
78 bool multiline() const;
80 JSValue
getBackref(ExecState
*, unsigned) const;
81 JSValue
getLastParen(ExecState
*) const;
82 JSValue
getLeftContext(ExecState
*) const;
83 JSValue
getRightContext(ExecState
*) const;
86 static const unsigned StructureFlags
= OverridesGetOwnPropertySlot
| ImplementsHasInstance
| InternalFunction::StructureFlags
;
89 virtual ConstructType
getConstructData(ConstructData
&);
90 virtual CallType
getCallData(CallData
&);
92 virtual const ClassInfo
* classInfo() const { return &info
; }
94 OwnPtr
<RegExpConstructorPrivate
> d
;
97 RegExpConstructor
* asRegExpConstructor(JSValue
);
99 JSObject
* constructRegExp(ExecState
*, const ArgList
&);
101 inline RegExpConstructor
* asRegExpConstructor(JSValue value
)
103 ASSERT(asObject(value
)->inherits(&RegExpConstructor::info
));
104 return static_cast<RegExpConstructor
*>(asObject(value
));
108 To facilitate result caching, exec(), test(), match(), search(), and replace() dipatch regular
109 expression matching through the performMatch function. We use cached results to calculate,
110 e.g., RegExp.lastMatch and RegExp.leftParen.
112 inline void RegExpConstructor::performMatch(RegExp
* r
, const UString
& s
, int startOffset
, int& position
, int& length
, int** ovector
)
114 position
= r
->match(s
, startOffset
, &d
->tempOvector());
117 *ovector
= d
->tempOvector().data();
119 if (position
!= -1) {
120 ASSERT(!d
->tempOvector().isEmpty());
122 length
= d
->tempOvector()[1] - d
->tempOvector()[0];
126 d
->changeLastOvector();
127 d
->lastNumSubPatterns
= r
->numSubpatterns();
133 #endif // RegExpConstructor_h