From 423be718ba95b66ae1aa9122b5857aad96151edb Mon Sep 17 00:00:00 2001 From: Yo! Symfony Date: Sat, 7 Mar 2015 15:34:16 +0000 Subject: [PATCH 1/3] Support for literal string using '@'prefix --- README.md | 2 ++ src/TomlBuilder.php | 7 ++++++- tests/TomlBuilderTest.php | 14 ++++++++++++++ 3 files changed, 22 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ac69cc2..3934183 100644 --- a/README.md +++ b/README.md @@ -69,6 +69,7 @@ You can create inline TOML string with TomlBuilder. TomlBuilder uses Fluent inte ->addValue('name', "Toml", 'This is your name') ->addValue('newline', "This string has a \n new line character.") ->addValue('winPath', "C:\\Users\\nodejs\\templates") + ->addValue('literal', '@<\i\c*\s*>') // literals starts with '@'. ->addValue('unicode', 'unicode character: ' . json_decode('"\u03B4"')) ->addTable('data.bool') @@ -120,6 +121,7 @@ The result of this example: name = "Toml" #This is your name newline = "This string has a \n new line character." winPath = "C:\\Users\\nodejs\\templates" + literal = '<\i\c*\s*>' unicode = "unicode character: δ" [data.bool] diff --git a/src/TomlBuilder.php b/src/TomlBuilder.php index e98156d..5400b85 100644 --- a/src/TomlBuilder.php +++ b/src/TomlBuilder.php @@ -214,6 +214,10 @@ private function dumpValue($val) private function dumpString($val) { + if (0 === strpos($val, '@')) { + return "'".ltrim($val, '@')."'"; + } + $normalized = $this->normalizeString($val); if (false === $this->isStringValid($normalized)) { @@ -257,7 +261,7 @@ private function dumpComment($val) private function dumpDatetime($val) { - return $val->format('Y-m-d\TH:i:s\Z'); // Only full zulu form + return $val->format('Y-m-d\TH:i:s\Z'); // ZULU form } private function dumpInteger($val) @@ -418,6 +422,7 @@ private function isStringValid($val) $noSpecialCharacter = str_replace($allowed, '', $val); $noSpecialCharacter = preg_replace('/\\\\u([0-9a-fA-F]{4})/', '', $noSpecialCharacter); + $noSpecialCharacter = preg_replace('/\\\\u([0-9a-fA-F]{8})/', '', $noSpecialCharacter); $pos = strpos($noSpecialCharacter, '\\'); diff --git a/tests/TomlBuilderTest.php b/tests/TomlBuilderTest.php index edd121c..c22fd96 100644 --- a/tests/TomlBuilderTest.php +++ b/tests/TomlBuilderTest.php @@ -141,6 +141,20 @@ public function testStringEscapesDoubleQuote() $this->assertNotNull(Toml::Parse($result)); } + public function testKeyLiteralString() + { + $tb = new TomlBuilder(); + + $result = $tb->addValue('regex', "@<\i\c*\s*>") + ->getTomlString(); + + $array = Toml::Parse($result); + + $this->assertNotNull($array); + + $this->assertEquals('<\i\c*\s*>', $array['regex']); + } + public function testKeySpecialChars() { $tb = new TomlBuilder(); From cb6bea092275d39e3a5aefaf094d5ad7ff4a66f1 Mon Sep 17 00:00:00 2001 From: Yo! Symfony Date: Sat, 7 Mar 2015 15:55:38 +0000 Subject: [PATCH 2/3] Escape '@' with literal strings --- src/TomlBuilder.php | 2 +- tests/TomlBuilderTest.php | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/src/TomlBuilder.php b/src/TomlBuilder.php index 5400b85..0e35732 100644 --- a/src/TomlBuilder.php +++ b/src/TomlBuilder.php @@ -215,7 +215,7 @@ private function dumpValue($val) private function dumpString($val) { if (0 === strpos($val, '@')) { - return "'".ltrim($val, '@')."'"; + return "'".preg_replace('/@/', '', $val, 1)."'"; } $normalized = $this->normalizeString($val); diff --git a/tests/TomlBuilderTest.php b/tests/TomlBuilderTest.php index c22fd96..b1fe736 100644 --- a/tests/TomlBuilderTest.php +++ b/tests/TomlBuilderTest.php @@ -155,6 +155,20 @@ public function testKeyLiteralString() $this->assertEquals('<\i\c*\s*>', $array['regex']); } + public function testKeyLiteralStringEscapingAt() + { + $tb = new TomlBuilder(); + + $result = $tb->addValue('regex', "@@<\i\c*\s*>") + ->getTomlString(); + + $array = Toml::Parse($result); + + $this->assertNotNull($array); + + $this->assertEquals('@<\i\c*\s*>', $array['regex']); + } + public function testKeySpecialChars() { $tb = new TomlBuilder(); From 4ca73ceaa6c3f15d9b57c90cd587a543b34e4146 Mon Sep 17 00:00:00 2001 From: Yo! Symfony Date: Sat, 7 Mar 2015 16:09:02 +0000 Subject: [PATCH 3/3] Added version --- CHANGELOG.md | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a602fd3..4400343 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,11 @@ CHANGELOG ========= +0.3.1 (2015-03-07) +------------------ +* Added support to literal strings in TomlBuilder. -0.3.0 ------ +0.3.0 (2015-03-06) +------------------ * Support for TOML 0.4.0. * CS fixes.