Skip to content

tree-wide: Refactor with zend_string_ends_with* - #23216

Open
LamentXU123 wants to merge 3 commits into
php:masterfrom
LamentXU123:zend_str_ends
Open

tree-wide: Refactor with zend_string_ends_with*#23216
LamentXU123 wants to merge 3 commits into
php:masterfrom
LamentXU123:zend_str_ends

Conversation

@LamentXU123

Copy link
Copy Markdown
Member

Utilize zend_string_ends_with* APIs added in #22819.

@NattyNarwhal NattyNarwhal left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok odbc

@jorgsowa jorgsowa left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good for ext/session

@Girgias Girgias left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor nits really

Comment thread ext/opcache/ZendAccelerator.c Outdated
Comment on lines +1522 to +1523
return filename && ZSTR_LEN(filename) > sizeof(".phar") - 1 &&
zend_string_ends_with_literal(filename, ".phar") &&

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You no longer need the ZSTR_LEN comparison, as that's taken care of by zend_string_ends_with_literal

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes this is correct!

Comment thread ext/pgsql/pgsql.c

int result;
if (ZSTR_LEN(tmp) > 0 && ZSTR_VAL(tmp)[ZSTR_LEN(tmp) - 1] != '\n') {
if (ZSTR_LEN(tmp) > 0 && !zend_string_ends_with_literal(tmp, "\n")) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

@LamentXU123 LamentXU123 Aug 12, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This has a behavior impact when you delete the ZSTR_LEN(tmp) > 0 check.

You see, when tmp is an empty string, here !zend_string_ends_with_literal(tmp, "\n") is true, and we clearly don't want to add \n in empty strings.

Comment thread ext/soap/php_http.c
} else {
return false;
}
return ZSTR_LEN(host) > ZSTR_LEN(domain) && zend_string_ends_with(host, domain);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto

@LamentXU123 LamentXU123 Aug 12, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this shouldn't be changed. This missed the ZSTR_LEN(host) == ZSTR_LEN(domain) case.

Comment thread ext/json/json_encoder.c Outdated
}

bool empty = ZSTR_VAL(buf->s)[ZSTR_LEN(buf->s) - 1] != ',';
bool empty = !zend_string_ends_with_literal(buf->s, ",");

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these calls inlined? As this might have a performance impact? (Mainly asking as I know some work was done to improve the performance of the encoder recently)

@LamentXU123 LamentXU123 Aug 12, 2026

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

They are inlined (zend_always_inline). The only performance diff is the function has a ZSTR_LEN(str) >= 1 check (in this case), which is faster in some cases and slower in some cases

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In JSON cases, the buffer has ensure the strings can't be empty strings. So comparing using raw bytes is faster here :)
I think this is the only case in the PR.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants