fixup 6837f00 with even more simplicity
authorMichael Wallner <mike@php.net>
Wed, 12 Jan 2022 08:11:23 +0000 (09:11 +0100)
committerMichael Wallner <mike@php.net>
Wed, 12 Jan 2022 08:42:32 +0000 (09:42 +0100)
ion.c
ion_private.h
tests/Writer/Buffer.phpt

diff --git a/ion.c b/ion.c
index 3cd4684d82ab84f1c08b4e99b8cdb9dfff1cc27c..40c7ea1944557b25d294d51a0ea0630730300213 100644 (file)
--- 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)
index 928b82665e5745c67f4f072c2b71b9ce02ccc1d1..2bf07edc5c06885cc4cd6f71799a77ca5299be1b 100644 (file)
@@ -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;
 }
 
index d9c870f85532ffe81d8e848e5cf83208e49f0e44..4eebf315ea58e681bbf7411b74b9a7683ecde318 100644 (file)
@@ -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