Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove parser function output parsing when unneeded #9

Draft
wants to merge 6 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 25 additions & 34 deletions includes/ListFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -405,15 +405,14 @@ public static function lstcntRender( Parser $parser, PPFrame $frame, array $para
$list = ParserPower::expand( $frame, $params[0] ?? '' );

if ( $list === '' ) {
return [ '0', 'noparse' => false ];
return '0';
}

$sep = ParserPower::expand( $frame, $params[1] ?? ',', ParserPower::UNESCAPE );

$sep = $parser->getStripState()->unstripNoWiki( $sep );

$count = count( self::arrayTrimUnescape( self::explodeList( $sep, $list ) ) );
return [ $count, 'noparse' => false ];
return count( self::arrayTrimUnescape( self::explodeList( $sep, $list ) ) );
}

/**
Expand All @@ -428,7 +427,7 @@ public static function lstsepRender( Parser $parser, PPFrame $frame, array $para
$inList = ParserPower::expand( $frame, $params[0] ?? '' );

if ( $inList === '' ) {
return [ '', 'noparse' => false ];
return '';
}

$inSep = ParserPower::expand( $frame, $params[1] ?? ',', ParserPower::UNESCAPE );
Expand All @@ -452,7 +451,7 @@ public static function lstelemRender( Parser $parser, PPFrame $frame, array $par
$inList = ParserPower::expand( $frame, $params[0] ?? '' );

if ( $inList === '' ) {
return [ '', 'noparse' => false ];
return '';
}

$inSep = ParserPower::expand( $frame, $params[1] ?? ',', ParserPower::UNESCAPE );
Expand Down Expand Up @@ -482,7 +481,7 @@ public static function lstsubRender( Parser $parser, PPFrame $frame, array $para
$inList = ParserPower::expand( $frame, $params[0] ?? '' );

if ( $inList === '' ) {
return [ '', 'noparse' => false ];
return '';
}

$inSep = ParserPower::expand( $frame, $params[1] ?? ',', ParserPower::UNESCAPE );
Expand All @@ -507,7 +506,7 @@ public static function lstsubRender( Parser $parser, PPFrame $frame, array $para
if ( count( $values ) > 0 ) {
return [ implode( $outSep, $values ), 'noparse' => false ];
} else {
return [ '', 'noparse' => false ];
return '';
}
}

Expand All @@ -523,7 +522,7 @@ public static function lstfndRender( Parser $parser, PPFrame $frame, array $para
$list = ParserPower::expand( $frame, $params[1] ?? '' );

if ( $list === '' ) {
return [ '', 'noparse' => false ];
return '';
}

$item = ParserPower::expand( $frame, $params[0] ?? '', ParserPower::UNESCAPE );
Expand All @@ -547,7 +546,7 @@ public static function lstfndRender( Parser $parser, PPFrame $frame, array $para
}
}
}
return [ '', 'noparse' => false ];
return '';
}

/**
Expand All @@ -562,7 +561,7 @@ public static function lstindRender( Parser $parser, PPFrame $frame, array $para
$list = ParserPower::expand( $frame, $params[1] ?? '' );

if ( $list === '' ) {
return [ '', 'noparse' => false ];
return '';
}

$item = ParserPower::expand( $frame, $params[0] ?? '', ParserPower::UNESCAPE );
Expand All @@ -578,40 +577,32 @@ public static function lstindRender( Parser $parser, PPFrame $frame, array $para
if ( $options & self::INDEX_CS ) {
for ( $index = $count - 1; $index > -1; --$index ) {
if ( $values[$index] === $item ) {
return [ strval( ( $options & self::INDEX_NEG ) ? $index - $count : $index + 1 ),
'noparse' => false
];
return strval( ( $options & self::INDEX_NEG ) ? $index - $count : $index + 1 );
}
}
} else {
for ( $index = $count - 1; $index > -1; --$index ) {
if ( strtolower( $values[$index] ) === strtolower( $item ) ) {
return [ strval( ( $options & self::INDEX_NEG ) ? $index - $count : $index + 1 ),
'noparse' => false
];
return strval( ( $options & self::INDEX_NEG ) ? $index - $count : $index + 1 );
}
}
}
} else {
if ( $options & self::INDEX_CS ) {
for ( $index = 0; $index < $count; ++$index ) {
if ( $values[$index] === $item ) {
return [ strval( ( $options & self::INDEX_NEG ) ? $index - $count : $index + 1 ),
'noparse' => false
];
return strval( ( $options & self::INDEX_NEG ) ? $index - $count : $index + 1 );
}
}
} else {
for ( $index = 0; $index < $count; ++$index ) {
if ( strtolower( $values[$index] ) === strtolower( $item ) ) {
return [ strval( ( $options & self::INDEX_NEG ) ? $index - $count : $index + 1 ),
'noparse' => false
];
return strval( ( $options & self::INDEX_NEG ) ? $index - $count : $index + 1 );
}
}
}
}
return [ '', 'noparse' => false ];
return '';
}

/**
Expand Down Expand Up @@ -681,7 +672,7 @@ public static function lstjoinRender( Parser $parser, PPFrame $frame, array $par
$inList2 = ParserPower::expand( $frame, $params[2] ?? '' );

if ( $inList1 === '' && $inList2 === '' ) {
return [ '', 'noparse' => false ];
return '';
}

if ( $inList1 === '' ) {
Expand Down Expand Up @@ -1113,7 +1104,7 @@ public static function lstfltrRender( Parser $parser, PPFrame $frame, array $par
$inList = ParserPower::expand( $frame, $params[2] ?? '' );

if ( $inList === '' ) {
return [ '', 'noparse' => false ];
return '';
}

$values = ParserPower::expand( $frame, $params[0] ?? '' );
Expand All @@ -1132,7 +1123,7 @@ public static function lstfltrRender( Parser $parser, PPFrame $frame, array $par
if ( count( $outValues ) > 0 ) {
return [ implode( $outSep, $outValues ), 'noparse' => false ];
} else {
return [ '', 'noparse' => false ];
return '';
}
}

Expand All @@ -1150,7 +1141,7 @@ public static function lstrmRender( Parser $parser, PPFrame $frame, array $param
$inList = ParserPower::expand( $frame, $params[1] ?? '' );

if ( $inList === '' ) {
return [ '', 'noparse' => false ];
return '';
}

$value = ParserPower::expand( $frame, $params[0] ?? '' );
Expand All @@ -1168,7 +1159,7 @@ public static function lstrmRender( Parser $parser, PPFrame $frame, array $param
if ( count( $outValues ) > 0 ) {
return [ implode( $outSep, $outValues ), 'noparse' => false ];
} else {
return [ '', 'noparse' => false ];
return '';
}
}

Expand Down Expand Up @@ -1199,7 +1190,7 @@ public static function lstcntuniqRender( Parser $parser, PPFrame $frame, array $
$inList = ParserPower::expand( $frame, $params[0] ?? '' );

if ( $inList === '' ) {
return [ '0', 'noparse' => false ];
return '0';
}

$sep = ParserPower::expand( $frame, $params[1] ?? ',', ParserPower::UNESCAPE );
Expand All @@ -1210,7 +1201,7 @@ public static function lstcntuniqRender( Parser $parser, PPFrame $frame, array $

$values = self::arrayTrimUnescape( self::explodeList( $sep, $inList ) );
$values = self::reduceToUniqueValues( $values, $csOption );
return [ strval( count( $values ) ), 'noparse' => false ];
return strval( count( $values ) );
}

/**
Expand Down Expand Up @@ -1386,7 +1377,7 @@ public static function lstuniqRender( Parser $parser, PPFrame $frame, array $par
$inList = ParserPower::expand( $frame, $params[0] ?? '' );

if ( $inList === '' ) {
return [ '', 'noparse' => false ];
return '';
}

$inSep = ParserPower::expand( $frame, $params[1] ?? ',', ParserPower::UNESCAPE );
Expand Down Expand Up @@ -1697,7 +1688,7 @@ public static function lstsrtRender( Parser $parser, PPFrame $frame, array $para
$inList = ParserPower::expand( $frame, $params[0] ?? '' );

if ( $inList === '' ) {
return [ '', 'noparse' => false ];
return '';
}

$inSep = ParserPower::expand( $frame, $params[1] ?? ',', ParserPower::UNESCAPE );
Expand Down Expand Up @@ -1986,7 +1977,7 @@ public static function lstmapRender( Parser $parser, PPFrame $frame, array $para
$inList = ParserPower::expand( $frame, $params[0] ?? '' );

if ( $inList === '' ) {
return [ '', 'noparse' => false ];
return '';
}

$inSep = ParserPower::expand( $frame, $params[1] ?? ',', ParserPower::UNESCAPE );
Expand Down Expand Up @@ -2032,7 +2023,7 @@ public static function lstmaptempRender( Parser $parser, PPFrame $frame, array $
$inList = ParserPower::expand( $frame, $params[0] ?? '' );

if ( $inList === '' ) {
return [ '', 'noparse' => false ];
return '';
}

$template = ParserPower::expand( $frame, $params[1] ?? '' );
Expand Down
28 changes: 11 additions & 17 deletions includes/SimpleFunctions.php
Original file line number Diff line number Diff line change
Expand Up @@ -62,9 +62,7 @@ public static function setup( &$parser ) {
* @return array The function output along with relevant parser options.
*/
public static function trimRender( Parser $parser, PPFrame $frame, array $params ) {
$text = ParserPower::expand( $frame, $params[0] ?? '' );

return [ $text, 'noparse' => false ];
return ParserPower::expand( $frame, $params[0] ?? '' );
}

/**
Expand All @@ -79,7 +77,7 @@ public static function trimRender( Parser $parser, PPFrame $frame, array $params
public static function uescRender( Parser $parser, PPFrame $frame, array $params ) {
$text = ParserPower::expand( $frame, $params[0] ?? '', ParserPower::UNESCAPE );

return [ $text, 'noparse' => false ];
return [ $text, 'noparse' => $text === '' ];
}

/**
Expand Down Expand Up @@ -108,9 +106,9 @@ public static function uescnowikiRender( Parser $parser, PPFrame $frame, array $
* @return array The function output along with relevant parser options.
*/
public static function trimuescRender( Parser $parser, PPFrame $frame, array $params ) {
$text = ParserPower::expand( $frame, $params[0] ?? '', ParserPower::UNESCAPE );
$text = trim( ParserPower::expand( $frame, $params[0] ?? '', ParserPower::UNESCAPE ) );

return [ trim( $text ), 'noparse' => false ];
return [ $text, 'noparse' => $text === '' ];
}

/**
Expand All @@ -127,7 +125,7 @@ public static function trimuescRender( Parser $parser, PPFrame $frame, array $pa
public static function linkpageRender( $text, array $attribs, Parser $parser, PPFrame $frame ) {
$text = $parser->replaceVariables( $text, $frame );

if ( $text ) {
if ( $text !== '' ) {
$text = preg_replace_callback( '/\[\[(.*?)\]\]/', [ __CLASS__, 'linkpageReplace' ], $text );
}

Expand Down Expand Up @@ -192,11 +190,7 @@ public static function linktextReplace( $matches ) {
* @return array The function output along with relevant parser options.
*/
public static function escRender( $text, array $attribs, Parser $parser, PPFrame $frame ) {
$text = ParserPower::escape( $text );

$text = $parser->replaceVariables( $text, $frame );

return [ $text, 'markerType' => 'none' ];
return [ ParserPower::escape( $text ), 'markerType' => 'none' ];
}

/**
Expand Down Expand Up @@ -232,11 +226,11 @@ public static function orRender( Parser $parser, PPFrame $frame, array $params )
$inValue = ParserPower::expand( $frame, $param );

if ( $inValue !== '' ) {
return [ $inValue, 'noparse' => false ];
return $inValue;
}
}

return [ '', 'noparse' => false ];
return '';
}

/**
Expand All @@ -256,7 +250,7 @@ public static function ueorRender( Parser $parser, PPFrame $frame, array $params
}
}

return [ '', 'noparse' => false ];
return '';
}

/**
Expand Down Expand Up @@ -332,7 +326,7 @@ public static function ueswitchRender( Parser $parser, PPFrame $frame, array $pa
$switchKey = isset( $params[0] ) ? ParserPower::expand( $frame, array_shift( $params ), ParserPower::UNESCAPE ) : '';

if ( count( $params ) === 0 ) {
return [ '', 'noparse' => false ];
return '';
}

$lastItem = $frame->expand( $params[count( $params ) - 1] );
Expand Down Expand Up @@ -494,7 +488,7 @@ public static function arraymaptemplateRender( $parser, $frame, $args ) {

public static function argmapRender( Parser $parser, PPFrame $frame, array $args ) {
if ( !isset( $args[0] ) ) {
return [ '', 'noparse' => false ];
return '';
}

// sort arguments, this is to disregard the position of named arguments
Expand Down