Use much faster str_repeat() instead of str_replace()

This is about 50 (!) times faster. Not that it matters much. We don't
expect massive amounts of intentation here. Still, if we can easily
save some CPU cycles for many thousand wiki pages, why not?

Change-Id: I99b2b635268eedd7a6c0db73a68fb0532467e77e
This commit is contained in:
Thiemo Kreuz 2020-06-12 08:48:40 +02:00 committed by Huji
parent 434b9eead3
commit 9fed6d3bee

View file

@ -44,7 +44,7 @@ class Poem {
);
// replace spaces at the beginning of a line with non-breaking spaces
$text = preg_replace_callback( '/^( +)/m', [ self::class, 'replaceSpaces' ], $text );
$text = preg_replace_callback( '/^ +/m', [ self::class, 'replaceSpaces' ], $text );
$text = $parser->recursiveTagParse( $text, $frame );
@ -67,11 +67,11 @@ class Poem {
/**
* Callback for preg_replace_callback() that replaces spaces with non-breaking spaces
* @param string[] $m Matches from the regular expression
* - $m[1] consists of 1 or more spaces
* - $m[0] consists of 1 or more spaces
* @return string
*/
protected static function replaceSpaces( array $m ) {
return str_replace( ' ', ' ', $m[1] );
return str_repeat( ' ', strlen( $m[0] ) );
}
/**