diff --git a/UPGRADING b/UPGRADING index 607f9427ed6b..9e5d0ee2eff2 100644 --- a/UPGRADING +++ b/UPGRADING @@ -224,6 +224,11 @@ PHP 8.7 UPGRADE NOTES . Parsing HTML with Dom\HTMLDocument or innerHTML uses much less memory for small inputs. +- Intl: + . Improved performance of grapheme_strlen(), grapheme_substr(), + grapheme_strpos() and the related grapheme functions on ASCII strings + longer than 8 bytes. + - PDO: . Improved performance of fetching rows with PDO::FETCH_OBJ. diff --git a/ext/intl/grapheme/grapheme_string.cpp b/ext/intl/grapheme/grapheme_string.cpp index 9c7fa09cb73e..3d9053a144b8 100644 --- a/ext/intl/grapheme/grapheme_string.cpp +++ b/ext/intl/grapheme/grapheme_string.cpp @@ -113,7 +113,7 @@ U_CFUNC PHP_FUNCTION(grapheme_strpos) /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */ - if (offset >= 0 && grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0) { + if (offset >= 0 && haystack_len <= INT32_MAX && grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0) { /* quick check to see if the string might be there * I realize that 'offset' is 'grapheme count offset' but will work in spite of that */ @@ -168,7 +168,7 @@ U_CFUNC PHP_FUNCTION(grapheme_stripos) /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */ - is_ascii = ( grapheme_ascii_check((unsigned char*)haystack, haystack_len) >= 0 ); + is_ascii = haystack_len <= INT32_MAX && grapheme_ascii_check((unsigned char*)haystack, haystack_len) >= 0; if ( is_ascii ) { char *haystack_dup, *needle_dup; @@ -234,7 +234,7 @@ U_CFUNC PHP_FUNCTION(grapheme_strrpos) /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */ - is_ascii = grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0; + is_ascii = haystack_len <= INT32_MAX && grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0; if ( is_ascii ) { @@ -293,7 +293,7 @@ U_CFUNC PHP_FUNCTION(grapheme_strripos) /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */ - is_ascii = grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0; + is_ascii = haystack_len <= INT32_MAX && grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0; if ( is_ascii ) { char *needle_dup, *haystack_dup; @@ -376,7 +376,7 @@ U_CFUNC PHP_FUNCTION(grapheme_substr) /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */ - if ( grapheme_ascii_check((unsigned char *)str, str_len) >= 0 ) { + if ( str_len <= INT32_MAX && grapheme_ascii_check((unsigned char *)str, str_len) >= 0 ) { int32_t asub_str_len; char *sub_str; grapheme_substr_ascii(str, str_len, start, (int32_t)length, &sub_str, &asub_str_len); diff --git a/ext/intl/grapheme/grapheme_util.cpp b/ext/intl/grapheme/grapheme_util.cpp index bf4be3f63801..fb6190c88952 100644 --- a/ext/intl/grapheme/grapheme_util.cpp +++ b/ext/intl/grapheme/grapheme_util.cpp @@ -230,15 +230,36 @@ int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle, /* }}} */ /* {{{ grapheme_ascii_check: ASCII check */ +/* Returns len when every byte is one grapheme: all bytes are ASCII and there is no CR LF pair (CR LF is one + * grapheme cluster). Returns -1 otherwise. */ zend_long grapheme_ascii_check(const unsigned char *day, size_t len) { - int ret_len = len; - while ( len-- ) { - if ( *day++ > 0x7f || (*day == '\n' && *(day - 1) == '\r') ) + const unsigned char *p = day; + const unsigned char *end = day + len; + + for (; end - p > 8; p += 8) { + uint64_t word, next; + memcpy(&word, p, sizeof(word)); + memcpy(&next, p + 1, sizeof(next)); + if (word & UINT64_C(0x8080808080808080)) { + return -1; + } + /* A zero byte i marks a CR LF pair at p[i], p[i + 1]. */ + uint64_t crlf = (word ^ UINT64_C(0x0d0d0d0d0d0d0d0d)) | (next ^ UINT64_C(0x0a0a0a0a0a0a0a0a)); + if ((crlf - UINT64_C(0x0101010101010101)) & ~crlf & UINT64_C(0x8080808080808080)) { + return -1; + } + } + for (; end - p > 1; p++) { + if (*p > 0x7f || (*p == '\r' && p[1] == '\n')) { + return -1; + } + } + if (p < end && *p > 0x7f) { return -1; } - return ret_len; + return (zend_long) len; } /* }}} */ diff --git a/ext/intl/tests/grapheme_ascii_check_positions.phpt b/ext/intl/tests/grapheme_ascii_check_positions.phpt new file mode 100644 index 000000000000..d788c2121995 --- /dev/null +++ b/ext/intl/tests/grapheme_ascii_check_positions.phpt @@ -0,0 +1,37 @@ +--TEST-- +grapheme_strlen() and grapheme_extract() with a non-ASCII byte or CR LF at every position +--EXTENSIONS-- +intl +--FILE-- + +--EXPECT-- +c3a9: 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 +f09f9880: 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 +ff: NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL +80: NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL NULL +0d0a: 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 +0d: 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 +0a: 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 18 +0d780a: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 +0d780d0a: 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 20 +"a" 1; "ab" 2; "abc" 3; "abc\r\n" 5; "abc\r\n" 5 +"a" 1; "ab" 2; "abc" 3; "abcd" 4; "abcde" 5; "abcdef" 6; "abcdefg" 7; "abcdefgh" 8; "abcdefgh\r\n" 10; "abcdefgh\r\ni" 11; "abcdefgh\r\nij" 12; "abcdefgh\r\nij" 12 +"a" 1; "ab" 2; "abc" 3; "abcd" 4; "abcde" 5; "abcdef" 6; "abcdefg" 7; "abcdefg\r\n" 9; "abcdefg\r\ni" 10; "abcdefg\r\nij" 11; "abcdefg\r\nij" 11