* A collection of static methods to play with strings.
*/
class StringUtils {
-
/**
* Test whether a string is valid UTF-8.
*
return false;
}
}
+
return true;
}
$output .= $replace . substr( $s, $endDelimPos + strlen( $endDelim ) );
}
}
+
return $output;
}
* @throws MWException
* @return string
*/
- static function delimiterReplaceCallback( $startDelim, $endDelim, $callback, $subject, $flags = '' ) {
+ static function delimiterReplaceCallback( $startDelim, $endDelim, $callback,
+ $subject, $flags = ''
+ ) {
$inputPos = 0;
$outputPos = 0;
$output = '';
$m = array();
while ( $inputPos < strlen( $subject ) &&
- preg_match( "!($encStart)|($encEnd)!S$flags", $subject, $m, PREG_OFFSET_CAPTURE, $inputPos ) )
- {
+ preg_match( "!($encStart)|($encEnd)!S$flags", $subject, $m, PREG_OFFSET_CAPTURE, $inputPos )
+ ) {
$tokenOffset = $m[0][1];
if ( $m[1][0] != '' ) {
if ( $foundStart &&
- $strcmp( $endDelim, substr( $subject, $tokenOffset, $endLength ) ) == 0 )
- {
+ $strcmp( $endDelim, substr( $subject, $tokenOffset, $endLength ) ) == 0
+ ) {
# An end match is present at the same location
$tokenType = 'end';
$tokenLength = $endLength;
$output .= call_user_func( $callback, array(
substr( $subject, $outputPos, $tokenOffset + $tokenLength - $outputPos ),
substr( $subject, $contentPos, $tokenOffset - $contentPos )
- ));
+ ) );
$foundStart = false;
} else {
# Non-matching end, write it out
if ( $outputPos < strlen( $subject ) ) {
$output .= substr( $subject, $outputPos );
}
+
return $output;
}
*/
static function delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags = '' ) {
$replacer = new RegexlikeReplacer( $replace );
+
return self::delimiterReplaceCallback( $startDelim, $endDelim,
$replacer->cb(), $subject, $flags );
}
static function escapeRegexReplacement( $string ) {
$string = str_replace( '\\', '\\\\', $string );
$string = str_replace( '$', '\\$', $string );
+
return $string;
}
* StringUtils::delimiterReplaceCallback()
*/
class Replacer {
-
/**
* @return array
*/
* Class to replace regex matches with a string similar to that used in preg_replace()
*/
class RegexlikeReplacer extends Replacer {
- var $r;
+ private $r;
/**
* @param string $r
foreach ( $matches as $i => $match ) {
$pairs["\$$i"] = $match;
}
+
return strtr( $this->r, $pairs );
}
-
}
/**
* Class to perform secondary replacement within each replacement string
*/
class DoubleReplacer extends Replacer {
-
/**
* @param $from
* @param $to
* Class to perform replacement based on a simple hashtable lookup
*/
class HashtableReplacer extends Replacer {
- var $table, $index;
+ private $table, $index;
/**
* @param $table
* Supports lazy initialisation of FSS resource
*/
class ReplacementArray {
- /*mostly private*/ var $data = false;
- /*mostly private*/ var $fss = false;
+ private $data = false;
+ private $fss = false;
/**
* Create an object with the specified replacement array
$result = strtr( $subject, $this->data );
wfProfileOut( __METHOD__ . '-strtr' );
}
+
return $result;
}
}
*/
class ExplodeIterator implements Iterator {
// The subject string
- var $subject, $subjectLength;
+ private $subject, $subjectLength;
// The delimiter
- var $delim, $delimLength;
+ private $delim, $delimLength;
// The position of the start of the line
- var $curPos;
+ private $curPos;
// The position after the end of the next delimiter
- var $endPos;
+ private $endPos;
// The current token
- var $current;
+ private $current;
/**
* Construct a DelimIterator
}
}
$this->refreshCurrent();
+
return $this->current;
}