Jump to content

User:Cyp/test

From Wikipedia, the free encyclopedia

If the running time of the $a.=$b operator depends only on the length of $b, and not on the length of $a, then this code might be faster.

Disclaimer: I don't know PHP and I'm tired. The strlen_or_whatever_its_called function is probably called something different.

function removeHTMLcomments( $text ) {
		$fname='Parser::removeHTMLcomments';
		wfProfileIn( $fname );
		$newtext = '';
		$start = 0;
		while (($comstart = strpos($text, '<!--'), $start) !== false) {
			$comend = strpos($text, '-->', $comstart + 4);
			if ($comend === false) {
				# Unterminated comment; bail out
				break;
			}

			$comend += 3;

			# Trim space and newline if the comment is both
			# preceded and followed by a newline
			$spaceStart = max($comstart - 1, 0);
			$spaceEnd = $comend;
			while (substr($text, $spaceStart, 1) === ' ' && $spaceStart > $start) {
				$spaceStart--;
				$spaceLen++;
			}
			while (substr($text, $spaceEnd, 1) === ' ')
				$spaceEnd++;
			if (substr($text, $spaceStart, 1) === "\n" and substr($text, $spaceEnd, 1) === "\n") {
				# Remove the comment, leading and trailing
				# spaces, and leave only one newline.
				$newtext .= substr($text, $start, $spaceStart - $start);
				$start = $spaceEnd;
			}
			else {
				# Remove just the comment.
				$newtext .= substr($text, $start, $comstart - $start);
				$start = $comend
			}
		}
		$newtext .= substr($text, $start, strlen_or_whatever_its_called($text) - $start);
		wfProfileOut( $fname );
		return $nwetext;
	}