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/string_object.cpp

    r27222 r27320  
    339339    RegExpObjectImp* regExpObj = static_cast<RegExpObjectImp*>(exec->lexicalInterpreter()->builtinRegExp());
    340340
    341     int matchIndex = 0;
    342341    int lastIndex = 0;
    343342    int startPosition = 0;
     
    352351    // This is either a loop (if global is set) or a one-way (if not).
    353352    do {
    354       int *ovector;
    355       UString matchString = regExpObj->performMatch(reg, source, startPosition, &matchIndex, &ovector);
    356       if (matchIndex == -1)
     353      int matchIndex;
     354      int matchLen;
     355      int* ovector;
     356      regExpObj->performMatch(reg, source, startPosition, matchIndex, matchLen, &ovector);
     357      if (matchIndex < 0)
    357358        break;
    358       int matchLen = matchString.size();
    359359
    360360      pushSourceRange(sourceRanges, sourceRangeCount, sourceRangeCapacity, UString::Range(lastIndex, matchIndex - lastIndex));
     
    365365          List args;
    366366
    367           args.append(jsString(matchString));
    368 
    369           for (unsigned i = 0; i < reg->subPatterns(); i++) {
    370               int matchStart = ovector[(i + 1) * 2];
    371               int matchLen = ovector[(i + 1) * 2 + 1] - matchStart;
     367          for (unsigned i = 0; i < reg->subPatterns() + 1; i++) {
     368              int matchStart = ovector[i * 2];
     369              int matchLen = ovector[i * 2 + 1] - matchStart;
    372370
    373371              if (matchStart < 0)
     
    525523    }
    526524    RegExpObjectImp* regExpObj = static_cast<RegExpObjectImp*>(exec->lexicalInterpreter()->builtinRegExp());
    527     UString mstr = regExpObj->performMatch(reg, u, 0, &pos);
     525    int pos;
     526    int matchLength;
     527    regExpObj->performMatch(reg, u, 0, pos, matchLength);
    528528    if (id == Search) {
    529529      result = jsNumber(pos);
    530530    } else {
    531       // Exec
     531      // Match
    532532      if ((reg->flags() & RegExp::Global) == 0) {
    533533        // case without 'g' flag is handled like RegExp.prototype.exec
    534         if (mstr.isNull()) {
     534        if (pos < 0)
    535535          result = jsNull();
    536         } else {
    537           result = regExpObj->arrayOfMatches(exec,mstr);
    538         }
     536        else
     537          result = regExpObj->arrayOfMatches(exec);
    539538      } else {
    540539        // return array of matches
     
    542541        int lastIndex = 0;
    543542        while (pos >= 0) {
    544           if (mstr.isNull())
    545             list.append(jsUndefined());
    546           else
    547             list.append(jsString(mstr));
     543          list.append(jsString(u.substr(pos, matchLength)));
    548544          lastIndex = pos;
    549           pos += mstr.isEmpty() ? 1 : mstr.size();
    550           mstr = regExpObj->performMatch(reg, u, pos, &pos);
     545          pos += matchLength == 0 ? 1 : matchLength;
     546          regExpObj->performMatch(reg, u, pos, pos, matchLength);
    551547        }
    552548        if (imp)
     
    600596    if (a0->isObject() && static_cast<JSObject *>(a0)->inherits(&RegExpImp::info)) {
    601597      RegExp *reg = static_cast<RegExpImp *>(a0)->regExp();
    602       if (u.isEmpty() && !reg->match(u, 0).isNull()) {
     598      if (u.isEmpty() && reg->match(u, 0) >= 0) {
    603599        // empty string matched by regexp -> empty array
    604600        res->put(exec, exec->propertyNames().length, jsNumber(0));
     
    607603      pos = 0;
    608604      while (static_cast<uint32_t>(i) != limit && pos < u.size()) {
    609         int mpos;
    610         int* ovector;
    611         UString mstr = reg->match(u, pos, &mpos, &ovector);
    612         if (mpos < 0) {
    613           delete [] ovector;
     605        OwnArrayPtr<int> ovector;
     606        int mpos = reg->match(u, pos, &ovector);
     607        if (mpos < 0)
    614608          break;
    615         }
    616         pos = mpos + (mstr.isEmpty() ? 1 : mstr.size());
    617         if (mpos != p0 || !mstr.isEmpty()) {
     609        int mlen = ovector[1] - ovector[0];
     610        pos = mpos + (mlen == 0 ? 1 : mlen);
     611        if (mpos != p0 || mlen) {
    618612          res->put(exec,i, jsString(u.substr(p0, mpos-p0)));
    619           p0 = mpos + mstr.size();
     613          p0 = mpos + mlen;
    620614          i++;
    621615        }
     
    627621            res->put(exec, i++, jsString(u.substr(spos, ovector[si * 2 + 1] - spos)));
    628622        }
    629         delete [] ovector;
    630623      }
    631624    } else {
Note: See TracChangeset for help on using the changeset viewer.