Browse Source

Improved spanning for ISO-2022-JP

span
J. King 3 years ago
parent
commit
e5aac0b409
  1. 58
      lib/Encoding/ISO2022JP.php

58
lib/Encoding/ISO2022JP.php

@ -185,28 +185,48 @@ class ISO2022JP extends AbstractEncoding implements ModalCoder, Decoder {
} }
public function asciiSpan(string $mask, int $length = null): string { public function asciiSpan(string $mask, int $length = null): string {
if ($this->mode === self::ASCII_STATE) { $out = "";
$exc = '/[\x0E\x0F\x1B\x80-\xFF]/s'; Process:
} elseif ($this->mode === self::ROMAN_STATE) { if ($this->mode === self::KATAKANA_STATE || $this->mode === self::LEAD_BYTE_STATE) {
$exc = '/[\x0E\x0F\x1B\x5C\x7E\x80-\xFF]/s'; // these modes will always return an empty span
} else {
// in other modes ASCII characters are never returned
return "";
}
$mask = preg_replace($exc, "", $mask);
if ($length !== null) {
$len = strspn($this->string, $mask, $this->posByte, $length);
} else { } else {
$len = strspn($this->string, $mask, $this->posByte); if ($this->mode === self::ASCII_STATE) {
$exc = '/[\x0E\x0F\x1B\x80-\xFF]/s';
} elseif ($this->mode === self::ROMAN_STATE) {
$exc = '/[\x0E\x0F\x1B\x5C\x7E\x80-\xFF]/s';
} else {
// in other modes ASCII characters are never returned
return "";
}
$effectiveMask = preg_replace($exc, "", $mask);
if ($length !== null) {
$len = strspn($this->string, $effectiveMask, $this->posByte, $length);
} else {
$len = strspn($this->string, $effectiveMask, $this->posByte);
}
if ($len) {
$out .= substr($this->string, $this->posByte, $len);
$this->posByte += $len;
$this->posChar += $len;
}
} }
if ($len) { // check if the current position has a mode change to ASCII or Roman modes and is followed by a desired character
$out = substr($this->string, $this->posByte, $len); if (@$this->string[$this->posByte] === "\x1B") {
$this->posByte += $len; if (substr($this->string, $this->posByte + 1, 2) === "\x28\x42") {
$this->posChar += $len; $exc = '/[\x0E\x0F\x1B\x80-\xFF]/s';
return $out; } elseif (substr($this->string, $this->posByte + 1, 2) === "\x28\x4A") {
} else { $exc = '/[\x0E\x0F\x1B\x5C\x7E\x80-\xFF]/s';
return ""; } else {
return $out;
}
$effectiveMask = preg_replace($exc, "", $mask);
// if the byte after the mode switch is a wanted one, consume it and go back to the start
if (strspn(@$this->string[$this->posByte + 3], $effectiveMask, $this->posByte)) {
$out .= $this->nextChar();
goto Process;
}
} }
return $out;
} }
public function asciiSpanNot(string $mask, int $length = null): string { public function asciiSpanNot(string $mask, int $length = null): string {

Loading…
Cancel
Save