1
0
mirror of https://github.com/danog/byte-stream.git synced 2024-11-26 20:04:51 +01:00
byte-stream/lib/Buffer.php

206 lines
4.6 KiB
PHP

<?php
namespace Amp\ByteStream;
/**
*/
class Buffer {
/** @var string */
private $data;
/**
* Initialize buffer with the given string.
*
* @param string $data
*/
public function __construct(string $data = '') {
$this->data = $data;
}
/**
* Current length of the buffer.
*
* @return int
*/
public function getLength(): int {
return \strlen($this->data);
}
/**
* Determines if the buffer is empty.
*
* @return bool
*/
public function isEmpty(): bool {
return $this->data === '';
}
/**
* Pushes the given string onto the end of the buffer.
*
* @param string $data
*/
public function push(string $data) {
$this->data .= $data;
}
/**
* Puts the given string at the beginning of the buffer.
*
* @param string $data
*/
public function unshift(string $data) {
$this->data = $data . $this->data;
}
/**
* @param int $length
*
* @return string
*/
public function shift(int $length): string {
if ($length <= 0) {
return '';
}
if (\strlen($this->data) <= $length) {
$buffer = $this->data;
$this->data = '';
return $buffer;
}
$buffer = (string) \substr($this->data, 0, $length);
$this->data = (string) \substr($this->data, $length);
return $buffer;
}
/**
* Returns the given number of characters (at most) from the buffer without removing them from the buffer.
*
* @param int $length
* @param int $offset
*
* @return string
*/
public function peek(int $length, int $offset = 0): string {
if ($length <= 0) {
return '';
}
if ($offset < 0) {
$offset = 0;
}
if ($offset === 0 && \strlen($this->data) <= $length) {
return $this->data;
}
return (string) \substr($this->data, $offset, $length);
}
/**
* @param int $length
*
* @return string
*/
public function pop(int $length): string {
if ($length <= 0) {
return '';
}
$buffer = (string) \substr($this->data, -$length);
$this->data = (string) \substr($this->data, 0, -$length);
return $buffer;
}
/**
* Removes and returns the given number of characters (at most) from the buffer.
*
* @param int $length
* @param int $offset
*
* @return string
*/
public function remove(int $length, int $offset): string {
if ($length <= 0) {
return '';
}
if ($offset < 0) {
$offset = 0;
}
$buffer = (string) \substr($this->data, $offset, $length);
if ($offset === 0) {
$this->data = (string) \substr($this->data, $length);
} else {
$this->data = (string) (\substr($this->data, 0, $offset) . \substr($this->data, $offset + $length));
}
return $buffer;
}
/**
* Removes and returns all data in the buffer.
*
* @return string
*/
public function drain(): string {
$buffer = $this->data;
$this->data = '';
return $buffer;
}
/**
* Inserts the string at the given position in the buffer.
*
* @param string $string
* @param int $position
*/
public function insert(string $string, int $position) {
$this->data = \substr_replace($this->data, $string, $position, 0);
}
/**
* Replaces all occurrences of $search with $replace. See str_replace() function.
*
* @param mixed $search
* @param mixed $replace
*
* @return int Number of replacements performed.
*/
public function replace($search, $replace): int {
$this->data = \str_replace($search, $replace, $this->data, $count);
return $count;
}
/**
* Returns the position of the given pattern in the buffer if it exists, or false if it does not.
*
* @param string $string String to search for.
* @param bool $reverse Start search from end of buffer.
*
* @return int|bool
*/
public function indexOf(string $string, bool $reverse = false): int {
if ($reverse) {
$result = \strrpos($this->data, $string);
} else {
$result = \strpos($this->data, $string);
}
return $result === false ? -1 : $result;
}
/**
* @return string
*/
public function __toString(): string {
return $this->data;
}
}