mirror of
https://github.com/php/php-src.git
synced 2025-08-15 13:38:49 +02:00
Use strcspn() to optimize dom_html5_escape_string() (#12948)
* Use strcspn() to optimize dom_html5_escape_string() This routine implemented by libc uses a faster algorithm than the old naive byte-per-byte approach here. It also is often optimized using SIMD. * Calculate mask outside of loop
This commit is contained in:
parent
82baeeb196
commit
0870da3364
1 changed files with 20 additions and 16 deletions
|
@ -70,7 +70,17 @@ static zend_result dom_html5_escape_string(dom_html5_serialize_context *ctx, con
|
|||
{
|
||||
const char *last_output = content;
|
||||
|
||||
while (*content != '\0') {
|
||||
/* Note: uses UTF-8 internally, so <C2 A0> indicates a non-breaking space */
|
||||
const char *mask = attribute_mode ? "&\xC2\"" : "&\xC2<>";
|
||||
|
||||
while (true) {
|
||||
size_t chunk_length = strcspn(content, mask);
|
||||
|
||||
content += chunk_length;
|
||||
if (*content == '\0') {
|
||||
break;
|
||||
}
|
||||
|
||||
switch (*content) {
|
||||
/* Step 1 */
|
||||
case '&': {
|
||||
|
@ -93,29 +103,23 @@ static zend_result dom_html5_escape_string(dom_html5_serialize_context *ctx, con
|
|||
|
||||
/* Step 3 */
|
||||
case '"': {
|
||||
if (attribute_mode) {
|
||||
TRY(ctx->write_string_len(ctx->application_data, last_output, content - last_output));
|
||||
TRY(ctx->write_string_len(ctx->application_data, """, strlen(""")));
|
||||
last_output = content + 1;
|
||||
}
|
||||
TRY(ctx->write_string_len(ctx->application_data, last_output, content - last_output));
|
||||
TRY(ctx->write_string_len(ctx->application_data, """, strlen(""")));
|
||||
last_output = content + 1;
|
||||
break;
|
||||
}
|
||||
|
||||
/* Step 4 */
|
||||
case '<': {
|
||||
if (!attribute_mode) {
|
||||
TRY(ctx->write_string_len(ctx->application_data, last_output, content - last_output));
|
||||
TRY(ctx->write_string_len(ctx->application_data, "<", strlen("<")));
|
||||
last_output = content + 1;
|
||||
}
|
||||
TRY(ctx->write_string_len(ctx->application_data, last_output, content - last_output));
|
||||
TRY(ctx->write_string_len(ctx->application_data, "<", strlen("<")));
|
||||
last_output = content + 1;
|
||||
break;
|
||||
}
|
||||
case '>': {
|
||||
if (!attribute_mode) {
|
||||
TRY(ctx->write_string_len(ctx->application_data, last_output, content - last_output));
|
||||
TRY(ctx->write_string_len(ctx->application_data, ">", strlen(">")));
|
||||
last_output = content + 1;
|
||||
}
|
||||
TRY(ctx->write_string_len(ctx->application_data, last_output, content - last_output));
|
||||
TRY(ctx->write_string_len(ctx->application_data, ">", strlen(">")));
|
||||
last_output = content + 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue