class stringbuilder extends component
{
private $buffer;
private $length;
public function set_buffer($_value) { $this->buffer = $_value; }
public function get_buffer() { return $this->buffer; }
public function set_length($_value) { $this->length = $_value; }
public function get_length() { return $this->length; }
public function __construct()
{
$this->buffer = array();
$this->length = 0;
}
public function __destruct()
{
unset($this->buffer);
$this->length = 0;
}
private function append($_args)
{
foreach($_args as $arg)
{
array_push($this->buffer, $arg);
$this->length += strlen($arg);
}
}
private function prepend($_args)
{
$_args = array_reverse($_args);
foreach($_args as $arg)
{
array_unshift($this->buffer, $arg);
$this->length += strlen($arg);
}
}
private function tostring()
{
return implode('', $this->buffer);
}
private function appendformat($_args)
{
$count = -1;
$format = array_shift($_args);
foreach($_args as $arg)
{
$format = preg_replace('#\{' . ($count+=1) . '\}#', $arg, $format);
}
array_push($this->buffer, $format);
$this->length += strlen($format);
}
private function insert($_args)
{
$string = $this->tostring();
$stringlength = $this->length;
$this->clear();
$string = substr_replace($string, $_args[0], $_args[1], 0);
array_push($this->buffer, $string);
$this->length += strlen($string);
}
private function replace($_args)
{
$string = $this->tostring();
$stringlength = $this->length;
$this->clear();
$string = str_replace($_args[0], $_args[1], $string);
array_push($this->buffer, $string);
$this->length += strlen($string);
}
private function clear()
{
$this->buffer = array();
$this->length = 0;
}
public function __call($_method, $_args)
{
return (method_exists($this, $_method)) ? $this->$_method($_args) : null;
}
}