Faster is_serialized(). Props duck_, hakre, Denis-de-Bernardy. see #14429

git-svn-id: https://develop.svn.wordpress.org/trunk@16300 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ryan Boren 2010-11-11 16:10:16 +00:00
parent d0f30f75e5
commit c79e13144c
1 changed files with 15 additions and 18 deletions

View File

@ -246,34 +246,31 @@ function maybe_unserialize( $original ) {
*/ */
function is_serialized( $data ) { function is_serialized( $data ) {
// if it isn't a string, it isn't serialized // if it isn't a string, it isn't serialized
if ( !is_string( $data ) ) if ( ! is_string( $data ) )
return false; return false;
$data = trim( $data ); $data = trim( $data );
if ( 'N;' == $data ) if ( 'N;' == $data )
return true; return true;
if ( function_exists('strpbrk') ) { $length = strlen( $data );
if ( strlen($data) > 1 && strpbrk($data,'adObis') == $data && $data[1] == ':' ) { if ( $length < 4 )
$badions = array();
$badions[1] = $data[0];
} else {
return false; return false;
} if ( ':' !== $data[1] )
} elseif ( !preg_match( '/^([adObis]):/', $data, $badions ) ) { return false;
$lastc = $data[$length-1];
if ( ';' !== $lastc && '}' !== $lastc )
return false;
$token = $data[0];
switch ( $token ) {
case 's' :
if ( '"' !== $data[$length-2] )
return false; return false;
}
switch ( $badions[1] ) {
case 'a' : case 'a' :
case 'O' : case 'O' :
case 's' : return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
return true;
break;
case 'b' : case 'b' :
case 'i' : case 'i' :
case 'd' : case 'd' :
if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) ) return (bool) preg_match( "/^{$token}:[0-9.E-]+;\$/", $data );
return true;
break;
} }
return false; return false;
} }