diff --git a/lib/Encoding/AbstractEncoding.php b/lib/Encoding/AbstractEncoding.php index c5e28bf..5ba2897 100644 --- a/lib/Encoding/AbstractEncoding.php +++ b/lib/Encoding/AbstractEncoding.php @@ -165,21 +165,37 @@ abstract class AbstractEncoding implements Decoder { } public function asciiSpan(string $mask, int $length = null): string { - $mask = preg_replace('/[\x80-\xFF]/gs', "", $mask); - $len = strspn($this->string, $mask, $this->posByte, $length); - $out = substr($this->string, $this->posByte, $len); - $this->posByte += $len; - $this->posChar += $len; - return $out; + $mask = preg_replace('/[\x80-\xFF]/s', "", $mask); + if ($length !== null) { + $len = strspn($this->string, $mask, $this->posByte, $length); + } else { + $len = strspn($this->string, $mask, $this->posByte); + } + if ($len) { + $out = substr($this->string, $this->posByte, $len); + $this->posByte += $len; + $this->posChar += $len; + return $out; + } else { + return ""; + } } public function asciiSpanNot(string $mask, int $length = null): string { $mask .= self::HIGH_BYTES; - $len = strcspn($this->string, $mask, $this->posByte, $length); - $out = substr($this->string, $this->posByte, $len); - $this->posByte += $len; - $this->posChar += $len; - return $out; + if ($length !== null) { + $len = strcspn($this->string, $mask, $this->posByte, $length); + } else { + $len = strcspn($this->string, $mask, $this->posByte); + } + if ($len) { + $out = substr($this->string, $this->posByte, $len); + $this->posByte += $len; + $this->posChar += $len; + return $out; + } else { + return ""; + } } /** Returns a copy of the decoder's state to keep in memory */ diff --git a/lib/Encoding/ISO2022JP.php b/lib/Encoding/ISO2022JP.php index 1ad84e9..ad38a74 100644 --- a/lib/Encoding/ISO2022JP.php +++ b/lib/Encoding/ISO2022JP.php @@ -186,19 +186,27 @@ class ISO2022JP extends AbstractEncoding implements ModalCoder, Decoder { public function asciiSpan(string $mask, int $length = null): string { if ($this->mode === self::ASCII_STATE) { - $exc = '/[\x0E\x0F\x1B\x80-\xFF]/gs'; + $exc = '/[\x0E\x0F\x1B\x80-\xFF]/s'; } elseif ($this->mode === self::ROMAN_STATE) { - $exc = '/[\x0E\x0F\x1B\x5C\x7E\x80-\xFF]/gs'; + $exc = '/[\x0E\x0F\x1B\x5C\x7E\x80-\xFF]/s'; } else { // in other modes ASCII characters are never returned return ""; } $mask = preg_replace($exc, "", $mask); - $len = strspn($this->string, $mask, $this->posByte, $length); - $out = substr($this->string, $this->posByte, $len); - $this->posByte += $len; - $this->posChar += $len; - return $out; + if ($length !== null) { + $len = strspn($this->string, $mask, $this->posByte, $length); + } else { + $len = strspn($this->string, $mask, $this->posByte); + } + if ($len) { + $out = substr($this->string, $this->posByte, $len); + $this->posByte += $len; + $this->posChar += $len; + return $out; + } else { + return ""; + } } public function asciiSpanNot(string $mask, int $length = null): string { @@ -211,11 +219,19 @@ class ISO2022JP extends AbstractEncoding implements ModalCoder, Decoder { return ""; } $mask .= self::HIGH_BYTES; - $len = strcspn($this->string, $mask, $this->posByte, $length); - $out = substr($this->string, $this->posByte, $len); - $this->posByte += $len; - $this->posChar += $len; - return $out; + if ($length !== null) { + $len = strcspn($this->string, $mask, $this->posByte, $length); + } else { + $len = strcspn($this->string, $mask, $this->posByte); + } + if ($len) { + $out = substr($this->string, $this->posByte, $len); + $this->posByte += $len; + $this->posChar += $len; + return $out; + } else { + return ""; + } } protected function stateSave(): array {