src/Debug/Utf8.php
private static function isOffsetUtf8(&$special = false, $checkSpecial = false)
    {
        $i = self::$curI;
        $special = false;
        $byte1 = \ord(self::$str[$i]);
        $byte2 = $i + 1 < self::$stats['strLen'] ? \ord(self::$str[$i+1]) : null;
        $byte3 = $i + 2 < self::$stats['strLen'] ? \ord(self::$str[$i+2]) : null;
        $byte4 = $i + 3 < self::$stats['strLen'] ? \ord(self::$str[$i+3]) : null;
        if ($byte1 < 0x80) {                 # 0bbbbbbb
            if (($byte1 < 0x20 || $byte1 == 0x7f) && !\in_array($byte1, array(0x09,0x0a,0x0d))) {
                $special = true;
            }
            self::$curI += 1;    // advance to next byte
        } elseif (($byte1 & 0xe0) == 0xc0) { # 110bbbbb 10bbbbbb
            if ($i + 1 >= self::$stats['strLen'] ||
                ($byte2 & 0xc0) !== 0x80 ||
                ($byte1 & 0xfe) === 0xc0  // overlong
            ) {
                self::$curI += 1;
                return false;
            }
            self::$curI += 2;    // skip the next byte
        } elseif (($byte1 & 0xf0) == 0xe0) { # 3-byte sequence 1110bbbb 10bbbbbb 10bbbbbb
            if ($i + 2 >= self::$stats['strLen'] ||
                ($byte2 & 0xc0) !== 0x80 ||
                ($byte3 & 0xc0) !== 0x80 ||
                $byte1 === 0xe0 && ($byte2 & 0xe0) === 0x80 ||  // overlong
                $byte1 === 0xed && ($byte2 & 0xe0) === 0xa0     // UTF-16 surrogate (U+D800 - U+DFFF)
            ) {
                self::$curI += 1;
                return false;
            }
            self::$curI += 3;    // skip the next 2 bytes
        } elseif (($byte1 & 0xf8) == 0xf0) { # 4-byte sequence: 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
            if ($i + 3 >= self::$stats['strLen'] ||
                ($byte2 & 0xc0) !== 0x80 ||
                ($byte3 & 0xc0) !== 0x80 ||
                ($byte4 & 0xc0) !== 0x80 ||
                $byte1 === 0xf0 && ($byte2 & 0xf0) === 0x80 ||      // overlong
                $byte1 === 0xf4 && $byte2 > 0x8f || $byte1 > 0xf4   // > U+10FFFF
            ) {
                self::$curI += 1;
                return false;
            }
            self::$curI += 4;    // skip the next 3 bytes
        } else {                            # Does not match any model
            self::$curI += 1;
            return false;
        }
        if ($checkSpecial) {
            $subStr = \substr(self::$str, $i, self::$curI-$i);
            $special = $special || self::hasSpecial($subStr);
        }
        return true;
    }
private static function isOffsetUtf8(&$special = false, $checkSpecial = false)
    {
        $i = self::$curI;
        $special = false;
        $byte1 = \ord(self::$str[$i]);
        $byte2 = $i + 1 < self::$stats['strLen'] ? \ord(self::$str[$i+1]) : null;
        $byte3 = $i + 2 < self::$stats['strLen'] ? \ord(self::$str[$i+2]) : null;
        $byte4 = $i + 3 < self::$stats['strLen'] ? \ord(self::$str[$i+3]) : null;
        if ($byte1 < 0x80) {                 # 0bbbbbbb
            if (($byte1 < 0x20 || $byte1 == 0x7f) && !\in_array($byte1, array(0x09,0x0a,0x0d))) {
                $special = true;
            }
            self::$curI += 1;    // advance to next byte
        } elseif (($byte1 & 0xe0) == 0xc0) { # 110bbbbb 10bbbbbb
            if ($i + 1 >= self::$stats['strLen'] ||
                ($byte2 & 0xc0) !== 0x80 ||
                ($byte1 & 0xfe) === 0xc0  // overlong
            ) {
                self::$curI += 1;
                return false;
            }
            self::$curI += 2;    // skip the next byte
        } elseif (($byte1 & 0xf0) == 0xe0) { # 3-byte sequence 1110bbbb 10bbbbbb 10bbbbbb
            if ($i + 2 >= self::$stats['strLen'] ||
                ($byte2 & 0xc0) !== 0x80 ||
                ($byte3 & 0xc0) !== 0x80 ||
                $byte1 === 0xe0 && ($byte2 & 0xe0) === 0x80 ||  // overlong
                $byte1 === 0xed && ($byte2 & 0xe0) === 0xa0     // UTF-16 surrogate (U+D800 - U+DFFF)
            ) {
                self::$curI += 1;
                return false;
            }
            self::$curI += 3;    // skip the next 2 bytes
        } elseif (($byte1 & 0xf8) == 0xf0) { # 4-byte sequence: 11110bbb 10bbbbbb 10bbbbbb 10bbbbbb
            if ($i + 3 >= self::$stats['strLen'] ||
                ($byte2 & 0xc0) !== 0x80 ||
                ($byte3 & 0xc0) !== 0x80 ||
                ($byte4 & 0xc0) !== 0x80 ||
                $byte1 === 0xf0 && ($byte2 & 0xf0) === 0x80 ||      // overlong
                $byte1 === 0xf4 && $byte2 > 0x8f || $byte1 > 0xf4   // > U+10FFFF
            ) {
                self::$curI += 1;
                return false;
            }
            self::$curI += 4;    // skip the next 3 bytes
        } else {                            # Does not match any model
            self::$curI += 1;
            return false;
        }
        if ($checkSpecial) {
            $subStr = \substr(self::$str, $i, self::$curI-$i);
            $special = $special || self::hasSpecial($subStr);
        }
        return true;
    }
Is the byte or byte-sequence beginning at the current offset a valid utf-8 character?

Increments the current offset

@param boolean $special      populated with whether offset is a control or "special" character
@param boolean $checkSpecial test for user-defined special chars?

@return boolean [description]