Make WordPress Core


Ignore:
Timestamp:
11/12/2009 04:05:43 PM (16 years ago)
Author:
ryan
Message:

pomo performance improvements. Props nbachiyski. fixes #10165

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/pomo/streams.php

    r12079 r12174  
    44 * Based on the classes from Danilo Segan <danilo@kvota.net>
    55 *
    6  * @version $Id: streams.php 223 2009-09-07 21:20:13Z nbachiyski $
     6 * @version $Id: streams.php 293 2009-11-12 15:43:50Z nbachiyski $
    77 * @package pomo
    88 * @subpackage streams
    99 */
    1010
    11 
    12 if ( !class_exists( 'POMO_StringReader' ) ):
    13 /**
    14  * Provides file-like methods for manipulating a string instead
    15  * of a physical file.
    16  */
    17 class POMO_StringReader {
    18   var $_pos;
    19   var $_str;
    20 
    21     function POMO_StringReader($str = '') {
    22         $this->_str = $str;
     11if ( !class_exists( 'POMO_Reader' ) ):
     12class POMO_Reader {
     13   
     14    var $endian = 'little';
     15    var $_post = '';
     16   
     17    function POMO_Reader() {
     18        $this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
    2319        $this->_pos = 0;
    24         $this->is_overloaded = ((ini_get("mbstring.func_overload") & 2) != 0) && function_exists('mb_substr');
    25     }
    26 
    27     function _substr($string, $start, $length) {
    28         if ($this->is_overloaded) {
    29             return mb_substr($string,$start,$length,'ascii');
    30         } else {
    31             return substr($string,$start,$length);
    32         }
    33     }
    34    
    35     function _strlen($string) {
    36         if ($this->is_overloaded) {
    37             return mb_strlen($string,'ascii');
    38         } else {
    39             return strlen($string);
    40         }
    41     }
    42 
    43     function read($bytes) {
    44         $data = $this->_substr($this->_str, $this->_pos, $bytes);
    45         $this->_pos += $bytes;
    46         if ($this->_strlen($this->_str) < $this->_pos) $this->_pos = $this->_strlen($this->_str);
    47         return $data;
    48     }
    49 
    50     function seekto($pos) {
    51         $this->_pos = $pos;
    52         if ($this->_strlen($this->_str) < $this->_pos) $this->_pos = $this->_strlen($this->_str);
    53         return $this->_pos;
    54     }
    55 
    56     function pos() {
    57         return $this->_pos;
    58     }
    59 
    60     function length() {
    61         return $this->_strlen($this->_str);
    62     }
    63 
    64 }
    65 endif;
    66 
    67 if ( !class_exists( 'POMO_CachedFileReader' ) ):
    68 /**
    69  * Reads the contents of the file in the beginning.
    70  */
    71 class POMO_CachedFileReader extends POMO_StringReader {
    72     function POMO_CachedFileReader($filename) {
    73         parent::POMO_StringReader();
    74         $this->_str = file_get_contents($filename);
    75         if (false === $this->_str)
    76             return false;
    77         $this->_pos = 0;
    78     }
    79 }
    80 endif;
    81 
    82 if ( !class_exists( 'POMO_CachedIntFileReader' ) ):
    83 /**
    84  * Allows reading integers from a file.
    85  */
    86 class POMO_CachedIntFileReader extends POMO_CachedFileReader {
    87 
    88     var $endian = 'little';
    89 
    90     /**
    91      * Opens a file and caches it.
    92      *
    93      * @param $filename string name of the file to be opened
    94      * @param $endian string endianness of the words in the file, allowed
    95      *  values are 'little' or 'big'. Default value is 'little'
    96      */
    97     function POMO_CachedIntFileReader($filename, $endian = 'little') {
    98         $this->endian = $endian;
    99         parent::POMO_CachedFileReader($filename);
    100     }
    101 
     20    }
     21   
    10222    /**
    10323     * Sets the endianness of the file.
     
    11737    function readint32() {
    11838        $bytes = $this->read(4);
    119         if (4 != $this->_strlen($bytes))
     39        if (4 != $this->strlen($bytes))
    12040            return false;
    12141        $endian_letter = ('big' == $this->endian)? 'N' : 'V';
     
    13353    function readint32array($count) {
    13454        $bytes = $this->read(4 * $count);
    135         if (4*$count != $this->_strlen($bytes))
     55        if (4*$count != $this->strlen($bytes))
    13656            return false;
    13757        $endian_letter = ('big' == $this->endian)? 'N' : 'V';
    13858        return unpack($endian_letter.$count, $bytes);
    13959    }
    140 }
    141 endif;
     60   
     61   
     62    function substr($string, $start, $length) {
     63        if ($this->is_overloaded) {
     64            return mb_substr($string, $start, $length, 'ascii');
     65        } else {
     66            return substr($string, $start, $length);
     67        }
     68    }
     69   
     70    function strlen($string) {
     71        if ($this->is_overloaded) {
     72            return mb_strlen($string, 'ascii');
     73        } else {
     74            return strlen($string);
     75        }
     76    }
     77   
     78    function str_split($string, $chunk_size) {
     79        if (!function_exists('str_split')) {
     80            $length = $this->strlen($string);
     81            $out = array();
     82            for ($i = 0; $i < $length; $i += $chunk_size)
     83                $out[] = $this->substr($string, $i, $chunk_size);
     84            return $out;
     85        } else {
     86            return str_split( $string, $chunk_size );
     87        }
     88    }
     89   
     90       
     91    function pos() {
     92        return $this->_pos;
     93    }
     94
     95    function is_resource() {
     96        return true;
     97    }
     98   
     99    function close() {
     100        return true;
     101    }
     102}
     103endif;
     104
     105if ( !class_exists( 'POMO_FileReader' ) ):
     106class POMO_FileReader extends POMO_Reader {
     107    function POMO_FileReader($filename) {
     108        parent::POMO_Reader();
     109        $this->_f = fopen($filename, 'r');
     110    }
     111   
     112    function read($bytes) {
     113        return fread($this->_f, $bytes);
     114    }
     115   
     116    function seekto($pos) {
     117        if ( -1 == fseek($this->_f, $pos, SEEK_SET)) {
     118            return false;
     119        }
     120        $this->_pos = $pos;
     121        return true;
     122    }
     123   
     124    function is_resource() {
     125        return is_resource($this->_f);
     126    }
     127   
     128    function feof() {
     129        return feof($this->_f);
     130    }
     131   
     132    function close() {
     133        return fclose($this->_f);
     134    }
     135   
     136    function read_all() {
     137        $all = '';
     138        while ( !$this->feof() )
     139            $all .= $this->read(4096);
     140        return $all;
     141    }
     142}
     143endif;
     144
     145if ( !class_exists( 'POMO_StringReader' ) ):
     146/**
     147 * Provides file-like methods for manipulating a string instead
     148 * of a physical file.
     149 */
     150class POMO_StringReader extends POMO_Reader {
     151   
     152    var $_str = '';
     153   
     154    function POMO_StringReader($str = '') {
     155        parent::POMO_Reader();
     156        $this->_str = $str;
     157        $this->_pos = 0;
     158    }
     159
     160
     161    function read($bytes) {
     162        $data = $this->substr($this->_str, $this->_pos, $bytes);
     163        $this->_pos += $bytes;
     164        if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
     165        return $data;
     166    }
     167
     168    function seekto($pos) {
     169        $this->_pos = $pos;
     170        if ($this->strlen($this->_str) < $this->_pos) $this->_pos = $this->strlen($this->_str);
     171        return $this->_pos;
     172    }
     173
     174    function length() {
     175        return $this->strlen($this->_str);
     176    }
     177
     178    function read_all() {
     179        return $this->substr($this->_str, $this->_pos, $this->strlen($this->_str));
     180    }
     181   
     182}
     183endif;
     184
     185if ( !class_exists( 'POMO_CachedFileReader' ) ):
     186/**
     187 * Reads the contents of the file in the beginning.
     188 */
     189class POMO_CachedFileReader extends POMO_StringReader {
     190    function POMO_CachedFileReader($filename) {
     191        parent::POMO_StringReader();
     192        $this->_str = file_get_contents($filename);
     193        if (false === $this->_str)
     194            return false;
     195        $this->_pos = 0;
     196    }
     197}
     198endif;
     199
     200if ( !class_exists( 'POMO_CachedIntFileReader' ) ):
     201/**
     202 * Reads the contents of the file in the beginning.
     203 */
     204class POMO_CachedIntFileReader extends POMO_CachedFileReader {
     205    function POMO_CachedIntFileReader($filename) {
     206        parent::POMO_CachedFileReader($filename);
     207    }
     208}
     209endif;
Note: See TracChangeset for help on using the changeset viewer.