jenkins-bot has submitted this change and it was merged.

Change subject: Update Spyc to latest version
......................................................................


Update Spyc to latest version

https://github.com/mustangostang/spyc/commit/4e2a585

Ran scripts/yaml-tests.php with the following results:

array(2) {
  ["syck"]=>
  float(21.254214048386)
  ["spyc"]=>
  float(0.45939993858337)
}
array(2) {
  ["syck"]=>
  int(1125147)
  ["spyc"]=>
  int(1290993)
}
array(2) {
  ["syck"]=>
  int(1126734)
  ["spyc"]=>
  int(1290993)
}

This means syck is about 40x slower when using hhvm with no significant
difference in memory usage. Also the output files are identical except
a couple of things which I will fix in a separate commit.

Change-Id: Ibec61251c45e0eef1210c99761838aea8ca98421
---
M libs/spyc/spyc.php
1 file changed, 148 insertions(+), 47 deletions(-)

Approvals:
  Siebrand: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/libs/spyc/spyc.php b/libs/spyc/spyc.php
index b895628..fcc7200 100644
--- a/libs/spyc/spyc.php
+++ b/libs/spyc/spyc.php
@@ -2,7 +2,7 @@
 // @codingStandardsIgnoreFile
 /**
    * Spyc -- A Simple PHP YAML Class
-   * @version 0.5
+   * @version 0.5.1
    * @author Vlad Andersen <[email protected]>
    * @author Chris Wanstrath <[email protected]>
    * @link http://code.google.com/p/spyc/
@@ -30,6 +30,17 @@
    */
   function spyc_load_file ($file) {
     return Spyc::YAMLLoad($file);
+  }
+}
+
+if (!function_exists('spyc_dump')) {
+  /**
+   * Dumps array to YAML.
+   * @param array $data Array.
+   * @return string
+   */
+  function spyc_dump ($data) {
+    return Spyc::YAMLDump($data, false, false, true);
   }
 }
 
@@ -184,10 +195,11 @@
      * @param array $array PHP array
      * @param int $indent Pass in false to use the default, which is 2
      * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40)
+     * @param int $no_opening_dashes Do not start YAML file with "---\n"
      */
-  public static function YAMLDump($array,$indent = false,$wordwrap = false) {
+  public static function YAMLDump($array, $indent = false, $wordwrap = false, 
$no_opening_dashes = false) {
     $spyc = new Spyc;
-    return $spyc->dump($array,$indent,$wordwrap);
+    return $spyc->dump($array, $indent, $wordwrap, $no_opening_dashes);
   }
 
 
@@ -211,7 +223,7 @@
      * @param int $indent Pass in false to use the default, which is 2
      * @param int $wordwrap Pass in 0 for no wordwrap, false for default (40)
      */
-  public function dump($array,$indent = false,$wordwrap = false) {
+  public function dump($array,$indent = false,$wordwrap = false, 
$no_opening_dashes = false) {
     // Dumps to some very clean YAML.  We'll have to add some more features
     // and options soon.  And better support for folding.
 
@@ -229,7 +241,8 @@
     }
 
     // New YAML document
-    $string = "---\n";
+    $string = "";
+    if (!$no_opening_dashes) $string = "---\n";
 
     // Start at the base of the array and move through it.
     if ($array) {
@@ -313,14 +326,15 @@
     }
 
     if ($value === array()) $value = '[ ]';
-    if (in_array ($value, array ('true', 'TRUE', 'false', 'FALSE', 'y', 'Y', 
'n', 'N', 'null', 'NULL'), true)) {
-       $value = $this->_doLiteralBlock($value,$indent);
+    if ($value === "") $value = '""';
+    if (self::isTranslationWord($value)) {
+      $value = $this->_doLiteralBlock($value, $indent);
     }
     if (trim ($value) != $value)
        $value = $this->_doLiteralBlock($value,$indent);
 
     if (is_bool($value)) {
-       $value = ($value) ? "true" : "false";
+       $value = $value ? "true" : "false";
     }
 
     if ($value === null) $value = 'null';
@@ -383,10 +397,66 @@
     } else {
       if ($this->setting_dump_force_quotes && is_string ($value) && $value !== 
self::REMPTY)
         $value = '"' . $value . '"';
+      if (is_numeric($value) && is_string($value))
+        $value = '"' . $value . '"';
     }
 
 
     return $value;
+  }
+
+  private function isTrueWord($value) {
+    $words = self::getTranslations(array('true', 'on', 'yes', 'y'));
+    return in_array($value, $words, true);
+  }
+
+  private function isFalseWord($value) {
+    $words = self::getTranslations(array('false', 'off', 'no', 'n'));
+    return in_array($value, $words, true);
+  }
+
+  private function isNullWord($value) {
+    $words = self::getTranslations(array('null', '~'));
+    return in_array($value, $words, true);
+  }
+
+  private function isTranslationWord($value) {
+    return (
+      self::isTrueWord($value)  ||
+      self::isFalseWord($value) ||
+      self::isNullWord($value)
+    );
+  }
+
+  /**
+     * Coerce a string into a native type
+     * Reference: http://yaml.org/type/bool.html
+     * TODO: Use only words from the YAML spec.
+     * @access private
+     * @param $value The value to coerce
+     */
+  private function coerceValue(&$value) {
+    if (self::isTrueWord($value)) {
+      $value = true;
+    } else if (self::isFalseWord($value)) {
+      $value = false;
+    } else if (self::isNullWord($value)) {
+      $value = null;
+    }
+  }
+
+  /**
+     * Given a set of words, perform the appropriate translations on them to
+     * match the YAML 1.1 specification for type coercing.
+     * @param $words The words to translate
+     * @access private
+     */
+  private static function getTranslations(array $words) {
+    $result = array();
+    foreach ($words as $i) {
+      $result = array_merge($result, array(ucfirst($i), strtoupper($i), 
strtolower($i)));
+    }
+    return $result;
   }
 
 // LOADING FUNCTIONS
@@ -404,7 +474,7 @@
   private function loadWithSource($Source) {
     if (empty ($Source)) return array();
     if ($this->setting_use_syck_is_possible && function_exists ('syck_load')) {
-      $array = syck_load (implode ('', $Source));
+      $array = syck_load (implode ("\n", $Source));
       return is_array($array) ? $array : array();
     }
 
@@ -426,7 +496,7 @@
       if ($literalBlockStyle) {
         $line = rtrim ($line, $literalBlockStyle . " \n");
         $literalBlock = '';
-        $line .= $this->LiteralPlaceHolder;
+        $line .= ' '.$this->LiteralPlaceHolder;
         $literal_block_indent = strlen($Source[$i+1]) - 
strlen(ltrim($Source[$i+1]));
         while (++$i < $cnt && $this->literalBlockContinues($Source[$i], 
$this->indent)) {
           $literalBlock = $this->addLiteralLine($literalBlock, $Source[$i], 
$literalBlockStyle, $literal_block_indent);
@@ -434,17 +504,15 @@
         $i--;
       }
 
+      // Strip out comments
+      if (strpos ($line, '#')) {
+          $line = preg_replace('/\s*#([^"\']+)$/','',$line);
+      }
+
       while (++$i < $cnt && self::greedilyNeedNextLine($line)) {
         $line = rtrim ($line, " \n\t\r") . ' ' . ltrim ($Source[$i], " \t");
       }
       $i--;
-
-
-
-      if (strpos ($line, '#')) {
-        if (strpos ($line, '"') === false && strpos ($line, "'") === false)
-          $line = preg_replace('/\s+#(.+)$/','',$line);
-      }
 
       $lineArray = $this->_parseLine($line);
 
@@ -464,7 +532,7 @@
 
   private function loadFromSource ($input) {
     if (!empty($input) && strpos($input, "\n") === false && 
file_exists($input))
-    return file($input);
+      $input = file_get_contents($input);
 
     return $this->loadFromString($input);
   }
@@ -520,7 +588,7 @@
      * @return mixed
      */
   private function _toType($value) {
-    if ($value === '') return null;
+    if ($value === '') return "";
     $first_character = $value[0];
     $last_character = substr($value, -1, 1);
 
@@ -532,13 +600,13 @@
       $is_quoted = true;
     } while (0);
 
-    if ($is_quoted)
+    if ($is_quoted) {
+      $value = str_replace('\n', "\n", $value);
       return strtr(substr ($value, 1, -1), array ('\\"' => '"', '\'\'' => 
'\'', '\\\'' => '\''));
+    }
 
     if (strpos($value, ' #') !== false && !$is_quoted)
       $value = preg_replace('/\s+#(.+)$/','',$value);
-
-    if (!$is_quoted) $value = str_replace('\n', "\n", $value);
 
     if ($first_character == '[' && $last_character == ']') {
       // Take out strings sequences and mappings
@@ -592,15 +660,12 @@
       return $value;
     }
 
-    if (in_array($value,
-                 array('true', 'on', '+', 'yes', 'y', 'True', 'TRUE', 'On', 
'ON', 'YES', 'Yes', 'Y'))) {
-      return true;
+    if (is_numeric($value) && preg_match('/^0[xX][0-9a-fA-F]+$/', $value)) {
+      // Hexadecimal value.
+      return hexdec($value);
     }
 
-    if (in_array(strtolower($value),
-                 array('false', 'off', '-', 'no', 'n'))) {
-      return false;
-    }
+    $this->coerceValue($value);
 
     if (is_numeric($value)) {
       if ($value === '0') return 0;
@@ -626,6 +691,15 @@
     $seqs = array();
     $maps = array();
     $saved_strings = array();
+    $saved_empties = array();
+
+    // Check for empty strings
+    $regex = '/("")|(\'\')/';
+    if (preg_match_all($regex,$inline,$strings)) {
+      $saved_empties = $strings[0];
+      $inline  = preg_replace($regex,'YAMLEmpty',$inline);
+    }
+    unset($regex);
 
     // Check for strings
     $regex = '/(?:(")|(?:\'))((?(1)[^"]+|[^\']+))(?(1)"|\')/';
@@ -634,6 +708,8 @@
       $inline  = preg_replace($regex,'YAMLString',$inline);
     }
     unset($regex);
+
+    // echo $inline;
 
     $i = 0;
     do {
@@ -654,7 +730,8 @@
 
     } while (strpos ($inline, '[') !== false || strpos ($inline, '{') !== 
false);
 
-    $explode = explode(', ',$inline);
+    $explode = explode(',',$inline);
+    $explode = array_map('trim', $explode);
     $stringi = 0; $i = 0;
 
     while (1) {
@@ -696,6 +773,17 @@
       }
     }
 
+
+    // Re-add the empties
+    if (!empty($saved_empties)) {
+      foreach ($explode as $key => $value) {
+        while (strpos($value,'YAMLEmpty') !== false) {
+          $explode[$key] = preg_replace('/YAMLEmpty/', '', $value, 1);
+          $value = $explode[$key];
+        }
+      }
+    }
+
     $finished = true;
     foreach ($explode as $key => $value) {
       if (strpos($value,'YAMLSeq') !== false) {
@@ -707,6 +795,9 @@
       if (strpos($value,'YAMLString') !== false) {
         $finished = false; break;
       }
+      if (strpos($value,'YAMLEmpty') !== false) {
+        $finished = false; break;
+      }
     }
     if ($finished) break;
 
@@ -714,6 +805,7 @@
     if ($i > 10)
       break; // Prevent infinite loops.
     }
+
 
     return $explode;
   }
@@ -788,7 +880,7 @@
       if (!is_array ($_arr)) { $_arr = array (); }
 
       $_arr = array_merge ($_arr, $value);
-    } elseif ($key || $key === '' || $key === '0') {
+    } else if ($key || $key === '' || $key === '0') {
       if (!is_array ($_arr))
         $_arr = array ($key=>$value);
       else
@@ -863,7 +955,7 @@
      foreach ($lineArray as $k => $_) {
       if (is_array($_))
         $lineArray[$k] = $this->revertLiteralPlaceHolder ($_, $literalBlock);
-      elseif (substr($_, -1 * strlen ($this->LiteralPlaceHolder)) == 
$this->LiteralPlaceHolder)
+      else if (substr($_, -1 * strlen ($this->LiteralPlaceHolder)) == 
$this->LiteralPlaceHolder)
               $lineArray[$k] = rtrim ($literalBlock, " \r\n");
      }
      return $lineArray;
@@ -912,8 +1004,8 @@
 
 
   private function isArrayElement ($line) {
-    if (!$line) return false;
-    if ($line[0] != '-') return false;
+    if (!$line || !is_scalar($line)) return false;
+    if (substr($line, 0, 2) != '- ') return false;
     if (strlen ($line) > 3)
       if (substr($line,0,3) == '---') return false;
 
@@ -940,7 +1032,7 @@
   }
 
   private function startsMappedSequence ($line) {
-    return ($line[0] == '-' && substr ($line, -1, 1) == ':');
+    return (substr($line, 0, 2) == '- ' && substr ($line, -1, 1) == ':');
   }
 
   private function returnMappedSequence ($line) {
@@ -951,7 +1043,16 @@
     return array($array);
   }
 
+  private function checkKeysInValue($value) {
+    if (strchr('[{"\'', $value[0]) === false) {
+      if (strchr($value, ': ') !== false) {
+          throw new Exception('Too many keys: '.$value);
+      }
+    }
+  }
+
   private function returnMappedValue ($line) {
+    $this->checkKeysInValue($line);
     $array = array();
     $key         = self::unquote (trim(substr($line,0,-1)));
     $array[$key] = '';
@@ -973,7 +1074,7 @@
   private function returnKeyValuePair ($line) {
     $array = array();
     $key = '';
-    if (strpos ($line, ':')) {
+    if (strpos ($line, ': ')) {
       // It's a key/value pair most likely
       // If the key is in double quotes pull it out
       if (($line[0] == '"' || $line[0] == "'") && 
preg_match('/^(["\'](.*)["\'](\s)*:)/',$line,$matches)) {
@@ -981,10 +1082,10 @@
         $key   = $matches[2];
       } else {
         // Do some guesswork as to the key and the value
-        $explode = explode(':',$line);
-        $key     = trim($explode[0]);
-        array_shift($explode);
-        $value   = trim(implode(':',$explode));
+        $explode = explode(': ', $line);
+        $key     = trim(array_shift($explode));
+        $value   = trim(implode(': ', $explode));
+        $this->checkKeysInValue($value);
       }
       // Set the type of the value.  Int, string, etc
       $value = $this->_toType($value);
@@ -1003,6 +1104,9 @@
      $array = array();
      $value   = trim(substr($line,1));
      $value   = $this->_toType($value);
+     if ($this->isArrayElement($value)) {
+       $value = $this->returnArrayElement($value);
+     }
      $array[] = $value;
      return $array;
   }
@@ -1033,15 +1137,12 @@
 }
 
 // Enable use of Spyc from command line
-// The syntax is the following: php spyc.php spyc.yaml
-
-define ('SPYC_FROM_COMMAND_LINE', false);
+// The syntax is the following: php Spyc.php spyc.yaml
 
 do {
-  if (!SPYC_FROM_COMMAND_LINE) break;
+  if (PHP_SAPI != 'cli') break;
   if (empty ($_SERVER['argc']) || $_SERVER['argc'] < 2) break;
-  if (empty ($_SERVER['PHP_SELF']) || $_SERVER['PHP_SELF'] != 'spyc.php') 
break;
+  if (empty ($_SERVER['PHP_SELF']) || FALSE === strpos ($_SERVER['PHP_SELF'], 
'Spyc.php') ) break;
   $file = $argv[1];
-  printf ("Spyc loading file: %s\n", $file);
-  print_r (spyc_load_file ($file));
+  echo json_encode (spyc_load_file ($file));
 } while (0);

-- 
To view, visit https://gerrit.wikimedia.org/r/147173
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: Ibec61251c45e0eef1210c99761838aea8ca98421
Gerrit-PatchSet: 4
Gerrit-Project: mediawiki/extensions/Translate
Gerrit-Branch: master
Gerrit-Owner: Nikerabbit <[email protected]>
Gerrit-Reviewer: Hashar <[email protected]>
Gerrit-Reviewer: Nikerabbit <[email protected]>
Gerrit-Reviewer: Siebrand <[email protected]>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
[email protected]
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to