From 79bd1dfd20c99cf0d6daceff0d427a3aa4f18167 Mon Sep 17 00:00:00 2001 From: Michael Wallner Date: Wed, 12 Jan 2022 09:11:23 +0100 Subject: [PATCH] fixup 6837f00 with even more simplicity --- ion.c | 7 +------ ion_private.h | 18 ++++++++++-------- tests/Writer/Buffer.phpt | 11 +++++++++-- 3 files changed, 20 insertions(+), 16 deletions(-) diff --git a/ion.c b/ion.c index 3cd4684..40c7ea1 100644 --- a/ion.c +++ b/ion.c @@ -1585,9 +1585,6 @@ static ZEND_METHOD(ion_Writer_Writer, flush) SIZE flushed; ION_CHECK(ion_writer_flush(obj->writer, &flushed)); - if (obj->type == BUFFER_WRITER) { - smart_str_0(&obj->buffer.str); - } RETURN_LONG(flushed); } static ZEND_METHOD(ion_Writer_Writer, finish) @@ -1599,9 +1596,6 @@ static ZEND_METHOD(ion_Writer_Writer, finish) SIZE flushed; ION_CHECK(ion_writer_finish(obj->writer, &flushed)); - if (obj->type == BUFFER_WRITER) { - smart_str_0(&obj->buffer.str); - } RETURN_LONG(flushed); } static ZEND_METHOD(ion_Writer_Writer, writeOne) @@ -1630,6 +1624,7 @@ static ZEND_METHOD(ion_Writer_Buffer_Writer, getBuffer) ZEND_PARSE_PARAMETERS_NONE(); + smart_str_0(&obj->buffer.str.s); RETVAL_STR_COPY(obj->buffer.str.s); } static ZEND_METHOD(ion_Writer_Buffer_Writer, resetBuffer) diff --git a/ion_private.h b/ion_private.h index 928b826..2bf07ed 100644 --- a/ion_private.h +++ b/ion_private.h @@ -1335,6 +1335,7 @@ typedef struct php_ion_writer { union { struct { smart_str str; + struct _ion_user_stream *usr; } buffer; struct { ION_STRING buf; @@ -1371,16 +1372,18 @@ LOCAL void php_ion_writer_stream_init(php_ion_writer *obj, php_ion_writer_option LOCAL void php_ion_writer_buffer_init(php_ion_writer *obj) { - smart_str_alloc(&obj->buffer.str, 0, 0); - smart_str_0(&obj->buffer.str); + smart_str_alloc(&obj->buffer.str, 0, false); + if (obj->buffer.usr) { + obj->buffer.usr->curr = (BYTE *) &obj->buffer.str.s->val[obj->buffer.str.s->len]; + obj->buffer.usr->limit = obj->buffer.usr->curr + obj->buffer.str.a - obj->buffer.str.s->len; + } } LOCAL void php_ion_writer_buffer_reset(php_ion_writer *obj) { smart_str_free(&obj->buffer.str); memset(&obj->buffer.str, 0, sizeof(obj->buffer.str)); - smart_str_alloc(&obj->buffer.str, 0, 0); - smart_str_0(&obj->buffer.str); + php_ion_writer_buffer_init(obj); } LOCAL void php_ion_writer_buffer_grow(php_ion_writer *obj) @@ -1391,7 +1394,6 @@ LOCAL void php_ion_writer_buffer_grow(php_ion_writer *obj) smart_str_alloc(&obj->buffer.str, obj->buffer.str.a << 1, 0); memcpy(obj->buffer.str.s->val, keep->val, keep->len + 1); obj->buffer.str.s->len = keep->len; - smart_str_0(&obj->buffer.str); zend_string_release(keep); } else { smart_str_erealloc(&obj->buffer.str, obj->buffer.str.a << 1); @@ -1401,17 +1403,17 @@ LOCAL void php_ion_writer_buffer_grow(php_ion_writer *obj) LOCAL iERR php_ion_writer_buffer_handler(struct _ion_user_stream *user) { php_ion_writer *writer = (php_ion_writer *) user->handler_state; + writer->buffer.usr = user; if (user->curr) { - writer->buffer.str.s->len += user->curr - (BYTE *) &writer->buffer.str.s->val[writer->buffer.str.s->len]; - smart_str_0(&writer->buffer.str); + writer->buffer.str.s->len = user->curr - (BYTE *) writer->buffer.str.s->val; if (user->limit == user->curr) { php_ion_writer_buffer_grow(writer); } } user->curr = (BYTE *) &writer->buffer.str.s->val[writer->buffer.str.s->len]; user->limit = user->curr + writer->buffer.str.a - writer->buffer.str.s->len; - + return IERR_OK; } diff --git a/tests/Writer/Buffer.phpt b/tests/Writer/Buffer.phpt index d9c870f..4eebf31 100644 --- a/tests/Writer/Buffer.phpt +++ b/tests/Writer/Buffer.phpt @@ -14,13 +14,20 @@ echo $w->getBuffer(),"\n"; $w->resetBuffer(); var_dump($w->getBuffer()); $w->writeSymbol("bar"); -$w->finish(); +$w->flush(); var_dump($w->getBuffer()); +$w->resetBuffer(); +// realloc +for ($i = 0; $i < 100; ++$i) + $w->writeTypedNull(ion\Type::Int); +$w->flush(); +echo $w->getBuffer(),"\n"; ?> DONE --EXPECTF-- TEST null.int%r( null.int)*%r string(0) "" -string(3) "bar" +string(4) " bar" + null.int%r( null.int)*%r DONE -- 2.30.2