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 # Only move the start position if we haven't already found a start
85 # This means that START START END matches outer pair
88 $inputPos = $tokenOffset +
$tokenLength;
89 # Write out the non-matching section
90 $output .= substr( $subject, $outputPos, $tokenOffset - $outputPos );
91 $outputPos = $tokenOffset;
92 $contentPos = $inputPos;
95 # Move the input position past the *first character* of START,
96 # to protect against missing END when it overlaps with START
97 $inputPos = $tokenOffset +
1;
99 } elseif ( $tokenType == 'end' ) {
102 $output .= call_user_func( $callback, array(
103 substr( $subject, $outputPos, $tokenOffset +
$tokenLength - $outputPos ),
104 substr( $subject, $contentPos, $tokenOffset - $contentPos )
108 # Non-matching end, write it out
109 $output .= substr( $subject, $inputPos, $tokenOffset +
$tokenLength - $outputPos );
111 $inputPos = $outputPos = $tokenOffset +
$tokenLength;
113 throw new MWException( 'Invalid delimiter given to ' . __METHOD__
);
116 if ( $outputPos < strlen( $subject ) ) {
117 $output .= substr( $subject, $outputPos );
123 * Perform an operation equivalent to
125 * preg_replace( "!$startDelim(.*)$endDelim!$flags", $replace, $subject )
127 * @param $startDelim String: start delimiter regular expression
128 * @param $endDelim String: end delimiter regular expression
129 * @param $replace String: replacement string. May contain $1, which will be
130 * replaced by the text between the delimiters
131 * @param $subject String to search
132 * @param $flags String: regular expression flags
133 * @return String: The string with the matches replaced
135 static function delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags = '' ) {
136 $replacer = new RegexlikeReplacer( $replace );
137 return self
::delimiterReplaceCallback( $startDelim, $endDelim,
138 $replacer->cb(), $subject, $flags );
142 * More or less "markup-safe" explode()
143 * Ignores any instances of the separator inside <...>
144 * @param $separator String
145 * @param $text String
148 static function explodeMarkup( $separator, $text ) {
149 $placeholder = "\x00";
151 // Remove placeholder instances
152 $text = str_replace( $placeholder, '', $text );
154 // Replace instances of the separator inside HTML-like tags with the placeholder
155 $replacer = new DoubleReplacer( $separator, $placeholder );
156 $cleaned = StringUtils
::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text );
158 // Explode, then put the replaced separators back in
159 $items = explode( $separator, $cleaned );
160 foreach( $items as $i => $str ) {
161 $items[$i] = str_replace( $placeholder, $separator, $str );
168 * Escape a string to make it suitable for inclusion in a preg_replace()
169 * replacement parameter.
171 * @param $string String
174 static function escapeRegexReplacement( $string ) {
175 $string = str_replace( '\\', '\\\\', $string );
176 $string = str_replace( '$', '\\$', $string );
181 * Workalike for explode() with limited memory usage.
182 * Returns an Iterator
184 static function explode( $separator, $subject ) {
185 if ( substr_count( $subject, $separator ) > 1000 ) {
186 return new ExplodeIterator( $separator, $subject );
188 return new ArrayIterator( explode( $separator, $subject ) );
194 * Base class for "replacers", objects used in preg_replace_callback() and
195 * StringUtils::delimiterReplaceCallback()
199 return array( &$this, 'replace' );
204 * Class to replace regex matches with a string similar to that used in preg_replace()
206 class RegexlikeReplacer
extends Replacer
{
208 function __construct( $r ) {
212 function replace( $matches ) {
214 foreach ( $matches as $i => $match ) {
215 $pairs["\$$i"] = $match;
217 return strtr( $this->r
, $pairs );
223 * Class to perform secondary replacement within each replacement string
225 class DoubleReplacer
extends Replacer
{
226 function __construct( $from, $to, $index = 0 ) {
229 $this->index
= $index;
232 function replace( $matches ) {
233 return str_replace( $this->from
, $this->to
, $matches[$this->index
] );
238 * Class to perform replacement based on a simple hashtable lookup
240 class HashtableReplacer
extends Replacer
{
243 function __construct( $table, $index = 0 ) {
244 $this->table
= $table;
245 $this->index
= $index;
248 function replace( $matches ) {
249 return $this->table
[$matches[$this->index
]];
254 * Replacement array for FSS with fallback to strtr()
255 * Supports lazy initialisation of FSS resource
257 class ReplacementArray
{
258 /*mostly private*/ var $data = false;
259 /*mostly private*/ var $fss = false;
262 * Create an object with the specified replacement array
263 * The array should have the same form as the replacement array for strtr()
265 function __construct( $data = array() ) {
270 return array( 'data' );
273 function __wakeup() {
278 * Set the whole replacement array at once
280 function setArray( $data ) {
285 function getArray() {
290 * Set an element of the replacement array
292 function setPair( $from, $to ) {
293 $this->data
[$from] = $to;
297 function mergeArray( $data ) {
298 $this->data
= array_merge( $this->data
, $data );
302 function merge( $other ) {
303 $this->data
= array_merge( $this->data
, $other->data
);
307 function removePair( $from ) {
308 unset($this->data
[$from]);
312 function removeArray( $data ) {
313 foreach( $data as $from => $to )
314 $this->removePair( $from );
318 function replace( $subject ) {
319 if ( function_exists( 'fss_prep_replace' ) ) {
320 wfProfileIn( __METHOD__
.'-fss' );
321 if ( $this->fss
=== false ) {
322 $this->fss
= fss_prep_replace( $this->data
);
324 $result = fss_exec_replace( $this->fss
, $subject );
325 wfProfileOut( __METHOD__
.'-fss' );
327 wfProfileIn( __METHOD__
.'-strtr' );
328 $result = strtr( $subject, $this->data
);
329 wfProfileOut( __METHOD__
.'-strtr' );
336 * An iterator which works exactly like:
338 * foreach ( explode( $delim, $s ) as $element ) {
342 * Except it doesn't use 193 byte per element
344 class ExplodeIterator
implements Iterator
{
345 // The subject string
346 var $subject, $subjectLength;
349 var $delim, $delimLength;
351 // The position of the start of the line
354 // The position after the end of the next delimiter
361 * Construct a DelimIterator
363 function __construct( $delim, $s ) {
365 $this->delim
= $delim;
367 // Micro-optimisation (theoretical)
368 $this->subjectLength
= strlen( $s );
369 $this->delimLength
= strlen( $delim );
376 $this->endPos
= strpos( $this->subject
, $this->delim
);
377 $this->refreshCurrent();
381 function refreshCurrent() {
382 if ( $this->curPos
=== false ) {
383 $this->current
= false;
384 } elseif ( $this->curPos
>= $this->subjectLength
) {
386 } elseif ( $this->endPos
=== false ) {
387 $this->current
= substr( $this->subject
, $this->curPos
);
389 $this->current
= substr( $this->subject
, $this->curPos
, $this->endPos
- $this->curPos
);
394 return $this->current
;
398 return $this->curPos
;
402 if ( $this->endPos
=== false ) {
403 $this->curPos
= false;
405 $this->curPos
= $this->endPos +
$this->delimLength
;
406 if ( $this->curPos
>= $this->subjectLength
) {
407 $this->endPos
= false;
409 $this->endPos
= strpos( $this->subject
, $this->delim
, $this->curPos
);
412 $this->refreshCurrent();
413 return $this->current
;
417 return $this->curPos
!== false;