From 9fed6d3bee9eaa74230775da25d12957d8c9b8c7 Mon Sep 17 00:00:00 2001 From: Thiemo Kreuz Date: Fri, 12 Jun 2020 08:48:40 +0200 Subject: [PATCH] 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 --- includes/Poem.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/includes/Poem.php b/includes/Poem.php index 390e3c4..1b5560a 100644 --- a/includes/Poem.php +++ b/includes/Poem.php @@ -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] ) ); } /**