Development

Changeset 2806

You must first sign up to be able to contribute.

Changeset 2806

Show
Ignore:
Timestamp:
11/25/06 08:15:29 (2 years ago)
Author:
fabien
Message:

majot speed improvements for Spyc + small changes

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/util/Spyc.class.php

    r2651 r2806  
    1616class YAMLNode 
    1717{ 
    18   /**#@+ 
    19    * @access public 
    20    * @var string 
    21    */  
    2218  public $parent; 
    2319  public $id; 
    24   /**#@+*/ 
    25   /**  
    26    * @access public 
    27    * @var mixed 
    28    */ 
    2920  public $data; 
    30   /**  
    31    * @access public 
    32    * @var int 
    33    */ 
    3421  public $indent; 
    35   /**  
    36    * @access public 
    37    * @var bool 
    38    */ 
    3922  public $children = false; 
    4023 
     24  static protected $lastNodeId = 0; 
     25 
    4126  /** 
    4227   * The constructor assigns the node a unique ID. 
    43    * @access public 
     28   * 
    4429   * @return void 
    4530   */ 
    46   public function YAMLNode() 
     31  public function __construct() 
    4732  { 
    48     $this->id = uniqid('')
     33    $this->id = ++self::$lastNodeId
    4934  } 
    5035} 
     
    7762   *   print_r($array); 
    7863   *  </code> 
    79    * @access public 
     64   * 
    8065   * @return array 
    8166   * @param string $input Path of YAML file or string containing YAML 
     
    10287   * you can turn off wordwrap by passing in 0. 
    10388   * 
    104    * @access public 
    10589   * @return string 
    10690   * @param array $array PHP array 
     
    126110   *   print_r($array); 
    127111   *  </code> 
    128    * @access public 
     112   * 
    129113   * @return array 
    130114   * @param string $input Path of YAML file or string containing YAML 
     
    146130 
    147131    // Initiate some objects and values 
    148     $base              = new YAMLNode
     132    $base              = new YAMLNode()
    149133    $base->indent      = 0; 
    150134    $this->_lastIndent = 0; 
     
    177161      { 
    178162        // Create a new node and get its indent 
    179         $node         = new YAMLNode
     163        $node         = new YAMLNode()
    180164        $node->indent = $this->_getIndent($line); 
    181165 
     
    332316   * you can turn off wordwrap by passing in 0. 
    333317   * 
    334    * @access public 
    335318   * @return string 
    336319   * @param array $array PHP array 
     
    374357  } 
    375358 
    376   /**** Private Properties ****/ 
    377  
    378   /**#@+ 
    379    * @access private 
    380    * @var mixed 
    381    */  
    382   private $_haveRefs; 
    383   private $_allNodes; 
    384   private $_lastIndent; 
    385   private $_lastNode; 
    386   private $_inBlock; 
    387   private $_isInline; 
    388   private $_dumpIndent; 
    389   private $_dumpWordWrap; 
    390   /**#@+*/ 
    391  
    392   /**** Private Methods ****/ 
     359  protected $_haveRefs; 
     360  protected $_allNodes; 
     361  protected $_lastIndent; 
     362  protected $_lastNode; 
     363  protected $_inBlock; 
     364  protected $_isInline; 
     365  protected $_dumpIndent; 
     366  protected $_dumpWordWrap; 
    393367 
    394368  /** 
    395369   * Attempts to convert a key / value array item to YAML 
    396    * @access private 
     370   * 
    397371   * @return string 
    398372   * @param $key The name of the key 
     
    400374   * @param $indent The indent of the current node 
    401375   */ 
    402    private function _yamlize($key, $value, $indent) 
     376   protected function _yamlize($key, $value, $indent) 
    403377   { 
    404378    if (is_array($value)) 
     
    423397  /** 
    424398   * Attempts to convert an array to YAML 
    425    * @access private 
     399   * 
    426400   * @return string 
    427401   * @param $array The array you want to convert 
    428402   * @param $indent The indent of the current level 
    429403   */ 
    430    private function _yamlizeArray($array, $indent) 
     404   protected function _yamlizeArray($array, $indent) 
    431405   { 
    432406    if (is_array($array)) 
     
    448422  /** 
    449423   * Returns YAML from a key and a value 
    450    * @access private 
     424   * 
    451425   * @return string 
    452426   * @param $key The name of the key 
     
    454428   * @param $indent The indent of the current node 
    455429   */ 
    456    private function _dumpNode($key, $value, $indent) 
     430   protected function _dumpNode($key, $value, $indent) 
    457431   { 
    458432    if (is_object($value)) 
     
    497471  /** 
    498472   * Creates a literal block for dumping 
    499    * @access private 
     473   * 
    500474   * @return string 
    501475   * @param $value  
    502476   * @param $indent int The value of the indent 
    503477   */  
    504    private function _doLiteralBlock($value, $indent) 
     478   protected function _doLiteralBlock($value, $indent) 
    505479   { 
    506480    $exploded = explode("\n", $value); 
     
    517491  /** 
    518492   * Folds a string of text, if necessary 
    519    * @access private 
     493   * 
    520494   * @return string 
    521495   * @param $value The string you wish to fold 
    522496   */ 
    523    private function _doFolding($value, $indent) 
     497   protected function _doFolding($value, $indent) 
    524498   { 
    525499    // Don't do anything if wordwrap is set to 0 
     
    544518  /** 
    545519   * Finds and returns the indentation of a YAML line 
    546    * @access private 
     520   * 
    547521   * @return int 
    548522   * @param string $line A line from the YAML file 
    549523   */ 
    550    private function _getIndent($line) 
     524   protected function _getIndent($line) 
    551525   { 
    552526    preg_match('/^\s{1,}/', $line, $match); 
     
    565539  /** 
    566540   * Parses YAML code and returns an array for a node 
    567    * @access private 
     541   * 
    568542   * @return array 
    569543   * @param string $line A line from the YAML file 
    570544   */ 
    571    private function _parseLine($line) 
     545   protected function _parseLine($line) 
    572546   { 
    573547    $line = trim($line);   
     
    631605  /** 
    632606   * Finds the type of the passed value, returns the value as the new type. 
    633    * @access private 
     607   * 
    634608   * @param string $value 
    635609   * @return mixed 
    636610   */ 
    637    private function _toType($value) 
     611   protected function _toType($value) 
    638612   { 
    639613    if (preg_match('/^("(.*)"|\'(.*)\')/', $value, $matches)) 
     
    720694  /** 
    721695   * Used in inlines to check for more inlines or quoted strings 
    722    * @access private 
     696   * 
    723697   * @return array 
    724698   */ 
    725    private function _inlineEscape($inline) 
     699   protected function _inlineEscape($inline) 
    726700   { 
    727701    // There's gotta be a cleaner way to do this... 
     
    802776  /** 
    803777   * Builds the PHP array from all the YAML nodes we've gathered 
    804    * @access private 
     778   * 
    805779   * @return array 
    806780   */ 
    807    private function _buildArray() 
     781   protected function _buildArray() 
    808782   { 
    809783    $trunk = array(); 
     
    831805  /** 
    832806   * Traverses node-space and sets references (& and *) accordingly 
    833    * @access private 
     807   * 
    834808   * @return bool 
    835809   */ 
    836    private function _linkReferences() 
     810   protected function _linkReferences() 
    837811   { 
    838812    if (is_array($this->_haveRefs)) 
     
    901875  /** 
    902876   * Finds the children of a node and aids in the building of the PHP array 
    903    * @access private 
     877   * 
    904878   * @param int $nid The id of the node whose children we're gathering 
    905879   * @return array 
    906880   */ 
    907    private function _gatherChildren($nid) 
     881   protected function _gatherChildren($nid) 
    908882   { 
    909883    $return = array(); 
     
    929903   * Turns a node's data and its children's data into a PHP array 
    930904   * 
    931    * @access private 
     905   * 
    932906   * @param array $node The node which you want to arrayize 
    933907   * @return boolean 
    934908   */ 
    935    private function _nodeArrayizeData(&$node) 
     909   protected function _nodeArrayizeData(&$node) 
    936910   { 
    937911    if (is_array($node->data) && $node->children == true) 
     
    966940  /** 
    967941   * Traverses node-space and copies references to / from this object. 
    968    * @access private 
     942   * 
    969943   * @param object $z A node whose references we wish to make real 
    970944   * @return bool 
    971945   */ 
    972    private function _makeReferences(&$z) 
     946   protected function _makeReferences(&$z) 
    973947   { 
    974948    // It is a reference 
     
    1001975   * http://us3.php.net/manual/en/function.array-merge.php#41394 
    1002976   * 
    1003    * @access private 
    1004977   * @param array $arr1 
    1005978   * @param array $arr2 
    1006979   * @return array 
    1007980   */ 
    1008   private function _array_kmerge($arr1,$arr2) 
    1009  
     981  protected function _array_kmerge($arr1, $arr2) 
     982 
    1010983    if (!is_array($arr1)) 
    1011984    { 
    1012985      $arr1 = array(); 
    1013986    } 
    1014  
    1015987    if (!is_array($arr2)) 
    1016988    { 
    1017       $arr2 = array();  
    1018     } 
    1019  
    1020     $keys1 = array_keys($arr1); 
    1021     $keys2 = array_keys($arr2); 
    1022     $keys  = array_merge($keys1, $keys2); 
    1023     $vals1 = array_values($arr1); 
    1024     $vals2 = array_values($arr2); 
    1025     $vals  = array_merge($vals1, $vals2); 
     989      $arr2 = array(); 
     990    } 
     991 
     992    $keys  = array_merge(array_keys($arr1), array_keys($arr2)); 
     993    $vals  = array_merge(array_values($arr1), array_values($arr2)); 
    1026994    $ret   = array(); 
    1027  
    1028     foreach($keys as $key) 
     995    foreach ($keys as $key) 
    1029996    { 
    1030997      list($unused, $val) = each($vals); 
    1031       // This is the good part!  If a key already exists, but it's part of a 
    1032       // sequence (an int), just keep addin numbers until we find a fresh one. 
    1033       if (isset($ret[$key]) and is_int($key)) 
    1034       { 
    1035         while (array_key_exists($key, $ret)) 
    1036         { 
    1037           $key++; 
    1038         } 
    1039       } 
    1040       $ret[$key] = $val; 
     998      if (isset($ret[$key]) && is_int($key)) 
     999      { 
     1000        $ret[] = $val; 
     1001      } 
     1002      else 
     1003      { 
     1004        $ret[$key] = $val; 
     1005      } 
    10411006    } 
    10421007