1 | // -*- c-basic-offset: 2 -*-
|
---|
2 | /*
|
---|
3 | * Copyright (C) 1999-2001, 2004 Harri Porten ([email protected])
|
---|
4 | * Copyright (c) 2007, 2008 Apple Inc. All rights reserved.
|
---|
5 | *
|
---|
6 | * This library is free software; you can redistribute it and/or
|
---|
7 | * modify it under the terms of the GNU Lesser General Public
|
---|
8 | * License as published by the Free Software Foundation; either
|
---|
9 | * version 2 of the License, or (at your option) any later version.
|
---|
10 | *
|
---|
11 | * This library is distributed in the hope that it will be useful,
|
---|
12 | * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
13 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
14 | * Lesser General Public License for more details.
|
---|
15 | *
|
---|
16 | * You should have received a copy of the GNU Lesser General Public
|
---|
17 | * License along with this library; if not, write to the Free Software
|
---|
18 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
|
---|
19 | *
|
---|
20 | */
|
---|
21 |
|
---|
22 | #include "config.h"
|
---|
23 | #include "regexp.h"
|
---|
24 |
|
---|
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 KJS {
|
---|
34 |
|
---|
35 | inline RegExp::RegExp(const UString& pattern)
|
---|
36 | : m_pattern(pattern)
|
---|
37 | , m_flagBits(0)
|
---|
38 | , m_constructionError(0)
|
---|
39 | , m_numSubpatterns(0)
|
---|
40 | {
|
---|
41 | m_regExp = jsRegExpCompile(reinterpret_cast<const ::UChar*>(pattern.data()), pattern.size(),
|
---|
42 | JSRegExpDoNotIgnoreCase, JSRegExpSingleLine, &m_numSubpatterns, &m_constructionError);
|
---|
43 | }
|
---|
44 |
|
---|
45 | PassRefPtr<RegExp> RegExp::create(const UString& pattern)
|
---|
46 | {
|
---|
47 | return adoptRef(new RegExp(pattern));
|
---|
48 | }
|
---|
49 |
|
---|
50 | inline RegExp::RegExp(const UString& pattern, const UString& flags)
|
---|
51 | : m_pattern(pattern)
|
---|
52 | , m_flags(flags)
|
---|
53 | , m_flagBits(0)
|
---|
54 | , m_constructionError(0)
|
---|
55 | , m_numSubpatterns(0)
|
---|
56 | {
|
---|
57 | // NOTE: The global flag is handled on a case-by-case basis by functions like
|
---|
58 | // String::match and RegExpImp::match.
|
---|
59 | if (flags.find('g') != -1)
|
---|
60 | m_flagBits |= Global;
|
---|
61 |
|
---|
62 | // FIXME: Eliminate duplication by adding a way ask a JSRegExp what its flags are?
|
---|
63 | JSRegExpIgnoreCaseOption ignoreCaseOption = JSRegExpDoNotIgnoreCase;
|
---|
64 | if (flags.find('i') != -1) {
|
---|
65 | m_flagBits |= IgnoreCase;
|
---|
66 | ignoreCaseOption = JSRegExpIgnoreCase;
|
---|
67 | }
|
---|
68 |
|
---|
69 | JSRegExpMultilineOption multilineOption = JSRegExpSingleLine;
|
---|
70 | if (flags.find('m') != -1) {
|
---|
71 | m_flagBits |= Multiline;
|
---|
72 | multilineOption = JSRegExpMultiline;
|
---|
73 | }
|
---|
74 |
|
---|
75 | m_regExp = jsRegExpCompile(reinterpret_cast<const ::UChar*>(pattern.data()), pattern.size(),
|
---|
76 | ignoreCaseOption, multilineOption, &m_numSubpatterns, &m_constructionError);
|
---|
77 | }
|
---|
78 |
|
---|
79 | PassRefPtr<RegExp> RegExp::create(const UString& pattern, const UString& flags)
|
---|
80 | {
|
---|
81 | return adoptRef(new RegExp(pattern, flags));
|
---|
82 | }
|
---|
83 |
|
---|
84 | RegExp::~RegExp()
|
---|
85 | {
|
---|
86 | jsRegExpFree(m_regExp);
|
---|
87 | }
|
---|
88 |
|
---|
89 | int RegExp::match(const UString& s, int i, OwnArrayPtr<int>* ovector)
|
---|
90 | {
|
---|
91 | if (i < 0)
|
---|
92 | i = 0;
|
---|
93 | if (ovector)
|
---|
94 | ovector->clear();
|
---|
95 |
|
---|
96 | if (i > s.size() || s.isNull())
|
---|
97 | return -1;
|
---|
98 |
|
---|
99 | if (!m_regExp)
|
---|
100 | return -1;
|
---|
101 |
|
---|
102 | // Set up the offset vector for the result.
|
---|
103 | // First 2/3 used for result, the last third used by PCRE.
|
---|
104 | int* offsetVector;
|
---|
105 | int offsetVectorSize;
|
---|
106 | int fixedSizeOffsetVector[3];
|
---|
107 | if (!ovector) {
|
---|
108 | offsetVectorSize = 3;
|
---|
109 | offsetVector = fixedSizeOffsetVector;
|
---|
110 | } else {
|
---|
111 | offsetVectorSize = (m_numSubpatterns + 1) * 3;
|
---|
112 | offsetVector = new int [offsetVectorSize];
|
---|
113 | ovector->set(offsetVector);
|
---|
114 | }
|
---|
115 |
|
---|
116 | int numMatches = jsRegExpExecute(m_regExp, reinterpret_cast<const ::UChar*>(s.data()), s.size(), i, offsetVector, offsetVectorSize);
|
---|
117 |
|
---|
118 | if (numMatches < 0) {
|
---|
119 | #ifndef NDEBUG
|
---|
120 | if (numMatches != JSRegExpErrorNoMatch)
|
---|
121 | fprintf(stderr, "jsRegExpExecute failed with result %d\n", numMatches);
|
---|
122 | #endif
|
---|
123 | if (ovector)
|
---|
124 | ovector->clear();
|
---|
125 | return -1;
|
---|
126 | }
|
---|
127 |
|
---|
128 | return offsetVector[0];
|
---|
129 | }
|
---|
130 |
|
---|
131 | } // namespace KJS
|
---|