Changeset 798 in webkit for trunk/JavaScriptCore/kjs/regexp.cpp
- Timestamp:
- Mar 21, 2002, 4:31:57 PM (23 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/regexp.cpp
r6 r798 1 // -*- c-basic-offset: 2 -*- 1 2 /* 2 3 * This file is part of the KDE libraries … … 16 17 * License along with this library; if not, write to the Free Software 17 18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 19 * 20 * $Id$ 18 21 */ 19 22 20 23 #include <stdio.h> 24 #include <stdlib.h> 25 #include <string.h> 21 26 22 #include "object.h"23 27 #include "regexp.h" 24 28 … … 26 30 27 31 RegExp::RegExp(const UString &p, int f) 28 : pattern(p), flags(f)32 : pattern(p), flags(f) 29 33 { 30 #ifdef REG_EXTENDED 31 regcomp(&preg, p.ascii(), REG_EXTENDED); 32 #else 33 regcomp(&preg, p.ascii(), 0); 34 #endif 35 /* TODO use flags, check for errors */ 34 35 #ifdef HAVE_PCREPOSIX 36 int pcreflags = 0; 37 const char *perrormsg; 38 int errorOffset; 39 40 if (flags & IgnoreCase) 41 pcreflags |= PCRE_CASELESS; 42 43 if (flags & Multiline) 44 pcreflags |= PCRE_MULTILINE; 45 46 pcregex = pcre_compile(p.ascii(), pcreflags, 47 &perrormsg, &errorOffset, NULL); 48 49 #ifdef PCRE_INFO_CAPTURECOUNT 50 // Get number of subpatterns that will be returned 51 int rc = pcre_fullinfo( pcregex, NULL, PCRE_INFO_CAPTURECOUNT, &nrSubPatterns); 52 if (rc != 0) 53 #endif 54 nrSubPatterns = 0; // fallback. We always need the first pair of offsets. 55 56 #else /* HAVE_PCREPOSIX */ 57 58 nrSubPatterns = 0; // not implemented with POSIX regex. 59 int regflags = 0; 60 #ifdef REG_EXTENDED 61 regflags |= REG_EXTENDED; 62 #endif 63 #ifdef REG_ICASE 64 if ( f & IgnoreCase ) 65 regflags |= REG_ICASE; 66 #endif 67 68 //NOTE: Multiline is not feasible with POSIX regex. 69 //if ( f & Multiline ) 70 // ; 71 // Note: the Global flag is already handled by RegExpProtoFunc::execute 72 73 regcomp(&preg, p.ascii(), regflags); 74 /* TODO check for errors */ 75 #endif 76 36 77 } 37 78 38 79 RegExp::~RegExp() 39 80 { 81 #ifdef HAVE_PCREPOSIX 82 pcre_free(pcregex); 83 84 #else 40 85 /* TODO: is this really okay after an error ? */ 41 86 regfree(&preg); 87 #endif 42 88 } 43 89 44 UString RegExp::match(const UString &s, int i, int *pos )90 UString RegExp::match(const UString &s, int i, int *pos, int **ovector) 45 91 { 92 93 #ifdef HAVE_PCREPOSIX 94 CString buffer(s.cstring()); 95 int ovecsize = (nrSubPatterns+1)*3; // see pcre docu 96 if (ovector) *ovector = new int[ovecsize]; 97 98 if (i < 0) 99 i = 0; 100 101 if (i > s.size() || s.isNull() || 102 pcre_exec(pcregex, NULL, buffer.c_str(), buffer.size(), i, 103 0, ovector ? *ovector : 0L, ovecsize) == PCRE_ERROR_NOMATCH) { 104 105 if (pos) 106 *pos = -1; 107 return UString::null; 108 } 109 110 if (!ovector) 111 return UString::null; // don't rely on the return value if you pass ovector==0 112 if (pos) 113 *pos = (*ovector)[0]; 114 return s.substr((*ovector)[0], (*ovector)[1] - (*ovector)[0]); 115 116 #else 46 117 regmatch_t rmatch[10]; 47 118 … … 49 120 i = 0; 50 121 122 char *str = strdup(s.ascii()); 51 123 if (i > s.size() || s.isNull() || 52 regexec(&preg, s .ascii()+ i, 10, rmatch, 0)) {124 regexec(&preg, str + i, 10, rmatch, 0)) { 53 125 if (pos) 54 *pos = -1;126 *pos = -1; 55 127 return UString::null; 56 128 } 129 free(str); 57 130 58 131 if (pos) 59 *pos = rmatch[0].rm_so + i; 132 *pos = rmatch[0].rm_so + i; 133 // TODO copy from rmatch to ovector 60 134 return s.substr(rmatch[0].rm_so + i, rmatch[0].rm_eo - rmatch[0].rm_so); 135 #endif 61 136 } 62 137 138 #if 0 // unused 63 139 bool RegExp::test(const UString &s, int) 64 140 { 65 int r = regexec(&preg, s.ascii(), 0, 0, 0); 141 #ifdef HAVE_PCREPOSIX 142 int ovector[300]; 143 CString buffer(s.cstring()); 144 145 if (s.isNull() || 146 pcre_exec(pcregex, NULL, buffer.c_str(), buffer.size(), 0, 147 0, ovector, 300) == PCRE_ERROR_NOMATCH) 148 return false; 149 else 150 return true; 151 152 #else 153 154 char *str = strdup(s.ascii()); 155 int r = regexec(&preg, str, 0, 0, 0); 156 free(str); 66 157 67 158 return r == 0; 159 #endif 68 160 } 161 #endif
Note:
See TracChangeset
for help on using the changeset viewer.