Changeset 27320 in webkit for trunk/JavaScriptCore/kjs/regexp_object.cpp
- Timestamp:
- Oct 31, 2007, 7:46:41 AM (18 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/JavaScriptCore/kjs/regexp_object.cpp
r27303 r27320 1 1 // -*- c-basic-offset: 2 -*- 2 2 /* 3 * This file is part of the KDE libraries4 3 * Copyright (C) 1999-2000 Harri Porten ([email protected]) 5 4 * Copyright (C) 2003, 2007 Apple Inc. All Rights Reserved. … … 103 102 104 103 int foundIndex; 105 UString match = regExpObj->performMatch(regExp, input, static_cast<int>(lastIndex), &foundIndex);106 bool didMatch = !match.isNull();104 int foundLength; 105 regExpObj->performMatch(regExp, input, static_cast<int>(lastIndex), foundIndex, foundLength); 107 106 108 107 // Test 109 108 if (id == Test) 110 return jsBoolean( didMatch);109 return jsBoolean(foundIndex >= 0); 111 110 112 111 // Exec 113 if (didMatch) { 114 if (globalFlag) 115 thisObj->put(exec, exec->propertyNames().lastIndex, jsNumber(foundIndex + match.size()), DontDelete | DontEnum); 116 return regExpObj->arrayOfMatches(exec, match); 117 } else { 112 if (foundIndex < 0) { 118 113 if (globalFlag) 119 114 thisObj->put(exec, exec->propertyNames().lastIndex, jsNumber(0), DontDelete | DontEnum); 120 115 return jsNull(); 121 116 } 117 if (globalFlag) 118 thisObj->put(exec, exec->propertyNames().lastIndex, jsNumber(foundIndex + foundLength), DontDelete | DontEnum); 119 return regExpObj->arrayOfMatches(exec); 122 120 } 123 121 break; … … 258 256 e.g., RegExp.lastMatch and RegExp.leftParen. 259 257 */ 260 UString RegExpObjectImp::performMatch(RegExp* r, const UString& s, int startOffset, int *endOffset, int **ovector) 261 { 262 int tmpOffset; 263 int *tmpOvector; 264 UString match = r->match(s, startOffset, &tmpOffset, &tmpOvector); 265 266 if (endOffset) 267 *endOffset = tmpOffset; 258 void RegExpObjectImp::performMatch(RegExp* r, const UString& s, int startOffset, int& position, int& length, int** ovector) 259 { 260 OwnArrayPtr<int> tmpOvector; 261 position = r->match(s, startOffset, &tmpOvector); 262 268 263 if (ovector) 269 *ovector = tmpOvector ;264 *ovector = tmpOvector.get(); 270 265 271 if ( !match.isNull()) {266 if (position != -1) { 272 267 ASSERT(tmpOvector); 273 268 269 length = tmpOvector[1] - tmpOvector[0]; 270 274 271 d->lastInput = s; 275 d->lastOvector.set(tmpOvector );272 d->lastOvector.set(tmpOvector.release()); 276 273 d->lastNumSubPatterns = r->subPatterns(); 277 274 } 278 279 return match; 280 } 281 282 JSObject *RegExpObjectImp::arrayOfMatches(ExecState *exec, const UString &result) const 283 { 284 List list; 285 // The returned array contains 'result' as first item, followed by the list of matches 286 list.append(jsString(result)); 287 if (d->lastOvector) 288 for (unsigned i = 1 ; i < d->lastNumSubPatterns + 1 ; ++i) 289 { 290 int start = d->lastOvector[2*i]; 291 if (start == -1) 292 list.append(jsUndefined()); 293 else { 294 UString substring = d->lastInput.substr(start, d->lastOvector[2*i+1] - start); 295 list.append(jsString(substring)); 296 } 297 } 298 JSObject *arr = exec->lexicalInterpreter()->builtinArray()->construct(exec, list); 275 } 276 277 JSObject* RegExpObjectImp::arrayOfMatches(ExecState* exec) const 278 { 279 unsigned lastNumSubpatterns = d->lastNumSubPatterns; 280 ArrayInstance* arr = new ArrayInstance(exec->lexicalInterpreter()->builtinArrayPrototype(), lastNumSubpatterns + 1); 281 for (unsigned i = 0; i <= lastNumSubpatterns; ++i) { 282 int start = d->lastOvector[2 * i]; 283 if (start >= 0) 284 arr->put(exec, i, jsString(d->lastInput.substr(start, d->lastOvector[2 * i + 1] - start))); 285 } 299 286 arr->put(exec, exec->propertyNames().index, jsNumber(d->lastOvector[0])); 300 287 arr->put(exec, exec->propertyNames().input, jsString(d->lastInput)); … … 302 289 } 303 290 304 JSValue *RegExpObjectImp::getBackref(unsigned i) const 305 { 306 if (d->lastOvector && i < d->lastNumSubPatterns + 1) { 307 UString substring = d->lastInput.substr(d->lastOvector[2*i], d->lastOvector[2*i+1] - d->lastOvector[2*i] ); 308 return jsString(substring); 309 } 310 291 JSValue* RegExpObjectImp::getBackref(unsigned i) const 292 { 293 if (d->lastOvector && i <= d->lastNumSubPatterns) 294 return jsString(d->lastInput.substr(d->lastOvector[2 * i], d->lastOvector[2 * i + 1] - d->lastOvector[2 * i])); 311 295 return jsString(""); 312 296 } 313 297 314 JSValue *RegExpObjectImp::getLastMatch() const 315 { 316 if (d->lastOvector) { 317 UString substring = d->lastInput.substr(d->lastOvector[0], d->lastOvector[1] - d->lastOvector[0]); 318 return jsString(substring); 319 } 320 321 return jsString(""); 322 } 323 324 JSValue *RegExpObjectImp::getLastParen() const 325 { 326 int i = d->lastNumSubPatterns; 298 JSValue* RegExpObjectImp::getLastParen() const 299 { 300 unsigned i = d->lastNumSubPatterns; 327 301 if (i > 0) { 328 302 ASSERT(d->lastOvector); 329 UString substring = d->lastInput.substr(d->lastOvector[2*i], d->lastOvector[2*i+1] - d->lastOvector[2*i]); 330 return jsString(substring); 331 } 332 303 return jsString(d->lastInput.substr(d->lastOvector[2 * i], d->lastOvector[2 * i + 1] - d->lastOvector[2 * i])); 304 } 333 305 return jsString(""); 334 306 } … … 336 308 JSValue *RegExpObjectImp::getLeftContext() const 337 309 { 338 if (d->lastOvector) { 339 UString substring = d->lastInput.substr(0, d->lastOvector[0]); 340 return jsString(substring); 341 } 342 310 if (d->lastOvector) 311 return jsString(d->lastInput.substr(0, d->lastOvector[0])); 343 312 return jsString(""); 344 313 } … … 348 317 if (d->lastOvector) { 349 318 UString s = d->lastInput; 350 UString substring = s.substr(d->lastOvector[1], s.size() - d->lastOvector[1]); 351 return jsString(substring); 352 } 353 319 return jsString(s.substr(d->lastOvector[1], s.size() - d->lastOvector[1])); 320 } 354 321 return jsString(""); 355 322 } … … 386 353 return jsBoolean(d->multiline); 387 354 case LastMatch: 388 return get LastMatch();355 return getBackref(0); 389 356 case LastParen: 390 357 return getLastParen();
Note:
See TracChangeset
for help on using the changeset viewer.