r61154 MediaWiki - Code Review archive

Repository:MediaWiki
Revision:r61153‎ | r61154 | r61155 >
Date:23:52, 16 January 2010
Author:platonides
Status:resolved (Comments)
Tags:
Comment:
Follow up r61149. Using preg_quote on $inReplaceTo replaces too much. However, we need to avoid \1 and $1 backreferences, since there won't be any (the user does no regex search, all parenthesis will be escaped).
Modified paths:
  • /trunk/extensions/ParserFunctions/ParserFunctions_body.php (modified) (history)

Diff [purge]

Index: trunk/extensions/ParserFunctions/ParserFunctions_body.php
@@ -710,7 +710,7 @@
711711
712712 // Use regex to allow limit and handle UTF-8 correctly.
713713 $inReplaceFrom = preg_quote( $inReplaceFrom, '/' );
714 - $inReplaceTo = preg_quote( $inReplaceTo, '/' );
 714+ $inReplaceTo = strtr( $inReplaceTo, array("\\" => "\\\\", '$'=>"\\$" ) );
715715
716716 $result = preg_replace( '/' . $inReplaceFrom . '/u',
717717 $inReplaceTo, $inStr, $limit);

Follow-up revisions

RevisionCommit summaryAuthorDate
r61569Follow-up r61154. Use StringUtils::escapeRegexReplacement() instead of manual...platonides11:25, 27 January 2010

Past revisions this follows-up on

RevisionCommit summaryAuthorDate
r61149(bug 22115) Explode: Subject does not need to be quoted....reedy21:03, 16 January 2010

Comments

#Comment by Tim Starling (talk | contribs)   03:00, 27 January 2010

Use StringUtils::escapeRegexReplacement().

#Comment by Platonides (talk | contribs)   11:26, 27 January 2010

Done on r61569.

Are the two str_replace() preferable yo the strtr() for any reason?

#Comment by Tim Starling (talk | contribs)   04:51, 28 January 2010

The array form of strtr() is implemented by doing at least one hashtable lookup for each byte position in the source string. I think you'd need quite a lot of str_replace() calls to be as slow as it. I probably benchmarked it at the time.

Status & tagging log