Skip to content

Commit 5942b5d

Browse files
authored
Merge pull request #95 from ndossche/smart-str
Switch to smart_str API
2 parents d6d26eb + 05f5d05 commit 5942b5d

1 file changed

Lines changed: 38 additions & 36 deletions

File tree

zstd.c

Lines changed: 38 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,10 @@
3333
#include <ext/standard/base64.h>
3434
#include <ext/standard/file.h>
3535
#include <ext/standard/info.h>
36-
#if PHP_VERSION_ID < 70200
37-
#include <ext/standard/php_smart_string.h>
38-
#else
39-
#include "Zend/zend_smart_string.h"
40-
#endif
36+
#include <zend_smart_str.h>
4137
#if defined(HAVE_APCU_SUPPORT)
4238
#include <ext/standard/php_var.h>
4339
#include <ext/apcu/apc_serializer.h>
44-
#include <zend_smart_str.h>
4540
#endif
4641
#include <Zend/zend_API.h>
4742
#include <Zend/zend_interfaces.h>
@@ -65,6 +60,19 @@
6560
#define zend_string_efree(string) zend_string_free(string)
6661
#endif
6762

63+
#if PHP_VERSION_ID < 70200
64+
static zend_always_inline zend_string *smart_str_extract(smart_str *str) {
65+
if (str->s) {
66+
smart_str_0(str);
67+
zend_string *res = str->s;
68+
str->s = NULL;
69+
return res;
70+
} else {
71+
return ZSTR_EMPTY_ALLOC();
72+
}
73+
}
74+
#endif
75+
6876
#define ZSTD_WARNING(...) \
6977
php_error_docref(NULL, E_WARNING, __VA_ARGS__)
7078

@@ -448,7 +456,7 @@ static size_t zstd_check_compress_level(zend_long level)
448456
ZEND_FUNCTION(zstd_compress)
449457
{
450458
size_t result;
451-
smart_string out = { 0 };
459+
smart_str out = { 0 };
452460
zend_long level = ZSTD_CLEVEL_DEFAULT;
453461
zend_string *input, *dict = NULL;
454462
php_zstd_context ctx;
@@ -480,15 +488,14 @@ ZEND_FUNCTION(zstd_compress)
480488
&ctx.input, ZSTD_e_end);
481489
if (ZSTD_isError(result)) {
482490
ZSTD_WARNING("%s", ZSTD_getErrorName(result));
483-
smart_string_free(&out);
491+
smart_str_free(&out);
484492
php_zstd_context_free(&ctx);
485493
RETURN_FALSE;
486494
}
487-
smart_string_appendl(&out, ctx.output.dst, ctx.output.pos);
495+
smart_str_appendl(&out, ctx.output.dst, ctx.output.pos);
488496
} while (result > 0);
489497

490-
RETVAL_STRINGL(out.c, out.len);
491-
smart_string_free(&out);
498+
RETVAL_STR(smart_str_extract(&out));
492499

493500
php_zstd_context_free(&ctx);
494501
}
@@ -497,7 +504,7 @@ ZEND_FUNCTION(zstd_uncompress)
497504
{
498505
size_t chunk, result;
499506
uint64_t size;
500-
smart_string out = { 0 };
507+
smart_str out = { 0 };
501508
zend_string *input, *dict = NULL;
502509
php_zstd_context ctx;
503510

@@ -540,29 +547,28 @@ ZEND_FUNCTION(zstd_uncompress)
540547
ctx.output.pos = 0;
541548
result = ZSTD_decompressStream(ctx.dctx, &ctx.output, &ctx.input);
542549
if (ZSTD_IS_ERROR(result)) {
543-
smart_string_free(&out);
550+
smart_str_free(&out);
544551
php_zstd_context_free(&ctx);
545552
ZSTD_WARNING("%s", ZSTD_getErrorName(result));
546553
RETURN_FALSE;
547554
}
548555

549-
smart_string_appendl(&out, ctx.output.dst, ctx.output.pos);
556+
smart_str_appendl(&out, ctx.output.dst, ctx.output.pos);
550557

551558
if (result == 0) {
552559
break;
553560
}
554561
}
555562

556-
RETVAL_STRINGL(out.c, out.len);
557-
smart_string_free(&out);
563+
RETVAL_STR(smart_str_extract(&out));
558564

559565
php_zstd_context_free(&ctx);
560566
}
561567

562568
ZEND_FUNCTION(zstd_compress_dict)
563569
{
564570
size_t result;
565-
smart_string out = { 0 };
571+
smart_str out = { 0 };
566572
zend_long level = ZSTD_CLEVEL_DEFAULT;
567573
zend_string *input, *dict;
568574
php_zstd_context ctx;
@@ -596,15 +602,14 @@ ZEND_FUNCTION(zstd_compress_dict)
596602
&ctx.input, ZSTD_e_end);
597603
if (ZSTD_isError(result)) {
598604
ZSTD_WARNING("%s", ZSTD_getErrorName(result));
599-
smart_string_free(&out);
605+
smart_str_free(&out);
600606
php_zstd_context_free(&ctx);
601607
RETURN_FALSE;
602608
}
603-
smart_string_appendl(&out, ctx.output.dst, ctx.output.pos);
609+
smart_str_appendl(&out, ctx.output.dst, ctx.output.pos);
604610
} while (result > 0);
605611

606-
RETVAL_STRINGL(out.c, out.len);
607-
smart_string_free(&out);
612+
RETVAL_STR(smart_str_extract(&out));
608613

609614
php_zstd_context_free(&ctx);
610615
}
@@ -613,7 +618,7 @@ ZEND_FUNCTION(zstd_uncompress_dict)
613618
{
614619
size_t chunk, result;
615620
unsigned long long size;
616-
smart_string out = { 0 };
621+
smart_str out = { 0 };
617622
zend_string *input, *dict;
618623
php_zstd_context ctx;
619624

@@ -659,21 +664,20 @@ ZEND_FUNCTION(zstd_uncompress_dict)
659664
ctx.output.pos = 0;
660665
result = ZSTD_decompressStream(ctx.dctx, &ctx.output, &ctx.input);
661666
if (ZSTD_IS_ERROR(result)) {
662-
smart_string_free(&out);
667+
smart_str_free(&out);
663668
php_zstd_context_free(&ctx);
664669
ZSTD_WARNING("%s", ZSTD_getErrorName(result));
665670
RETURN_FALSE;
666671
}
667672

668-
smart_string_appendl(&out, ctx.output.dst, ctx.output.pos);
673+
smart_str_appendl(&out, ctx.output.dst, ctx.output.pos);
669674

670675
if (result == 0) {
671676
break;
672677
}
673678
}
674679

675-
RETVAL_STRINGL(out.c, out.len);
676-
smart_string_free(&out);
680+
RETVAL_STR(smart_str_extract(&out));
677681

678682
php_zstd_context_free(&ctx);
679683
}
@@ -706,7 +710,7 @@ ZEND_FUNCTION(zstd_compress_add)
706710
php_zstd_context *ctx;
707711
zend_string *input;
708712
zend_bool end = 0;
709-
smart_string out = {0};
713+
smart_str out = {0};
710714
#if PHP_VERSION_ID >= 80000
711715
zend_object *obj;
712716
#else
@@ -743,14 +747,13 @@ ZEND_FUNCTION(zstd_compress_add)
743747
&in, end ? ZSTD_e_end : ZSTD_e_flush);
744748
if (ZSTD_isError(res)) {
745749
ZSTD_WARNING("%s", ZSTD_getErrorName(res));
746-
smart_string_free(&out);
750+
smart_str_free(&out);
747751
RETURN_FALSE;
748752
}
749-
smart_string_appendl(&out, ctx->output.dst, ctx->output.pos);
753+
smart_str_appendl(&out, ctx->output.dst, ctx->output.pos);
750754
} while (res > 0);
751755

752-
RETVAL_STRINGL(out.c, out.len);
753-
smart_string_free(&out);
756+
RETVAL_STR(smart_str_extract(&out));
754757
}
755758

756759
ZEND_FUNCTION(zstd_uncompress_init)
@@ -775,7 +778,7 @@ ZEND_FUNCTION(zstd_uncompress_add)
775778
zend_object *context;
776779
php_zstd_context *ctx;
777780
zend_string *input;
778-
smart_string out = {0};
781+
smart_str out = {0};
779782
#if PHP_VERSION_ID >= 80000
780783
zend_object *obj;
781784
#else
@@ -815,15 +818,14 @@ ZEND_FUNCTION(zstd_uncompress_add)
815818
res = ZSTD_decompressStream(ctx->dctx, &ctx->output, &in);
816819
if (ZSTD_isError(res)) {
817820
ZSTD_WARNING("%s", ZSTD_getErrorName(res));
818-
smart_string_free(&out);
821+
smart_str_free(&out);
819822
RETURN_FALSE;
820823
}
821824

822-
smart_string_appendl(&out, ctx->output.dst, ctx->output.pos);
825+
smart_str_appendl(&out, ctx->output.dst, ctx->output.pos);
823826
}
824827

825-
RETVAL_STRINGL(out.c, out.len);
826-
smart_string_free(&out);
828+
RETVAL_STR(smart_str_extract(&out));
827829
}
828830

829831
typedef struct _php_zstd_stream_data {

0 commit comments

Comments
 (0)