From 5eb23f8e3c4fc366c0beb803941b3c867dab2016 Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Fri, 4 Jul 2014 01:15:15 +0000 Subject: [PATCH] Optimize regexp usage in `wptexturize()` for a "3x Performance Boost." Props miqrogroove. See #28724. Built from https://develop.svn.wordpress.org/trunk@28986 git-svn-id: http://core.svn.wordpress.org/trunk@28775 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-includes/formatting.php | 61 +++++++++++++++++++++++++------------- 1 file changed, 41 insertions(+), 20 deletions(-) diff --git a/wp-includes/formatting.php b/wp-includes/formatting.php index b4cbbcf8e9..4afb487a07 100644 --- a/wp-includes/formatting.php +++ b/wp-includes/formatting.php @@ -96,11 +96,13 @@ function wptexturize($text, $reset = false) { $static_characters = array_merge( array( '...', '``', '\'\'', ' (tm)' ), $cockney ); $static_replacements = array_merge( array( '…', $opening_quote, $closing_quote, ' ™' ), $cockneyreplace ); - $spaces = wp_spaces_regexp(); - // Pattern-based replacements of characters. + // Sort the remaining patterns into several arrays for performance tuning. + $dynamic_characters = array( 'apos' => array(), 'quote' => array(), 'dash' => array() ); + $dynamic_replacements = array( 'apos' => array(), 'quote' => array(), 'dash' => array() ); $dynamic = array(); + $spaces = wp_spaces_regexp(); // '99' and '99" are ambiguous among other patterns; assume it's an abbreviated year at the end of a quotation. if ( "'" !== $apos || "'" !== $closing_single_quote ) { @@ -115,10 +117,7 @@ function wptexturize($text, $reset = false) { $dynamic[ '/\'(?=\d\d(?:\Z|(?![%\d]|[.,]\d)))/' ] = $apos; } - // Quoted Numbers like "42" or '42.00' - if ( '"' !== $opening_quote && '"' !== $closing_quote ) { - $dynamic[ '/(?<=\A|' . $spaces . ')"(\d[.,\d]*)"/' ] = $opening_quote . '$1' . $closing_quote; - } + // Quoted Numbers like '0.42' if ( "'" !== $opening_single_quote && "'" !== $closing_single_quote ) { $dynamic[ '/(?<=\A|' . $spaces . ')\'(\d[.,\d]*)\'/' ] = $opening_single_quote . '$1' . $closing_single_quote; } @@ -133,16 +132,30 @@ function wptexturize($text, $reset = false) { $dynamic[ '/(?