@@ -88,23 +88,22 @@ static VALUE rb_compress_using_dict(int argc, VALUE *argv, VALUE self)
88
88
static VALUE decompress_buffered (ZSTD_DCtx * dctx , const char * input_data , size_t input_size )
89
89
{
90
90
ZSTD_inBuffer input = { input_data , input_size , 0 };
91
- VALUE result = rb_str_new (0 , 0 );
92
-
91
+ size_t const buffOutSize = ZSTD_DStreamOutSize ();
92
+ VALUE output_string = rb_str_buf_new (buffOutSize );
93
+ ZSTD_outBuffer output = { RSTRING_PTR (output_string ), buffOutSize , 0 };
93
94
while (input .pos < input .size ) {
94
- ZSTD_outBuffer output = { NULL , 0 , 0 };
95
- output .size += ZSTD_DStreamOutSize ();
96
- VALUE output_string = rb_str_new (NULL , output .size );
97
- output .dst = RSTRING_PTR (output_string );
98
-
99
95
size_t ret = zstd_stream_decompress (dctx , & output , & input , false);
100
96
if (ZSTD_isError (ret )) {
101
97
ZSTD_freeDCtx (dctx );
102
98
rb_raise (rb_eRuntimeError , "%s: %s" , "ZSTD_decompressStream failed" , ZSTD_getErrorName (ret ));
103
99
}
104
- rb_str_cat (result , output .dst , output .pos );
100
+ rb_str_modify_expand (output_string , buffOutSize );
101
+ output .dst = RSTRING_PTR (output_string ) + output .size ;
102
+ output .size += buffOutSize ;
105
103
}
104
+ rb_str_set_len (output_string , output .pos );
106
105
ZSTD_freeDCtx (dctx );
107
- return result ;
106
+ return output_string ;
108
107
}
109
108
110
109
static VALUE rb_decompress (int argc , VALUE * argv , VALUE self )
0 commit comments