From cde4100b8a6ddecd9e88bfdfcf75c955612c2678 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Sat, 17 Oct 2020 14:13:36 -0400 Subject: [PATCH] Make correct termination of an ISO 2022-JP output string easier --- lib/Encoding/Encoder.php | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/lib/Encoding/Encoder.php b/lib/Encoding/Encoder.php index 09d0927..d3724cc 100644 --- a/lib/Encoding/Encoder.php +++ b/lib/Encoding/Encoder.php @@ -29,15 +29,13 @@ class Encoder { public function encode(iterable $codePoints): string { $oldMode = $this->mode; - $this->reset(); + $this->mode = self::MODE_ASCII; $out = ""; try { foreach ($codePoints as $codePoint) { $out .= $this->encodeChar($codePoint); } - if ($this->name === "ISO-2022-JP" && $this->mode !== self::MODE_ASCII) { - $out .= "\x1B\x28\x42"; - } + $out .= $this->finalize(); } finally { $this->mode = $oldMode; } @@ -178,7 +176,10 @@ class Encoder { } } - public function reset() { - $this->mode = self::MODE_ASCII; + public function finalize(): string { + if ($this->mode !== self::MODE_ASCII) { + return $this->modeSet(self::MODE_ASCII, ""); + } + return ""; } } \ No newline at end of file