Skip to content

Commit 22e384d

Browse files
committed
Encoder: uses more readable single quote strings
1 parent 0cf8118 commit 22e384d

File tree

3 files changed

+49
-24
lines changed

3 files changed

+49
-24
lines changed

src/Neon/Node/StringNode.php

+19-10
Original file line numberDiff line numberDiff line change
@@ -79,18 +79,27 @@ function (array $m): string {
7979

8080
public function toString(): string
8181
{
82-
$res = json_encode($this->value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
83-
if ($res === false) {
84-
throw new Nette\Neon\Exception('Invalid UTF-8 sequence: ' . $this->value);
85-
}
82+
if (strpos($this->value, "\n") === false) {
83+
return "'" . str_replace("'", "''", $this->value) . "'";
84+
85+
} elseif (preg_match('~\n[\t ]+\'{3}~', $this->value)) {
86+
$s = json_encode($this->value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES);
87+
$s = preg_replace_callback(
88+
'#[^\\\\]|\\\\(.)#s',
89+
function ($m) {
90+
return ['n' => "\n", 't' => "\t", '"' => '"'][$m[1] ?? ''] ?? $m[0];
91+
},
92+
substr($s, 1, -1)
93+
);
94+
$s = str_replace('"""', '""\"', $s);
95+
$delim = '"""';
8696

87-
if (strpos($this->value, "\n") !== false) {
88-
$res = preg_replace_callback('#[^\\\\]|\\\\(.)#s', function ($m) {
89-
return ['n' => "\n\t", 't' => "\t", '"' => '"'][$m[1] ?? ''] ?? $m[0];
90-
}, $res);
91-
$res = '"""' . "\n\t" . substr($res, 1, -1) . "\n" . '"""';
97+
} else {
98+
$s = $this->value;
99+
$delim = "'''";
92100
}
93101

94-
return $res;
102+
$s = preg_replace('#^(?=.)#m', "\t", $s);
103+
return $delim . "\n" . $s . "\n" . $delim;
95104
}
96105
}

tests/Neon/Encoder.phpt

+28-12
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ require __DIR__ . '/../bootstrap.php';
1515

1616

1717
Assert::same(
18-
'[true, "TRUE", "tRuE", "true", false, "FALSE", "fAlSe", "false", null, "NULL", "nUlL", "null", "yes", "no", "on", "off"]',
18+
"[true, 'TRUE', 'tRuE', 'true', false, 'FALSE', 'fAlSe', 'false', null, 'NULL', 'nUlL', 'null', 'yes', 'no', 'on', 'off']",
1919
Neon::encode([
2020
true, 'TRUE', 'tRuE', 'true',
2121
false, 'FALSE', 'fAlSe', 'false',
@@ -25,27 +25,47 @@ Assert::same(
2525
);
2626

2727
Assert::same(
28-
'[1, 1.0, 0, 0.0, -1, -1.2, "1", "1.0", "-1"]',
28+
"[1, 1.0, 0, 0.0, -1, -1.2, '1', '1.0', '-1']",
2929
Neon::encode([1, 1.0, 0, 0.0, -1, -1.2, '1', '1.0', '-1'])
3030
);
3131

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

3737
Assert::same(
38-
'[\, "\'", "\"", "\r", "\\\\ \r"]',
39-
Neon::encode(['\\', "'", '"', "\r", "\\ \r"])
38+
"[\\, '''', '\"', '''\n\n\n''', '''\n\t\\ \n\n''']",
39+
Neon::encode(['\\', "'", '"', "\n", "\\ \n"])
4040
);
4141

4242
Assert::same(
43-
"{multi: [\"\"\"\n\tone\n\ttwo\n\tthree\\\\ne \"'\t\n\"\"\"]}",
43+
"'''\n\taaa\n\t'''bbb\n'''",
44+
Neon::encode("aaa\n'''bbb")
45+
);
46+
47+
Assert::same(
48+
"\"\"\"\n\taaa\n\t \t'''bbb\n\"\"\"",
49+
Neon::encode("aaa\n \t'''bbb")
50+
);
51+
52+
Assert::same(
53+
"'''\n\taaa'''\n\tbbb\n'''",
54+
Neon::encode("aaa'''\nbbb")
55+
);
56+
57+
Assert::same(
58+
"\"\"\"\n\taaa\n\t \t'''bbb\n\t \t\"\"\\\"ccc\n\"\"\"",
59+
Neon::encode("aaa\n \t'''bbb\n \t\"\"\"ccc")
60+
);
61+
62+
Assert::same(
63+
"{multi: ['''\n\tone\n\ttwo\n\tthree\\ne \"'\t\n''']}",
4464
Neon::encode(['multi' => ["one\ntwo\nthree\\ne \"'\t"]])
4565
);
4666

4767
Assert::same(
48-
'["[", "]", "{", "}", ":", ": ", "=", "#"]',
68+
"['[', ']', '{', '}', ':', ': ', '=', '#']",
4969
Neon::encode(['[', ']', '{', '}', ':', ': ', '=', '#'])
5070
);
5171

@@ -92,7 +112,7 @@ Assert::same(
92112
);
93113

94114
Assert::same(
95-
'",žlu/ťoučký"',
115+
"',žlu/ťoučký'",
96116
Neon::encode(',žlu/ťoučký')
97117
);
98118

@@ -144,7 +164,3 @@ Assert::same(
144164
'[]',
145165
Neon::encode([], Neon::BLOCK)
146166
);
147-
148-
Assert::exception(function () {
149-
Neon::encode("a invalid utf8 char sequence: \xc2\x82\x28\xfc\xa1\xa1\xa1\xa1\xa1\xe2\x80\x82");
150-
}, Nette\Neon\Exception::class);

tests/Neon/fixtures/Parser.nodes.neon

+2-2
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ dash subblock:
2424
- a
2525
- b
2626

27-
text: """
27+
text: '''
2828
one
2929
two
30-
"""
30+
'''

0 commit comments

Comments
 (0)