Ignore:
Timestamp:
Jun 22, 2005, 10:26:17 AM (20 years ago)
Author:
darin
Message:

Change by Anders Carlsson.
Reviewed by me.

  • kjs/string_object.cpp: (replace): Added code to handle functions.
  • tests/mozilla/expected.html: Updated since ecma_3/RegExp/regress-209067.js is fixed now.
  • tests/mozilla/run-mozilla-tests: Fix a minor coding style issue that leads to a warning each time we run the tests.
File:
1 edited

Legend:

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

    r9455 r9462  
    265265static Value replace(ExecState *exec, const UString &source, const Value &pattern, const Value &replacement)
    266266{
     267  ObjectImp *replacementFunction = 0;
     268  UString replacementString;
     269
     270  if (replacement.type() == ObjectType && replacement.toObject(exec).implementsCall())
     271    replacementFunction = replacement.toObject(exec).imp();
     272  else
     273    replacementString = replacement.toString(exec);
     274
    267275  if (pattern.type() == ObjectType && pattern.toObject(exec).inherits(&RegExpImp::info)) {
    268276    RegExpImp* imp = static_cast<RegExpImp *>( pattern.toObject(exec).imp() );
     
    271279
    272280    RegExpObjectImp* regExpObj = static_cast<RegExpObjectImp*>(exec->lexicalInterpreter()->builtinRegExp().imp());
    273 
    274     UString replacementString = replacement.toString(exec);
    275281
    276282    int matchIndex = 0;
     
    296302      pushSourceRange(sourceRanges, sourceRangeCount, sourceRangeCapacity, UString::Range(lastIndex, matchIndex - lastIndex));
    297303
     304      if (replacementFunction) {
     305          int completeMatchStart = (*ovector)[0];
     306          List args;
     307
     308          args.append(Value(matchString));
     309         
     310          for (unsigned i = 0; i < reg->subPatterns(); i++) {
     311              int matchStart = (*ovector)[(i + 1) * 2];
     312              int matchLen = (*ovector)[(i + 1) * 2 + 1] - matchStart;
     313             
     314              args.append(Value(source.substr(matchStart, matchLen)));
     315          }
     316         
     317          args.append(Value(completeMatchStart));
     318          args.append(Value(source));
     319
     320          replacementString = replacementFunction->call(exec, exec->dynamicInterpreter()->globalObject(),
     321                                                        args).toString(exec);
     322      }
     323     
    298324      UString substitutedReplacement = substituteBackreferences(replacementString, source, ovector, reg);
    299325      pushReplacement(replacements, replacementCount, replacementCapacity, substitutedReplacement);
     
    328354  if (matchPos == -1)
    329355    return String(source);
    330   return String(source.substr(0, matchPos) + replacement.toString(exec) + source.substr(matchPos + matchLen));
     356 
     357  if (replacementFunction) {
     358      List args;
     359     
     360      args.append(Value(source.substr(matchPos, matchLen)));
     361      args.append(Value(matchPos));
     362      args.append(Value(source));
     363     
     364      replacementString = replacementFunction->call(exec, exec->dynamicInterpreter()->globalObject(),
     365                                                    args).toString(exec);
     366  }
     367
     368  return String(source.substr(0, matchPos) + replacementString + source.substr(matchPos + matchLen));
    331369}
    332370
Note: See TracChangeset for help on using the changeset viewer.