Skip to content

Commit 7d485a9

Browse files
committed
Get rid of acc
1 parent 144b0e1 commit 7d485a9

File tree

1 file changed

+9
-5
lines changed

1 file changed

+9
-5
lines changed

ext/json/json_encoder.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -508,27 +508,31 @@ zend_result php_json_escape_string(
508508
const __m128i result_individual_bytes = _mm_cmpistrm(_mm_setr_epi8(34, 38, 39, 47, 60, 62, 92, 0, 0, 0, 0, 0, 0, 0, 0, 0), input, _SIDD_SBYTE_OPS | _SIDD_CMP_EQUAL_ANY | _SIDD_BIT_MASK);
509509
int mask = _mm_cvtsi128_si32(result_individual_bytes);
510510
#endif
511-
int acc = 0;
512511
if (mask != 0) {
512+
int shift = __builtin_clz(mask) - 16;
513513
do {
514+
/* Note that we shift the input forward, so we have to shift the mask as well,
515+
* beyond the to-be-escaped character */
514516
int len = __builtin_ctz(mask);
515517
mask >>= len + 1;
516518

517519
smart_str_appendl(buf, s, len + pos);
518520

519-
acc += len + 1;
520521
pos += len;
521-
us = (unsigned char)s[pos++];
522-
s += pos;
522+
us = (unsigned char)s[pos];
523+
s += pos + 1; /* skip 'us' */
523524
pos = 0;
524525

525526
bool handled = php_json_printable_ascii_escape(buf, us, options);
526527
ZEND_ASSERT(handled == true);
527528
} while (mask != 0);
529+
530+
pos += shift;
531+
} else {
532+
pos += sizeof(__m128i);
528533
}
529534

530535
len -= sizeof(__m128i);
531-
pos += sizeof(__m128i) - acc;
532536
}
533537

534538
if (!len) {

0 commit comments

Comments
 (0)