Make WordPress Core

Changeset 16300


Ignore:
Timestamp:
11/11/2010 04:10:16 PM (16 years ago)
Author:
ryan
Message:

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

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/functions.php

    r16215 r16300  
    247247function is_serialized( $data ) {
    248248        // if it isn't a string, it isn't serialized
    249         if ( !is_string( $data ) )
     249        if ( ! is_string( $data ) )
    250250                return false;
    251251        $data = trim( $data );
    252252        if ( 'N;' == $data )
    253253                return true;
    254         if ( function_exists('strpbrk') ) {
    255                 if ( strlen($data) > 1 && strpbrk($data,'adObis') == $data && $data[1] == ':' ) {
    256                         $badions = array();
    257                         $badions[1] = $data[0];
    258                 } else {
    259                         return false;
    260                 }
    261         } elseif ( !preg_match( '/^([adObis]):/', $data, $badions ) ) {
     254        $length = strlen( $data );
     255        if ( $length < 4 )
    262256                return false;
    263         }
    264         switch ( $badions[1] ) {
     257        if ( ':' !== $data[1] )
     258                return false;
     259        $lastc = $data[$length-1];
     260        if ( ';' !== $lastc && '}' !== $lastc )
     261                return false;
     262        $token = $data[0];
     263        switch ( $token ) {
     264                case 's' :
     265                        if ( '"' !== $data[$length-2] )
     266                                return false;
    265267                case 'a' :
    266268                case 'O' :
    267                 case 's' :
    268                         if ( preg_match( "/^{$badions[1]}:[0-9]+:.*[;}]\$/s", $data ) )
    269                                 return true;
    270                         break;
     269                        return (bool) preg_match( "/^{$token}:[0-9]+:/s", $data );
    271270                case 'b' :
    272271                case 'i' :
    273272                case 'd' :
    274                         if ( preg_match( "/^{$badions[1]}:[0-9.E-]+;\$/", $data ) )
    275                                 return true;
    276                         break;
     273                        return (bool) preg_match( "/^{$token}:[0-9.E-]+;\$/", $data );
    277274        }
    278275        return false;
Note: See TracChangeset for help on using the changeset viewer.