5 * Perform an operation equivalent to
7 * preg_replace( "!$startDelim(.*?)$endDelim!", $replace, $subject );
9 * except that it's worst-case O(N) instead of O(N^2)
11 * Compared to delimiterReplace(), this implementation is fast but memory-
12 * hungry and inflexible. The memory requirements are such that I don't
13 * recommend using it on anything but guaranteed small chunks of text.
15 static function hungryDelimiterReplace( $startDelim, $endDelim, $replace, $subject ) {
16 $segments = explode( $startDelim, $subject );
17 $output = array_shift( $segments );
18 foreach ( $segments as $s ) {
19 $endDelimPos = strpos( $s, $endDelim );
20 if ( $endDelimPos === false ) {
21 $output .= $startDelim . $s;
23 $output .= $replace . substr( $s, $endDelimPos +
strlen( $endDelim ) );
30 * Perform an operation equivalent to
32 * preg_replace_callback( "!$startDelim(.*)$endDelim!s$flags", $callback, $subject )
34 * This implementation is slower than hungryDelimiterReplace but uses far less
35 * memory. The delimiters are literal strings, not regular expressions.
37 * @param string $flags Regular expression flags
39 # If the start delimiter ends with an initial substring of the end delimiter,
40 # e.g. in the case of C-style comments, the behaviour differs from the model
41 # regex. In this implementation, the end must share no characters with the
42 # start, so e.g. /*/ is not considered to be both the start and end of a
43 # comment. /*/xy/*/ is considered to be a single comment with contents /xy/.
44 static function delimiterReplaceCallback( $startDelim, $endDelim, $callback, $subject, $flags = '' ) {
49 $encStart = preg_quote( $startDelim, '!' );
50 $encEnd = preg_quote( $endDelim, '!' );
51 $strcmp = strpos( $flags, 'i' ) === false ?
'strcmp' : 'strcasecmp';
52 $endLength = strlen( $endDelim );
54 while ( $inputPos < strlen( $subject ) &&
55 preg_match( "!($encStart)|($encEnd)!S$flags", $subject, $m, PREG_OFFSET_CAPTURE
, $inputPos ) )
57 $tokenOffset = $m[0][1];
58 if ( $m[1][0] != '' ) {
60 $strcmp( $endDelim, substr( $subject, $tokenOffset, $endLength ) ) == 0 )
62 # An end match is present at the same location
64 $tokenLength = $endLength;
67 $tokenLength = strlen( $m[0][0] );
69 } elseif ( $m[2][0] != '' ) {
71 $tokenLength = strlen( $m[0][0] );
73 throw new MWException( 'Invalid delimiter given to ' . __METHOD__
);
76 if ( $tokenType == 'start' ) {
77 $inputPos = $tokenOffset +
$tokenLength;
78 # Only move the start position if we haven't already found a start
79 # This means that START START END matches outer pair
82 # Write out the non-matching section
83 $output .= substr( $subject, $outputPos, $tokenOffset - $outputPos );
84 $outputPos = $tokenOffset;
85 $contentPos = $inputPos;
88 } elseif ( $tokenType == 'end' ) {
91 $output .= call_user_func( $callback, array(
92 substr( $subject, $outputPos, $tokenOffset +
$tokenLength - $outputPos ),
93 substr( $subject, $contentPos, $tokenOffset - $contentPos )
97 # Non-matching end, write it out
98 $output .= substr( $subject, $inputPos, $tokenOffset +
$tokenLength - $outputPos );
100 $inputPos = $outputPos = $tokenOffset +
$tokenLength;
102 throw new MWException( 'Invalid delimiter given to ' . __METHOD__
);
105 if ( $outputPos < strlen( $subject ) ) {
106 $output .= substr( $subject, $outputPos );
112 * Perform an operation equivalent to
114 * preg_replace( "!$startDelim(.*)$endDelim!$flags", $replace, $subject )
116 * @param string $startDelim Start delimiter regular expression
117 * @param string $endDelim End delimiter regular expression
118 * @param string $replace Replacement string. May contain $1, which will be
119 * replaced by the text between the delimiters
120 * @param string $subject String to search
121 * @return string The string with the matches replaced
123 static function delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags = '' ) {
124 $replacer = new RegexlikeReplacer( $replace );
125 return self
::delimiterReplaceCallback( $startDelim, $endDelim,
126 $replacer->cb(), $subject, $flags );
130 * More or less "markup-safe" explode()
131 * Ignores any instances of the separator inside <...>
132 * @param string $separator
133 * @param string $text
136 static function explodeMarkup( $separator, $text ) {
137 $placeholder = "\x00";
139 // Remove placeholder instances
140 $text = str_replace( $placeholder, '', $text );
142 // Replace instances of the separator inside HTML-like tags with the placeholder
143 $replacer = new DoubleReplacer( $separator, $placeholder );
144 $cleaned = StringUtils
::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text );
146 // Explode, then put the replaced separators back in
147 $items = explode( $separator, $cleaned );
148 foreach( $items as $i => $str ) {
149 $items[$i] = str_replace( $placeholder, $separator, $str );
156 * Escape a string to make it suitable for inclusion in a preg_replace()
157 * replacement parameter.
159 * @param string $string
162 static function escapeRegexReplacement( $string ) {
163 $string = str_replace( '\\', '\\\\', $string );
164 $string = str_replace( '$', '\\$', $string );
170 * Base class for "replacers", objects used in preg_replace_callback() and
171 * StringUtils::delimiterReplaceCallback()
175 return array( &$this, 'replace' );
180 * Class to replace regex matches with a string similar to that used in preg_replace()
182 class RegexlikeReplacer
extends Replacer
{
184 function __construct( $r ) {
188 function replace( $matches ) {
190 foreach ( $matches as $i => $match ) {
191 $pairs["\$$i"] = $match;
193 return strtr( $this->r
, $pairs );
199 * Class to perform secondary replacement within each replacement string
201 class DoubleReplacer
extends Replacer
{
202 function __construct( $from, $to, $index = 0 ) {
205 $this->index
= $index;
208 function replace( $matches ) {
209 return str_replace( $this->from
, $this->to
, $matches[$this->index
] );
214 * Class to perform replacement based on a simple hashtable lookup
216 class HashtableReplacer
extends Replacer
{
219 function __construct( $table, $index = 0 ) {
220 $this->table
= $table;
221 $this->index
= $index;
224 function replace( $matches ) {
225 return $this->table
[$matches[$this->index
]];
230 * Replacement array for FSS with fallback to strtr()
231 * Supports lazy initialisation of FSS resource
233 class ReplacementArray
{
234 /*mostly private*/ var $data = false;
235 /*mostly private*/ var $fss = false;
238 * Create an object with the specified replacement array
239 * The array should have the same form as the replacement array for strtr()
241 function __construct( $data = array() ) {
246 return array( 'data' );
249 function __wakeup() {
254 * Set the whole replacement array at once
256 function setArray( $data ) {
261 function getArray() {
266 * Set an element of the replacement array
268 function setPair( $from, $to ) {
269 $this->data
[$from] = $to;
273 function mergeArray( $data ) {
274 $this->data
= array_merge( $this->data
, $data );
278 function merge( $other ) {
279 $this->data
= array_merge( $this->data
, $other->data
);
283 function replace( $subject ) {
284 if ( function_exists( 'fss_prep_replace' ) ) {
285 wfProfileIn( __METHOD__
.'-fss' );
286 if ( $this->fss
=== false ) {
287 $this->fss
= fss_prep_replace( $this->data
);
289 $result = fss_exec_replace( $this->fss
, $subject );
290 wfProfileOut( __METHOD__
.'-fss' );
292 wfProfileIn( __METHOD__
.'-strtr' );
293 $result = strtr( $subject, $this->data
);
294 wfProfileOut( __METHOD__
.'-strtr' );