From 60a5487e46e9c509c0ddbd7924520ce15151fd27 Mon Sep 17 00:00:00 2001 From: "J. King" Date: Tue, 16 Mar 2021 18:58:18 -0400 Subject: [PATCH] Fix spanning with single-byte encodings --- lib/Encoding/SingleByteEncoding.php | 7 +++---- lib/Encoding/XUserDefined.php | 8 +++----- 2 files changed, 6 insertions(+), 9 deletions(-) diff --git a/lib/Encoding/SingleByteEncoding.php b/lib/Encoding/SingleByteEncoding.php index 2a69090..d43c37d 100644 --- a/lib/Encoding/SingleByteEncoding.php +++ b/lib/Encoding/SingleByteEncoding.php @@ -16,6 +16,7 @@ abstract class SingleByteEncoding extends AbstractEncoding implements Coder, Dec return ""; } $this->posChar++; + $this->posByte++; $p = ord($b); if ($p < 0x80) { // if the byte is an ASCII character or end of input, simply return it @@ -32,6 +33,7 @@ abstract class SingleByteEncoding extends AbstractEncoding implements Coder, Dec return false; } $this->posChar++; + $this->posByte++; $p = ord($b); if ($p < 0x80) { // if the byte is an ASCII character or end of input, simply return it @@ -62,6 +64,7 @@ abstract class SingleByteEncoding extends AbstractEncoding implements Coder, Dec $distance = abs($distance); while ($this->posChar > 0 && $distance > 0) { $this->posChar--; + $this->posByte--; $distance--; } return $distance; @@ -76,10 +79,6 @@ abstract class SingleByteEncoding extends AbstractEncoding implements Coder, Dec return 0; } - public function posByte(): int { - return $this->posChar; - } - public function lenChar(): int { return $this->lenByte; } diff --git a/lib/Encoding/XUserDefined.php b/lib/Encoding/XUserDefined.php index 4f1e840..7bb5719 100644 --- a/lib/Encoding/XUserDefined.php +++ b/lib/Encoding/XUserDefined.php @@ -21,6 +21,7 @@ class XUserDefined extends AbstractEncoding implements Coder, Decoder { return ""; } $this->posChar++; + $this->posByte++; $p = ord($b); if ($p < 0x80) { // if the byte is an ASCII character or end of input, simply return it @@ -43,6 +44,7 @@ class XUserDefined extends AbstractEncoding implements Coder, Decoder { return false; } $this->posChar++; + $this->posByte++; $p = ord($b); if ($p < 0x80) { // if the byte is an ASCII character or end of input, simply return it @@ -69,6 +71,7 @@ class XUserDefined extends AbstractEncoding implements Coder, Decoder { $distance = abs($distance); while ($this->posChar > 0 && $distance > 0) { $this->posChar--; + $this->posByte--; $distance--; } return $distance; @@ -95,11 +98,6 @@ class XUserDefined extends AbstractEncoding implements Coder, Decoder { return 0; } - /** Returns the current byte position of the decoder */ - public function posByte(): int { - return $this->posChar; - } - /** Calculates the length of the string in code points * * Note that this may involve processing to the end of the string