Skip to content

Commit

Permalink
Apply same optimization for escape_quotes
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsdos committed Feb 23, 2024
1 parent 4e96b75 commit c754be8
Showing 1 changed file with 9 additions and 5 deletions.
14 changes: 9 additions & 5 deletions ext/mysqlnd/mysqlnd_charset.c
Original file line number Diff line number Diff line change
Expand Up @@ -769,6 +769,12 @@ PHPAPI const MYSQLND_CHARSET * mysqlnd_find_charset_name(const char * const name
}
/* }}} */

static zend_always_inline bool mysqlnd_mb_validity_prerequisites_check(const MYSQLND_CHARSET * const cset, const zend_uchar *str)
{
/* Encodings that have a minimum length of 1 are compatible with ASCII.
* So we can skip (for performance reasons) the check to mb_valid for them. */
return cset->char_maxlen > 1 && (*str > 0x80 || cset->char_minlen > 1);
}

/* {{{ mysqlnd_cset_escape_quotes */
PHPAPI zend_ulong mysqlnd_cset_escape_quotes(const MYSQLND_CHARSET * const cset, char * newstr,
Expand All @@ -784,7 +790,7 @@ PHPAPI zend_ulong mysqlnd_cset_escape_quotes(const MYSQLND_CHARSET * const cset,
unsigned int len = 0;
/* check unicode characters */

if (cset->char_maxlen > 1 && (len = cset->mb_valid(escapestr, end))) {
if (mysqlnd_mb_validity_prerequisites_check(cset, (const zend_uchar *) escapestr) && (len = cset->mb_valid(escapestr, end))) {
ZEND_ASSERT(newstr + len <= newstr_e);
/* copy mb char without escaping it */
while (len--) {
Expand Down Expand Up @@ -823,10 +829,8 @@ PHPAPI zend_ulong mysqlnd_cset_escape_slashes(const MYSQLND_CHARSET * const cset
for (;escapestr < end; escapestr++) {
char esc = '\0';

/* check unicode characters
* Encodings that have a minimum length of 1 are compatible with ASCII.
* So we can skip (for performance reasons) the check to mb_valid for them. */
if (cset->char_maxlen > 1 && (*((zend_uchar *) escapestr) > 0x80 || cset->char_minlen > 1)) {
/* check unicode characters */
if (mysqlnd_mb_validity_prerequisites_check(cset, (const zend_uchar *) escapestr)) {
unsigned int len = cset->mb_valid(escapestr, end);
if (len) {
ZEND_ASSERT(newstr + len <= newstr_e);
Expand Down

0 comments on commit c754be8

Please sign in to comment.