From c79e13144c6eb05d79bfc1d8e540a947acc1afe8 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Thu, 11 Nov 2010 16:10:16 +0000 Subject: [PATCH] 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 --- wp-includes/functions.php | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/wp-includes/functions.php b/wp-includes/functions.php index fcbe86e5b3..30cb0d6161 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -246,34 +246,31 @@ function maybe_unserialize( $original ) { */ function is_serialized( $data ) { // if it isn't a string, it isn't serialized - if ( !is_string( $data ) ) + if ( ! is_string( $data ) ) return false; $data = trim( $data ); if ( 'N;' == $data ) return true; - if ( function_exists('strpbrk') ) { - if ( strlen($data) > 1 && strpbrk($data,'adObis') == $data && $data[1] == ':' ) { - $badions = array(); - $badions[1] = $data[0]; - } else { - return false; - } - } elseif ( !preg_match( '/^([adObis]):/', $data, $badions ) ) { + $length = strlen( $data ); + if ( $length < 4 ) return false; - } - switch ( $badions[1] ) { + if ( ':' !== $data[1] ) + return false; + $lastc = $data[$length-1]; + if ( ';' !== $lastc && '}' !== $lastc ) + return false; + $token = $data[0]; + switch ( $token ) { + case 's' : + if ( '"' !== $data[$length-2] ) + return false; case 'a' : case 'O' : - case 's' : - if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) ) - return true; - break; + return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data ); case 'b' : case 'i' : case 'd' : - if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) ) - return true; - break; + return (bool) preg_match( "/^{$token}:[0-9.E-]+;\$/", $data ); } return false; }