* Compared to delimiterReplace(), this implementation is fast but memory-
* hungry and inflexible. The memory requirements are such that I don't
* recommend using it on anything but guaranteed small chunks of text.
+ *
+ * @param $startDelim
+ * @param $endDelim
+ * @param $replace
+ * @param $subject
+ *
+ * @return string
*/
static function hungryDelimiterReplace( $startDelim, $endDelim, $replace, $subject ) {
$segments = explode( $startDelim, $subject );
* This implementation is slower than hungryDelimiterReplace but uses far less
* memory. The delimiters are literal strings, not regular expressions.
*
- * @param string $flags Regular expression flags
+ * If the start delimiter ends with an initial substring of the end delimiter,
+ * e.g. in the case of C-style comments, the behaviour differs from the model
+ * regex. In this implementation, the end must share no characters with the
+ * start, so e.g. /*\/ is not considered to be both the start and end of a
+ * comment. /*\/xy/*\/ is considered to be a single comment with contents /xy/.
+ *
+ * @param $startDelim String: start delimiter
+ * @param $endDelim String: end delimiter
+ * @param $callback Callback: function to call on each match
+ * @param $subject String
+ * @param $flags String: regular expression flags
+ * @return string
*/
- # If the start delimiter ends with an initial substring of the end delimiter,
- # e.g. in the case of C-style comments, the behaviour differs from the model
- # regex. In this implementation, the end must share no characters with the
- # start, so e.g. /*/ is not considered to be both the start and end of a
- # comment. /*/xy/*/ is considered to be a single comment with contents /xy/.
static function delimiterReplaceCallback( $startDelim, $endDelim, $callback, $subject, $flags = '' ) {
$inputPos = 0;
$outputPos = 0;
}
if ( $tokenType == 'start' ) {
- $inputPos = $tokenOffset + $tokenLength;
# Only move the start position if we haven't already found a start
# This means that START START END matches outer pair
if ( !$foundStart ) {
# Found start
+ $inputPos = $tokenOffset + $tokenLength;
# Write out the non-matching section
$output .= substr( $subject, $outputPos, $tokenOffset - $outputPos );
$outputPos = $tokenOffset;
$contentPos = $inputPos;
$foundStart = true;
+ } else {
+ # Move the input position past the *first character* of START,
+ # to protect against missing END when it overlaps with START
+ $inputPos = $tokenOffset + 1;
}
} elseif ( $tokenType == 'end' ) {
if ( $foundStart ) {
return $output;
}
- /*
+ /**
* Perform an operation equivalent to
*
* preg_replace( "!$startDelim(.*)$endDelim!$flags", $replace, $subject )
*
- * @param string $startDelim Start delimiter regular expression
- * @param string $endDelim End delimiter regular expression
- * @param string $replace Replacement string. May contain $1, which will be
- * replaced by the text between the delimiters
- * @param string $subject String to search
- * @return string The string with the matches replaced
+ * @param $startDelim String: start delimiter regular expression
+ * @param $endDelim String: end delimiter regular expression
+ * @param $replace String: replacement string. May contain $1, which will be
+ * replaced by the text between the delimiters
+ * @param $subject String to search
+ * @param $flags String: regular expression flags
+ * @return String: The string with the matches replaced
*/
static function delimiterReplace( $startDelim, $endDelim, $replace, $subject, $flags = '' ) {
$replacer = new RegexlikeReplacer( $replace );
/**
* More or less "markup-safe" explode()
* Ignores any instances of the separator inside <...>
- * @param string $separator
- * @param string $text
+ * @param $separator String
+ * @param $text String
* @return array
*/
static function explodeMarkup( $separator, $text ) {
* Escape a string to make it suitable for inclusion in a preg_replace()
* replacement parameter.
*
- * @param string $string
- * @return string
+ * @param $string String
+ * @return String
*/
static function escapeRegexReplacement( $string ) {
$string = str_replace( '\\', '\\\\', $string );
/**
* Workalike for explode() with limited memory usage.
* Returns an Iterator
+ * @param $separator
+ * @param $subject
+ * @return \ArrayIterator|\ExplodeIterator
*/
static function explode( $separator, $subject ) {
if ( substr_count( $subject, $separator ) > 1000 ) {
return new ArrayIterator( explode( $separator, $subject ) );
}
}
-
- /**
- * Workalike for preg_split() with limited memory usage.
- * Returns an Iterator
- */
- static function preg_split( $pattern, $subject, $limit = -1, $flags = 0 ) {
- return new PregSplitIterator( $pattern, $subject, $limit, $flags );
- }
}
/**
}
}
-
-/**
- * An iterator which works exactly like:
- *
- * foreach ( preg_split( $pattern, $s, $limit, $flags ) as $element ) {
- * ...
- * }
- *
- * Except it doesn't use huge amounts of memory when $limit is -1
- *
- * The flag PREG_SPLIT_OFFSET_CAPTURE isn't supported.
- */
-class PregSplitIterator implements Iterator {
- // The subject string
- var $pattern, $subject, $originalLimit, $flags;
-
- // The last extracted group of items.
- var $smallArray;
-
- // The position on the iterator.
- var $curPos;
-
- const MAX_LIMIT = 100;
-
- /**
- * Construct a PregSplitIterator
- */
- function __construct( $pattern, $s, $limit, $flags) {
- $this->pattern = $pattern;
- $this->subject = $s;
- $this->originalLimit = $limit;
- $this->flags = $flags;
-
- $this->rewind();
- }
-
- private function effectiveLimit() {
- if ($this->originalLimit == -1) {
- return self::MAX_LIMIT + 1;
- } else if ($this->limit > self::MAX_LIMIT) {
- $this->limit -= self::MAX_LIMIT;
- return self::MAX_LIMIT + 1;
- } else {
- $old = $this->limit;
- $this->limit = 0;
- return $old;
- }
- }
-
- function rewind() {
- $this->curPos = 0;
- $this->limit = $this->originalLimit;
- if ($this->limit == -1) $this->limit = self::MAX_LIMIT;
- $this->smallArray = preg_split( $this->pattern, $this->subject, $this->effectiveLimit(), $this->flags);
- }
-
- function current() {
- return $this->smallArray[$this->curPos % self::MAX_LIMIT];
- }
-
- function key() {
- return $this->curPos;
- }
-
- function next() {
- $this->curPos++;
- if ( $this->curPos % self::MAX_LIMIT == 0 ) {
- # Last item contains the rest unsplitted.
- if ($this->limit > 0) {
- $this->smallArray = preg_split( $this->pattern, $this->smallArray[self::MAX_LIMIT], $this->effectiveLimit(), $this->flags);
- }
- }
- return;
- }
-
- function valid() {
- return $this->curPos % self::MAX_LIMIT < count($this->smallArray);
- }
-}