posChar++; while(($b = @$this->string[$this->posByte++]) !== "") { $b = ord($b); if ($first === 0) { if ($b < 0x80) { return $b; } elseif ($b === 0x80) { return 0x20AC; } elseif ($b > 0x80 && $b < 0xFF) { $first = $b; } else { return self::err($this->errMode, [$this->posChar - 1, $this->posByte - 1]); } } elseif ($second === 0) { if ($b > 0x29 && $b < 0x40) { $second = $b; } else { if (($b > 0x39 && $b < 0x7F) || ($b > 0x7F && $b < 0xFF)) { $offset = ($b < 0x7F) ? 0x40 : 0x41; $pointer = ($first - 0x81) * 190 + ($b - $offset); return self::TABLE_GBK[$pointer]; } elseif ($b < 0x80) { return self::err($this->errMode, [$this->posChar - 1, --$this->posByte]); } else { return self::err($this->errMode, [$this->posChar - 1, $this->posByte - 1]); } } } elseif ($third === 0) { if ($b > 0x80 && $b < 0xFF) { $third = $b; } else { $this->posByte -= 2; return self::err($this->errMode, [$this->posChar - 1, $this->posByte - 1]); } } else { if ($b > 0x29 && $b < 0x40) { // look up code point $pointer = (($first - 0x81) * (10 * 126 * 10)) + (($second - 0x30) * (10 * 126)) + (($third - 0x81) * 10) + $b - 0x30; if ($pointer === 7457) { return 0xE7C7; } for ($a = 1; $a < sizeof(self::TABLE_RANGES); $a++) { if ($pointer < self::TABLE_RANGES[$a]) { $offset = self::TABLE_RANGES[$a - 1]; $codePointOffset = self::TABLE_OFFSETS[$a - 1]; break; } } if (isset($codePointOffset)) { return $codePointOffset + $pointer - $offset; } else { return self::err($this->errMode, [$this->posChar - 1, $this->posByte - 1]); } } else { $this->posByte -= 3; return self::err($this->errMode, [$this->posChar - 1, $this->posByte - 1]); } } } if (($first + $second + $third) == 0) { // clean EOF return false; } else { // dirty EOF return self::err($this->errMode, [--$this->posChar, --$this->posByte]); } } /** Returns the encoding of $codePoint as a byte string * * If $codePoint is less than 0 or greater than 1114111, an exception is thrown * * If $fatal is true, an exception will be thrown if the code point cannot be encoded into a character; otherwise HTML character references will be substituted */ public static function encode(int $codePoint, bool $fatal = true): string { if ($codePoint < 0 || $codePoint > 0x10FFFF) { throw new EncoderException("Encountered code point outside Unicode range ($codePoint)", self::E_INVALID_CODE_POINT); } elseif ($codePoint < 128) { return chr($codePoint); } elseif ($codePoint == 0xE5E5) { return self::err($fatal ? self::MODE_FATAL_ENC : self::MODE_HTML, $codePoint); } elseif (static::GBK && $codePoint == 0x20AC) { return "\x80"; } else { $pointer = array_flip(self::TABLE_GBK)[$codePoint] ?? null; if (isset($pointer)) { $lead = (int) ($pointer / 190) + 0x81; $trail = $pointer % 190; $offset = ($trail < 0x3F) ? 0x40 : 0x41; return chr($lead).chr($trail + $offset); } elseif (static::GBK) { return self::err($fatal ? self::MODE_FATAL_ENC : self::MODE_HTML, $codePoint); } else { if ($codePoint == 0xE7C7) { $pointer = 7457; } else { $index = 0; while ($codePoint >= self::TABLE_OFFSETS[$index + 1]) { $index++; } $offset = self::TABLE_OFFSETS[$index]; $pointer_offset = self::TABLE_RANGES[$index]; $pointer = $pointer_offset + $codePoint - $offset; } $byte1 = (int) ($pointer / (10 * 126 * 10)) + 0x81; $pointer %= (10 * 126 * 10); $byte2 = (int) ($pointer / (10 * 126)) + 0x30; $pointer %= (10 * 126); $byte3 = (int) ($pointer / 10) + 0x81; $byte4 = ($pointer % 10) + 0x30; return chr($byte1).chr($byte2).chr($byte3).chr($byte4); } } } /** Advance $distance characters through the string * * If $distance is negative, the operation will be performed in reverse * * If the end (or beginning) of the string was reached before the end of the operation, the remaining number of requested characters is returned */ public function seek(int $distance): int { // stub } }