Ignore:
Timestamp:
Oct 31, 2007, 7:46:41 AM (18 years ago)
Author:
darin
Message:

Reviewed by Maciej.

Speeds things up 0.4% according to SunSpider.

  • kjs/config.h: Define USE(PCRE16) instead of HAVE(PCREPOSIX), because this library doesn't use the real PCRE -- it uses its own PCRE that works on UTF-16.
  • kjs/regexp.h: Removed a few unused functions. Changed the ifdef. Use Noncopyable. Change the return value of match.
  • kjs/regexp.cpp: (KJS::RegExp::RegExp): Call pcre_compile2, for a slight speed boost. (KJS::RegExp::~RegExp): PCRE16 rather than PCREPOSIX. (KJS::RegExp::match): Change to return the position as an int and the ovector as a OwnArrayPtr<int> for efficiency and clearer storage management.
  • kjs/regexp_object.h: Change performMatch and arrayOfMatches to no longer require a result string.
  • kjs/regexp_object.cpp: (RegExpProtoFunc::callAsFunction): Update for new signature of performMatch. (RegExpObjectImp::performMatch): Change so it doesn't return a string. (RegExpObjectImp::arrayOfMatches): Simplify by unifying the handling of the main result with the backreferences; now it doesn't need to take a result parameter. (RegExpObjectImp::getBackref): Minor tweaks. (RegExpObjectImp::getLastParen): Ditto. (RegExpObjectImp::getLeftContext): Ditto. (RegExpObjectImp::getRightContext): Ditto. (RegExpObjectImp::getValueProperty): Change LastMatch case to call getBackref(0) so we don't need a separate getLastMatch function.
  • kjs/string_object.cpp: (KJS::replace): Update to use new performMatch, including merging the matched string section with the other substrings. (KJS::StringProtoFunc::callAsFunction): Update functions to use the new performMatch and match. Also change to use OwnArrayPtr.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/JavaScriptCore/kjs/regexp_object.cpp

    r27303 r27320  
    11// -*- c-basic-offset: 2 -*-
    22/*
    3  *  This file is part of the KDE libraries
    43 *  Copyright (C) 1999-2000 Harri Porten ([email protected])
    54 *  Copyright (C) 2003, 2007 Apple Inc. All Rights Reserved.
     
    103102
    104103    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);
    107106
    108107    // Test
    109108    if (id == Test)
    110       return jsBoolean(didMatch);
     109      return jsBoolean(foundIndex >= 0);
    111110
    112111    // 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) {
    118113      if (globalFlag)
    119114        thisObj->put(exec, exec->propertyNames().lastIndex, jsNumber(0), DontDelete | DontEnum);
    120115      return jsNull();
    121116    }
     117    if (globalFlag)
     118      thisObj->put(exec, exec->propertyNames().lastIndex, jsNumber(foundIndex + foundLength), DontDelete | DontEnum);
     119    return regExpObj->arrayOfMatches(exec);
    122120  }
    123121  break;
     
    258256  e.g., RegExp.lastMatch and RegExp.leftParen.
    259257*/
    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;
     258void 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
    268263  if (ovector)
    269     *ovector = tmpOvector;
     264    *ovector = tmpOvector.get();
    270265 
    271   if (!match.isNull()) {
     266  if (position != -1) {
    272267    ASSERT(tmpOvector);
    273    
     268
     269    length = tmpOvector[1] - tmpOvector[0];
     270
    274271    d->lastInput = s;
    275     d->lastOvector.set(tmpOvector);
     272    d->lastOvector.set(tmpOvector.release());
    276273    d->lastNumSubPatterns = r->subPatterns();
    277274  }
    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
     277JSObject* 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  }
    299286  arr->put(exec, exec->propertyNames().index, jsNumber(d->lastOvector[0]));
    300287  arr->put(exec, exec->propertyNames().input, jsString(d->lastInput));
     
    302289}
    303290
    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 
     291JSValue* 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]));
    311295  return jsString("");
    312296}
    313297
    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;
     298JSValue* RegExpObjectImp::getLastParen() const
     299{
     300  unsigned i = d->lastNumSubPatterns;
    327301  if (i > 0) {
    328302    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  }
    333305  return jsString("");
    334306}
     
    336308JSValue *RegExpObjectImp::getLeftContext() const
    337309{
    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]));
    343312  return jsString("");
    344313}
     
    348317  if (d->lastOvector) {
    349318    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  }
    354321  return jsString("");
    355322}
     
    386353      return jsBoolean(d->multiline);
    387354    case LastMatch:
    388       return getLastMatch();
     355      return getBackref(0);
    389356    case LastParen:
    390357      return getLastParen();
Note: See TracChangeset for help on using the changeset viewer.