Skip to content

Commit

Permalink
Fixing parsing keys with quotes (issue #8).
Browse files Browse the repository at this point in the history
  • Loading branch information
vlad.andersen committed Sep 20, 2009
1 parent bcf9c80 commit 501cea8
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 4 deletions.
4 changes: 3 additions & 1 deletion examples/yaml-load.php
Expand Up @@ -20,4 +20,6 @@
echo '</pre>';


?>
echo '<pre>YAML Data dumped back:<br/>';
echo Spyc::YAMLDump($array);
echo '</pre>';
14 changes: 11 additions & 3 deletions spyc.php
Expand Up @@ -319,7 +319,7 @@ private function _dumpNode($key, $value, $indent, $previous_key = -1, $first_key
// It's a sequence
$string = $spaces.'- '.$value."\n";
} else {
if ($first_key===0) throw new Exception('Keys are all screwy. The first one was zero, now it\'s "'. $key .'"');
// if ($first_key===0) throw new Exception('Keys are all screwy. The first one was zero, now it\'s "'. $key .'"');
// It's mapped
if (strpos($key, ":") !== false) { $key = '"' . $key . '"'; }
$string = $spaces.$key.': '.$value."\n";
Expand Down Expand Up @@ -908,21 +908,29 @@ private function isLiteral ($line) {
}


private static function unquote ($value) {
if (!$value) return $value;
if (!is_string($value)) return $value;
if ($value[0] == '\'') return trim ($value, '\'');
if ($value[0] == '"') return trim ($value, '"');
return $value;
}

private function startsMappedSequence ($line) {
return ($line[0] == '-' && substr ($line, -1, 1) == ':');
}

private function returnMappedSequence ($line) {
$array = array();
$key = trim(substr($line,1,-1));
$key = self::unquote(trim(substr($line,1,-1)));
$array[$key] = array();
$this->delayedPath = array(strpos ($line, $key) + $this->indent => $key);
return array($array);
}

private function returnMappedValue ($line) {
$array = array();
$key = trim(substr($line,0,-1));
$key = self::unquote (trim(substr($line,0,-1)));
$array[$key] = '';
return $array;
}
Expand Down
4 changes: 4 additions & 0 deletions spyc.yaml
Expand Up @@ -187,6 +187,10 @@ noindent_records:
- record1: value1
- record2: value2

"a:1": [1000]
"a:2":
- 2000

# [Endloop]
endloop: |
Does this line in the end indeed make Spyc go to an infinite loop?
8 changes: 8 additions & 0 deletions tests/ParseTest.php
Expand Up @@ -278,6 +278,14 @@ public function testWerte() {
, $this->yaml['noindent_records']);
} */

public function testColonsInKeys() {
$this->assertSame (array (1000), $this->yaml['a:1']);
}

public function testColonsInKeys2() {
$this->assertSame (array (2000), $this->yaml['a:2']);
}

public function testSpecialCharacters() {
$this->assertSame ('[{]]{{]]', $this->yaml['special_characters']);
}
Expand Down

0 comments on commit 501cea8

Please sign in to comment.