Skip to content

Commit

Permalink
Encoder: uses more readable single quote strings
Browse files Browse the repository at this point in the history
  • Loading branch information
dg committed Mar 10, 2022
1 parent 0cf8118 commit 22e384d
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 24 deletions.
29 changes: 19 additions & 10 deletions src/Neon/Node/StringNode.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,27 @@ function (array $m): string {

public function toString(): string
{
$res = json_encode($this->value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
if ($res === false) {
throw new Nette\Neon\Exception('Invalid UTF-8 sequence: ' . $this->value);
}
if (strpos($this->value, "\n") === false) {
return "'" . str_replace("'", "''", $this->value) . "'";

} elseif (preg_match('~\n[\t ]+\'{3}~', $this->value)) {
$s = json_encode($this->value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
$s = preg_replace_callback(
'#[^\\\\]|\\\\(.)#s',
function ($m) {
return ['n' => "\n", 't' => "\t", '"' => '"'][$m[1] ?? ''] ?? $m[0];
},
substr($s, 1, -1)
);
$s = str_replace('"""', '""\"', $s);
$delim = '"""';

if (strpos($this->value, "\n") !== false) {
$res = preg_replace_callback('#[^\\\\]|\\\\(.)#s', function ($m) {
return ['n' => "\n\t", 't' => "\t", '"' => '"'][$m[1] ?? ''] ?? $m[0];
}, $res);
$res = '"""' . "\n\t" . substr($res, 1, -1) . "\n" . '"""';
} else {
$s = $this->value;
$delim = "'''";
}

return $res;
$s = preg_replace('#^(?=.)#m', "\t", $s);
return $delim . "\n" . $s . "\n" . $delim;
}
}
40 changes: 28 additions & 12 deletions tests/Neon/Encoder.phpt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ require __DIR__ . '/../bootstrap.php';


Assert::same(
'[true, "TRUE", "tRuE", "true", false, "FALSE", "fAlSe", "false", null, "NULL", "nUlL", "null", "yes", "no", "on", "off"]',
"[true, 'TRUE', 'tRuE', 'true', false, 'FALSE', 'fAlSe', 'false', null, 'NULL', 'nUlL', 'null', 'yes', 'no', 'on', 'off']",
Neon::encode([
true, 'TRUE', 'tRuE', 'true',
false, 'FALSE', 'fAlSe', 'false',
Expand All @@ -25,27 +25,47 @@ Assert::same(
);

Assert::same(
'[1, 1.0, 0, 0.0, -1, -1.2, "1", "1.0", "-1"]',
"[1, 1.0, 0, 0.0, -1, -1.2, '1', '1.0', '-1']",
Neon::encode([1, 1.0, 0, 0.0, -1, -1.2, '1', '1.0', '-1'])
);

Assert::same(
'["1", "0xAA", "0o12", "0b110", "+1", "-1", ".50", "1e10"]',
"['1', '0xAA', '0o12', '0b110', '+1', '-1', '.50', '1e10']",
Neon::encode(['1', '0xAA', '0o12', '0b110', '+1', '-1', '.50', '1e10'])
);

Assert::same(
'[\, "\'", "\"", "\r", "\\\\ \r"]',
Neon::encode(['\\', "'", '"', "\r", "\\ \r"])
"[\\, '''', '\"', '''\n\n\n''', '''\n\t\\ \n\n''']",
Neon::encode(['\\', "'", '"', "\n", "\\ \n"])
);

Assert::same(
"{multi: [\"\"\"\n\tone\n\ttwo\n\tthree\\\\ne \"'\t\n\"\"\"]}",
"'''\n\taaa\n\t'''bbb\n'''",
Neon::encode("aaa\n'''bbb")
);

Assert::same(
"\"\"\"\n\taaa\n\t \t'''bbb\n\"\"\"",
Neon::encode("aaa\n \t'''bbb")
);

Assert::same(
"'''\n\taaa'''\n\tbbb\n'''",
Neon::encode("aaa'''\nbbb")
);

Assert::same(
"\"\"\"\n\taaa\n\t \t'''bbb\n\t \t\"\"\\\"ccc\n\"\"\"",
Neon::encode("aaa\n \t'''bbb\n \t\"\"\"ccc")
);

Assert::same(
"{multi: ['''\n\tone\n\ttwo\n\tthree\\ne \"'\t\n''']}",
Neon::encode(['multi' => ["one\ntwo\nthree\\ne \"'\t"]])
);

Assert::same(
'["[", "]", "{", "}", ":", ": ", "=", "#"]',
"['[', ']', '{', '}', ':', ': ', '=', '#']",
Neon::encode(['[', ']', '{', '}', ':', ': ', '=', '#'])
);

Expand Down Expand Up @@ -92,7 +112,7 @@ Assert::same(
);

Assert::same(
'",žlu/ťoučký"',
"',žlu/ťoučký'",
Neon::encode(',žlu/ťoučký')
);

Expand Down Expand Up @@ -144,7 +164,3 @@ Assert::same(
'[]',
Neon::encode([], Neon::BLOCK)
);

Assert::exception(function () {
Neon::encode("a invalid utf8 char sequence: \xc2\x82\x28\xfc\xa1\xa1\xa1\xa1\xa1\xe2\x80\x82");
}, Nette\Neon\Exception::class);
4 changes: 2 additions & 2 deletions tests/Neon/fixtures/Parser.nodes.neon
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ dash subblock:
- a
- b

text: """
text: '''
one
two
"""
'''

0 comments on commit 22e384d

Please sign in to comment.