Update.
[mediawiki.git] / includes / StringUtils.php
blob0090604dd589f6a17ac4911687a3e39ec51e8539
1 <?php
3 class StringUtils {
4 /**
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;
22 } else {
23 $output .= $replace . substr( $s, $endDelimPos + strlen( $endDelim ) );
26 return $output;
29 /**
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 = '' ) {
45 $inputPos = 0;
46 $outputPos = 0;
47 $output = '';
48 $foundStart = false;
49 $encStart = preg_quote( $startDelim, '!' );
50 $encEnd = preg_quote( $endDelim, '!' );
51 $strcmp = strpos( $flags, 'i' ) === false ? 'strcmp' : 'strcasecmp';
52 $endLength = strlen( $endDelim );
53 $m = array();
55 while ( $inputPos < strlen( $subject ) &&
56 preg_match( "!($encStart)|($encEnd)!S$flags", $subject, $m, PREG_OFFSET_CAPTURE, $inputPos ) )
58 $tokenOffset = $m[0][1];
59 if ( $m[1][0] != '' ) {
60 if ( $foundStart &&
61 $strcmp( $endDelim, substr( $subject, $tokenOffset, $endLength ) ) == 0 )
63 # An end match is present at the same location
64 $tokenType = 'end';
65 $tokenLength = $endLength;
66 } else {
67 $tokenType = 'start';
68 $tokenLength = strlen( $m[0][0] );
70 } elseif ( $m[2][0] != '' ) {
71 $tokenType = 'end';
72 $tokenLength = strlen( $m[0][0] );
73 } else {
74 throw new MWException( 'Invalid delimiter given to ' . __METHOD__ );
77 if ( $tokenType == 'start' ) {
78 $inputPos = $tokenOffset + $tokenLength;
79 # Only move the start position if we haven't already found a start
80 # This means that START START END matches outer pair
81 if ( !$foundStart ) {
82 # Found start
83 # Write out the non-matching section
84 $output .= substr( $subject, $outputPos, $tokenOffset - $outputPos );
85 $outputPos = $tokenOffset;
86 $contentPos = $inputPos;
87 $foundStart = true;
89 } elseif ( $tokenType == 'end' ) {
90 if ( $foundStart ) {
91 # Found match
92 $output .= call_user_func( $callback, array(
93 substr( $subject, $outputPos, $tokenOffset + $tokenLength - $outputPos ),
94 substr( $subject, $contentPos, $tokenOffset - $contentPos )
95 ));
96 $foundStart = false;
97 } else {
98 # Non-matching end, write it out
99 $output .= substr( $subject, $inputPos, $tokenOffset + $tokenLength - $outputPos );
101 $inputPos = $outputPos = $tokenOffset + $tokenLength;
102 } else {
103 throw new MWException( 'Invalid delimiter given to ' . __METHOD__ );
106 if ( $outputPos < strlen( $subject ) ) {
107 $output .= substr( $subject, $outputPos );
109 return $output;
113 * Perform an operation equivalent to
115 * preg_replace( "!$startDelim(.*)$endDelim!$flags", $replace, $subject )
117 * @param string $startDelim Start delimiter regular expression
118 * @param string $endDelim End delimiter regular expression
119 * @param string $replace Replacement string. May contain $1, which will be
120 * replaced by the text between the delimiters
121 * @param string $subject String to search
122 * @return string The string with the matches replaced
124 static function delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags = '' ) {
125 $replacer = new RegexlikeReplacer( $replace );
126 return self::delimiterReplaceCallback( $startDelim, $endDelim,
127 $replacer->cb(), $subject, $flags );
131 * More or less "markup-safe" explode()
132 * Ignores any instances of the separator inside <...>
133 * @param string $separator
134 * @param string $text
135 * @return array
137 static function explodeMarkup( $separator, $text ) {
138 $placeholder = "\x00";
140 // Remove placeholder instances
141 $text = str_replace( $placeholder, '', $text );
143 // Replace instances of the separator inside HTML-like tags with the placeholder
144 $replacer = new DoubleReplacer( $separator, $placeholder );
145 $cleaned = StringUtils::delimiterReplaceCallback( '<', '>', $replacer->cb(), $text );
147 // Explode, then put the replaced separators back in
148 $items = explode( $separator, $cleaned );
149 foreach( $items as $i => $str ) {
150 $items[$i] = str_replace( $placeholder, $separator, $str );
153 return $items;
157 * Escape a string to make it suitable for inclusion in a preg_replace()
158 * replacement parameter.
160 * @param string $string
161 * @return string
163 static function escapeRegexReplacement( $string ) {
164 $string = str_replace( '\\', '\\\\', $string );
165 $string = str_replace( '$', '\\$', $string );
166 return $string;
171 * Base class for "replacers", objects used in preg_replace_callback() and
172 * StringUtils::delimiterReplaceCallback()
174 class Replacer {
175 function cb() {
176 return array( &$this, 'replace' );
181 * Class to replace regex matches with a string similar to that used in preg_replace()
183 class RegexlikeReplacer extends Replacer {
184 var $r;
185 function __construct( $r ) {
186 $this->r = $r;
189 function replace( $matches ) {
190 $pairs = array();
191 foreach ( $matches as $i => $match ) {
192 $pairs["\$$i"] = $match;
194 return strtr( $this->r, $pairs );
200 * Class to perform secondary replacement within each replacement string
202 class DoubleReplacer extends Replacer {
203 function __construct( $from, $to, $index = 0 ) {
204 $this->from = $from;
205 $this->to = $to;
206 $this->index = $index;
209 function replace( $matches ) {
210 return str_replace( $this->from, $this->to, $matches[$this->index] );
215 * Class to perform replacement based on a simple hashtable lookup
217 class HashtableReplacer extends Replacer {
218 var $table, $index;
220 function __construct( $table, $index = 0 ) {
221 $this->table = $table;
222 $this->index = $index;
225 function replace( $matches ) {
226 return $this->table[$matches[$this->index]];
231 * Replacement array for FSS with fallback to strtr()
232 * Supports lazy initialisation of FSS resource
234 class ReplacementArray {
235 /*mostly private*/ var $data = false;
236 /*mostly private*/ var $fss = false;
239 * Create an object with the specified replacement array
240 * The array should have the same form as the replacement array for strtr()
242 function __construct( $data = array() ) {
243 $this->data = $data;
246 function __sleep() {
247 return array( 'data' );
250 function __wakeup() {
251 $this->fss = false;
255 * Set the whole replacement array at once
257 function setArray( $data ) {
258 $this->data = $data;
259 $this->fss = false;
262 function getArray() {
263 return $this->data;
267 * Set an element of the replacement array
269 function setPair( $from, $to ) {
270 $this->data[$from] = $to;
271 $this->fss = false;
274 function mergeArray( $data ) {
275 $this->data = array_merge( $this->data, $data );
276 $this->fss = false;
279 function merge( $other ) {
280 $this->data = array_merge( $this->data, $other->data );
281 $this->fss = false;
284 function replace( $subject ) {
285 if ( function_exists( 'fss_prep_replace' ) ) {
286 wfProfileIn( __METHOD__.'-fss' );
287 if ( $this->fss === false ) {
288 $this->fss = fss_prep_replace( $this->data );
290 $result = fss_exec_replace( $this->fss, $subject );
291 wfProfileOut( __METHOD__.'-fss' );
292 } else {
293 wfProfileIn( __METHOD__.'-strtr' );
294 $result = strtr( $subject, $this->data );
295 wfProfileOut( __METHOD__.'-strtr' );
297 return $result;