| | |
| | public function seek( int $offset, int $whence = SEEK_SET ): bool { |
| | - $success = $this->handle !== false && \fseek( |
| | - $this->handle, |
| | - $whence === SEEK_CUR |
| | - ? (int)\ftell( $this->handle ) |
| | - - $this->bufferLen |
| | - + $this->bufferPos |
| | - + $offset |
| | - : $offset, |
| | - $whence === SEEK_CUR ? SEEK_SET : $whence |
| | - ) === 0; |
| | + $current = $this->tell(); |
| | + $target = $whence === SEEK_CUR ? $current + $offset : $offset; |
| | + $bufSt = $current - $this->bufferPos; |
| | + $bufEn = $bufSt + $this->bufferLen; |
| | + $success = false; |
| | |
| | - if( $success ) { |
| | - $this->buffer = ''; |
| | - $this->bufferPos = 0; |
| | - $this->bufferLen = 0; |
| | + if( $whence !== SEEK_END && $target >= $bufSt && $target <= $bufEn ) { |
| | + $this->bufferPos = $target - $bufSt; |
| | + $success = true; |
| | + } else { |
| | + $seekTgt = $whence === SEEK_END ? $offset : $target; |
| | + $seekWh = $whence === SEEK_END ? SEEK_END : SEEK_SET; |
| | + $success = $this->handle !== false |
| | + && \fseek( $this->handle, $seekTgt, $seekWh ) === 0; |
| | + |
| | + if( $success ) { |
| | + $this->buffer = ''; |
| | + $this->bufferPos = 0; |
| | + $this->bufferLen = 0; |
| | + } |
| | } |
| | |