Skip to content

Commit d8e13ee

Browse files
committed
ext/json: Various small refactorings
1 parent d0274e7 commit d8e13ee

File tree

1 file changed

+14
-23
lines changed

1 file changed

+14
-23
lines changed

ext/json/json_encoder.c

Lines changed: 14 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -41,15 +41,9 @@ static zend_always_inline bool php_json_check_stack_limit(void)
4141
#endif
4242
}
4343

44-
static int php_json_determine_array_type(zval *val) /* {{{ */
44+
static int php_json_determine_array_type(const HashTable *ht) /* {{{ */
4545
{
46-
zend_array *myht = Z_ARRVAL_P(val);
47-
48-
if (myht) {
49-
return zend_array_is_list(myht) ? PHP_JSON_OUTPUT_ARRAY : PHP_JSON_OUTPUT_OBJECT;
50-
}
51-
52-
return PHP_JSON_OUTPUT_ARRAY;
46+
return zend_array_is_list(ht) ? PHP_JSON_OUTPUT_ARRAY : PHP_JSON_OUTPUT_OBJECT;
5347
}
5448
/* }}} */
5549

@@ -63,12 +57,10 @@ static inline void php_json_pretty_print_char(smart_str *buf, int options, char
6357
}
6458
/* }}} */
6559

66-
static inline void php_json_pretty_print_indent(smart_str *buf, int options, php_json_encoder *encoder) /* {{{ */
60+
static inline void php_json_pretty_print_indent(smart_str *buf, int options, const php_json_encoder *encoder) /* {{{ */
6761
{
68-
int i;
69-
7062
if (options & PHP_JSON_PRETTY_PRINT) {
71-
for (i = 0; i < encoder->depth; ++i) {
63+
for (int i = 0; i < encoder->depth; ++i) {
7264
smart_str_appendl(buf, " ", 4);
7365
}
7466
}
@@ -122,7 +114,8 @@ static inline void php_json_encode_double(smart_str *buf, double d, int options)
122114

123115
static zend_result php_json_encode_array(smart_str *buf, zval *val, int options, php_json_encoder *encoder) /* {{{ */
124116
{
125-
int r, need_comma = 0;
117+
int r;
118+
bool need_comma = false;
126119
HashTable *myht, *prop_ht;
127120
zend_refcounted *recursion_rc;
128121

@@ -138,17 +131,15 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
138131
myht = Z_ARRVAL_P(val);
139132
recursion_rc = (zend_refcounted *)myht;
140133
prop_ht = NULL;
141-
r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : php_json_determine_array_type(val);
134+
r = (options & PHP_JSON_FORCE_OBJECT) ? PHP_JSON_OUTPUT_OBJECT : php_json_determine_array_type(myht);
142135
} else if (Z_OBJ_P(val)->properties == NULL
143136
&& Z_OBJ_HT_P(val)->get_properties_for == NULL
144137
&& Z_OBJ_HT_P(val)->get_properties == zend_std_get_properties
145138
&& Z_OBJ_P(val)->ce->num_hooked_props == 0
146139
&& !zend_object_is_lazy(Z_OBJ_P(val))) {
147140
/* Optimized version without rebuilding properties HashTable */
148141
zend_object *obj = Z_OBJ_P(val);
149-
zend_class_entry *ce = obj->ce;
150-
zend_property_info *prop_info;
151-
zval *prop;
142+
const zend_class_entry *ce = obj->ce;
152143

153144
if (GC_IS_RECURSIVE(obj)) {
154145
encoder->error_code = PHP_JSON_ERROR_RECURSION;
@@ -163,15 +154,15 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
163154
++encoder->depth;
164155

165156
for (int i = 0; i < ce->default_properties_count; i++) {
166-
prop_info = ce->properties_info_table[i];
157+
zend_property_info *prop_info = ce->properties_info_table[i];
167158
if (!prop_info) {
168159
continue;
169160
}
170161
if (ZSTR_VAL(prop_info->name)[0] == '\0' && ZSTR_LEN(prop_info->name) > 0) {
171162
/* Skip protected and private members. */
172163
continue;
173164
}
174-
prop = OBJ_PROP(obj, prop_info->offset);
165+
zval *prop = OBJ_PROP(obj, prop_info->offset);
175166
if (Z_TYPE_P(prop) == IS_UNDEF) {
176167
continue;
177168
}
@@ -270,7 +261,8 @@ static zend_result php_json_encode_array(smart_str *buf, zval *val, int options,
270261

271262
php_json_pretty_print_char(buf, options, '\n');
272263
php_json_pretty_print_indent(buf, options, encoder);
273-
} else if (r == PHP_JSON_OUTPUT_OBJECT) {
264+
} else {
265+
ZEND_ASSERT(r == PHP_JSON_OUTPUT_OBJECT);
274266
if (key) {
275267
if (ZSTR_VAL(key)[0] == '\0' && ZSTR_LEN(key) > 0 && Z_TYPE_P(val) == IS_OBJECT) {
276268
/* Skip protected and private members. */
@@ -369,7 +361,6 @@ zend_result php_json_escape_string(
369361
smart_str *buf, const char *s, size_t len,
370362
int options, php_json_encoder *encoder) /* {{{ */
371363
{
372-
unsigned int us;
373364
size_t pos, checkpoint;
374365
char *dst;
375366

@@ -407,7 +398,7 @@ zend_result php_json_escape_string(
407398
0xffffffff, 0x500080c4, 0x10000000, 0x00000000,
408399
0xffffffff, 0xffffffff, 0xffffffff, 0xffffffff};
409400

410-
us = (unsigned char)s[pos];
401+
unsigned int us = (unsigned char)s[pos];
411402
if (EXPECTED(!ZEND_BIT_TEST(charmap, us))) {
412403
pos++;
413404
len--;
@@ -626,7 +617,7 @@ static zend_result php_json_encode_serializable_object(smart_str *buf, zend_obje
626617

627618
static zend_result php_json_encode_serializable_enum(smart_str *buf, zval *val, int options, php_json_encoder *encoder)
628619
{
629-
zend_class_entry *ce = Z_OBJCE_P(val);
620+
const zend_class_entry *ce = Z_OBJCE_P(val);
630621
if (ce->enum_backing_type == IS_UNDEF) {
631622
encoder->error_code = PHP_JSON_ERROR_NON_BACKED_ENUM;
632623
smart_str_appendc(buf, '0');

0 commit comments

Comments
 (0)