source: webkit/trunk/JavaScriptCore/kjs/regexp.cpp@ 2740

Last change on this file since 2740 was 1791, checked in by darin, 23 years ago

JavaScriptCore:

Some string speedups. Makes sony.com cached 11% faster.

  • kjs/ustring.h: Made it possible for UChar objects to be uninitialized, which gives a speed boost. Inlined CString's +=, UString's destructor, +=, and +.
  • kjs/ustring.cpp: (UString::UString): Optimize const char * version, which showed up heavily in performance analysis. Added new two-UString version, which makes the + operator fast. (UString::ascii): Remove thread safety changes. Change static buffer to remember its size, and to always be at least 4096 bytes long; that way we never have to reallocate unless it's for a long string. Also make code to extract the characters significantly faster by getting rid of two pointer dereferences per character. (UString::is8Bit): Avoid one pointer dereference per character. (UString::toDouble): Use ascii() instead of cstring() to avoid copying the string.
  • kjs/collector.cpp: Remove unneeded APPLE_CHANGES.
  • kjs/regexp.cpp: Remove ifdefs around some APPLE_CHANGES that we want to keep, because they just fix warnings.
  • kjs/value.h: Remove obsolete APPLE_CHANGES comment.
  • JavaScriptCore.pbproj/project.pbxproj: Project Builder decided to move a line around in the file.

WebCore:

  • force-clean-timestamp: JavaScriptCore headers changed that require a full build here.
  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 4.3 KB
Line 
1// -*- c-basic-offset: 2 -*-
2/*
3 * This file is part of the KDE libraries
4 * Copyright (C) 1999-2001 Harri Porten ([email protected])
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 *
20 */
21
22#include "regexp.h"
23
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27
28using namespace KJS;
29
30RegExp::RegExp(const UString &p, int f)
31 : pattern(p), flgs(f)
32{
33#ifdef HAVE_PCREPOSIX
34 int pcreflags = 0;
35 const char *perrormsg;
36 int errorOffset;
37
38 if (flgs & IgnoreCase)
39 pcreflags |= PCRE_CASELESS;
40
41 if (flgs & Multiline)
42 pcreflags |= PCRE_MULTILINE;
43
44 pcregex = pcre_compile(p.ascii(), pcreflags,
45 &perrormsg, &errorOffset, NULL);
46#ifndef NDEBUG
47 if (!pcregex)
48 fprintf(stderr, "KJS: pcre_compile() failed with '%s'\n", perrormsg);
49#endif
50
51#ifdef PCRE_INFO_CAPTURECOUNT
52 // Get number of subpatterns that will be returned
53 int rc = pcre_fullinfo( pcregex, NULL, PCRE_INFO_CAPTURECOUNT, &nrSubPatterns);
54 if (rc != 0)
55#endif
56 nrSubPatterns = 0; // fallback. We always need the first pair of offsets.
57
58#else /* HAVE_PCREPOSIX */
59
60 nrSubPatterns = 0; // determined in match() with POSIX regex.
61 int regflags = 0;
62#ifdef REG_EXTENDED
63 regflags |= REG_EXTENDED;
64#endif
65#ifdef REG_ICASE
66 if ( f & IgnoreCase )
67 regflags |= REG_ICASE;
68#endif
69
70 //NOTE: Multiline is not feasible with POSIX regex.
71 //if ( f & Multiline )
72 // ;
73 // Note: the Global flag is already handled by RegExpProtoFunc::execute
74
75 regcomp(&preg, p.ascii(), regflags);
76 /* TODO check for errors */
77#endif
78
79}
80
81RegExp::~RegExp()
82{
83#ifdef HAVE_PCREPOSIX
84 if (pcregex)
85 pcre_free(pcregex);
86#else
87 /* TODO: is this really okay after an error ? */
88 regfree(&preg);
89#endif
90}
91
92UString RegExp::match(const UString &s, int i, int *pos, int **ovector)
93{
94 if (i < 0)
95 i = 0;
96 if (ovector)
97 *ovector = 0L;
98 int dummyPos;
99 if (!pos)
100 pos = &dummyPos;
101 *pos = -1;
102 if (i > s.size() || s.isNull())
103 return UString::null;
104
105#ifdef HAVE_PCREPOSIX
106 CString buffer(s.cstring());
107 int ovecsize = (nrSubPatterns+1)*3; // see pcre docu
108 if (ovector) *ovector = new int[ovecsize];
109
110 if (!pcregex || pcre_exec(pcregex, NULL, buffer.c_str(), buffer.size(), i,
111 0, ovector ? *ovector : 0L, ovecsize) == PCRE_ERROR_NOMATCH)
112 return UString::null;
113
114 if (!ovector)
115 return UString::null; // don't rely on the return value if you pass ovector==0
116#else
117 const uint maxMatch = 10;
118 regmatch_t rmatch[maxMatch];
119
120 char *str = strdup(s.ascii()); // TODO: why ???
121 if (regexec(&preg, str + i, maxMatch, rmatch, 0)) {
122 free(str);
123 return UString::null;
124 }
125 free(str);
126
127 if (!ovector) {
128 *pos = rmatch[0].rm_so + i;
129 return s.substr(rmatch[0].rm_so + i, rmatch[0].rm_eo - rmatch[0].rm_so);
130 }
131
132 // map rmatch array to ovector used in PCRE case
133 nrSubPatterns = 0;
134 for(uint j = 1; j < maxMatch && rmatch[j].rm_so >= 0 ; j++)
135 nrSubPatterns++;
136 int ovecsize = (nrSubPatterns+1)*3; // see above
137 *ovector = new int[ovecsize];
138 for (uint j = 0; j < nrSubPatterns + 1; j++) {
139 if (j>maxMatch)
140 break;
141 (*ovector)[2*j] = rmatch[j].rm_so + i;
142 (*ovector)[2*j+1] = rmatch[j].rm_eo + i;
143 }
144#endif
145
146 *pos = (*ovector)[0];
147 return s.substr((*ovector)[0], (*ovector)[1] - (*ovector)[0]);
148}
149
150#if 0 // unused
151bool RegExp::test(const UString &s, int)
152{
153#ifdef HAVE_PCREPOSIX
154 int ovector[300];
155 CString buffer(s.cstring());
156
157 if (s.isNull() ||
158 pcre_exec(pcregex, NULL, buffer.c_str(), buffer.size(), 0,
159 0, ovector, 300) == PCRE_ERROR_NOMATCH)
160 return false;
161 else
162 return true;
163
164#else
165
166 char *str = strdup(s.ascii());
167 int r = regexec(&preg, str, 0, 0, 0);
168 free(str);
169
170 return r == 0;
171#endif
172}
173#endif
Note: See TracBrowser for help on using the repository browser.