Skip to content

Commit cd9192c

Browse files
committed
Update json_encoder.c
1 parent c3ed1af commit cd9192c

1 file changed

Lines changed: 14 additions & 13 deletions

File tree

ext/json/json_encoder.c

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,20 @@ static inline void php_json_pretty_print_char(smart_str *buf, int options, char
5353

5454
static inline void php_json_pretty_print_indent(smart_str *buf, int options, const php_json_encoder *encoder) /* {{{ */
5555
{
56-
if (options & PHP_JSON_PRETTY_PRINT) {
57-
int depth = encoder->depth;
58-
if (depth <= 8) {
59-
int i;
60-
for (i = 0; i < depth; i++) {
61-
smart_str_appendl(buf, " ", 4);
62-
}
63-
} else {
64-
size_t remaining = (size_t) depth * 4;
65-
char *dst = smart_str_extend(buf, remaining);
66-
memset(dst, ' ', remaining);
67-
}
68-
}
56+
if (options & PHP_JSON_PRETTY_PRINT) {
57+
static const char spaces[] =
58+
" ";
59+
size_t remaining = (size_t) encoder->depth * 4;
60+
61+
while (remaining >= sizeof(spaces) - 1) {
62+
smart_str_appendl(buf, spaces, sizeof(spaces) - 1);
63+
remaining -= sizeof(spaces) - 1;
64+
}
65+
66+
if (remaining) {
67+
smart_str_appendl(buf, spaces, remaining);
68+
}
69+
}
6970
}
7071
/* }}} */
7172

0 commit comments

Comments
 (0)