1 // -*- c-basic-offset: 2 -*-
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2001, 2004 Harri Porten (porten@kde.org)
5 * Copyright (c) 2007, Apple Inc.
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2 of the License, or (at your option) any later version.
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
30 #include <wtf/Assertions.h>
34 RegExp::RegExp(const UString
& pattern
)
37 , m_constructionError(0)
40 m_regExp
= jsRegExpCompile(reinterpret_cast<const ::UChar
*>(pattern
.data()), pattern
.size(),
41 JSRegExpDoNotIgnoreCase
, JSRegExpSingleLine
, &m_numSubpatterns
, &m_constructionError
);
44 RegExp::RegExp(const UString
& pattern
, const UString
& flags
)
48 , m_constructionError(0)
51 // NOTE: The global flag is handled on a case-by-case basis by functions like
52 // String::match and RegExpImp::match.
53 if (flags
.find('g') != -1)
56 // FIXME: Eliminate duplication by adding a way ask a JSRegExp what its flags are?
57 JSRegExpIgnoreCaseOption ignoreCaseOption
= JSRegExpDoNotIgnoreCase
;
58 if (flags
.find('i') != -1) {
59 m_flagBits
|= IgnoreCase
;
60 ignoreCaseOption
= JSRegExpIgnoreCase
;
63 JSRegExpMultilineOption multilineOption
= JSRegExpSingleLine
;
64 if (flags
.find('m') != -1) {
65 m_flagBits
|= Multiline
;
66 multilineOption
= JSRegExpMultiline
;
69 m_regExp
= jsRegExpCompile(reinterpret_cast<const ::UChar
*>(pattern
.data()), pattern
.size(),
70 ignoreCaseOption
, multilineOption
, &m_numSubpatterns
, &m_constructionError
);
75 jsRegExpFree(m_regExp
);
78 int RegExp::match(const UString
& s
, int i
, OwnArrayPtr
<int>* ovector
)
85 if (i
> s
.size() || s
.isNull())
91 // Set up the offset vector for the result.
92 // First 2/3 used for result, the last third used by PCRE.
95 int fixedSizeOffsetVector
[3];
98 offsetVector
= fixedSizeOffsetVector
;
100 offsetVectorSize
= (m_numSubpatterns
+ 1) * 3;
101 offsetVector
= new int [offsetVectorSize
];
102 ovector
->set(offsetVector
);
105 int numMatches
= jsRegExpExecute(m_regExp
, reinterpret_cast<const ::UChar
*>(s
.data()), s
.size(), i
, offsetVector
, offsetVectorSize
);
107 if (numMatches
< 0) {
109 if (numMatches
!= JSRegExpErrorNoMatch
)
110 fprintf(stderr
, "jsRegExpExecute failed with result %d\n", numMatches
);
117 return offsetVector
[0];