mirror of
https://github.com/pmmp/ext-encoding.git
synced 2025-09-27 16:55:09 +00:00
Using zend_string for buffers was convenient and allowed multiple buffers to reference the same buffer, but in practice this was just slowing things down. Writers will now always copy memory when cloned, ensuring that every writer has full ownership of the memory in question. This eliminates the potential for a string to be shared, and we don't have to worry about zend interned strings or any of that jazz.
37 lines
846 B
PHP
37 lines
846 B
PHP
--TEST--
|
|
Test that reserving works correctly
|
|
--FILE--
|
|
<?php
|
|
|
|
use pmmp\encoding\ByteBufferWriter;
|
|
use pmmp\encoding\Byte;
|
|
|
|
$buffer = new ByteBufferWriter("");
|
|
|
|
$buffer->reserve(40);
|
|
var_dump($buffer->getReservedLength()); //40
|
|
var_dump($buffer->getData()); //still empty, we haven't used any space
|
|
|
|
Byte::writeSigned($buffer, ord("a"));
|
|
var_dump($buffer->getReservedLength()); //40
|
|
var_dump($buffer->getData());
|
|
|
|
$buffer->writeByteArray(str_repeat("a", 40)); //cause new allocation, this should double the buffer size to 80
|
|
var_dump($buffer->getReservedLength()); //80
|
|
var_dump($buffer->getData());
|
|
|
|
try{
|
|
$buffer->reserve(-1);
|
|
}catch(\ValueError $e){
|
|
echo $e->getMessage() . PHP_EOL;
|
|
}
|
|
?>
|
|
--EXPECT--
|
|
int(40)
|
|
string(0) ""
|
|
int(40)
|
|
string(1) "a"
|
|
int(80)
|
|
string(41) "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
|
|
Length must be greater than zero
|