|
| 1 | +--- src/Neon/Node/StringNode.php 2022-03-10 03:04:26 |
| 2 | ++++ src/Neon/Node/StringNode.php 2024-08-26 14:53:45 |
| 3 | +@@ -79,27 +79,18 @@ |
| 4 | + |
| 5 | + public function toString(): string |
| 6 | + { |
| 7 | +- if (strpos($this->value, "\n") === false) { |
| 8 | +- return "'" . str_replace("'", "''", $this->value) . "'"; |
| 9 | ++ $res = json_encode($this->value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 10 | ++ if ($res === false) { |
| 11 | ++ throw new Nette\Neon\Exception('Invalid UTF-8 sequence: ' . $this->value); |
| 12 | ++ } |
| 13 | + |
| 14 | +- } elseif (preg_match('~\n[\t ]+\'{3}~', $this->value)) { |
| 15 | +- $s = json_encode($this->value, JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES); |
| 16 | +- $s = preg_replace_callback( |
| 17 | +- '#[^\\\\]|\\\\(.)#s', |
| 18 | +- function ($m) { |
| 19 | +- return ['n' => "\n", 't' => "\t", '"' => '"'][$m[1] ?? ''] ?? $m[0]; |
| 20 | +- }, |
| 21 | +- substr($s, 1, -1) |
| 22 | +- ); |
| 23 | +- $s = str_replace('"""', '""\"', $s); |
| 24 | +- $delim = '"""'; |
| 25 | +- |
| 26 | +- } else { |
| 27 | +- $s = $this->value; |
| 28 | +- $delim = "'''"; |
| 29 | ++ if (strpos($this->value, "\n") !== false) { |
| 30 | ++ $res = preg_replace_callback('#[^\\\\]|\\\\(.)#s', function ($m) { |
| 31 | ++ return ['n' => "\n\t", 't' => "\t", '"' => '"'][$m[1] ?? ''] ?? $m[0]; |
| 32 | ++ }, $res); |
| 33 | ++ $res = '"""' . "\n\t" . substr($res, 1, -1) . "\n" . '"""'; |
| 34 | + } |
| 35 | + |
| 36 | +- $s = preg_replace('#^(?=.)#m', "\t", $s); |
| 37 | +- return $delim . "\n" . $s . "\n" . $delim; |
| 38 | ++ return $res; |
| 39 | + } |
| 40 | + } |
0 commit comments