Make WordPress Core


Ignore:
Timestamp:
07/04/2014 01:14:08 AM (11 years ago)
Author:
wonderboymusic
Message:

Optimize regexp usage in wptexturize() for a "3x Performance Boost."

Props miqrogroove.
See #28724.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/formatting.php

    r28973 r28986  
    9797        $static_replacements = array_merge( array( '…', $opening_quote, $closing_quote, ' ™' ), $cockneyreplace );
    9898
     99
     100        // Pattern-based replacements of characters.
     101        // Sort the remaining patterns into several arrays for performance tuning.
     102        $dynamic_characters = array( 'apos' => array(), 'quote' => array(), 'dash' => array() );
     103        $dynamic_replacements = array( 'apos' => array(), 'quote' => array(), 'dash' => array() );
     104        $dynamic = array();
    99105        $spaces = wp_spaces_regexp();
    100 
    101 
    102         // Pattern-based replacements of characters.
    103         $dynamic = array();
    104106
    105107        // '99' and '99" are ambiguous among other patterns; assume it's an abbreviated year at the end of a quotation.
     
    116118        }
    117119
    118         // Quoted Numbers like "42" or '42.00'
    119         if ( '"' !== $opening_quote && '"' !== $closing_quote ) {
    120             $dynamic[ '/(?<=\A|' . $spaces . ')"(\d[.,\d]*)"/' ] = $opening_quote . '$1' . $closing_quote;
    121         }
     120        // Quoted Numbers like '0.42'
    122121        if ( "'" !== $opening_single_quote && "'" !== $closing_single_quote ) {
    123122            $dynamic[ '/(?<=\A|' . $spaces . ')\'(\d[.,\d]*)\'/' ] = $opening_single_quote . '$1' . $closing_single_quote;
     
    134133        }
    135134
     135        // 9' (prime)
     136        if ( "'" !== $prime ) {
     137            $dynamic[ '/(?<=\d)\'/' ] = $prime;
     138        }
     139
     140        // Single quotes followed by spaces or ending punctuation.
     141        if ( "'" !== $closing_single_quote ) {
     142            $dynamic[ '/\'(?=\Z|[.,)}\-\]]|&gt;|' . $spaces . ')/' ] = $closing_single_quote;
     143        }
     144
     145        $dynamic_characters['apos'] = array_keys( $dynamic );
     146        $dynamic_replacements['apos'] = array_values( $dynamic );
     147        $dynamic = array();
     148       
     149        // Quoted Numbers like "42"
     150        if ( '"' !== $opening_quote && '"' !== $closing_quote ) {
     151            $dynamic[ '/(?<=\A|' . $spaces . ')"(\d[.,\d]*)"/' ] = $opening_quote . '$1' . $closing_quote;
     152        }
     153
    136154        // 9" (double prime)
    137155        if ( '"' !== $double_prime ) {
     
    139157        }
    140158
    141         // 9' (prime)
    142         if ( "'" !== $prime ) {
    143             $dynamic[ '/(?<=\d)\'/' ] = $prime;
    144         }
    145 
    146159        // Double quote at start, or preceded by (, {, <, [, -, or spaces, and not followed by spaces.
    147160        if ( '"' !== $opening_quote ) {
     
    153166            $dynamic[ '/"/' ] = $closing_quote;
    154167        }
    155 
    156         // Single quotes followed by spaces or ending punctuation.
    157         if ( "'" !== $closing_single_quote ) {
    158             $dynamic[ '/\'(?=\Z|[.,)}\-\]]|&gt;|' . $spaces . ')/' ] = $closing_single_quote;
    159         }
    160 
     168       
     169        $dynamic_characters['quote'] = array_keys( $dynamic );
     170        $dynamic_replacements['quote'] = array_values( $dynamic );
     171        $dynamic = array();
     172       
    161173        // Dashes and spaces
    162174        $dynamic[ '/---/' ] = $em_dash;
     
    165177        $dynamic[ '/(?<=' . $spaces . ')-(?=' . $spaces . ')/' ] = $en_dash;
    166178
    167         $dynamic_characters = array_keys( $dynamic );
    168         $dynamic_replacements = array_values( $dynamic );
     179        $dynamic_characters['dash'] = array_keys( $dynamic );
     180        $dynamic_replacements['dash'] = array_values( $dynamic );
    169181    }
    170182
     
    238250
    239251            $curl = str_replace( $static_characters, $static_replacements, $curl );
    240             $curl = preg_replace( $dynamic_characters, $dynamic_replacements, $curl );
     252
     253            if ( false !== strpos( $curl, "'" ) ) {
     254                $curl = preg_replace( $dynamic_characters['apos'], $dynamic_replacements['apos'], $curl );
     255            }
     256            if ( false !== strpos( $curl, '"' ) ) {
     257                $curl = preg_replace( $dynamic_characters['quote'], $dynamic_replacements['quote'], $curl );
     258            }
     259            if ( false !== strpos( $curl, '-' ) ) {
     260                $curl = preg_replace( $dynamic_characters['dash'], $dynamic_replacements['dash'], $curl );
     261            }
    241262
    242263            // 9x9 (times), but never 0x9999
Note: See TracChangeset for help on using the changeset viewer.