3 * A collection of static methods to play with strings.
7 * Perform an operation equivalent to
9 * preg_replace( "!$startDelim(.*?)$endDelim!", $replace, $subject );
11 * except that it's worst-case O(N) instead of O(N^2)
13 * Compared to delimiterReplace(), this implementation is fast but memory-
14 * hungry and inflexible. The memory requirements are such that I don't
15 * recommend using it on anything but guaranteed small chunks of text.
17 static function hungryDelimiterReplace( $startDelim, $endDelim, $replace, $subject ) {
18 $segments = explode( $startDelim, $subject );
19 $output = array_shift( $segments );
20 foreach ( $segments as $s ) {
21 $endDelimPos = strpos( $s, $endDelim );
22 if ( $endDelimPos === false ) {
23 $output .= $startDelim . $s;
25 $output .= $replace . substr( $s, $endDelimPos +
strlen( $endDelim ) );
32 * Perform an operation equivalent to
34 * preg_replace_callback( "!$startDelim(.*)$endDelim!s$flags", $callback, $subject )
36 * This implementation is slower than hungryDelimiterReplace but uses far less
37 * memory. The delimiters are literal strings, not regular expressions.
39 * @param $startDelim String: start delimiter
40 * @param $endDelim String: end delimiter
41 * @param $callback Callback: function to call on each match
42 * @param $subject String
43 * @param $flags String: regular expression flags
45 # If the start delimiter ends with an initial substring of the end delimiter,
46 # e.g. in the case of C-style comments, the behaviour differs from the model
47 # regex. In this implementation, the end must share no characters with the
48 # start, so e.g. /*/ is not considered to be both the start and end of a
49 # comment. /*/xy/*/ is considered to be a single comment with contents /xy/.
50 static function delimiterReplaceCallback( $startDelim, $endDelim, $callback, $subject, $flags = '' ) {
55 $encStart = preg_quote( $startDelim, '!' );
56 $encEnd = preg_quote( $endDelim, '!' );
57 $strcmp = strpos( $flags, 'i' ) === false ?
'strcmp' : 'strcasecmp';
58 $endLength = strlen( $endDelim );
61 while ( $inputPos < strlen( $subject ) &&
62 preg_match( "!($encStart)|($encEnd)!S$flags", $subject, $m, PREG_OFFSET_CAPTURE
, $inputPos ) )
64 $tokenOffset = $m[0][1];
65 if ( $m[1][0] != '' ) {
67 $strcmp( $endDelim, substr( $subject, $tokenOffset, $endLength ) ) == 0 )
69 # An end match is present at the same location
71 $tokenLength = $endLength;
74 $tokenLength = strlen( $m[0][0] );
76 } elseif ( $m[2][0] != '' ) {
78 $tokenLength = strlen( $m[0][0] );
80 throw new MWException( 'Invalid delimiter given to ' . __METHOD__
);
83 if ( $tokenType == 'start' ) {
84 $inputPos = $tokenOffset +
$tokenLength;
85 # Only move the start position if we haven't already found a start
86 # This means that START START END matches outer pair
89 # Write out the non-matching section
90 $output .= substr( $subject, $outputPos, $tokenOffset - $outputPos );
91 $outputPos = $tokenOffset;
92 $contentPos = $inputPos;
95 } elseif ( $tokenType == 'end' ) {
98 $output .= call_user_func( $callback, array(
99 substr( $subject, $outputPos, $tokenOffset +
$tokenLength - $outputPos ),
100 substr( $subject, $contentPos, $tokenOffset - $contentPos )
104 # Non-matching end, write it out
105 $output .= substr( $subject, $inputPos, $tokenOffset +
$tokenLength - $outputPos );
107 $inputPos = $outputPos = $tokenOffset +
$tokenLength;
109 throw new MWException( 'Invalid delimiter given to ' . __METHOD__
);
112 if ( $outputPos < strlen( $subject ) ) {
113 $output .= substr( $subject, $outputPos );
119 * Perform an operation equivalent to
121 * preg_replace( "!$startDelim(.*)$endDelim!$flags", $replace, $subject )
123 * @param $startDelim String: start delimiter regular expression
124 * @param $endDelim String: end delimiter regular expression
125 * @param $replace String: replacement string. May contain $1, which will be
126 * replaced by the text between the delimiters
127 * @param $subject String to search
128 * @param $flags String: regular expression flags
129 * @return String: The string with the matches replaced
131 static function delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags = '' ) {
132 $replacer = new RegexlikeReplacer( $replace );
133 return self
::delimiterReplaceCallback( $startDelim, $endDelim,
134 $replacer->cb(), $subject, $flags );
138 * More or less "markup-safe" explode()
139 * Ignores any instances of the separator inside <...>
140 * @param $separator String
141 * @param $text String
144 static function explodeMarkup( $separator, $text ) {
145 $placeholder = "\x00";
147 // Remove placeholder instances
148 $text = str_replace( $placeholder, '', $text );
150 // Replace instances of the separator inside HTML-like tags with the placeholder
151 $replacer = new DoubleReplacer( $separator, $placeholder );
152 $cleaned = StringUtils
::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text );
154 // Explode, then put the replaced separators back in
155 $items = explode( $separator, $cleaned );
156 foreach( $items as $i => $str ) {
157 $items[$i] = str_replace( $placeholder, $separator, $str );
164 * Escape a string to make it suitable for inclusion in a preg_replace()
165 * replacement parameter.
167 * @param $string String
170 static function escapeRegexReplacement( $string ) {
171 $string = str_replace( '\\', '\\\\', $string );
172 $string = str_replace( '$', '\\$', $string );
177 * Workalike for explode() with limited memory usage.
178 * Returns an Iterator
180 static function explode( $separator, $subject ) {
181 if ( substr_count( $subject, $separator ) > 1000 ) {
182 return new ExplodeIterator( $separator, $subject );
184 return new ArrayIterator( explode( $separator, $subject ) );
190 * Base class for "replacers", objects used in preg_replace_callback() and
191 * StringUtils::delimiterReplaceCallback()
195 return array( &$this, 'replace' );
200 * Class to replace regex matches with a string similar to that used in preg_replace()
202 class RegexlikeReplacer
extends Replacer
{
204 function __construct( $r ) {
208 function replace( $matches ) {
210 foreach ( $matches as $i => $match ) {
211 $pairs["\$$i"] = $match;
213 return strtr( $this->r
, $pairs );
219 * Class to perform secondary replacement within each replacement string
221 class DoubleReplacer
extends Replacer
{
222 function __construct( $from, $to, $index = 0 ) {
225 $this->index
= $index;
228 function replace( $matches ) {
229 return str_replace( $this->from
, $this->to
, $matches[$this->index
] );
234 * Class to perform replacement based on a simple hashtable lookup
236 class HashtableReplacer
extends Replacer
{
239 function __construct( $table, $index = 0 ) {
240 $this->table
= $table;
241 $this->index
= $index;
244 function replace( $matches ) {
245 return $this->table
[$matches[$this->index
]];
250 * Replacement array for FSS with fallback to strtr()
251 * Supports lazy initialisation of FSS resource
253 class ReplacementArray
{
254 /*mostly private*/ var $data = false;
255 /*mostly private*/ var $fss = false;
258 * Create an object with the specified replacement array
259 * The array should have the same form as the replacement array for strtr()
261 function __construct( $data = array() ) {
266 return array( 'data' );
269 function __wakeup() {
274 * Set the whole replacement array at once
276 function setArray( $data ) {
281 function getArray() {
286 * Set an element of the replacement array
288 function setPair( $from, $to ) {
289 $this->data
[$from] = $to;
293 function mergeArray( $data ) {
294 $this->data
= array_merge( $this->data
, $data );
298 function merge( $other ) {
299 $this->data
= array_merge( $this->data
, $other->data
);
303 function removePair( $from ) {
304 unset($this->data
[$from]);
308 function removeArray( $data ) {
309 foreach( $data as $from => $to )
310 $this->removePair( $from );
314 function replace( $subject ) {
315 if ( function_exists( 'fss_prep_replace' ) ) {
316 wfProfileIn( __METHOD__
.'-fss' );
317 if ( $this->fss
=== false ) {
318 $this->fss
= fss_prep_replace( $this->data
);
320 $result = fss_exec_replace( $this->fss
, $subject );
321 wfProfileOut( __METHOD__
.'-fss' );
323 wfProfileIn( __METHOD__
.'-strtr' );
324 $result = strtr( $subject, $this->data
);
325 wfProfileOut( __METHOD__
.'-strtr' );
332 * An iterator which works exactly like:
334 * foreach ( explode( $delim, $s ) as $element ) {
338 * Except it doesn't use 193 byte per element
340 class ExplodeIterator
implements Iterator
{
341 // The subject string
342 var $subject, $subjectLength;
345 var $delim, $delimLength;
347 // The position of the start of the line
350 // The position after the end of the next delimiter
357 * Construct a DelimIterator
359 function __construct( $delim, $s ) {
361 $this->delim
= $delim;
363 // Micro-optimisation (theoretical)
364 $this->subjectLength
= strlen( $s );
365 $this->delimLength
= strlen( $delim );
372 $this->endPos
= strpos( $this->subject
, $this->delim
);
373 $this->refreshCurrent();
377 function refreshCurrent() {
378 if ( $this->curPos
=== false ) {
379 $this->current
= false;
380 } elseif ( $this->curPos
>= $this->subjectLength
) {
382 } elseif ( $this->endPos
=== false ) {
383 $this->current
= substr( $this->subject
, $this->curPos
);
385 $this->current
= substr( $this->subject
, $this->curPos
, $this->endPos
- $this->curPos
);
390 return $this->current
;
394 return $this->curPos
;
398 if ( $this->endPos
=== false ) {
399 $this->curPos
= false;
401 $this->curPos
= $this->endPos +
$this->delimLength
;
402 if ( $this->curPos
>= $this->subjectLength
) {
403 $this->endPos
= false;
405 $this->endPos
= strpos( $this->subject
, $this->delim
, $this->curPos
);
408 $this->refreshCurrent();
409 return $this->current
;
413 return $this->curPos
!== false;