If 'tables' is a string that starts with a space, treat it as user-enforced FROM...
[lhc/web/wiklou.git] / includes / StringUtils.php
1 <?php
2
3 class StringUtils {
4 /**
5 * Perform an operation equivalent to
6 *
7 * preg_replace( "!$startDelim(.*?)$endDelim!", $replace, $subject );
8 *
9 * except that it's worst-case O(N) instead of O(N^2)
10 *
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.
14 */
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 ) );
24 }
25 }
26 return $output;
27 }
28
29 /**
30 * Perform an operation equivalent to
31 *
32 * preg_replace_callback( "!$startDelim(.*)$endDelim!s$flags", $callback, $subject )
33 *
34 * This implementation is slower than hungryDelimiterReplace but uses far less
35 * memory. The delimiters are literal strings, not regular expressions.
36 *
37 * @param string $flags Regular expression flags
38 */
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();
54
55 while ( $inputPos < strlen( $subject ) &&
56 preg_match( "!($encStart)|($encEnd)!S$flags", $subject, $m, PREG_OFFSET_CAPTURE, $inputPos ) )
57 {
58 $tokenOffset = $m[0][1];
59 if ( $m[1][0] != '' ) {
60 if ( $foundStart &&
61 $strcmp( $endDelim, substr( $subject, $tokenOffset, $endLength ) ) == 0 )
62 {
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] );
69 }
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__ );
75 }
76
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;
88 }
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 );
100 }
101 $inputPos = $outputPos = $tokenOffset + $tokenLength;
102 } else {
103 throw new MWException( 'Invalid delimiter given to ' . __METHOD__ );
104 }
105 }
106 if ( $outputPos < strlen( $subject ) ) {
107 $output .= substr( $subject, $outputPos );
108 }
109 return $output;
110 }
111
112 /*
113 * Perform an operation equivalent to
114 *
115 * preg_replace( "!$startDelim(.*)$endDelim!$flags", $replace, $subject )
116 *
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
123 */
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 );
128 }
129
130 /**
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
136 */
137 static function explodeMarkup( $separator, $text ) {
138 $placeholder = "\x00";
139
140 // Remove placeholder instances
141 $text = str_replace( $placeholder, '', $text );
142
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 );
146
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 );
151 }
152
153 return $items;
154 }
155
156 /**
157 * Escape a string to make it suitable for inclusion in a preg_replace()
158 * replacement parameter.
159 *
160 * @param string $string
161 * @return string
162 */
163 static function escapeRegexReplacement( $string ) {
164 $string = str_replace( '\\', '\\\\', $string );
165 $string = str_replace( '$', '\\$', $string );
166 return $string;
167 }
168 }
169
170 /**
171 * Base class for "replacers", objects used in preg_replace_callback() and
172 * StringUtils::delimiterReplaceCallback()
173 */
174 class Replacer {
175 function cb() {
176 return array( &$this, 'replace' );
177 }
178 }
179
180 /**
181 * Class to replace regex matches with a string similar to that used in preg_replace()
182 */
183 class RegexlikeReplacer extends Replacer {
184 var $r;
185 function __construct( $r ) {
186 $this->r = $r;
187 }
188
189 function replace( $matches ) {
190 $pairs = array();
191 foreach ( $matches as $i => $match ) {
192 $pairs["\$$i"] = $match;
193 }
194 return strtr( $this->r, $pairs );
195 }
196
197 }
198
199 /**
200 * Class to perform secondary replacement within each replacement string
201 */
202 class DoubleReplacer extends Replacer {
203 function __construct( $from, $to, $index = 0 ) {
204 $this->from = $from;
205 $this->to = $to;
206 $this->index = $index;
207 }
208
209 function replace( $matches ) {
210 return str_replace( $this->from, $this->to, $matches[$this->index] );
211 }
212 }
213
214 /**
215 * Class to perform replacement based on a simple hashtable lookup
216 */
217 class HashtableReplacer extends Replacer {
218 var $table, $index;
219
220 function __construct( $table, $index = 0 ) {
221 $this->table = $table;
222 $this->index = $index;
223 }
224
225 function replace( $matches ) {
226 return $this->table[$matches[$this->index]];
227 }
228 }
229
230 /**
231 * Replacement array for FSS with fallback to strtr()
232 * Supports lazy initialisation of FSS resource
233 */
234 class ReplacementArray {
235 /*mostly private*/ var $data = false;
236 /*mostly private*/ var $fss = false;
237
238 /**
239 * Create an object with the specified replacement array
240 * The array should have the same form as the replacement array for strtr()
241 */
242 function __construct( $data = array() ) {
243 $this->data = $data;
244 }
245
246 function __sleep() {
247 return array( 'data' );
248 }
249
250 function __wakeup() {
251 $this->fss = false;
252 }
253
254 /**
255 * Set the whole replacement array at once
256 */
257 function setArray( $data ) {
258 $this->data = $data;
259 $this->fss = false;
260 }
261
262 function getArray() {
263 return $this->data;
264 }
265
266 /**
267 * Set an element of the replacement array
268 */
269 function setPair( $from, $to ) {
270 $this->data[$from] = $to;
271 $this->fss = false;
272 }
273
274 function mergeArray( $data ) {
275 $this->data = array_merge( $this->data, $data );
276 $this->fss = false;
277 }
278
279 function merge( $other ) {
280 $this->data = array_merge( $this->data, $other->data );
281 $this->fss = false;
282 }
283
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 );
289 }
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' );
296 }
297 return $result;
298 }
299 }
300
301 ?>