cvs: pecl /unicode CREDITS TODO config.m4 config.w32 php_unicode.c php_unicode.h unicode_common.h unicode_convert.c unicode_convert.h /unicode/doc
grapheme_api.php /unicode/grapheme grapheme.h grapheme_string.c grapheme_util.h /unicode/tests grapheme.phpt
| From: | Ed Batutis | Date: | Mon, 14 Jan 2008 21:50:07 +0000 |
| Subject: | cvs: pecl /unicode CREDITS TODO config.m4 config.w32 php_unicode.c php_unicode.h unicode_common.h unicode_convert.c unicode_convert.h /unicode/doc grapheme_api.php /unicode/grapheme grapheme.h grapheme_string.c grapheme_util.h /unicode/tests grapheme.phpt |
||
| Groups: | php.pecl.cvs | ||
| Request: | Send a blank email to pecl-cvs+get-9518@lists.php.net to get a copy of this message | ||
ebatutis Mon Jan 14 21:50:07 2008 UTC
Added files:
/pecl/unicode CREDITS TODO config.m4 config.w32 php_unicode.c
php_unicode.h unicode_common.h unicode_convert.c
unicode_convert.h
/pecl/unicode/doc grapheme_api.php
/pecl/unicode/grapheme grapheme.h grapheme_string.c grapheme_util.h
/pecl/unicode/tests grapheme.phpt
Log:
Initial revision of grapheme API and related files
http://cvs.php.net/viewvc.cgi/pecl/unicode/config.m4?view=markup&rev=1.1 Index: pecl/unicode/config.m4 +++ pecl/unicode/config.m4 dnl config.m4 for extension unicode dnl ########################################################################## dnl Initialize the extension PHP_ARG_ENABLE(unicode, whether to enable unicode support, [ --enable-unicode Enable unicode support]) if test "$PHP_UNICODE" != "no"; then PHP_SETUP_ICU(INTL_SHARED_LIBADD) PHP_SUBST(INTL_SHARED_LIBADD) PHP_REQUIRE_CXX() PHP_NEW_EXTENSION(unicode, php_unicode.c \ unicode_convert.c \ grapheme/grapheme_string.c \ ,$ext_shared,,$ICU_INCS) PHP_ADD_BUILD_DIR($ext_builddir/grapheme) fi http://cvs.php.net/viewvc.cgi/pecl/unicode/config.w32?view=markup&rev=1.1 Index: pecl/unicode/config.w32 +++ pecl/unicode/config.w32 // $Id: config.w32,v 1.1 2008/01/14 21:50:07 ebatutis Exp $ // vim:ft=javascript ARG_ENABLE("unicode", "Enable unicode support", "no"); if (PHP_UNICODE != "no") { if (CHECK_LIB("icuuc.lib", "unicode", PHP_UNICODE) && CHECK_HEADER_ADD_INCLUDE("unicode/utf.h", "CFLAGS_INTL")) { // always build as shared - zend_strtod.c/ICU type conflict EXTENSION("unicode", "php_unicode.c unicode_convert.c", true, "/I \"" + configure_module_dirname + "\""); ADD_SOURCES(configure_module_dirname + "/grapheme", "\ grapheme_string.c \ ", "unicode"); ADD_FLAG("LIBS_INTL", "icudt.lib icuin.lib icuio.lib icule.lib iculx.lib"); // if int32_t and uint32_t types are made available in PHP, uncomment next line // ADD_FLAG("CFLAGS_INTL", "/D U_HAVE_INT32_T /D U_HAVE_UINT32_T"); AC_DEFINE("HAVE_UNICODE", 1, "Unicode support enabled"); } else { WARNING("unicode not enabled; libraries and/or headers not found"); } } http://cvs.php.net/viewvc.cgi/pecl/unicode/php_unicode.c?view=markup&rev=1.1 Index: pecl/unicode/php_unicode.c +++ pecl/unicode/php_unicode.c /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Ed Batutis <ed@batutis.com> | | Stanislav Malyshev <stas@zend.com> | +----------------------------------------------------------------------+ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php_unicode.h" #include "grapheme/grapheme.h" #include <ext/standard/info.h> #include "php_ini.h" #define INTL_MODULE_VERSION "0.1a" ZEND_DECLARE_MODULE_GLOBALS( unicode ) /* {{{ Arguments info */ static ZEND_BEGIN_ARG_INFO_EX( grapheme_0_args, 0, 0, 0 ) ZEND_END_ARG_INFO() static ZEND_BEGIN_ARG_INFO_EX( grapheme_1_arg, 0, 0, 1 ) ZEND_ARG_INFO( 0, arg1 ) ZEND_END_ARG_INFO() static ZEND_BEGIN_ARG_INFO_EX( grapheme_2_args, 0, 0, 2 ) ZEND_ARG_INFO( 0, arg1 ) ZEND_ARG_INFO( 0, arg2 ) ZEND_END_ARG_INFO() static ZEND_BEGIN_ARG_INFO_EX( grapheme_3_args, 0, 0, 3 ) ZEND_ARG_INFO( 0, arg1 ) ZEND_ARG_INFO( 0, arg2 ) ZEND_ARG_INFO( 0, arg3 ) ZEND_END_ARG_INFO() /* }}} */ /* {{{ unicode_functions * * Every user visible function must have an entry in unicode_functions[]. */ zend_function_entry unicode_functions[] = { // grapheme functions PHP_FE( grapheme_strlen, grapheme_1_arg ) PHP_FE( grapheme_strpos, grapheme_3_args ) PHP_FE( grapheme_stripos, grapheme_3_args ) PHP_FE( grapheme_strrpos, grapheme_3_args ) PHP_FE( grapheme_strripos, grapheme_3_args ) PHP_FE( grapheme_substr, grapheme_3_args ) PHP_FE( grapheme_strstr, grapheme_3_args ) PHP_FE( grapheme_stristr, grapheme_3_args ) PHP_FE( grapheme_extract, grapheme_3_args ) PHP_FE( grapheme_extractB, grapheme_3_args ) { NULL, NULL, NULL } }; /* }}} */ static PHP_GINIT_FUNCTION(unicode); /* {{{ unicode_module_entry */ zend_module_entry unicode_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif "unicode", unicode_functions, PHP_MINIT( unicode ), PHP_MSHUTDOWN( unicode ), PHP_RINIT( unicode ), PHP_RSHUTDOWN( unicode ), PHP_MINFO( unicode ), INTL_MODULE_VERSION, PHP_MODULE_GLOBALS(unicode), /* globals descriptor */ PHP_GINIT(unicode), /* globals ctor */ NULL, /* globals dtor */ NULL, /* post deactivate */ STANDARD_MODULE_PROPERTIES_EX }; /* }}} */ #ifdef COMPILE_DL_UNICODE ZEND_GET_MODULE( unicode ) #endif /* {{{ unicode_init_globals */ static PHP_GINIT_FUNCTION(unicode) { memset( unicode_globals, 0, sizeof(zend_unicode_globals) ); } /* }}} */ /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION( unicode ) { return SUCCESS; } /* }}} */ /* {{{ PHP_MSHUTDOWN_FUNCTION */ PHP_MSHUTDOWN_FUNCTION( unicode ) { return SUCCESS; } /* }}} */ /* {{{ PHP_RINIT_FUNCTION */ PHP_RINIT_FUNCTION( unicode ) { return SUCCESS; } /* }}} */ /* {{{ PHP_RSHUTDOWN_FUNCTION */ PHP_RSHUTDOWN_FUNCTION( unicode ) { if (INTL_G(grapheme_iterator)) { grapheme_close_global_iterator( TSRMLS_C ); INTL_G(grapheme_iterator) = NULL; } return SUCCESS; } /* }}} */ /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION( unicode ) { php_info_print_table_start(); php_info_print_table_header( 2, "Unicode support", "enabled" ); php_info_print_table_row( 2, "version", INTL_MODULE_VERSION ); php_info_print_table_end(); //For the default locale php.ini setting DISPLAY_INI_ENTRIES() ; } /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/viewvc.cgi/pecl/unicode/php_unicode.h?view=markup&rev=1.1 Index: pecl/unicode/php_unicode.h +++ pecl/unicode/php_unicode.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: | | Stanislav Malyshev <stas@zend.com> | | Ed Batutis <ed@batutis.com> | +----------------------------------------------------------------------+ */ #ifndef PHP_UNICODE_H #define PHP_UNICODE_H #include <php.h> #include "grapheme/grapheme.h" extern zend_module_entry unicode_module_entry; #define phpext_unicode_ptr &unicode_module_entry #ifdef PHP_WIN32 #define PHP_UNICODE_API __declspec(dllexport) #else #define PHP_UNICODE_API #endif #ifdef ZTS #include "TSRM.h" #endif ZEND_BEGIN_MODULE_GLOBALS(unicode) UBreakIterator* grapheme_iterator; ZEND_END_MODULE_GLOBALS(unicode) /* Macro to access request-wide global variables. */ #ifdef ZTS #define UNICODE_G(v) TSRMG(unicode_globals_id, zend_unicode_globals *, v) #else #define UNICODE_G(v) (unicode_globals.v) #endif ZEND_EXTERN_MODULE_GLOBALS(unicode) PHP_MINIT_FUNCTION(unicode); PHP_MSHUTDOWN_FUNCTION(unicode); PHP_RINIT_FUNCTION(unicode); PHP_RSHUTDOWN_FUNCTION(unicode); PHP_MINFO_FUNCTION(unicode); #endif /* PHP_UNICODE_H */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/viewvc.cgi/pecl/unicode/unicode_common.h?view=markup&rev=1.1 Index: pecl/unicode/unicode_common.h +++ pecl/unicode/unicode_common.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Vadim Savchuk <vsavchuk@productengine.com> | | Dmitry Lakhtyuk <dlakhtyuk@productengine.com> | | Stanislav Malyshev <stas@zend.com> | +----------------------------------------------------------------------+ */ #ifndef UNICODE_COMMON_H #define UNICODE_COMMON_H /* Auxiliary macros */ #include <php.h> #include <unicode/utypes.h> #ifndef UBYTES # define UBYTES(len) ((len) * sizeof(UChar)) #endif #ifndef eumalloc # define eumalloc(size) (UChar*)safe_emalloc(size, sizeof(UChar), 0) #endif #ifndef eurealloc # define eurealloc(ptr, size) (UChar*)erealloc((ptr), size * sizeof(UChar)) #endif #define USIZE(data) sizeof((data))/sizeof(UChar) #define UCHARS(len) ((len) / sizeof(UChar)) #define UNICODE_Z_STRVAL_P(str) (UChar*) Z_STRVAL_P(str) #define UNICODE_Z_STRLEN_P(str) UCHARS( Z_STRLEN_P(str) ) #endif /* UNICODE_COMMON_H */ http://cvs.php.net/viewvc.cgi/pecl/unicode/unicode_convert.c?view=markup&rev=1.1 Index: pecl/unicode/unicode_convert.c +++ pecl/unicode/unicode_convert.c /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Vadim Savchuk <vsavchuk@productengine.com> | | Dmitry Lakhtyuk <dlakhtyuk@productengine.com> | +----------------------------------------------------------------------+ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include <php.h> #include "unicode_common.h" #include "unicode_convert.h" /* {{{ unicode_convert_utf8_to_utf16 * Convert given string from UTF-8 to UTF-16 to *target buffer. * * It *target is NULL then we allocate a large enough buffer, * store the converted string into it, and make target point to it. * * Otherwise, if *target is non-NULL, we assume that it points to a * dynamically allocated buffer of *target_len bytes length. * In this case the buffer will be used to store the converted string to, * and may be resized (made larger) if needed. * * @param target Where to place the result. * @param target_len Result length. * @param source String to convert. * @param source_len Length of the source string. * @param status Conversion status. * * @return void This function does not return anything. */ void unicode_convert_utf8_to_utf16( UChar** target, int* target_len, const char* src, int src_len, UErrorCode* status ) { UChar* dst_buf = NULL; int32_t dst_len = 0; // If *target is NULL determine required destination buffer size (pre-flighting). // Otherwise, attempt to convert source string; if *target buffer is not large enough // it will be resized appropriately. *status = U_ZERO_ERROR; u_strFromUTF8( *target, *target_len, &dst_len, src, src_len, status ); if( *status == U_ZERO_ERROR ) { // String is converted successfuly (*target)[dst_len] = 0; *target_len = dst_len; return; } // Bail out if an unexpected error occured. // (U_BUFFER_OVERFLOW_ERROR means that *target buffer is not large enough). // (U_STRING_NOT_TERMINATED_WARNING usually means that the input string is empty). if( *status != U_BUFFER_OVERFLOW_ERROR && *status != U_STRING_NOT_TERMINATED_WARNING ) return; // Allocate memory for the destination buffer (it will be zero-terminated). dst_buf = eumalloc( dst_len + 1 ); // Convert source string from UTF-8 to UTF-16. *status = U_ZERO_ERROR; u_strFromUTF8( dst_buf, dst_len+1, NULL, src, src_len, status ); if( U_FAILURE( *status ) ) { efree( dst_buf ); return; } dst_buf[dst_len] = 0; if( *target ) efree( *target ); *target = dst_buf; *target_len = dst_len; } /* }}} */ /* {{{ unicode_convert_utf16_to_utf8 * Convert given string from UTF-16 to UTF-8. * * @param target Where to place the result. * @param target_len Result length. * @param source String to convert. * @param source_len Length of the source string. * @param status Conversion status. * * @return void This function does not return anything. */ void unicode_convert_utf16_to_utf8( char** target, int* target_len, const UChar* src, int src_len, UErrorCode* status ) { char* dst_buf = NULL; int32_t dst_len; // Determine required destination buffer size (pre-flighting). *status = U_ZERO_ERROR; u_strToUTF8( NULL, 0, &dst_len, src, src_len, status ); // Bail out if an unexpected error occured. // (U_BUFFER_OVERFLOW_ERROR means that *target buffer is not large enough). // (U_STRING_NOT_TERMINATED_WARNING usually means that the input string is empty). if( *status != U_BUFFER_OVERFLOW_ERROR && *status != U_STRING_NOT_TERMINATED_WARNING ) return; // Allocate memory for the destination buffer (it will be zero-terminated). dst_buf = emalloc( dst_len+1 ); // Convert source string from UTF-8 to UTF-16. *status = U_ZERO_ERROR; u_strToUTF8( dst_buf, dst_len, NULL, src, src_len, status ); if( U_FAILURE( *status ) ) { efree( dst_buf ); return; } // U_STRING_NOT_TERMINATED_WARNING is OK for us => reset 'status'. *status = U_ZERO_ERROR; dst_buf[dst_len] = 0; *target = dst_buf; *target_len = dst_len; } /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/viewvc.cgi/pecl/unicode/unicode_convert.h?view=markup&rev=1.1 Index: pecl/unicode/unicode_convert.h +++ pecl/unicode/unicode_convert.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Vadim Savchuk <vsavchuk@productengine.com> | | Dmitry Lakhtyuk <dlakhtyuk@productengine.com> | +----------------------------------------------------------------------+ */ #ifndef UNICODE_CONVERT_H #define UNICODE_CONVERT_H #include <unicode/ustring.h> void unicode_convert_utf8_to_utf16( UChar** target, int* target_len, const char* src, int src_len, UErrorCode* status ); void unicode_convert_utf16_to_utf8( char** target, int* target_len, const UChar* src, int src_len, UErrorCode* status ); #endif // UNICODE_CONVERT_H http://cvs.php.net/viewvc.cgi/pecl/unicode/doc/grapheme_api.php?view=markup&rev=1.1 Index: pecl/unicode/doc/grapheme_api.php +++ pecl/unicode/doc/grapheme_api.php <?php ############################################################################# # Grapheme API ############################################################################# /** * Get string length in grapheme units * @param string $input The string being measured for length. * @return int The length of the string on success, and 0 if the string is empty. */ function grapheme_strlen($input) {} /** * Find position (in grapheme units) of first occurrence of a string * @param string $haystack The string to look in * @param string $needle The string to look for * @param [int] $offset The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the beginning of haystack. * @return int Returns the position as an integer. If needle is not found, strpos() will return boolean FALSE. */ function grapheme_strpos($haystack, $needle, $offset = 0) {} /** * Find position (in grapheme units) of first occurrence of a case-insensitive string * @param string $haystack The string to look in * @param string $needle The string to look for * @param [int] $offset The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the beginning of haystack. * @return int Returns the position as an integer. If needle is not found, grapheme_stripos() will return boolean FALSE. */ function grapheme_stripos($haystack, $needle, $offset = 0) {} /** * Find position (in grapheme units) of last occurrence of a string * @param string $haystack The string to look in * @param string $needle The string to look for * @param [int] $offset The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the beginning of haystack. * @return int Returns the position as an integer. If needle is not found, grapheme_strrpos() will return boolean FALSE. */ function grapheme_strrpos($haystack, $needle, $offset = 0) {} /** * Find position (in grapheme units) of last occurrence of a case-insensitive string * @param string $haystack The string to look in * @param string $needle The string to look for * @param [int] $offset The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the beginning of haystack. * @return int Returns the position as an integer. If needle is not found, grapheme_strripos() will return boolean FALSE. */ function grapheme_strripos($haystack, $needle, $offset = 0) {} /** * Return part of a string * @param string $string The input string. * @param int $start If start is non-negative, the returned string will start at the start'th position in string, counting from zero. If start is negative, the returned string will start at the start'th character from the end of string. * @param [int] $length If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string). If string is less than or equal to start characters long, FALSE will be returned. If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes a position beyond this truncation, an empty string will be returned. * @return int Returns the extracted part of string. */ function grapheme_substr($string, $start, $length = -1) {} /** * Returns part of haystack string from the first occurrence of needle to the end of haystack. * @param string $haystack The input string. * @param string $needle The string to look for. * @param [boolean] $before_needle If TRUE (the default is FALSE), grapheme_strstr() returns the part of the haystack before the first occurence of the needle. * @return string Returns the portion of string, or FALSE if needle is not found. */ function grapheme_strstr($haystack, $needle, $before_needle = FALSE) {} /** * Returns part of haystack string from the first occurrence of case-insensitive needle to the end of haystack. * @param string $haystack The input string. * @param string $needle The string to look for. * @param [boolean] $before_needle If TRUE (the default is FALSE), grapheme_strstr() returns the part of the haystack before the first occurence of the needle. * @return string Returns the portion of string, or FALSE if needle is not found. */ function grapheme_stristr($haystack, $needle, $before_needle = FALSE) {} /** * Function to extract a sequence of default grapheme clusters from a text buffer, which must be encoded in UTF-8. * @param string $haystack string to search * @param int $size maximum number of grapheme clusters to return * @param [int] $start starting position in $haystack in bytes * @return string A string starting at offset $start containing no more than $size grapheme clusters and ending on a default grapheme cluster boundary. */ public function grapheme_extract($haystack, $size, $start = 0) {} /** * Function to extract a sequence of default grapheme clusters from a text buffer, which must be encoded in UTF-8. * @param string $haystack string to search * @param int $bsize maximum number of bytes to return * @param [int] $start starting position in $haystack in bytes * @return string A string starting at offset $start containing no more than $bsize bytes and ending on a default grapheme cluster boundary. */ public function grapheme_extractB($haystack, $bsize, $start = 0) {} ?> http://cvs.php.net/viewvc.cgi/pecl/unicode/grapheme/grapheme.h?view=markup&rev=1.1 Index: pecl/unicode/grapheme/grapheme.h +++ pecl/unicode/grapheme/grapheme.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Ed Batutis <ed@batutis.com> | +----------------------------------------------------------------------+ */ #ifndef GRAPHEME_GRAPHEME_H #define GRAPHEME_GRAPHEME_H #include <php.h> #include <unicode/utypes.h> #include <unicode/ubrk.h> PHP_FUNCTION(grapheme_strlen); PHP_FUNCTION(grapheme_strpos); PHP_FUNCTION(grapheme_stripos); PHP_FUNCTION(grapheme_strrpos); PHP_FUNCTION(grapheme_strripos); PHP_FUNCTION(grapheme_substr); PHP_FUNCTION(grapheme_strstr); PHP_FUNCTION(grapheme_stristr); PHP_FUNCTION(grapheme_extract); PHP_FUNCTION(grapheme_extractB); void grapheme_register_constants( INIT_FUNC_ARGS ); void grapheme_close_global_iterator( TSRMLS_D ); #endif // GRAPHEME_GRAPHEME_H http://cvs.php.net/viewvc.cgi/pecl/unicode/grapheme/grapheme_string.c?view=markup&rev=1.1 Index: pecl/unicode/grapheme/grapheme_string.c +++ pecl/unicode/grapheme/grapheme_string.c /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Ed Batutis <ed@batutis.com> | +----------------------------------------------------------------------+ */ /* {{{ includes */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include <php.h> #include "grapheme.h" #include "grapheme_util.h" #include <unicode/utypes.h> #include <unicode/ucol.h> #include <unicode/ustring.h> #include <unicode/ubrk.h> #include "ext/standard/php_string.h" /*#include "zend_API.h"*/ /* }}} */ /* {{{ grapheme_register_constants * Register API constants */ void grapheme_register_constants( INIT_FUNC_ARGS ) { } /* }}} */ /* {{{ cov to lc */ void intl_case_fold(UChar** ptr_to_free, UChar **str, int32_t *str_len, UErrorCode *pstatus ) { UChar *dest; int32_t dest_len, size_required; /* allocate a destination string that is a bit larger than the src, hoping that is enough */ dest_len = (*str_len) + ( *str_len / 10 ); dest = (UChar*) eumalloc(dest_len); *pstatus = U_ZERO_ERROR; size_required = u_strFoldCase(dest, dest_len, *str, *str_len, U_FOLD_CASE_DEFAULT, pstatus); dest_len = size_required; if ( U_BUFFER_OVERFLOW_ERROR == *pstatus ) { dest = (UChar*) eurealloc(dest, dest_len); *pstatus = U_ZERO_ERROR; size_required = u_strFoldCase(dest, dest_len, *str, *str_len, U_FOLD_CASE_DEFAULT, pstatus); } if ( U_FAILURE(*pstatus) ) { return; } if ( NULL != ptr_to_free) { efree(*ptr_to_free); *ptr_to_free = dest; } *str = dest; *str_len = dest_len; return; } /* }}} */ /* {{{ ASCII check */ static inline int ascii_check(const unsigned char *day, size_t len) { int ret_len = len; while ( len-- ) { if ( *day++ > 0x7f ) return -1; } return ret_len; } /* }}} */ /* {{{ split_string: find and optionally return grapheme boundaries */ static int split_string(const UChar *text, int32_t text_length, int boundary_array[], int boundary_array_len TSRMLS_DC ) { unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE]; UErrorCode status = U_ZERO_ERROR; int ret_len, pos; UBreakIterator* bi; bi = get_break_iterator((void*)u_break_iterator_buffer, &status TSRMLS_CC ); if( U_FAILURE(status) ) { return -1; } ubrk_setText(bi, text, text_length, &status); pos = 0; for ( ret_len = 0; pos != UBRK_DONE; ) { pos = ubrk_next(bi); if ( pos != UBRK_DONE ) { if ( NULL != boundary_array && ret_len < boundary_array_len ) { boundary_array[ret_len] = pos; } ret_len++; } } ubrk_close(bi); return ret_len; } /* }}} */ /* {{{ count_graphemes */ static inline int32_t count_graphemes(UBreakIterator *bi, UChar *string, int32_t string_len) { int ret_len = 0; int pos = 0; UErrorCode status = U_ZERO_ERROR; ubrk_setText(bi, string, string_len, &status); do { pos = ubrk_next(bi); if ( UBRK_DONE != pos ) { ret_len++; } } while ( UBRK_DONE != pos ); return ret_len; } /* }}} */ /* {{{ memnstr_grapheme: find needle in haystack using grapheme boundaries */ static inline int32_t memnstr_grapheme(UBreakIterator *bi, UChar *haystack, UChar *needle, int32_t needle_len, UChar *end) { UChar *p = haystack; UChar ne = needle[needle_len-1]; UErrorCode status; int32_t grapheme_offset; end -= needle_len; while (p <= end) { if ((p = u_memchr(p, *needle, (end-p+1))) && ne == p[needle_len-1]) { if (!u_memcmp(needle, p, needle_len - 1)) { /* needle_len - 1 works because if needle_len is 1, we've already tested the char */ /* does the grapheme end here? */ status = U_ZERO_ERROR; ubrk_setText (bi, haystack, (end - haystack) + needle_len, &status); if ( ubrk_isBoundary (bi, (p - haystack) + needle_len) ) { /* found it, get grapheme count offset */ grapheme_offset = count_graphemes(bi, haystack, (p - haystack)); return grapheme_offset; } } } if (p == NULL) { return -1; } p++; } return -1; } /* }}} */ /* {{{ memrstr_grapheme: reverse find needle in haystack using grapheme boundaries */ static inline void *memrchr_grapheme(const void *s, int c, size_t n) { register unsigned char *e; if (n <= 0) { return NULL; } for (e = (unsigned char *)s + n - 1; e >= (unsigned char *)s; e--) { if (*e == (unsigned char)c) { return (void *)e; } } return NULL; } /* }}} */ /* {{{ get_haystack_offset - bump the haystack pointer based on the grapheme count offset */ static UChar * get_haystack_offset(UBreakIterator* bi, UChar *uhaystack, int32_t uhaystack_len, int32_t offset) { UErrorCode status; int32_t pos; int32_t (*iter_op)(UBreakIterator* bi); int iter_incr; if ( NULL != bi ) { status = U_ZERO_ERROR; ubrk_setText (bi, uhaystack, uhaystack_len, &status); } if ( 0 == offset ) { return uhaystack; } if ( offset < 0 ) { iter_op = ubrk_previous; ubrk_last(bi); /* one past the end */ iter_incr = 1; } else { iter_op = ubrk_next; iter_incr = -1; } pos = 0; while ( pos != UBRK_DONE && offset != 0 ) { pos = iter_op(bi); if ( UBRK_DONE != pos ) { offset += iter_incr; } } if ( offset != 0 ) { return NULL; } return uhaystack + pos; } /* }}} */ /* {{{ strrpos_ascii: borrowed from the php ext/standard/string.c */ static int32_t strrpos_ascii(unsigned char *haystack, int32_t haystack_len, unsigned char *needle, int32_t needle_len, int32_t offset) { unsigned char *p, *e; if (offset >= 0) { p = haystack + offset; e = haystack + haystack_len - needle_len; } else { p = haystack; if (needle_len > -offset) { e = haystack + haystack_len - needle_len; } else { e = haystack + haystack_len + offset; } } if (needle_len == 1) { /* Single character search can shortcut memcmps */ while (e >= p) { if (*e == *needle) { return (e - p + (offset > 0 ? offset : 0)); } e--; } return -1; } while (e >= p) { if (memcmp(e, needle, needle_len) == 0) { return (e - p + (offset > 0 ? offset : 0)); } e--; } return -1; } /* }}} */ /* {{{ get_break_iterator: get a clone of the global character break iterator */ static UBreakIterator* get_break_iterator(void *stack_buffer, UErrorCode *status TSRMLS_DC ) { int32_t buffer_size; UBreakIterator *global_break_iterator = UNICODE_G( grapheme_iterator ); if ( NULL == global_break_iterator ) { global_break_iterator = ubrk_open(UBRK_CHARACTER, NULL, /* icu default locale - locale has no effect on this iterator */ NULL, /* text not set in global iterator */ 0, /* text length = 0 */ status); UNICODE_G(grapheme_iterator) = global_break_iterator; } buffer_size = U_BRK_SAFECLONE_BUFFERSIZE; return ubrk_safeClone(global_break_iterator, stack_buffer, &buffer_size, status); } /* }}} */ /* {{{ grapheme_close_global_iterator - clean up */ void grapheme_close_global_iterator( TSRMLS_D ) { UBreakIterator *global_break_iterator = UNICODE_G( grapheme_iterator ); if ( NULL != global_break_iterator ) { ubrk_close(global_break_iterator); } } /* }}} */ #define OUTSIDE_STRING(offset, max_len) ( offset < 0 ? -offset > max_len : offset >= max_len ) /* {{{ proto int grapheme_strlen(string str) Get number of graphemes in a string */ PHP_FUNCTION(grapheme_strlen) { unsigned char* string; int string_len; UChar* ustring = NULL; int ustring_len = 0; int ret_len; UErrorCode status; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", (char **)&string, &string_len) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strlen: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } ret_len = ascii_check(string, string_len); if ( ret_len >= 0 ) RETURN_LONG(ret_len); /* convert the string to UTF-16. */ status = U_ZERO_ERROR; intl_convert_utf8_to_utf16(&ustring, &ustring_len, (char*) string, string_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC ); efree( ustring ); RETURN_NULL(); } ret_len = split_string(ustring, ustring_len, NULL, 0 TSRMLS_CC ); efree( ustring ); if (ret_len >= 0) { RETVAL_LONG(ret_len); } else { RETVAL_FALSE; } } /* }}} */ /* {{{ strpos_utf16 - strrpos using utf16*/ static int strpos_utf16(unsigned char *haystack, int haystack_len, unsigned char*needle, int needle_len, int offset, int *puchar_pos, int f_ignore_case TSRMLS_DC) { UChar *uhaystack, *puhaystack, *uneedle; int32_t uhaystack_len, uneedle_len; int ret_pos; unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE]; UBreakIterator* bi; UErrorCode status; *puchar_pos = -1; /* convert the strings to UTF-16. */ uhaystack = NULL; uhaystack_len = 0; status = U_ZERO_ERROR; intl_convert_utf8_to_utf16(&uhaystack, &uhaystack_len, (char *) haystack, haystack_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC ); efree( uhaystack ); return -1; } /* get a pointer to the haystack taking into account the offset */ bi = NULL; status = U_ZERO_ERROR; bi = get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC ); puhaystack = get_haystack_offset(bi, uhaystack, uhaystack_len, offset); uhaystack_len = (uhaystack_len - ( puhaystack - uhaystack)); if ( NULL == puhaystack ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 TSRMLS_CC ); efree( uhaystack ); ubrk_close (bi); return -1; } if ( f_ignore_case ) { intl_case_fold(&uhaystack, &puhaystack, &uhaystack_len, &status ); } uneedle = NULL; uneedle_len = 0; status = U_ZERO_ERROR; intl_convert_utf8_to_utf16(&uneedle, &uneedle_len, (char *) needle, needle_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC ); efree( uhaystack ); efree( uneedle ); ubrk_close (bi); return -1; } if ( f_ignore_case ) { intl_case_fold(&uneedle, &uneedle, &uneedle_len, &status ); } ret_pos = memnstr_grapheme(bi, puhaystack, uneedle, uneedle_len, puhaystack + uhaystack_len ); *puchar_pos = ubrk_current(bi); efree( uhaystack ); efree( uneedle ); ubrk_close (bi); return ret_pos; } /* }}} */ /* {{{ strrpos_utf16 - strrpos using utf16 */ static int strrpos_utf16(unsigned char *haystack, int haystack_len, unsigned char*needle, int needle_len, int offset, int f_ignore_case TSRMLS_DC) { UChar *uhaystack, *puhaystack, *uhaystack_end, *uneedle; int32_t uhaystack_len, uneedle_len; UErrorCode status; unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE]; UBreakIterator* bi = NULL; int ret_pos, pos; /* convert the strings to UTF-16. */ uhaystack = NULL; uhaystack_len = 0; status = U_ZERO_ERROR; intl_convert_utf8_to_utf16(&uhaystack, &uhaystack_len, (char *) haystack, haystack_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC ); efree( uhaystack ); return -1; } if ( f_ignore_case ) { intl_case_fold(&uhaystack, &uhaystack, &uhaystack_len, &status ); } /* get a pointer to the haystack taking into account the offset */ bi = NULL; status = U_ZERO_ERROR; bi = get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC ); puhaystack = get_haystack_offset(bi, uhaystack, uhaystack_len, offset); if ( NULL == puhaystack ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 TSRMLS_CC ); efree( uhaystack ); ubrk_close (bi); return -1; } uneedle = NULL; uneedle_len = 0; status = U_ZERO_ERROR; intl_convert_utf8_to_utf16(&uneedle, &uneedle_len, (char *) needle, needle_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC ); efree( uhaystack ); efree( uneedle ); ubrk_close (bi); return -1; } if ( f_ignore_case ) { intl_case_fold(&uneedle, &uneedle, &uneedle_len, &status ); } ret_pos = -1; /* -1 represents 'not found' */ /* back up until there's needle_len characters to compare */ uhaystack_end = uhaystack + uhaystack_len; pos = ubrk_last(bi); puhaystack = uhaystack + pos; while ( uhaystack_end - puhaystack < uneedle_len ) { pos = ubrk_previous(bi); if ( UBRK_DONE == pos ) { break; } puhaystack = uhaystack + pos; } /* is there enough haystack left to hold the needle? */ if ( ( uhaystack_end - puhaystack ) < uneedle_len ) { /* not enough, not found */ goto exit; } while ( UBRK_DONE != pos ) { if (!u_memcmp(uneedle, puhaystack, uneedle_len)) { /* needle_len - 1 in zend memnstr? */ /* does the grapheme in the haystack end at the same place as the last grapheme in the needle? */ if ( ubrk_isBoundary(bi, pos + uneedle_len) ) { /* found it, get grapheme count offset */ ret_pos = count_graphemes(bi, uhaystack, pos); break; } /* set position back */ ubrk_isBoundary(bi, pos); } pos = ubrk_previous(bi); puhaystack = uhaystack + pos; } exit: efree( uhaystack ); efree( uneedle ); ubrk_close (bi); return ret_pos; } /* }}} */ /* {{{ proto int grapheme_strpos(string haystack, string needle [, int offset ]) Find position of first occurrence of a string within another */ PHP_FUNCTION(grapheme_strpos) { unsigned char *haystack, *needle; int haystack_len, needle_len; unsigned char *found; int offset = 0; int ret_pos, uchar_pos; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", (char **)&haystack, &haystack_len, (char **)&needle, &needle_len, &offset) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } if ( OUTSIDE_STRING(offset, haystack_len) ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 TSRMLS_CC ); RETURN_FALSE; } /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */ if (needle_len == 0) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 TSRMLS_CC ); RETURN_FALSE; } /* 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 */ found = (unsigned char *)php_memnstr((char *)haystack + offset, (char *)needle, needle_len, (char *)haystack + haystack_len); /* if it isn't there the we are done */ if (!found) { RETURN_FALSE; } /* if it is there, and if the haystack is ascii, we are all done */ if ( ascii_check(haystack, haystack_len) >= 0 ) { RETURN_LONG(found - haystack); } /* do utf16 part of the strpos */ ret_pos = strpos_utf16(haystack, haystack_len, needle, needle_len, offset, &uchar_pos, 0 /* fIgnoreCase */ TSRMLS_CC ); if ( ret_pos >= 0 ) { RETURN_LONG(ret_pos + offset); } else { RETURN_FALSE; } } /* }}} */ /* {{{ proto int grapheme_stripos(string haystack, string needle [, int offset ]) Find position of first occurrence of a string within another, ignoring case differences */ PHP_FUNCTION(grapheme_stripos) { unsigned char *haystack, *needle, *haystack_dup, *needle_dup; int haystack_len, needle_len; unsigned char *found; int offset = 0; int ret_pos, uchar_pos; int is_ascii; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", (char **)&haystack, &haystack_len, (char **)&needle, &needle_len, &offset) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_stripos: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } if ( OUTSIDE_STRING(offset, haystack_len) ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_stripos: Offset not contained in string", 1 TSRMLS_CC ); RETURN_FALSE; } /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */ if (needle_len == 0) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_stripos: Empty delimiter", 1 TSRMLS_CC ); RETURN_FALSE; } is_ascii = ( ascii_check(haystack, haystack_len) >= 0 ); if ( is_ascii ) { needle_dup = (unsigned char *)estrndup((char *)needle, needle_len); php_strtolower((char *)needle_dup, needle_len); haystack_dup = (unsigned char *)estrndup((char *)haystack, haystack_len); php_strtolower((char *)haystack_dup, haystack_len); found = (unsigned char*) php_memnstr((char *)haystack_dup + offset, (char *)needle_dup, needle_len, (char *)haystack_dup + haystack_len); efree(haystack_dup); efree(needle_dup); if (found) { RETURN_LONG(found - haystack_dup); } /* if needle was ascii too, we are all done, otherwise we need to try using Unicode to see what we get */ if ( ascii_check(needle, needle_len) >= 0 ) { RETURN_FALSE; } } /* do utf16 part of the strpos */ ret_pos = strpos_utf16(haystack, haystack_len, needle, needle_len, offset, &uchar_pos, 1 /* fIgnoreCase */ TSRMLS_CC ); if ( ret_pos >= 0 ) { RETURN_LONG(ret_pos + offset); } else { RETURN_FALSE; } } /* }}} */ /* {{{ proto int grapheme_strrpos(string haystack, string needle [, int offset]) Find position of last occurrence of a string within another */ PHP_FUNCTION(grapheme_strrpos) { unsigned char *haystack, *needle; int haystack_len, needle_len; int32_t offset = 0; int32_t ret_pos; int is_ascii; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", (char **)&haystack, &haystack_len, (char **)&needle, &needle_len, &offset) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strrpos: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } if ( OUTSIDE_STRING(offset, haystack_len) ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 TSRMLS_CC ); RETURN_FALSE; } /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */ if (needle_len == 0) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 TSRMLS_CC ); RETURN_FALSE; } is_ascii = ascii_check(haystack, haystack_len) >= 0; if ( is_ascii ) { ret_pos = strrpos_ascii(haystack, haystack_len, needle, needle_len, offset); if ( ret_pos >= 0 ) { RETURN_LONG(ret_pos); } /* if the needle was ascii too, we are done */ if ( ascii_check(needle, needle_len) >= 0 ) { RETURN_FALSE; } /* else we need to continue via utf16 */ } ret_pos = strrpos_utf16(haystack, haystack_len, needle, needle_len, offset, 0 /* f_ignore_case */ TSRMLS_CC); if ( ret_pos >= 0 ) { RETURN_LONG(ret_pos); } else { RETURN_FALSE; } } /* }}} */ /* {{{ proto int grapheme_strripos(string haystack, string needle [, int offset]) Find position of last occurrence of a string within another, ignoring case */ PHP_FUNCTION(grapheme_strripos) { unsigned char *haystack, *needle; int haystack_len, needle_len; int32_t offset = 0; int32_t ret_pos; int is_ascii; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", (char **)&haystack, &haystack_len, (char **)&needle, &needle_len, &offset) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strrpos: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } if ( OUTSIDE_STRING(offset, haystack_len) ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 TSRMLS_CC ); RETURN_FALSE; } /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */ if (needle_len == 0) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 TSRMLS_CC ); RETURN_FALSE; } is_ascii = ascii_check(haystack, haystack_len) >= 0; if ( is_ascii ) { unsigned char *needle_dup, *haystack_dup; needle_dup = (unsigned char *)estrndup((char *)needle, needle_len); php_strtolower((char *)needle_dup, needle_len); haystack_dup = (unsigned char *)estrndup((char *)haystack, haystack_len); php_strtolower((char *)haystack_dup, haystack_len); ret_pos = strrpos_ascii(haystack_dup, haystack_len, needle_dup, needle_len, offset); efree(haystack_dup); efree(needle_dup); if ( ret_pos >= 0 ) { RETURN_LONG(ret_pos); } /* if the needle was ascii too, we are done */ if ( ascii_check(needle, needle_len) >= 0 ) { RETURN_FALSE; } /* else we need to continue via utf16 */ } ret_pos = strrpos_utf16(haystack, haystack_len, needle, needle_len, offset, 1 /* f_ignore_case */ TSRMLS_CC); if ( ret_pos >= 0 ) { RETURN_LONG(ret_pos); } else { RETURN_FALSE; } } /* }}} */ /* {{{ substr_ascii */ static void substr_ascii(char *str, int str_len, int f, int l, int argc, char **sub_str, int *sub_str_len) { *sub_str = NULL; if (argc > 2) { if ((l < 0 && -l > str_len)) { return; } else if (l > str_len) { l = str_len; } } else { l = str_len; } if (f > str_len || (f < 0 && -f > str_len)) { return; } if (l < 0 && (l + str_len - f) < 0) { return; } /* if "from" position is negative, count start position from the end * of the string */ if (f < 0) { f = str_len + f; if (f < 0) { f = 0; } } /* if "length" position is negative, set it to the length * needed to stop that many chars from the end of the string */ if (l < 0) { l = (str_len - f) + l; if (l < 0) { l = 0; } } if (f >= str_len) { return; } if ((f + l) > str_len) { l = str_len - f; } *sub_str = str + f; *sub_str_len = l; return; } /* }}} */ /* {{{ proto string grapheme_substr(string str, int start [, int length]) Returns part of a string */ PHP_FUNCTION(grapheme_substr) { unsigned char *str, *sub_str; UChar *ustr; int str_len, sub_str_len, ustr_len, start = 0, length = 0; int iter_val; UErrorCode status; unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE]; UBreakIterator* bi = NULL; int sub_str_start_pos, sub_str_end_pos; int32_t (*iter_func)(UBreakIterator *); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|l", (char **)&str, &str_len, &start, &length) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_substr: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } if ( OUTSIDE_STRING(start, str_len) ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_substr: start not contained in string", 1 TSRMLS_CC ); RETURN_FALSE; } /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */ if ( ascii_check(str, str_len) >= 0 ) { substr_ascii((char *)str, str_len, start, length, ZEND_NUM_ARGS(), (char **) &sub_str, &sub_str_len); if ( NULL == sub_str ) { RETURN_FALSE; } RETURN_STRINGL(((char *)sub_str), sub_str_len, 1); } ustr = NULL; ustr_len = 0; status = U_ZERO_ERROR; intl_convert_utf8_to_utf16(&ustr, &ustr_len, (char *)str, str_len, &status); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC ); efree( ustr ); RETURN_FALSE; } bi = get_break_iterator((void*)u_break_iterator_buffer, &status TSRMLS_CC ); if( U_FAILURE(status) ) { RETURN_FALSE; } ubrk_setText(bi, ustr, ustr_len, &status); if ( start < 0 ) { iter_func = ubrk_previous; ubrk_last(bi); iter_val = 1; } else { iter_func = ubrk_next; iter_val = -1; } sub_str_start_pos = 0; while ( start ) { sub_str_start_pos = iter_func(bi); if ( UBRK_DONE == sub_str_start_pos ) { break; } start += iter_val; } if ( 0 != start || sub_str_start_pos >= ustr_len ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_substr: start not contained in string", 1 TSRMLS_CC ); efree(ustr); ubrk_close(bi); RETURN_FALSE; } if (ZEND_NUM_ARGS() <= 2) { /* no length supplied, return the rest of the string */ sub_str = NULL; sub_str_len = 0; status = U_ZERO_ERROR; intl_convert_utf16_to_utf8((char **)&sub_str, &sub_str_len, ustr + sub_str_start_pos, ustr_len - sub_str_start_pos, &status); efree( ustr ); ubrk_close( bi ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 1 TSRMLS_CC ); efree( sub_str ); RETURN_FALSE; } /* return the allocated string, not a duplicate */ RETURN_STRINGL(((char *)sub_str), sub_str_len, 0); } /* find the end point of the string to return */ if ( length < 0 ) { iter_func = ubrk_previous; ubrk_last(bi); iter_val = 1; } else { iter_func = ubrk_next; iter_val = -1; } sub_str_end_pos = ustr_len; while ( length ) { sub_str_end_pos = iter_func(bi); if ( UBRK_DONE == sub_str_end_pos ) { break; } length += iter_val; } if ( UBRK_DONE == sub_str_end_pos ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_substr: length not contained in string", 1 TSRMLS_CC ); efree(ustr); ubrk_close(bi); RETURN_FALSE; } sub_str = NULL; status = U_ZERO_ERROR; intl_convert_utf16_to_utf8((char **)&sub_str, &sub_str_len, ustr + sub_str_start_pos, ( sub_str_end_pos - sub_str_start_pos ), &status); efree( ustr ); ubrk_close( bi ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 1 TSRMLS_CC ); if ( NULL != sub_str ) efree( sub_str ); RETURN_FALSE; } /* return the allocated string, not a duplicate */ RETURN_STRINGL(((char *)sub_str), sub_str_len, 0); } /* }}} */ /* {{{ strstr_common_handler */ static void strstr_common_handler(INTERNAL_FUNCTION_PARAMETERS, int f_ignore_case) { unsigned char *haystack, *needle, *found; int haystack_len, needle_len; int ret_pos, uchar_pos; zend_bool part = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", (char **)&haystack, &haystack_len, (char **)&needle, &needle_len, &part) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strstr: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } if (needle_len == 0) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 TSRMLS_CC ); RETURN_FALSE; } if ( !f_ignore_case ) { /* ASCII optimization: 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 */ found = (unsigned char *)php_memnstr((char *)haystack, (char *)needle, needle_len, (char *)haystack + haystack_len); /* if it isn't there the we are done */ if ( !found ) { RETURN_FALSE; } /* if it is there, and if the haystack is ascii, we are all done */ if ( ascii_check(haystack, haystack_len) >= 0 ) { size_t found_offset = found - haystack; if (part) { RETURN_STRINGL(((char *)haystack) , found_offset, 1); } else { RETURN_STRINGL(((char *)found), haystack_len - found_offset, 1); } } } /* need to work in utf16 */ ret_pos = strpos_utf16(haystack, haystack_len, needle, needle_len, 0, &uchar_pos, f_ignore_case TSRMLS_CC ); if ( ret_pos < 0 ) { RETURN_FALSE; } /* uchar_pos is the 'nth' Unicode character position of the needle */ ret_pos = 0; U8_FWD_N(haystack, ret_pos, haystack_len, uchar_pos); if (part) { RETURN_STRINGL(((char *)haystack), ret_pos, 1); } else { RETURN_STRINGL(((char *)haystack) + ret_pos, haystack_len - ret_pos, 1); } } /* }}} */ /* {{{ proto string grapheme_strstr(string haystack, string needle[, bool part]) Finds first occurrence of a string within another */ PHP_FUNCTION(grapheme_strstr) { strstr_common_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0 /* f_ignore_case */); } /* }}} */ /* {{{ proto string grapheme_stristr(string haystack, string needle[, bool part]) Finds first occurrence of a string within another */ PHP_FUNCTION(grapheme_stristr) { strstr_common_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1 /* f_ignore_case */); } /* }}} */ /* {{{ proto string grapheme_extract(string str, size[, start]) Function to extract a sequence of default grapheme clusters */ PHP_FUNCTION(grapheme_extract) { unsigned char *str, *pstr; UChar *ustr; int str_len, ustr_len; int size; /* maximum number of grapheme clusters to return */ int start = 0; /* starting position in str in bytes */ UErrorCode status; unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE]; UBreakIterator* bi = NULL; int ret_pos, pos, next_pos; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|l", (char **)&str, &str_len, &size, &start) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strstr: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } pstr = str + start; /* just in case pstr points in the middle of a character, move forward to the start of the next char */ if ( !UTF8_IS_SINGLE(*pstr) && !U8_IS_LEAD(*pstr) ) { unsigned char *str_end = str + str_len; while ( !UTF8_IS_SINGLE(*pstr) && !U8_IS_LEAD(*pstr) ) { pstr++; if ( pstr >= str_end ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_extract: invalid input string", 0 TSRMLS_CC ); RETURN_FALSE; } } } str_len -= (pstr - str); /* convert the strings to UTF-16. */ ustr = NULL; ustr_len = 0; status = U_ZERO_ERROR; intl_convert_utf8_to_utf16(&ustr, &ustr_len, (char *)pstr, str_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC ); if ( NULL != ustr ) efree( ustr ); RETURN_FALSE; } bi = NULL; status = U_ZERO_ERROR; bi = get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC ); ubrk_setText(bi, ustr, ustr_len, &status); /* if the caller put us in the middle of a grapheme, we can't detect it in all cases since we can't back up. So, we will not do anything. */ /* now we need to find the end of the chunk the user wants us to return */ pos = 0; while ( size ) { next_pos = ubrk_next(bi); if ( UBRK_DONE == next_pos ) { break; } pos = next_pos; size--; } efree(ustr); ubrk_close(bi); /* pos is one past the last UChar - and represent the number of code units to advance in the utf-8 buffer */ ret_pos = 0; U8_FWD_N(pstr, ret_pos, str_len, pos); RETURN_STRINGL(((char *)pstr), ret_pos, 1); } /* }}} */ /* {{{ proto string grapheme_extractB(string str, bsize[, start]) Function to extract a sequence of default grapheme clusters up to a byte limit */ PHP_FUNCTION(grapheme_extractB) { char *str, *pstr; UChar *ustr; int str_len, ustr_len; int bsize; /* maximum number of bytes to return */ int start = 0; /* starting position in str in bytes */ UErrorCode status; unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE]; UBreakIterator* bi = NULL; int ret_pos, pos, prev_pos, prev_ret_pos; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|l", (char **)&str, &str_len, &bsize, &start) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_extractB: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } pstr = str + start; /* just in case pstr points in the middle of a character, move forward to the start of the next char */ if ( !UTF8_IS_SINGLE(*pstr) && !U8_IS_LEAD(*pstr) ) { char *str_end = str + str_len; while ( !UTF8_IS_SINGLE(*pstr) && !U8_IS_LEAD(*pstr) ) { pstr++; if ( pstr >= str_end ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_extract: invalid input string", 0 TSRMLS_CC ); RETURN_FALSE; } } } str_len -= (pstr - str); /* convert the strings to UTF-16. */ ustr = NULL; ustr_len = 0; status = U_ZERO_ERROR; intl_convert_utf8_to_utf16(&ustr, &ustr_len, pstr, str_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC ); if ( NULL != ustr ) efree( ustr ); RETURN_FALSE; } bi = NULL; status = U_ZERO_ERROR; bi = get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC ); ubrk_setText(bi, ustr, ustr_len, &status); pos = 0; prev_pos = 0; ret_pos = 0; prev_ret_pos = 0; while ( 1 ) { pos = ubrk_next(bi); if ( UBRK_DONE == pos ) { break; } prev_ret_pos = ret_pos; U8_FWD_N(pstr, ret_pos, str_len, pos - prev_pos); if ( ret_pos > bsize ) { ret_pos = prev_ret_pos; break; } if ( prev_ret_pos == ret_pos ) { /* something wrong - malformed utf8? */ break; } prev_pos = pos; } efree(ustr); ubrk_close(bi); RETURN_STRINGL(pstr, ret_pos, 1); } /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: fdm=marker * vim: noet sw=4 ts=4 */ http://cvs.php.net/viewvc.cgi/pecl/unicode/grapheme/grapheme_util.h?view=markup&rev=1.1 Index: pecl/unicode/grapheme/grapheme_util.h +++ pecl/unicode/grapheme/grapheme_util.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Ed Batutis <ed@batutis.com> | +----------------------------------------------------------------------+ */ #ifndef GRAPHEME_GRAPHEME_UTIL_H #define GRAPHEME_GRAPHEME_UTIL_H #include "php_unicode.h" #include "unicode_convert.h" static UBreakIterator* get_break_iterator(void *stack_buffer, UErrorCode *status TSRMLS_DC ); /* the following are brought over from the pecl/intl package. They are not renamed but remapped so this code can be easily moved back to pecl/intl if necessary */ /* we don't have an error code mechanism in the unicode package */ #ifndef intl_error_set # define intl_error_set( err, code, msg, copyMsg ) unicode_error_set #endif #ifndef unicode_error_set # define unicode_error_set #endif #ifndef intl_error_set_code # define intl_error_set_code( x, y ) #endif #ifndef intl_error_set_custom_msg # define intl_error_set_custom_msg( x, y, z ) #endif /* memory allocation macros borrowed from pecl/intl */ #ifndef eumalloc # define eumalloc(size) (UChar*)safe_emalloc(size, sizeof(UChar), 0) #endif #ifndef eurealloc # define eurealloc(ptr, size) (UChar*)erealloc((ptr), size * sizeof(UChar)) #endif /* map intl_ routines onto unicode_ routines */ #ifndef intl_convert_utf8_to_utf16 # define intl_convert_utf8_to_utf16 unicode_convert_utf8_to_utf16 #endif #ifndef intl_convert_utf16_to_utf8 # define intl_convert_utf16_to_utf8 unicode_convert_utf16_to_utf8 #endif #endif // GRAPHEME_GRAPHEME_UTIL_H http://cvs.php.net/viewvc.cgi/pecl/unicode/tests/grapheme.phpt?view=markup&rev=1.1 Index: pecl/unicode/tests/grapheme.phpt +++ pecl/unicode/tests/grapheme.phpt --TEST-- grapheme() --SKIPIF-- <?php if( !extension_loaded( 'intl' ) || !extension_loaded( 'unicode' ) ) print 'skip'; ?> --FILE-- <?php /* * Test grapheme functions (procedural only) */ function ut_main() { $res_str = ''; $char_a_diaeresis = "\xC3\xA4"; // 'LATIN SMALL LETTER A WITH DIAERESIS' (U+00E4) $char_a_ring = "\xC3\xA5"; // 'LATIN SMALL LETTER A WITH RING ABOVE' (U+00E5) $char_o_diaeresis = "\xC3\xB6"; // 'LATIN SMALL LETTER O WITH DIAERESIS' (U+00F6) $char_O_diaeresis = "\xC3\x96"; // 'LATIN CAPITAL LETTER O WITH DIAERESIS' (U+00D6) $char_angstrom_sign = "\xE2\x84\xAB"; // 'ANGSTROM SIGN' (U+212B) $char_A_ring = "\xC3\x85"; // 'LATIN CAPITAL LETTER A WITH RING ABOVE' (U+00C5) $char_ohm_sign = "\xE2\x84\xA6"; // 'OHM SIGN' (U+2126) $char_omega = "\xCE\xA9"; // 'GREEK CAPITAL LETTER OMEGA' (U+03A9) $char_combining_ring_above = "\xCC\x8A"; // 'COMBINING RING ABOVE' (U+030A) $char_fi_ligature = "\xEF\xAC\x81"; // 'LATIN SMALL LIGATURE FI' (U+FB01) $char_long_s_dot = "\xE1\xBA\x9B"; // 'LATIN SMALL LETTER LONG S WITH DOT ABOVE' (U+1E9B) // the word 'hindi' using Devanagari characters: $hindi = "\xe0\xa4\xb9\xe0\xa4\xbf\xe0\xa4\xa8\xe0\xa5\x8d\xe0\xa4\xa6\xe0\xa5\x80"; $char_a_ring_nfd = "a\xCC\x8A"; $char_A_ring_nfd = "A\xCC\x8A"; $char_o_diaeresis_nfd = "o\xCC\x88"; $char_O_diaeresis_nfd = "O\xCC\x88"; $char_diaeresis = "\xCC\x88"; //===================================================================================== $res_str .= "\n" . 'function grapheme_strlen($string) {}' . "\n\n"; $res_str .= "\"hindi\" in devanagari strlen " . grapheme_strlen($hindi) . "\n"; $res_str .= "\"ab\" + \"hindi\" + \"cde\" strlen " . grapheme_strlen('ab' . $hindi . 'cde') . "\n"; $res_str .= "\"\" strlen " . grapheme_strlen("") . "\n"; $res_str .= "char_a_ring_nfd strlen " . grapheme_strlen($char_a_ring_nfd) . "\n"; $res_str .= "char_a_ring_nfd + \"bc\" strlen " . grapheme_strlen($char_a_ring_nfd . 'bc') . "\n"; $res_str .= "\"abc\" strlen " . grapheme_strlen('abc') . "\n"; //===================================================================================== $res_str .= "\n" . 'function grapheme_strpos($haystack, $needle, $offset = 0) {}' . "\n\n"; $tests = array( array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o", "o", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "o", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, $char_o_diaeresis_nfd, 4 ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, 2 ), array( "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, 1 ), array( "abc", $char_a_ring_nfd, "false" ), array( $char_a_ring_nfd . "bc", "a", "false" ), array( "abc", "d", "false" ), array( "abc", "c", 2 ), array( "abc", "b", 1 ), array( "abc", "a", 0 ), array( "abc", "a", 0, 0 ), array( "abc", "a", 1, "false" ), array( "ababc", "a", 1, 2 ), array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o", "o", 2, 6 ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, 2, 3 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "opq", "op", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "opq", "opq", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "abc", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "bc" . $char_o_diaeresis_nfd, $char_o_diaeresis_nfd . "bc" . $char_o_diaeresis_nfd, 4 ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd . "bc", 2 ), array( "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd . "bc", 1 ), array( "abc", $char_a_ring_nfd . "bc", "false" ), array( $char_a_ring_nfd . "bc", "abcdefg", "false" ), array( "abc", "defghijklmnopq", "false" ), array( "abc", "ab", 0 ), array( "abc", "bc", 1 ), array( "abc", "abc", 0 ), array( "abc", "abcd", "false" ), array( "abc", "ab", 0, 0 ), array( "abc", "abc", 0, 0 ), array( "abc", "abc", 1, "false" ), array( "ababc", "ab", 1, 2 ), array( "ababc", "abc", 1, 2 ), array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o" . $char_a_ring_nfd . "bc", "o" . $char_a_ring_nfd . "bc", 2, 6 ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_a_ring_nfd . "bc" . $char_a_ring_nfd . "def", $char_a_ring_nfd . "bc" . $char_a_ring_nfd, 2, 3 ), ); foreach( $tests as $test ) { $arg1 = urlencode($test[1]); $arg0 = urlencode($test[0]); $res_str .= "find \"$arg1\" in \"$arg0\" - grapheme_strpos"; if ( 3 == count( $test ) ) { $result = grapheme_strpos($test[0], $test[1]); } else { $res_str .= " from $test[2]"; $result = grapheme_strpos($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= $result; } $res_str .= " == " . $test[count($test)-1] . "\n"; } //===================================================================================== $res_str .= "\n" . 'function grapheme_stripos($haystack, $needle, $offset = 0) {}' . "\n\n"; $tests = array( array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", "o", 2, 6 ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_A_ring_nfd . "bc", $char_a_ring_nfd, 2, 3 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", "o", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "O", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_O_diaeresis_nfd, $char_o_diaeresis_nfd, 4 ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_A_ring_nfd, 2 ), array( "a" . $char_A_ring_nfd . "bc", $char_a_ring_nfd, 1 ), array( "Abc", $char_a_ring_nfd, "false" ), array( $char_a_ring_nfd . "bc", "A", "false" ), array( "abc", "D", "false" ), array( "abC", "c", 2 ), array( "abc", "B", 1 ), array( "Abc", "a", 0 ), array( "abc", "A", 0, 0 ), array( "Abc", "a", 1, "false" ), array( "ababc", "A", 1, 2 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", "oP", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", "opQ", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "abc", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "bC" . $char_o_diaeresis_nfd, $char_O_diaeresis_nfd . "bc" . $char_o_diaeresis_nfd, 4 ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "Bc", $char_A_ring_nfd . "bc", 2 ), array( "a" . $char_a_ring_nfd . "BC", $char_a_ring_nfd . "bc", 1 ), array( "abc", $char_a_ring_nfd . "BC", "false" ), array( $char_a_ring_nfd . "BC", "aBCdefg", "false" ), array( "aBC", "Defghijklmnopq", "false" ), array( "abC", "Ab", 0 ), array( "aBC", "bc", 1 ), array( "abC", "Abc", 0 ), array( "abC", "aBcd", "false" ), array( "ABc", "ab", 0, 0 ), array( "aBc", "abC", 0, 0 ), array( "abc", "aBc", 1, "false" ), array( "ABabc", "AB", 1, 2 ), array( "abaBc", "aBc", 1, 2 ), array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o" . $char_A_ring_nfd . "bC", "O" . $char_a_ring_nfd . "bC", 2, 6 ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_A_ring_nfd . "bC" . $char_a_ring_nfd . "def", $char_a_ring_nfd . "Bc" . $char_a_ring_nfd, 2, 3 ), ); foreach( $tests as $test ) { $arg1 = urlencode($test[1]); $arg0 = urlencode($test[0]); $res_str .= "find \"$arg1\" in \"$arg0\" - grapheme_stripos"; if ( 3 == count( $test ) ) { $result = grapheme_stripos($test[0], $test[1]); } else { $res_str .= " from $test[2]"; $result = grapheme_stripos($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= $result; } $res_str .= " == " . $test[count($test)-1] . "\n"; } //===================================================================================== $res_str .= "\n" . 'function grapheme_strrpos($haystack, $needle, $offset = 0) {}' . "\n\n"; $tests = array( array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o", "o", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "o", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, $char_o_diaeresis_nfd, 4 ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, 2 ), array( "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, 1 ), array( "abc", $char_a_ring_nfd, "false" ), array( $char_a_ring_nfd . "bc", "a", "false" ), array( "abc", "d", "false" ), array( "abc", "c", 2 ), array( "abc", "b", 1 ), array( "abc", "a", 0 ), array( "abc", "a", 0, 0 ), array( "abc", "a", 1, "false" ), array( "ababc", "a", 1, 2 ), array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o", "o", 2, 6 ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, 2, 3 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "opq", "op", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "opq", "opq", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "abc", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "bc" . $char_o_diaeresis_nfd, $char_o_diaeresis_nfd . "bc" . $char_o_diaeresis_nfd, 4 ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd . "bc", 2 ), array( "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd . "bc", 1 ), array( "abc", $char_a_ring_nfd . "bc", "false" ), array( $char_a_ring_nfd . "bc", "abcdefg", "false" ), array( "abc", "defghijklmnopq", "false" ), array( "abc", "ab", 0 ), array( "abc", "bc", 1 ), array( "abc", "abc", 0 ), array( "abc", "abcd", "false" ), array( "abc", "ab", 0, 0 ), array( "abc", "abc", 0, 0 ), array( "abc", "abc", 1, "false" ), array( "ababc", "ab", 1, 2 ), array( "ababc", "abc", 1, 2 ), array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o" . $char_a_ring_nfd . "bc", "o" . $char_a_ring_nfd . "bc", 2, 6 ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_a_ring_nfd . "bc" . $char_a_ring_nfd . "def", $char_a_ring_nfd . "bc" . $char_a_ring_nfd, 2, 3 ), ); foreach( $tests as $test ) { $arg1 = urlencode($test[1]); $arg0 = urlencode($test[0]); $res_str .= "find \"$arg1\" in \"$arg0\" - grapheme_strrpos"; if ( 3 == count( $test ) ) { $result = grapheme_strrpos($test[0], $test[1]); } else { $res_str .= " from $test[2]"; $result = grapheme_strrpos($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= $result; } $res_str .= " == " . $test[count($test)-1] . "\n"; } //===================================================================================== $res_str .= "\n" . 'function grapheme_strripos($haystack, $needle, $offset = 0) {}' . "\n\n"; $tests = array( array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", "o", 2, 6 ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_A_ring_nfd . "bc", $char_a_ring_nfd, 2, 3 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", "o", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "O", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_O_diaeresis_nfd, $char_o_diaeresis_nfd, 4 ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_A_ring_nfd, 2 ), array( "a" . $char_A_ring_nfd . "bc", $char_a_ring_nfd, 1 ), array( "Abc", $char_a_ring_nfd, "false" ), array( $char_a_ring_nfd . "bc", "A", "false" ), array( "abc", "D", "false" ), array( "abC", "c", 2 ), array( "abc", "B", 1 ), array( "Abc", "a", 0 ), array( "abc", "A", 0, 0 ), array( "Abc", "a", 1, "false" ), array( "ababc", "A", 1, 2 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", "oP", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", "opQ", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "abc", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "bC" . $char_o_diaeresis_nfd, $char_O_diaeresis_nfd . "bc" . $char_o_diaeresis_nfd, 4 ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "Bc", $char_A_ring_nfd . "bc", 2 ), array( "a" . $char_a_ring_nfd . "BC", $char_a_ring_nfd . "bc", 1 ), array( "abc", $char_a_ring_nfd . "BC", "false" ), array( $char_a_ring_nfd . "BC", "aBCdefg", "false" ), array( "aBC", "Defghijklmnopq", "false" ), array( "abC", "Ab", 0 ), array( "aBC", "bc", 1 ), array( "abC", "Abc", 0 ), array( "abC", "aBcd", "false" ), array( "ABc", "ab", 0, 0 ), array( "aBc", "abC", 0, 0 ), array( "abc", "aBc", 1, "false" ), array( "ABabc", "AB", 1, 2 ), array( "abaBc", "aBc", 1, 2 ), array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o" . $char_A_ring_nfd . "bC", "O" . $char_a_ring_nfd . "bC", 2, 6 ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_A_ring_nfd . "bC" . $char_a_ring_nfd . "def", $char_a_ring_nfd . "Bc" . $char_a_ring_nfd, 2, 3 ), ); foreach( $tests as $test ) { $arg1 = urlencode($test[1]); $arg0 = urlencode($test[0]); $res_str .= "find \"$arg1\" in \"$arg0\" - grapheme_strripos"; if ( 3 == count( $test ) ) { $result = grapheme_strripos($test[0], $test[1]); } else { $res_str .= " from $test[2]"; $result = grapheme_strripos($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= $result; } $res_str .= " == " . $test[count($test)-1] . "\n"; } //===================================================================================== $res_str .= "\n" . 'function grapheme_substr($string, $start, $length = -1) {}' . "\n\n"; $tests = array( array( "abc", 3, "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, 5, "false" ), array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", 2, $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O" ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_A_ring_nfd . "bc", 2, "a" . $char_A_ring_nfd . "bc" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", 5, "O" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, 5, "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_O_diaeresis_nfd, 4, $char_O_diaeresis_nfd ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", 2, $char_a_ring_nfd . "bc" ), array( "a" . $char_A_ring_nfd . "bc", 1, $char_A_ring_nfd . "bc" ), array( "Abc", -5, "false" ), array( $char_a_ring_nfd . "bc", 3, "false" ), array( "abc", 4, "false" ), array( "abC", 2, "C" ), array( "abc", 1, "bc" ), array( "Abc", 1, 1, "b" ), array( "abc", 0, 2, "ab" ), array( "Abc", -4, 1, "false" ), array( "ababc", 1, 2, "ba" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 5, "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 5, -1, "Op" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 5, -2, "O" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 5, -3, "" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 5, -4, "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -1, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Op" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -2, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -3, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -4, "a" . $char_a_ring_nfd . "bc" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -5, "a" . $char_a_ring_nfd . "b" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -6, "a" . $char_a_ring_nfd ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -7, "a" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -8, "" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -9, "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -7, $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -6, "bc" . $char_o_diaeresis_nfd . "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -5, "c" . $char_o_diaeresis_nfd . "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -4, $char_o_diaeresis_nfd . "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -3, "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -2, "pq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -1, "q" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -999, "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 8, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 7, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Op" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 6, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 5, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 4, "a" . $char_a_ring_nfd . "bc" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 3, "a" . $char_a_ring_nfd . "b" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 2, "a" . $char_a_ring_nfd ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 1, "a" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 0, "" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -999, "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -1, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Op" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -2, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -3, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -4, "a" . $char_a_ring_nfd . "bc" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -5, "a" . $char_a_ring_nfd . "b" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -6, "a" . $char_a_ring_nfd ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -7, "a" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -8, "" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -9, "false" ), ); foreach( $tests as $test ) { $arg0 = urlencode($test[0]); $res_str .= "substring of \"$arg0\" from \"$test[1]\" - grapheme_substr"; if ( 3 == count( $test ) ) { $result = grapheme_substr($test[0], $test[1]); } else { $res_str .= " with length $test[2]"; $result = grapheme_substr($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= urlencode($result); } $res_str .= " == " . urlencode($test[count($test)-1]) . "\n"; } //===================================================================================== $res_str .= "\n" . 'function grapheme_strstr($haystack, $needle, $before_needle = FALSE) {}' . "\n\n"; $tests = array( array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o", "o", "o" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "o", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, $char_o_diaeresis_nfd, $char_o_diaeresis_nfd ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, $char_a_ring_nfd . "bc"), array( "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, $char_a_ring_nfd . "bc"), array( "abc", $char_a_ring_nfd, "false" ), array( $char_a_ring_nfd . "bc", "a", "false" ), array( "abc", "d", "false" ), array( "abc", "c", "c" ), array( "abc", "b", "bc" ), array( "abc", "a", "abc" ), array( "abc", "ab", "abc" ), array( "abc", "abc", "abc" ), array( "abc", "bc", "bc" ), array( "abc", "a", FALSE, "abc" ), array( "abc", "a", TRUE, "" ), array( "abc", "b", TRUE, "a" ), array( "abc", "c", TRUE, "ab" ), array( "ababc", "bab", TRUE, "a" ), array( "ababc", "abc", TRUE, "ab" ), array( "ababc", "abc", FALSE, "abc" ), array( "ab" . $char_a_ring_nfd . "c", "d", "false" ), array( "bc" . $char_a_ring_nfd . "a", "a", "a" ), array( "a" . $char_a_ring_nfd . "bc", "b", "bc" ), array( $char_a_ring_nfd . "bc", "a", "false" ), array( $char_a_ring_nfd . "abc", "ab", "abc" ), array( "abc" . $char_a_ring_nfd, "abc", "abc" . $char_a_ring_nfd), array( "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd . "bc", $char_a_ring_nfd . "bc" ), array( "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, FALSE, $char_a_ring_nfd . "bc"), array( "a" . $char_a_ring_nfd . "bc", "a", TRUE, "" ), array( $char_a_ring_nfd . "abc", "b", TRUE, $char_a_ring_nfd . "a" ), array( "ab" . $char_a_ring_nfd . "c", "c", TRUE, "ab" . $char_a_ring_nfd ), array( "aba" . $char_a_ring_nfd . "bc", "ba" . $char_a_ring_nfd . "b", TRUE, "a" ), array( "ababc" . $char_a_ring_nfd, "abc" . $char_a_ring_nfd, TRUE, "ab" ), array( "abab" . $char_a_ring_nfd . "c", "ab" . $char_a_ring_nfd . "c", FALSE, "ab" . $char_a_ring_nfd . "c" ), ); foreach( $tests as $test ) { $arg1 = urlencode($test[1]); $arg0 = urlencode($test[0]); $res_str .= "find \"$arg1\" in \"$arg0\" - grapheme_strstr"; if ( 3 == count( $test ) ) { $result = grapheme_strstr($test[0], $test[1]); } else { $res_str .= " before flag is " . ( $test[2] ? "TRUE" : "FALSE" ); $result = grapheme_strstr($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= urlencode($result); } $res_str .= " == " . urlencode($test[count($test)-1]) . "\n"; } //===================================================================================== $res_str .= "\n" . 'function grapheme_stristr($haystack, $needle, $before_needle = FALSE) {}' . "\n\n"; $tests = array( array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, $char_O_diaeresis_nfd, $char_o_diaeresis_nfd ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", "o", "O" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "o", "false" ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, $char_a_ring_nfd . "bc"), array( "a" . $char_a_ring_nfd . "bc", $char_A_ring_nfd, $char_a_ring_nfd . "bc"), array( "abc", $char_a_ring_nfd, "false" ), array( $char_a_ring_nfd . "bc", "A", "false" ), array( "abc", "d", "false" ), array( "abc", "C", "c" ), array( "aBc", "b", "Bc" ), array( "abc", "A", "abc" ), array( "abC", "ab", "abC" ), array( "abc", "aBc", "abc" ), array( "abC", "bc", "bC" ), array( "abc", "A", FALSE, "abc" ), array( "abc", "a", TRUE, "" ), array( "aBc", "b", TRUE, "a" ), array( "abc", "C", TRUE, "ab" ), array( "aBabc", "bab", TRUE, "a" ), array( "ababc", "aBc", TRUE, "ab" ), array( "ababc", "abC", FALSE, "abc" ), array( "ab" . $char_a_ring_nfd . "c", "d", "false" ), array( "bc" . $char_a_ring_nfd . "A", "a", "A" ), array( "a" . $char_a_ring_nfd . "bc", "B", "bc" ), array( $char_A_ring_nfd . "bc", "a", "false" ), array( $char_a_ring_nfd . "abc", "Ab", "abc" ), array( "abc" . $char_A_ring_nfd, "abc", "abc" . $char_A_ring_nfd), array( "a" . $char_a_ring_nfd . "bc", $char_A_ring_nfd . "bc", $char_a_ring_nfd . "bc" ), array( "a" . $char_A_ring_nfd . "bc", $char_a_ring_nfd, FALSE, $char_A_ring_nfd . "bc" ), array( "a" . $char_a_ring_nfd . "bc", "A", TRUE, "" ), array( $char_a_ring_nfd . "aBc", "b", TRUE, $char_a_ring_nfd . "a" ), array( "ab" . $char_a_ring_nfd . "c", "C", TRUE, "ab" . $char_a_ring_nfd ), array( "aba" . $char_A_ring_nfd . "bc", "ba" . $char_a_ring_nfd . "b", TRUE, "a" ), array( "ababc" . $char_a_ring_nfd, "aBc" . $char_A_ring_nfd, TRUE, "ab" ), array( "abAB" . $char_A_ring_nfd . "c", "ab" . $char_a_ring_nfd . "c", FALSE, "AB" . $char_A_ring_nfd . "c" ), ); foreach( $tests as $test ) { $arg1 = urlencode($test[1]); $arg0 = urlencode($test[0]); $res_str .= "find \"$arg1\" in \"$arg0\" - grapheme_stristr"; if ( 3 == count( $test ) ) { $result = grapheme_stristr($test[0], $test[1]); } else { $res_str .= " before flag is " . ( $test[2] ? "TRUE" : "FALSE" ); $result = grapheme_stristr($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= urlencode($result); } $res_str .= " == " . urlencode($test[count($test)-1]) . "\n"; } //===================================================================================== $res_str .= "\n" . 'function grapheme_extract($haystack, $size, $start = 0)' . "\n\n"; $tests = array( array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 1, 4, $char_o_diaeresis_nfd), array( "abc", 3, "abc" ), array( "abc", 2, "ab" ), array( "abc", 1, "a" ), array( "abc", 0, "" ), array( $char_a_ring_nfd . "bc", 3, $char_a_ring_nfd . "bc" ), array( $char_a_ring_nfd . "bc", 2, $char_a_ring_nfd . "b" ), array( $char_a_ring_nfd . "bc", 1, $char_a_ring_nfd . "" ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 3, $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, $char_a_ring_nfd . $char_o_diaeresis_nfd ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . "c", 1, $char_a_ring_nfd . "" ), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 1, 0, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 1, 2, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 1, 3, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 1, 4, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 0, $char_o_diaeresis_nfd . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 2, $char_o_diaeresis_nfd . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 3, $char_o_diaeresis_nfd . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 4, $char_diaeresis . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 7, $char_diaeresis . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 8, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 10, $char_diaeresis), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 11, "false"), ); foreach( $tests as $test ) { $arg0 = urlencode($test[0]); $res_str .= "extract from \"$arg0\" \"$test[1]\" graphemes - grapheme_extract"; if ( 3 == count( $test ) ) { $result = grapheme_extract($test[0], $test[1]); } else { $res_str .= " starting at byte position $test[2]"; $result = grapheme_extract($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= urlencode($result); } $res_str .= " == " . urlencode($test[count($test)-1]) . "\n"; } //===================================================================================== $res_str .= "\n" . 'function grapheme_extractB($haystack, $size, $start = 0)' . "\n\n"; $tests = array( array( "abc", 3, "abc" ), array( "abc", 2, "ab" ), array( "abc", 1, "a" ), array( "abc", 0, "" ), array( $char_a_ring_nfd . "bc", 5, $char_a_ring_nfd . "bc" ), array( $char_a_ring_nfd . "bc", 4, $char_a_ring_nfd . "b" ), array( $char_a_ring_nfd . "bc", 1, "" ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 9, $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 10, $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 11, $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 6, $char_a_ring_nfd . $char_o_diaeresis_nfd ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . "c", 3, $char_a_ring_nfd . "" ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . "c", 4, $char_a_ring_nfd . "" ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . "c", 5, $char_a_ring_nfd . "" ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . "c", 6, $char_a_ring_nfd . $char_o_diaeresis_nfd ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . "c", 7, $char_a_ring_nfd . $char_o_diaeresis_nfd . "c" ), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 3, 0, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 3, 2, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 3, 3, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 3, 4, $char_diaeresis), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 6, 0, $char_o_diaeresis_nfd . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 6, 2, $char_o_diaeresis_nfd . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 6, 3, $char_o_diaeresis_nfd . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 5, 4, $char_diaeresis . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 5, 7, $char_diaeresis . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 3, 8, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 10, $char_diaeresis), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 11, "false"), ); foreach( $tests as $test ) { $arg0 = urlencode($test[0]); $res_str .= "extract from \"$arg0\" \"$test[1]\" graphemes - grapheme_extractB"; if ( 3 == count( $test ) ) { $result = grapheme_extractB($test[0], $test[1]); } else { $res_str .= " starting at byte position $test[2]"; $result = grapheme_extractB($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= urlencode($result); } $res_str .= " == " . urlencode($test[count($test)-1]) . "\n"; } //===================================================================================== return $res_str; } echo ut_main(); ?> --EXPECT-- function grapheme_strlen($string) {} "hindi" in devanagari strlen 5 "ab" + "hindi" + "cde" strlen 10 "" strlen 0 char_a_ring_nfd strlen 1 char_a_ring_nfd + "bc" strlen 3 "abc" strlen 3 function grapheme_strpos($haystack, $needle, $offset = 0) {} find "o" in "aa%CC%8Abco%CC%88o" - grapheme_strpos = 5 == 5 find "o" in "aa%CC%8Abco%CC%88" - grapheme_strpos = false == false find "o%CC%88" in "aa%CC%8Abco%CC%88" - grapheme_strpos = 4 == 4 find "a%CC%8A" in "o%CC%88aa%CC%8Abc" - grapheme_strpos = 2 == 2 find "a%CC%8A" in "aa%CC%8Abc" - grapheme_strpos = 1 == 1 find "a%CC%8A" in "abc" - grapheme_strpos = false == false find "a" in "a%CC%8Abc" - grapheme_strpos = false == false find "d" in "abc" - grapheme_strpos = false == false find "c" in "abc" - grapheme_strpos = 2 == 2 find "b" in "abc" - grapheme_strpos = 1 == 1 find "a" in "abc" - grapheme_strpos = 0 == 0 find "a" in "abc" - grapheme_strpos from 0 = 0 == 0 find "a" in "abc" - grapheme_strpos from 1 = false == false find "a" in "ababc" - grapheme_strpos from 1 = 2 == 2 find "o" in "aoa%CC%8Abco%CC%88o" - grapheme_strpos from 2 = 6 == 6 find "a%CC%8A" in "o%CC%88a%CC%8Aaa%CC%8Abc" - grapheme_strpos from 2 = 3 == 3 find "op" in "aa%CC%8Abco%CC%88opq" - grapheme_strpos = 5 == 5 find "opq" in "aa%CC%8Abco%CC%88opq" - grapheme_strpos = 5 == 5 find "abc" in "aa%CC%8Abco%CC%88" - grapheme_strpos = false == false find "o%CC%88bco%CC%88" in "aa%CC%8Abco%CC%88bco%CC%88" - grapheme_strpos = 4 == 4 find "a%CC%8Abc" in "o%CC%88aa%CC%8Abc" - grapheme_strpos = 2 == 2 find "a%CC%8Abc" in "aa%CC%8Abc" - grapheme_strpos = 1 == 1 find "a%CC%8Abc" in "abc" - grapheme_strpos = false == false find "abcdefg" in "a%CC%8Abc" - grapheme_strpos = false == false find "defghijklmnopq" in "abc" - grapheme_strpos = false == false find "ab" in "abc" - grapheme_strpos = 0 == 0 find "bc" in "abc" - grapheme_strpos = 1 == 1 find "abc" in "abc" - grapheme_strpos = 0 == 0 find "abcd" in "abc" - grapheme_strpos = false == false find "ab" in "abc" - grapheme_strpos from 0 = 0 == 0 find "abc" in "abc" - grapheme_strpos from 0 = 0 == 0 find "abc" in "abc" - grapheme_strpos from 1 = false == false find "ab" in "ababc" - grapheme_strpos from 1 = 2 == 2 find "abc" in "ababc" - grapheme_strpos from 1 = 2 == 2 find "oa%CC%8Abc" in "aoa%CC%8Abco%CC%88oa%CC%8Abc" - grapheme_strpos from 2 = 6 == 6 find "a%CC%8Abca%CC%8A" in "o%CC%88a%CC%8Aaa%CC%8Abca%CC%8Adef" - grapheme_strpos from 2 = 3 == 3 function grapheme_stripos($haystack, $needle, $offset = 0) {} find "o" in "aoa%CC%8Abco%CC%88O" - grapheme_stripos from 2 = 6 == 6 find "a%CC%8A" in "o%CC%88a%CC%8AaA%CC%8Abc" - grapheme_stripos from 2 = 3 == 3 find "o" in "aa%CC%8Abco%CC%88O" - grapheme_stripos = 5 == 5 find "O" in "aa%CC%8Abco%CC%88" - grapheme_stripos = false == false find "o%CC%88" in "aa%CC%8AbcO%CC%88" - grapheme_stripos = 4 == 4 find "A%CC%8A" in "o%CC%88aa%CC%8Abc" - grapheme_stripos = 2 == 2 find "a%CC%8A" in "aA%CC%8Abc" - grapheme_stripos = 1 == 1 find "a%CC%8A" in "Abc" - grapheme_stripos = false == false find "A" in "a%CC%8Abc" - grapheme_stripos = false == false find "D" in "abc" - grapheme_stripos = false == false find "c" in "abC" - grapheme_stripos = 2 == 2 find "B" in "abc" - grapheme_stripos = 1 == 1 find "a" in "Abc" - grapheme_stripos = 0 == 0 find "A" in "abc" - grapheme_stripos from 0 = 0 == 0 find "a" in "Abc" - grapheme_stripos from 1 = false == false find "A" in "ababc" - grapheme_stripos from 1 = 2 == 2 find "oP" in "aa%CC%8Abco%CC%88Opq" - grapheme_stripos = 5 == 5 find "opQ" in "aa%CC%8Abco%CC%88Opq" - grapheme_stripos = 5 == 5 find "abc" in "aa%CC%8Abco%CC%88" - grapheme_stripos = false == false find "O%CC%88bco%CC%88" in "aa%CC%8Abco%CC%88bCo%CC%88" - grapheme_stripos = 4 == 4 find "A%CC%8Abc" in "o%CC%88aa%CC%8ABc" - grapheme_stripos = 2 == 2 find "a%CC%8Abc" in "aa%CC%8ABC" - grapheme_stripos = 1 == 1 find "a%CC%8ABC" in "abc" - grapheme_stripos = false == false find "aBCdefg" in "a%CC%8ABC" - grapheme_stripos = false == false find "Defghijklmnopq" in "aBC" - grapheme_stripos = false == false find "Ab" in "abC" - grapheme_stripos = 0 == 0 find "bc" in "aBC" - grapheme_stripos = 1 == 1 find "Abc" in "abC" - grapheme_stripos = 0 == 0 find "aBcd" in "abC" - grapheme_stripos = false == false find "ab" in "ABc" - grapheme_stripos from 0 = 0 == 0 find "abC" in "aBc" - grapheme_stripos from 0 = 0 == 0 find "aBc" in "abc" - grapheme_stripos from 1 = false == false find "AB" in "ABabc" - grapheme_stripos from 1 = 2 == 2 find "aBc" in "abaBc" - grapheme_stripos from 1 = 2 == 2 find "Oa%CC%8AbC" in "aoa%CC%8Abco%CC%88oA%CC%8AbC" - grapheme_stripos from 2 = 6 == 6 find "a%CC%8ABca%CC%8A" in "o%CC%88a%CC%8AaA%CC%8AbCa%CC%8Adef" - grapheme_stripos from 2 = 3 == 3 function grapheme_strrpos($haystack, $needle, $offset = 0) {} find "o" in "aa%CC%8Abco%CC%88o" - grapheme_strrpos = 5 == 5 find "o" in "aa%CC%8Abco%CC%88" - grapheme_strrpos = false == false find "o%CC%88" in "aa%CC%8Abco%CC%88" - grapheme_strrpos = 4 == 4 find "a%CC%8A" in "o%CC%88aa%CC%8Abc" - grapheme_strrpos = 2 == 2 find "a%CC%8A" in "aa%CC%8Abc" - grapheme_strrpos = 1 == 1 find "a%CC%8A" in "abc" - grapheme_strrpos = false == false find "a" in "a%CC%8Abc" - grapheme_strrpos = false == false find "d" in "abc" - grapheme_strrpos = false == false find "c" in "abc" - grapheme_strrpos = 2 == 2 find "b" in "abc" - grapheme_strrpos = 1 == 1 find "a" in "abc" - grapheme_strrpos = 0 == 0 find "a" in "abc" - grapheme_strrpos from 0 = 0 == 0 find "a" in "abc" - grapheme_strrpos from 1 = false == false find "a" in "ababc" - grapheme_strrpos from 1 = 2 == 2 find "o" in "aoa%CC%8Abco%CC%88o" - grapheme_strrpos from 2 = 6 == 6 find "a%CC%8A" in "o%CC%88a%CC%8Aaa%CC%8Abc" - grapheme_strrpos from 2 = 3 == 3 find "op" in "aa%CC%8Abco%CC%88opq" - grapheme_strrpos = 5 == 5 find "opq" in "aa%CC%8Abco%CC%88opq" - grapheme_strrpos = 5 == 5 find "abc" in "aa%CC%8Abco%CC%88" - grapheme_strrpos = false == false find "o%CC%88bco%CC%88" in "aa%CC%8Abco%CC%88bco%CC%88" - grapheme_strrpos = 4 == 4 find "a%CC%8Abc" in "o%CC%88aa%CC%8Abc" - grapheme_strrpos = 2 == 2 find "a%CC%8Abc" in "aa%CC%8Abc" - grapheme_strrpos = 1 == 1 find "a%CC%8Abc" in "abc" - grapheme_strrpos = false == false find "abcdefg" in "a%CC%8Abc" - grapheme_strrpos = false == false find "defghijklmnopq" in "abc" - grapheme_strrpos = false == false find "ab" in "abc" - grapheme_strrpos = 0 == 0 find "bc" in "abc" - grapheme_strrpos = 1 == 1 find "abc" in "abc" - grapheme_strrpos = 0 == 0 find "abcd" in "abc" - grapheme_strrpos = false == false find "ab" in "abc" - grapheme_strrpos from 0 = 0 == 0 find "abc" in "abc" - grapheme_strrpos from 0 = 0 == 0 find "abc" in "abc" - grapheme_strrpos from 1 = false == false find "ab" in "ababc" - grapheme_strrpos from 1 = 2 == 2 find "abc" in "ababc" - grapheme_strrpos from 1 = 2 == 2 find "oa%CC%8Abc" in "aoa%CC%8Abco%CC%88oa%CC%8Abc" - grapheme_strrpos from 2 = 6 == 6 find "a%CC%8Abca%CC%8A" in "o%CC%88a%CC%8Aaa%CC%8Abca%CC%8Adef" - grapheme_strrpos from 2 = 3 == 3 function grapheme_strripos($haystack, $needle, $offset = 0) {} find "o" in "aoa%CC%8Abco%CC%88O" - grapheme_strripos from 2 = 6 == 6 find "a%CC%8A" in "o%CC%88a%CC%8AaA%CC%8Abc" - grapheme_strripos from 2 = 3 == 3 find "o" in "aa%CC%8Abco%CC%88O" - grapheme_strripos = 5 == 5 find "O" in "aa%CC%8Abco%CC%88" - grapheme_strripos = false == false find "o%CC%88" in "aa%CC%8AbcO%CC%88" - grapheme_strripos = 4 == 4 find "A%CC%8A" in "o%CC%88aa%CC%8Abc" - grapheme_strripos = 2 == 2 find "a%CC%8A" in "aA%CC%8Abc" - grapheme_strripos = 1 == 1 find "a%CC%8A" in "Abc" - grapheme_strripos = false == false find "A" in "a%CC%8Abc" - grapheme_strripos = false == false find "D" in "abc" - grapheme_strripos = false == false find "c" in "abC" - grapheme_strripos = 2 == 2 find "B" in "abc" - grapheme_strripos = 1 == 1 find "a" in "Abc" - grapheme_strripos = 0 == 0 find "A" in "abc" - grapheme_strripos from 0 = 0 == 0 find "a" in "Abc" - grapheme_strripos from 1 = false == false find "A" in "ababc" - grapheme_strripos from 1 = 2 == 2 find "oP" in "aa%CC%8Abco%CC%88Opq" - grapheme_strripos = 5 == 5 find "opQ" in "aa%CC%8Abco%CC%88Opq" - grapheme_strripos = 5 == 5 find "abc" in "aa%CC%8Abco%CC%88" - grapheme_strripos = false == false find "O%CC%88bco%CC%88" in "aa%CC%8Abco%CC%88bCo%CC%88" - grapheme_strripos = 4 == 4 find "A%CC%8Abc" in "o%CC%88aa%CC%8ABc" - grapheme_strripos = 2 == 2 find "a%CC%8Abc" in "aa%CC%8ABC" - grapheme_strripos = 1 == 1 find "a%CC%8ABC" in "abc" - grapheme_strripos = false == false find "aBCdefg" in "a%CC%8ABC" - grapheme_strripos = false == false find "Defghijklmnopq" in "aBC" - grapheme_strripos = false == false find "Ab" in "abC" - grapheme_strripos = 0 == 0 find "bc" in "aBC" - grapheme_strripos = 1 == 1 find "Abc" in "abC" - grapheme_strripos = 0 == 0 find "aBcd" in "abC" - grapheme_strripos = false == false find "ab" in "ABc" - grapheme_strripos from 0 = 0 == 0 find "abC" in "aBc" - grapheme_strripos from 0 = 0 == 0 find "aBc" in "abc" - grapheme_strripos from 1 = false == false find "AB" in "ABabc" - grapheme_strripos from 1 = 2 == 2 find "aBc" in "abaBc" - grapheme_strripos from 1 = 2 == 2 find "Oa%CC%8AbC" in "aoa%CC%8Abco%CC%88oA%CC%8AbC" - grapheme_strripos from 2 = 6 == 6 find "a%CC%8ABca%CC%8A" in "o%CC%88a%CC%8AaA%CC%8AbCa%CC%8Adef" - grapheme_strripos from 2 = 3 == 3 function grapheme_substr($string, $start, $length = -1) {} substring of "abc" from "3" - grapheme_substr = false == false substring of "aa%CC%8Abco%CC%88" from "5" - grapheme_substr = false == false substring of "aoa%CC%8Abco%CC%88O" from "2" - grapheme_substr = a%CC%8Abco%CC%88O == a%CC%8Abco%CC%88O substring of "o%CC%88a%CC%8AaA%CC%8Abc" from "2" - grapheme_substr = aA%CC%8Abc == aA%CC%8Abc substring of "aa%CC%8Abco%CC%88O" from "5" - grapheme_substr = O == O substring of "aa%CC%8Abco%CC%88" from "5" - grapheme_substr = false == false substring of "aa%CC%8AbcO%CC%88" from "4" - grapheme_substr = O%CC%88 == O%CC%88 substring of "o%CC%88aa%CC%8Abc" from "2" - grapheme_substr = a%CC%8Abc == a%CC%8Abc substring of "aA%CC%8Abc" from "1" - grapheme_substr = A%CC%8Abc == A%CC%8Abc substring of "Abc" from "-5" - grapheme_substr = false == false substring of "a%CC%8Abc" from "3" - grapheme_substr = false == false substring of "abc" from "4" - grapheme_substr = false == false substring of "abC" from "2" - grapheme_substr = C == C substring of "abc" from "1" - grapheme_substr = bc == bc substring of "Abc" from "1" - grapheme_substr with length 1 = b == b substring of "abc" from "0" - grapheme_substr with length 2 = ab == ab substring of "Abc" from "-4" - grapheme_substr with length 1 = false == false substring of "ababc" from "1" - grapheme_substr with length 2 = ba == ba substring of "aa%CC%8Abco%CC%88Opq" from "5" - grapheme_substr = Opq == Opq substring of "aa%CC%8Abco%CC%88Opq" from "5" - grapheme_substr with length -1 = Op == Op substring of "aa%CC%8Abco%CC%88Opq" from "5" - grapheme_substr with length -2 = O == O substring of "aa%CC%8Abco%CC%88Opq" from "5" - grapheme_substr with length -3 = == substring of "aa%CC%8Abco%CC%88Opq" from "5" - grapheme_substr with length -4 = false == false substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr = aa%CC%8Abco%CC%88Opq == aa%CC%8Abco%CC%88Opq substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -1 = aa%CC%8Abco%CC%88Op == aa%CC%8Abco%CC%88Op substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -2 = aa%CC%8Abco%CC%88O == aa%CC%8Abco%CC%88O substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -3 = aa%CC%8Abco%CC%88 == aa%CC%8Abco%CC%88 substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -4 = aa%CC%8Abc == aa%CC%8Abc substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -5 = aa%CC%8Ab == aa%CC%8Ab substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -6 = aa%CC%8A == aa%CC%8A substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -7 = a == a substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -8 = == substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -9 = false == false substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr = aa%CC%8Abco%CC%88Opq == aa%CC%8Abco%CC%88Opq substring of "aa%CC%8Abco%CC%88Opq" from "-7" - grapheme_substr = a%CC%8Abco%CC%88Opq == a%CC%8Abco%CC%88Opq substring of "aa%CC%8Abco%CC%88Opq" from "-6" - grapheme_substr = bco%CC%88Opq == bco%CC%88Opq substring of "aa%CC%8Abco%CC%88Opq" from "-5" - grapheme_substr = co%CC%88Opq == co%CC%88Opq substring of "aa%CC%8Abco%CC%88Opq" from "-4" - grapheme_substr = o%CC%88Opq == o%CC%88Opq substring of "aa%CC%8Abco%CC%88Opq" from "-3" - grapheme_substr = Opq == Opq substring of "aa%CC%8Abco%CC%88Opq" from "-2" - grapheme_substr = pq == pq substring of "aa%CC%8Abco%CC%88Opq" from "-1" - grapheme_substr = q == q substring of "aa%CC%8Abco%CC%88Opq" from "-999" - grapheme_substr = false == false substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 8 = aa%CC%8Abco%CC%88Opq == aa%CC%8Abco%CC%88Opq substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 7 = aa%CC%8Abco%CC%88Op == aa%CC%8Abco%CC%88Op substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 6 = aa%CC%8Abco%CC%88O == aa%CC%8Abco%CC%88O substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 5 = aa%CC%8Abco%CC%88 == aa%CC%8Abco%CC%88 substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 4 = aa%CC%8Abc == aa%CC%8Abc substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 3 = aa%CC%8Ab == aa%CC%8Ab substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 2 = aa%CC%8A == aa%CC%8A substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 1 = a == a substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 0 = aa%CC%8Abco%CC%88Opq == substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -999 = false == false substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -1 = aa%CC%8Abco%CC%88Op == aa%CC%8Abco%CC%88Op substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -2 = aa%CC%8Abco%CC%88O == aa%CC%8Abco%CC%88O substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -3 = aa%CC%8Abco%CC%88 == aa%CC%8Abco%CC%88 substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -4 = aa%CC%8Abc == aa%CC%8Abc substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -5 = aa%CC%8Ab == aa%CC%8Ab substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -6 = aa%CC%8A == aa%CC%8A substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -7 = a == a substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -8 = == substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -9 = false == false function grapheme_strstr($haystack, $needle, $before_needle = FALSE) {} find "o" in "aa%CC%8Abco%CC%88o" - grapheme_strstr = o == o find "o" in "aa%CC%8Abco%CC%88" - grapheme_strstr = false == false find "o%CC%88" in "aa%CC%8Abco%CC%88" - grapheme_strstr = o%CC%88 == o%CC%88 find "a%CC%8A" in "o%CC%88aa%CC%8Abc" - grapheme_strstr = a%CC%8Abc == a%CC%8Abc find "a%CC%8A" in "aa%CC%8Abc" - grapheme_strstr = a%CC%8Abc == a%CC%8Abc find "a%CC%8A" in "abc" - grapheme_strstr = false == false find "a" in "a%CC%8Abc" - grapheme_strstr = false == false find "d" in "abc" - grapheme_strstr = false == false find "c" in "abc" - grapheme_strstr = c == c find "b" in "abc" - grapheme_strstr = bc == bc find "a" in "abc" - grapheme_strstr = abc == abc find "ab" in "abc" - grapheme_strstr = abc == abc find "abc" in "abc" - grapheme_strstr = abc == abc find "bc" in "abc" - grapheme_strstr = bc == bc find "a" in "abc" - grapheme_strstr before flag is FALSE = abc == abc find "a" in "abc" - grapheme_strstr before flag is TRUE = == find "b" in "abc" - grapheme_strstr before flag is TRUE = a == a find "c" in "abc" - grapheme_strstr before flag is TRUE = ab == ab find "bab" in "ababc" - grapheme_strstr before flag is TRUE = a == a find "abc" in "ababc" - grapheme_strstr before flag is TRUE = ab == ab find "abc" in "ababc" - grapheme_strstr before flag is FALSE = abc == abc find "d" in "aba%CC%8Ac" - grapheme_strstr = false == false find "a" in "bca%CC%8Aa" - grapheme_strstr = a == a find "b" in "aa%CC%8Abc" - grapheme_strstr = bc == bc find "a" in "a%CC%8Abc" - grapheme_strstr = false == false find "ab" in "a%CC%8Aabc" - grapheme_strstr = abc == abc find "abc" in "abca%CC%8A" - grapheme_strstr = abca%CC%8A == abca%CC%8A find "a%CC%8Abc" in "aa%CC%8Abc" - grapheme_strstr = a%CC%8Abc == a%CC%8Abc find "a%CC%8A" in "aa%CC%8Abc" - grapheme_strstr before flag is FALSE = a%CC%8Abc == a%CC%8Abc find "a" in "aa%CC%8Abc" - grapheme_strstr before flag is TRUE = == find "b" in "a%CC%8Aabc" - grapheme_strstr before flag is TRUE = a%CC%8Aa == a%CC%8Aa find "c" in "aba%CC%8Ac" - grapheme_strstr before flag is TRUE = aba%CC%8A == aba%CC%8A find "baa%CC%8Ab" in "abaa%CC%8Abc" - grapheme_strstr before flag is TRUE = a == a find "abca%CC%8A" in "ababca%CC%8A" - grapheme_strstr before flag is TRUE = ab == ab find "aba%CC%8Ac" in "ababa%CC%8Ac" - grapheme_strstr before flag is FALSE = aba%CC%8Ac == aba%CC%8Ac function grapheme_stristr($haystack, $needle, $before_needle = FALSE) {} find "O%CC%88" in "aa%CC%8Abco%CC%88" - grapheme_stristr = o%CC%88 == o%CC%88 find "o" in "aa%CC%8Abco%CC%88O" - grapheme_stristr = O == O find "o" in "aa%CC%8Abco%CC%88" - grapheme_stristr = false == false find "a%CC%8A" in "o%CC%88aa%CC%8Abc" - grapheme_stristr = a%CC%8Abc == a%CC%8Abc find "A%CC%8A" in "aa%CC%8Abc" - grapheme_stristr = a%CC%8Abc == a%CC%8Abc find "a%CC%8A" in "abc" - grapheme_stristr = false == false find "A" in "a%CC%8Abc" - grapheme_stristr = false == false find "d" in "abc" - grapheme_stristr = false == false find "C" in "abc" - grapheme_stristr = c == c find "b" in "aBc" - grapheme_stristr = Bc == Bc find "A" in "abc" - grapheme_stristr = abc == abc find "ab" in "abC" - grapheme_stristr = abC == abC find "aBc" in "abc" - grapheme_stristr = abc == abc find "bc" in "abC" - grapheme_stristr = bC == bC find "A" in "abc" - grapheme_stristr before flag is FALSE = abc == abc find "a" in "abc" - grapheme_stristr before flag is TRUE = == find "b" in "aBc" - grapheme_stristr before flag is TRUE = a == a find "C" in "abc" - grapheme_stristr before flag is TRUE = ab == ab find "bab" in "aBabc" - grapheme_stristr before flag is TRUE = a == a find "aBc" in "ababc" - grapheme_stristr before flag is TRUE = ab == ab find "abC" in "ababc" - grapheme_stristr before flag is FALSE = abc == abc find "d" in "aba%CC%8Ac" - grapheme_stristr = false == false find "a" in "bca%CC%8AA" - grapheme_stristr = A == A find "B" in "aa%CC%8Abc" - grapheme_stristr = bc == bc find "a" in "A%CC%8Abc" - grapheme_stristr = false == false find "Ab" in "a%CC%8Aabc" - grapheme_stristr = abc == abc find "abc" in "abcA%CC%8A" - grapheme_stristr = abcA%CC%8A == abcA%CC%8A find "A%CC%8Abc" in "aa%CC%8Abc" - grapheme_stristr = a%CC%8Abc == a%CC%8Abc find "a%CC%8A" in "aA%CC%8Abc" - grapheme_stristr before flag is FALSE = A%CC%8Abc == A%CC%8Abc find "A" in "aa%CC%8Abc" - grapheme_stristr before flag is TRUE = == find "b" in "a%CC%8AaBc" - grapheme_stristr before flag is TRUE = a%CC%8Aa == a%CC%8Aa find "C" in "aba%CC%8Ac" - grapheme_stristr before flag is TRUE = aba%CC%8A == aba%CC%8A find "baa%CC%8Ab" in "abaA%CC%8Abc" - grapheme_stristr before flag is TRUE = a == a find "aBcA%CC%8A" in "ababca%CC%8A" - grapheme_stristr before flag is TRUE = ab == ab find "aba%CC%8Ac" in "abABA%CC%8Ac" - grapheme_stristr before flag is FALSE = ABA%CC%8Ac == ABA%CC%8Ac function grapheme_extract($haystack, $size, $start = 0) extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "1" graphemes - grapheme_extract starting at byte position 4 = %CC%88 == o%CC%88 extract from "abc" "3" graphemes - grapheme_extract = abc == abc extract from "abc" "2" graphemes - grapheme_extract = ab == ab extract from "abc" "1" graphemes - grapheme_extract = a == a extract from "abc" "0" graphemes - grapheme_extract = == extract from "a%CC%8Abc" "3" graphemes - grapheme_extract = a%CC%8Abc == a%CC%8Abc extract from "a%CC%8Abc" "2" graphemes - grapheme_extract = a%CC%8Ab == a%CC%8Ab extract from "a%CC%8Abc" "1" graphemes - grapheme_extract = a%CC%8A == a%CC%8A extract from "a%CC%8Ao%CC%88o%CC%88" "3" graphemes - grapheme_extract = a%CC%8Ao%CC%88o%CC%88 == a%CC%8Ao%CC%88o%CC%88 extract from "a%CC%8Ao%CC%88o%CC%88" "2" graphemes - grapheme_extract = a%CC%8Ao%CC%88 == a%CC%8Ao%CC%88 extract from "a%CC%8Ao%CC%88c" "1" graphemes - grapheme_extract = a%CC%8A == a%CC%8A extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "1" graphemes - grapheme_extract starting at byte position 0 = o%CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "1" graphemes - grapheme_extract starting at byte position 2 = o%CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "1" graphemes - grapheme_extract starting at byte position 3 = o%CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "1" graphemes - grapheme_extract starting at byte position 4 = %CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extract starting at byte position 0 = o%CC%88o%CC%88 == o%CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extract starting at byte position 2 = o%CC%88o%CC%88 == o%CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extract starting at byte position 3 = o%CC%88o%CC%88 == o%CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extract starting at byte position 4 = %CC%88o%CC%88 == %CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extract starting at byte position 7 = %CC%88o%CC%88 == %CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extract starting at byte position 8 = o%CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extract starting at byte position 10 = %CC%88 == %CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extract starting at byte position 11 = false == false function grapheme_extractB($haystack, $size, $start = 0) extract from "abc" "3" graphemes - grapheme_extractB = abc == abc extract from "abc" "2" graphemes - grapheme_extractB = ab == ab extract from "abc" "1" graphemes - grapheme_extractB = a == a extract from "abc" "0" graphemes - grapheme_extractB = == extract from "a%CC%8Abc" "5" graphemes - grapheme_extractB = a%CC%8Abc == a%CC%8Abc extract from "a%CC%8Abc" "4" graphemes - grapheme_extractB = a%CC%8Ab == a%CC%8Ab extract from "a%CC%8Abc" "1" graphemes - grapheme_extractB = == extract from "a%CC%8Ao%CC%88o%CC%88" "9" graphemes - grapheme_extractB = a%CC%8Ao%CC%88o%CC%88 == a%CC%8Ao%CC%88o%CC%88 extract from "a%CC%8Ao%CC%88o%CC%88" "10" graphemes - grapheme_extractB = a%CC%8Ao%CC%88o%CC%88 == a%CC%8Ao%CC%88o%CC%88 extract from "a%CC%8Ao%CC%88o%CC%88" "11" graphemes - grapheme_extractB = a%CC%8Ao%CC%88o%CC%88 == a%CC%8Ao%CC%88o%CC%88 extract from "a%CC%8Ao%CC%88o%CC%88" "6" graphemes - grapheme_extractB = a%CC%8Ao%CC%88 == a%CC%8Ao%CC%88 extract from "a%CC%8Ao%CC%88c" "3" graphemes - grapheme_extractB = a%CC%8A == a%CC%8A extract from "a%CC%8Ao%CC%88c" "4" graphemes - grapheme_extractB = a%CC%8A == a%CC%8A extract from "a%CC%8Ao%CC%88c" "5" graphemes - grapheme_extractB = a%CC%8A == a%CC%8A extract from "a%CC%8Ao%CC%88c" "6" graphemes - grapheme_extractB = a%CC%8Ao%CC%88 == a%CC%8Ao%CC%88 extract from "a%CC%8Ao%CC%88c" "7" graphemes - grapheme_extractB = a%CC%8Ao%CC%88c == a%CC%8Ao%CC%88c extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "3" graphemes - grapheme_extractB starting at byte position 0 = o%CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "3" graphemes - grapheme_extractB starting at byte position 2 = o%CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "3" graphemes - grapheme_extractB starting at byte position 3 = o%CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "3" graphemes - grapheme_extractB starting at byte position 4 = %CC%88 == %CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "6" graphemes - grapheme_extractB starting at byte position 0 = o%CC%88o%CC%88 == o%CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "6" graphemes - grapheme_extractB starting at byte position 2 = o%CC%88o%CC%88 == o%CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "6" graphemes - grapheme_extractB starting at byte position 3 = o%CC%88o%CC%88 == o%CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "5" graphemes - grapheme_extractB starting at byte position 4 = %CC%88o%CC%88 == %CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "5" graphemes - grapheme_extractB starting at byte position 7 = %CC%88o%CC%88 == %CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "3" graphemes - grapheme_extractB starting at byte position 8 = o%CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extractB starting at byte position 10 = %CC%88 == %CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extractB starting at byte position 11 = false == false
http://cvs.php.net/viewvc.cgi/pecl/unicode/config.m4?view=markup&rev=1.1 Index: pecl/unicode/config.m4 +++ pecl/unicode/config.m4 dnl config.m4 for extension unicode dnl ########################################################################## dnl Initialize the extension PHP_ARG_ENABLE(unicode, whether to enable unicode support, [ --enable-unicode Enable unicode support]) if test "$PHP_UNICODE" != "no"; then PHP_SETUP_ICU(INTL_SHARED_LIBADD) PHP_SUBST(INTL_SHARED_LIBADD) PHP_REQUIRE_CXX() PHP_NEW_EXTENSION(unicode, php_unicode.c \ unicode_convert.c \ grapheme/grapheme_string.c \ ,$ext_shared,,$ICU_INCS) PHP_ADD_BUILD_DIR($ext_builddir/grapheme) fi http://cvs.php.net/viewvc.cgi/pecl/unicode/config.w32?view=markup&rev=1.1 Index: pecl/unicode/config.w32 +++ pecl/unicode/config.w32 // $Id: config.w32,v 1.1 2008/01/14 21:50:07 ebatutis Exp $ // vim:ft=javascript ARG_ENABLE("unicode", "Enable unicode support", "no"); if (PHP_UNICODE != "no") { if (CHECK_LIB("icuuc.lib", "unicode", PHP_UNICODE) && CHECK_HEADER_ADD_INCLUDE("unicode/utf.h", "CFLAGS_INTL")) { // always build as shared - zend_strtod.c/ICU type conflict EXTENSION("unicode", "php_unicode.c unicode_convert.c", true, "/I \"" + configure_module_dirname + "\""); ADD_SOURCES(configure_module_dirname + "/grapheme", "\ grapheme_string.c \ ", "unicode"); ADD_FLAG("LIBS_INTL", "icudt.lib icuin.lib icuio.lib icule.lib iculx.lib"); // if int32_t and uint32_t types are made available in PHP, uncomment next line // ADD_FLAG("CFLAGS_INTL", "/D U_HAVE_INT32_T /D U_HAVE_UINT32_T"); AC_DEFINE("HAVE_UNICODE", 1, "Unicode support enabled"); } else { WARNING("unicode not enabled; libraries and/or headers not found"); } } http://cvs.php.net/viewvc.cgi/pecl/unicode/php_unicode.c?view=markup&rev=1.1 Index: pecl/unicode/php_unicode.c +++ pecl/unicode/php_unicode.c /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Ed Batutis <ed@batutis.com> | | Stanislav Malyshev <stas@zend.com> | +----------------------------------------------------------------------+ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include "php_unicode.h" #include "grapheme/grapheme.h" #include <ext/standard/info.h> #include "php_ini.h" #define INTL_MODULE_VERSION "0.1a" ZEND_DECLARE_MODULE_GLOBALS( unicode ) /* {{{ Arguments info */ static ZEND_BEGIN_ARG_INFO_EX( grapheme_0_args, 0, 0, 0 ) ZEND_END_ARG_INFO() static ZEND_BEGIN_ARG_INFO_EX( grapheme_1_arg, 0, 0, 1 ) ZEND_ARG_INFO( 0, arg1 ) ZEND_END_ARG_INFO() static ZEND_BEGIN_ARG_INFO_EX( grapheme_2_args, 0, 0, 2 ) ZEND_ARG_INFO( 0, arg1 ) ZEND_ARG_INFO( 0, arg2 ) ZEND_END_ARG_INFO() static ZEND_BEGIN_ARG_INFO_EX( grapheme_3_args, 0, 0, 3 ) ZEND_ARG_INFO( 0, arg1 ) ZEND_ARG_INFO( 0, arg2 ) ZEND_ARG_INFO( 0, arg3 ) ZEND_END_ARG_INFO() /* }}} */ /* {{{ unicode_functions * * Every user visible function must have an entry in unicode_functions[]. */ zend_function_entry unicode_functions[] = { // grapheme functions PHP_FE( grapheme_strlen, grapheme_1_arg ) PHP_FE( grapheme_strpos, grapheme_3_args ) PHP_FE( grapheme_stripos, grapheme_3_args ) PHP_FE( grapheme_strrpos, grapheme_3_args ) PHP_FE( grapheme_strripos, grapheme_3_args ) PHP_FE( grapheme_substr, grapheme_3_args ) PHP_FE( grapheme_strstr, grapheme_3_args ) PHP_FE( grapheme_stristr, grapheme_3_args ) PHP_FE( grapheme_extract, grapheme_3_args ) PHP_FE( grapheme_extractB, grapheme_3_args ) { NULL, NULL, NULL } }; /* }}} */ static PHP_GINIT_FUNCTION(unicode); /* {{{ unicode_module_entry */ zend_module_entry unicode_module_entry = { #if ZEND_MODULE_API_NO >= 20010901 STANDARD_MODULE_HEADER, #endif "unicode", unicode_functions, PHP_MINIT( unicode ), PHP_MSHUTDOWN( unicode ), PHP_RINIT( unicode ), PHP_RSHUTDOWN( unicode ), PHP_MINFO( unicode ), INTL_MODULE_VERSION, PHP_MODULE_GLOBALS(unicode), /* globals descriptor */ PHP_GINIT(unicode), /* globals ctor */ NULL, /* globals dtor */ NULL, /* post deactivate */ STANDARD_MODULE_PROPERTIES_EX }; /* }}} */ #ifdef COMPILE_DL_UNICODE ZEND_GET_MODULE( unicode ) #endif /* {{{ unicode_init_globals */ static PHP_GINIT_FUNCTION(unicode) { memset( unicode_globals, 0, sizeof(zend_unicode_globals) ); } /* }}} */ /* {{{ PHP_MINIT_FUNCTION */ PHP_MINIT_FUNCTION( unicode ) { return SUCCESS; } /* }}} */ /* {{{ PHP_MSHUTDOWN_FUNCTION */ PHP_MSHUTDOWN_FUNCTION( unicode ) { return SUCCESS; } /* }}} */ /* {{{ PHP_RINIT_FUNCTION */ PHP_RINIT_FUNCTION( unicode ) { return SUCCESS; } /* }}} */ /* {{{ PHP_RSHUTDOWN_FUNCTION */ PHP_RSHUTDOWN_FUNCTION( unicode ) { if (INTL_G(grapheme_iterator)) { grapheme_close_global_iterator( TSRMLS_C ); INTL_G(grapheme_iterator) = NULL; } return SUCCESS; } /* }}} */ /* {{{ PHP_MINFO_FUNCTION */ PHP_MINFO_FUNCTION( unicode ) { php_info_print_table_start(); php_info_print_table_header( 2, "Unicode support", "enabled" ); php_info_print_table_row( 2, "version", INTL_MODULE_VERSION ); php_info_print_table_end(); //For the default locale php.ini setting DISPLAY_INI_ENTRIES() ; } /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/viewvc.cgi/pecl/unicode/php_unicode.h?view=markup&rev=1.1 Index: pecl/unicode/php_unicode.h +++ pecl/unicode/php_unicode.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: | | Stanislav Malyshev <stas@zend.com> | | Ed Batutis <ed@batutis.com> | +----------------------------------------------------------------------+ */ #ifndef PHP_UNICODE_H #define PHP_UNICODE_H #include <php.h> #include "grapheme/grapheme.h" extern zend_module_entry unicode_module_entry; #define phpext_unicode_ptr &unicode_module_entry #ifdef PHP_WIN32 #define PHP_UNICODE_API __declspec(dllexport) #else #define PHP_UNICODE_API #endif #ifdef ZTS #include "TSRM.h" #endif ZEND_BEGIN_MODULE_GLOBALS(unicode) UBreakIterator* grapheme_iterator; ZEND_END_MODULE_GLOBALS(unicode) /* Macro to access request-wide global variables. */ #ifdef ZTS #define UNICODE_G(v) TSRMG(unicode_globals_id, zend_unicode_globals *, v) #else #define UNICODE_G(v) (unicode_globals.v) #endif ZEND_EXTERN_MODULE_GLOBALS(unicode) PHP_MINIT_FUNCTION(unicode); PHP_MSHUTDOWN_FUNCTION(unicode); PHP_RINIT_FUNCTION(unicode); PHP_RSHUTDOWN_FUNCTION(unicode); PHP_MINFO_FUNCTION(unicode); #endif /* PHP_UNICODE_H */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/viewvc.cgi/pecl/unicode/unicode_common.h?view=markup&rev=1.1 Index: pecl/unicode/unicode_common.h +++ pecl/unicode/unicode_common.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Vadim Savchuk <vsavchuk@productengine.com> | | Dmitry Lakhtyuk <dlakhtyuk@productengine.com> | | Stanislav Malyshev <stas@zend.com> | +----------------------------------------------------------------------+ */ #ifndef UNICODE_COMMON_H #define UNICODE_COMMON_H /* Auxiliary macros */ #include <php.h> #include <unicode/utypes.h> #ifndef UBYTES # define UBYTES(len) ((len) * sizeof(UChar)) #endif #ifndef eumalloc # define eumalloc(size) (UChar*)safe_emalloc(size, sizeof(UChar), 0) #endif #ifndef eurealloc # define eurealloc(ptr, size) (UChar*)erealloc((ptr), size * sizeof(UChar)) #endif #define USIZE(data) sizeof((data))/sizeof(UChar) #define UCHARS(len) ((len) / sizeof(UChar)) #define UNICODE_Z_STRVAL_P(str) (UChar*) Z_STRVAL_P(str) #define UNICODE_Z_STRLEN_P(str) UCHARS( Z_STRLEN_P(str) ) #endif /* UNICODE_COMMON_H */ http://cvs.php.net/viewvc.cgi/pecl/unicode/unicode_convert.c?view=markup&rev=1.1 Index: pecl/unicode/unicode_convert.c +++ pecl/unicode/unicode_convert.c /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Vadim Savchuk <vsavchuk@productengine.com> | | Dmitry Lakhtyuk <dlakhtyuk@productengine.com> | +----------------------------------------------------------------------+ */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include <php.h> #include "unicode_common.h" #include "unicode_convert.h" /* {{{ unicode_convert_utf8_to_utf16 * Convert given string from UTF-8 to UTF-16 to *target buffer. * * It *target is NULL then we allocate a large enough buffer, * store the converted string into it, and make target point to it. * * Otherwise, if *target is non-NULL, we assume that it points to a * dynamically allocated buffer of *target_len bytes length. * In this case the buffer will be used to store the converted string to, * and may be resized (made larger) if needed. * * @param target Where to place the result. * @param target_len Result length. * @param source String to convert. * @param source_len Length of the source string. * @param status Conversion status. * * @return void This function does not return anything. */ void unicode_convert_utf8_to_utf16( UChar** target, int* target_len, const char* src, int src_len, UErrorCode* status ) { UChar* dst_buf = NULL; int32_t dst_len = 0; // If *target is NULL determine required destination buffer size (pre-flighting). // Otherwise, attempt to convert source string; if *target buffer is not large enough // it will be resized appropriately. *status = U_ZERO_ERROR; u_strFromUTF8( *target, *target_len, &dst_len, src, src_len, status ); if( *status == U_ZERO_ERROR ) { // String is converted successfuly (*target)[dst_len] = 0; *target_len = dst_len; return; } // Bail out if an unexpected error occured. // (U_BUFFER_OVERFLOW_ERROR means that *target buffer is not large enough). // (U_STRING_NOT_TERMINATED_WARNING usually means that the input string is empty). if( *status != U_BUFFER_OVERFLOW_ERROR && *status != U_STRING_NOT_TERMINATED_WARNING ) return; // Allocate memory for the destination buffer (it will be zero-terminated). dst_buf = eumalloc( dst_len + 1 ); // Convert source string from UTF-8 to UTF-16. *status = U_ZERO_ERROR; u_strFromUTF8( dst_buf, dst_len+1, NULL, src, src_len, status ); if( U_FAILURE( *status ) ) { efree( dst_buf ); return; } dst_buf[dst_len] = 0; if( *target ) efree( *target ); *target = dst_buf; *target_len = dst_len; } /* }}} */ /* {{{ unicode_convert_utf16_to_utf8 * Convert given string from UTF-16 to UTF-8. * * @param target Where to place the result. * @param target_len Result length. * @param source String to convert. * @param source_len Length of the source string. * @param status Conversion status. * * @return void This function does not return anything. */ void unicode_convert_utf16_to_utf8( char** target, int* target_len, const UChar* src, int src_len, UErrorCode* status ) { char* dst_buf = NULL; int32_t dst_len; // Determine required destination buffer size (pre-flighting). *status = U_ZERO_ERROR; u_strToUTF8( NULL, 0, &dst_len, src, src_len, status ); // Bail out if an unexpected error occured. // (U_BUFFER_OVERFLOW_ERROR means that *target buffer is not large enough). // (U_STRING_NOT_TERMINATED_WARNING usually means that the input string is empty). if( *status != U_BUFFER_OVERFLOW_ERROR && *status != U_STRING_NOT_TERMINATED_WARNING ) return; // Allocate memory for the destination buffer (it will be zero-terminated). dst_buf = emalloc( dst_len+1 ); // Convert source string from UTF-8 to UTF-16. *status = U_ZERO_ERROR; u_strToUTF8( dst_buf, dst_len, NULL, src, src_len, status ); if( U_FAILURE( *status ) ) { efree( dst_buf ); return; } // U_STRING_NOT_TERMINATED_WARNING is OK for us => reset 'status'. *status = U_ZERO_ERROR; dst_buf[dst_len] = 0; *target = dst_buf; *target_len = dst_len; } /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: noet sw=4 ts=4 fdm=marker * vim<600: noet sw=4 ts=4 */ http://cvs.php.net/viewvc.cgi/pecl/unicode/unicode_convert.h?view=markup&rev=1.1 Index: pecl/unicode/unicode_convert.h +++ pecl/unicode/unicode_convert.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Vadim Savchuk <vsavchuk@productengine.com> | | Dmitry Lakhtyuk <dlakhtyuk@productengine.com> | +----------------------------------------------------------------------+ */ #ifndef UNICODE_CONVERT_H #define UNICODE_CONVERT_H #include <unicode/ustring.h> void unicode_convert_utf8_to_utf16( UChar** target, int* target_len, const char* src, int src_len, UErrorCode* status ); void unicode_convert_utf16_to_utf8( char** target, int* target_len, const UChar* src, int src_len, UErrorCode* status ); #endif // UNICODE_CONVERT_H http://cvs.php.net/viewvc.cgi/pecl/unicode/doc/grapheme_api.php?view=markup&rev=1.1 Index: pecl/unicode/doc/grapheme_api.php +++ pecl/unicode/doc/grapheme_api.php <?php ############################################################################# # Grapheme API ############################################################################# /** * Get string length in grapheme units * @param string $input The string being measured for length. * @return int The length of the string on success, and 0 if the string is empty. */ function grapheme_strlen($input) {} /** * Find position (in grapheme units) of first occurrence of a string * @param string $haystack The string to look in * @param string $needle The string to look for * @param [int] $offset The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the beginning of haystack. * @return int Returns the position as an integer. If needle is not found, strpos() will return boolean FALSE. */ function grapheme_strpos($haystack, $needle, $offset = 0) {} /** * Find position (in grapheme units) of first occurrence of a case-insensitive string * @param string $haystack The string to look in * @param string $needle The string to look for * @param [int] $offset The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the beginning of haystack. * @return int Returns the position as an integer. If needle is not found, grapheme_stripos() will return boolean FALSE. */ function grapheme_stripos($haystack, $needle, $offset = 0) {} /** * Find position (in grapheme units) of last occurrence of a string * @param string $haystack The string to look in * @param string $needle The string to look for * @param [int] $offset The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the beginning of haystack. * @return int Returns the position as an integer. If needle is not found, grapheme_strrpos() will return boolean FALSE. */ function grapheme_strrpos($haystack, $needle, $offset = 0) {} /** * Find position (in grapheme units) of last occurrence of a case-insensitive string * @param string $haystack The string to look in * @param string $needle The string to look for * @param [int] $offset The optional offset parameter allows you to specify which character in haystack to start searching. The position returned is still relative to the beginning of haystack. * @return int Returns the position as an integer. If needle is not found, grapheme_strripos() will return boolean FALSE. */ function grapheme_strripos($haystack, $needle, $offset = 0) {} /** * Return part of a string * @param string $string The input string. * @param int $start If start is non-negative, the returned string will start at the start'th position in string, counting from zero. If start is negative, the returned string will start at the start'th character from the end of string. * @param [int] $length If length is given and is positive, the string returned will contain at most length characters beginning from start (depending on the length of string). If string is less than or equal to start characters long, FALSE will be returned. If length is given and is negative, then that many characters will be omitted from the end of string (after the start position has been calculated when a start is negative). If start denotes a position beyond this truncation, an empty string will be returned. * @return int Returns the extracted part of string. */ function grapheme_substr($string, $start, $length = -1) {} /** * Returns part of haystack string from the first occurrence of needle to the end of haystack. * @param string $haystack The input string. * @param string $needle The string to look for. * @param [boolean] $before_needle If TRUE (the default is FALSE), grapheme_strstr() returns the part of the haystack before the first occurence of the needle. * @return string Returns the portion of string, or FALSE if needle is not found. */ function grapheme_strstr($haystack, $needle, $before_needle = FALSE) {} /** * Returns part of haystack string from the first occurrence of case-insensitive needle to the end of haystack. * @param string $haystack The input string. * @param string $needle The string to look for. * @param [boolean] $before_needle If TRUE (the default is FALSE), grapheme_strstr() returns the part of the haystack before the first occurence of the needle. * @return string Returns the portion of string, or FALSE if needle is not found. */ function grapheme_stristr($haystack, $needle, $before_needle = FALSE) {} /** * Function to extract a sequence of default grapheme clusters from a text buffer, which must be encoded in UTF-8. * @param string $haystack string to search * @param int $size maximum number of grapheme clusters to return * @param [int] $start starting position in $haystack in bytes * @return string A string starting at offset $start containing no more than $size grapheme clusters and ending on a default grapheme cluster boundary. */ public function grapheme_extract($haystack, $size, $start = 0) {} /** * Function to extract a sequence of default grapheme clusters from a text buffer, which must be encoded in UTF-8. * @param string $haystack string to search * @param int $bsize maximum number of bytes to return * @param [int] $start starting position in $haystack in bytes * @return string A string starting at offset $start containing no more than $bsize bytes and ending on a default grapheme cluster boundary. */ public function grapheme_extractB($haystack, $bsize, $start = 0) {} ?> http://cvs.php.net/viewvc.cgi/pecl/unicode/grapheme/grapheme.h?view=markup&rev=1.1 Index: pecl/unicode/grapheme/grapheme.h +++ pecl/unicode/grapheme/grapheme.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Ed Batutis <ed@batutis.com> | +----------------------------------------------------------------------+ */ #ifndef GRAPHEME_GRAPHEME_H #define GRAPHEME_GRAPHEME_H #include <php.h> #include <unicode/utypes.h> #include <unicode/ubrk.h> PHP_FUNCTION(grapheme_strlen); PHP_FUNCTION(grapheme_strpos); PHP_FUNCTION(grapheme_stripos); PHP_FUNCTION(grapheme_strrpos); PHP_FUNCTION(grapheme_strripos); PHP_FUNCTION(grapheme_substr); PHP_FUNCTION(grapheme_strstr); PHP_FUNCTION(grapheme_stristr); PHP_FUNCTION(grapheme_extract); PHP_FUNCTION(grapheme_extractB); void grapheme_register_constants( INIT_FUNC_ARGS ); void grapheme_close_global_iterator( TSRMLS_D ); #endif // GRAPHEME_GRAPHEME_H http://cvs.php.net/viewvc.cgi/pecl/unicode/grapheme/grapheme_string.c?view=markup&rev=1.1 Index: pecl/unicode/grapheme/grapheme_string.c +++ pecl/unicode/grapheme/grapheme_string.c /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Author: Ed Batutis <ed@batutis.com> | +----------------------------------------------------------------------+ */ /* {{{ includes */ #ifdef HAVE_CONFIG_H #include "config.h" #endif #include <php.h> #include "grapheme.h" #include "grapheme_util.h" #include <unicode/utypes.h> #include <unicode/ucol.h> #include <unicode/ustring.h> #include <unicode/ubrk.h> #include "ext/standard/php_string.h" /*#include "zend_API.h"*/ /* }}} */ /* {{{ grapheme_register_constants * Register API constants */ void grapheme_register_constants( INIT_FUNC_ARGS ) { } /* }}} */ /* {{{ cov to lc */ void intl_case_fold(UChar** ptr_to_free, UChar **str, int32_t *str_len, UErrorCode *pstatus ) { UChar *dest; int32_t dest_len, size_required; /* allocate a destination string that is a bit larger than the src, hoping that is enough */ dest_len = (*str_len) + ( *str_len / 10 ); dest = (UChar*) eumalloc(dest_len); *pstatus = U_ZERO_ERROR; size_required = u_strFoldCase(dest, dest_len, *str, *str_len, U_FOLD_CASE_DEFAULT, pstatus); dest_len = size_required; if ( U_BUFFER_OVERFLOW_ERROR == *pstatus ) { dest = (UChar*) eurealloc(dest, dest_len); *pstatus = U_ZERO_ERROR; size_required = u_strFoldCase(dest, dest_len, *str, *str_len, U_FOLD_CASE_DEFAULT, pstatus); } if ( U_FAILURE(*pstatus) ) { return; } if ( NULL != ptr_to_free) { efree(*ptr_to_free); *ptr_to_free = dest; } *str = dest; *str_len = dest_len; return; } /* }}} */ /* {{{ ASCII check */ static inline int ascii_check(const unsigned char *day, size_t len) { int ret_len = len; while ( len-- ) { if ( *day++ > 0x7f ) return -1; } return ret_len; } /* }}} */ /* {{{ split_string: find and optionally return grapheme boundaries */ static int split_string(const UChar *text, int32_t text_length, int boundary_array[], int boundary_array_len TSRMLS_DC ) { unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE]; UErrorCode status = U_ZERO_ERROR; int ret_len, pos; UBreakIterator* bi; bi = get_break_iterator((void*)u_break_iterator_buffer, &status TSRMLS_CC ); if( U_FAILURE(status) ) { return -1; } ubrk_setText(bi, text, text_length, &status); pos = 0; for ( ret_len = 0; pos != UBRK_DONE; ) { pos = ubrk_next(bi); if ( pos != UBRK_DONE ) { if ( NULL != boundary_array && ret_len < boundary_array_len ) { boundary_array[ret_len] = pos; } ret_len++; } } ubrk_close(bi); return ret_len; } /* }}} */ /* {{{ count_graphemes */ static inline int32_t count_graphemes(UBreakIterator *bi, UChar *string, int32_t string_len) { int ret_len = 0; int pos = 0; UErrorCode status = U_ZERO_ERROR; ubrk_setText(bi, string, string_len, &status); do { pos = ubrk_next(bi); if ( UBRK_DONE != pos ) { ret_len++; } } while ( UBRK_DONE != pos ); return ret_len; } /* }}} */ /* {{{ memnstr_grapheme: find needle in haystack using grapheme boundaries */ static inline int32_t memnstr_grapheme(UBreakIterator *bi, UChar *haystack, UChar *needle, int32_t needle_len, UChar *end) { UChar *p = haystack; UChar ne = needle[needle_len-1]; UErrorCode status; int32_t grapheme_offset; end -= needle_len; while (p <= end) { if ((p = u_memchr(p, *needle, (end-p+1))) && ne == p[needle_len-1]) { if (!u_memcmp(needle, p, needle_len - 1)) { /* needle_len - 1 works because if needle_len is 1, we've already tested the char */ /* does the grapheme end here? */ status = U_ZERO_ERROR; ubrk_setText (bi, haystack, (end - haystack) + needle_len, &status); if ( ubrk_isBoundary (bi, (p - haystack) + needle_len) ) { /* found it, get grapheme count offset */ grapheme_offset = count_graphemes(bi, haystack, (p - haystack)); return grapheme_offset; } } } if (p == NULL) { return -1; } p++; } return -1; } /* }}} */ /* {{{ memrstr_grapheme: reverse find needle in haystack using grapheme boundaries */ static inline void *memrchr_grapheme(const void *s, int c, size_t n) { register unsigned char *e; if (n <= 0) { return NULL; } for (e = (unsigned char *)s + n - 1; e >= (unsigned char *)s; e--) { if (*e == (unsigned char)c) { return (void *)e; } } return NULL; } /* }}} */ /* {{{ get_haystack_offset - bump the haystack pointer based on the grapheme count offset */ static UChar * get_haystack_offset(UBreakIterator* bi, UChar *uhaystack, int32_t uhaystack_len, int32_t offset) { UErrorCode status; int32_t pos; int32_t (*iter_op)(UBreakIterator* bi); int iter_incr; if ( NULL != bi ) { status = U_ZERO_ERROR; ubrk_setText (bi, uhaystack, uhaystack_len, &status); } if ( 0 == offset ) { return uhaystack; } if ( offset < 0 ) { iter_op = ubrk_previous; ubrk_last(bi); /* one past the end */ iter_incr = 1; } else { iter_op = ubrk_next; iter_incr = -1; } pos = 0; while ( pos != UBRK_DONE && offset != 0 ) { pos = iter_op(bi); if ( UBRK_DONE != pos ) { offset += iter_incr; } } if ( offset != 0 ) { return NULL; } return uhaystack + pos; } /* }}} */ /* {{{ strrpos_ascii: borrowed from the php ext/standard/string.c */ static int32_t strrpos_ascii(unsigned char *haystack, int32_t haystack_len, unsigned char *needle, int32_t needle_len, int32_t offset) { unsigned char *p, *e; if (offset >= 0) { p = haystack + offset; e = haystack + haystack_len - needle_len; } else { p = haystack; if (needle_len > -offset) { e = haystack + haystack_len - needle_len; } else { e = haystack + haystack_len + offset; } } if (needle_len == 1) { /* Single character search can shortcut memcmps */ while (e >= p) { if (*e == *needle) { return (e - p + (offset > 0 ? offset : 0)); } e--; } return -1; } while (e >= p) { if (memcmp(e, needle, needle_len) == 0) { return (e - p + (offset > 0 ? offset : 0)); } e--; } return -1; } /* }}} */ /* {{{ get_break_iterator: get a clone of the global character break iterator */ static UBreakIterator* get_break_iterator(void *stack_buffer, UErrorCode *status TSRMLS_DC ) { int32_t buffer_size; UBreakIterator *global_break_iterator = UNICODE_G( grapheme_iterator ); if ( NULL == global_break_iterator ) { global_break_iterator = ubrk_open(UBRK_CHARACTER, NULL, /* icu default locale - locale has no effect on this iterator */ NULL, /* text not set in global iterator */ 0, /* text length = 0 */ status); UNICODE_G(grapheme_iterator) = global_break_iterator; } buffer_size = U_BRK_SAFECLONE_BUFFERSIZE; return ubrk_safeClone(global_break_iterator, stack_buffer, &buffer_size, status); } /* }}} */ /* {{{ grapheme_close_global_iterator - clean up */ void grapheme_close_global_iterator( TSRMLS_D ) { UBreakIterator *global_break_iterator = UNICODE_G( grapheme_iterator ); if ( NULL != global_break_iterator ) { ubrk_close(global_break_iterator); } } /* }}} */ #define OUTSIDE_STRING(offset, max_len) ( offset < 0 ? -offset > max_len : offset >= max_len ) /* {{{ proto int grapheme_strlen(string str) Get number of graphemes in a string */ PHP_FUNCTION(grapheme_strlen) { unsigned char* string; int string_len; UChar* ustring = NULL; int ustring_len = 0; int ret_len; UErrorCode status; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", (char **)&string, &string_len) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strlen: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } ret_len = ascii_check(string, string_len); if ( ret_len >= 0 ) RETURN_LONG(ret_len); /* convert the string to UTF-16. */ status = U_ZERO_ERROR; intl_convert_utf8_to_utf16(&ustring, &ustring_len, (char*) string, string_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC ); efree( ustring ); RETURN_NULL(); } ret_len = split_string(ustring, ustring_len, NULL, 0 TSRMLS_CC ); efree( ustring ); if (ret_len >= 0) { RETVAL_LONG(ret_len); } else { RETVAL_FALSE; } } /* }}} */ /* {{{ strpos_utf16 - strrpos using utf16*/ static int strpos_utf16(unsigned char *haystack, int haystack_len, unsigned char*needle, int needle_len, int offset, int *puchar_pos, int f_ignore_case TSRMLS_DC) { UChar *uhaystack, *puhaystack, *uneedle; int32_t uhaystack_len, uneedle_len; int ret_pos; unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE]; UBreakIterator* bi; UErrorCode status; *puchar_pos = -1; /* convert the strings to UTF-16. */ uhaystack = NULL; uhaystack_len = 0; status = U_ZERO_ERROR; intl_convert_utf8_to_utf16(&uhaystack, &uhaystack_len, (char *) haystack, haystack_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC ); efree( uhaystack ); return -1; } /* get a pointer to the haystack taking into account the offset */ bi = NULL; status = U_ZERO_ERROR; bi = get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC ); puhaystack = get_haystack_offset(bi, uhaystack, uhaystack_len, offset); uhaystack_len = (uhaystack_len - ( puhaystack - uhaystack)); if ( NULL == puhaystack ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 TSRMLS_CC ); efree( uhaystack ); ubrk_close (bi); return -1; } if ( f_ignore_case ) { intl_case_fold(&uhaystack, &puhaystack, &uhaystack_len, &status ); } uneedle = NULL; uneedle_len = 0; status = U_ZERO_ERROR; intl_convert_utf8_to_utf16(&uneedle, &uneedle_len, (char *) needle, needle_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC ); efree( uhaystack ); efree( uneedle ); ubrk_close (bi); return -1; } if ( f_ignore_case ) { intl_case_fold(&uneedle, &uneedle, &uneedle_len, &status ); } ret_pos = memnstr_grapheme(bi, puhaystack, uneedle, uneedle_len, puhaystack + uhaystack_len ); *puchar_pos = ubrk_current(bi); efree( uhaystack ); efree( uneedle ); ubrk_close (bi); return ret_pos; } /* }}} */ /* {{{ strrpos_utf16 - strrpos using utf16 */ static int strrpos_utf16(unsigned char *haystack, int haystack_len, unsigned char*needle, int needle_len, int offset, int f_ignore_case TSRMLS_DC) { UChar *uhaystack, *puhaystack, *uhaystack_end, *uneedle; int32_t uhaystack_len, uneedle_len; UErrorCode status; unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE]; UBreakIterator* bi = NULL; int ret_pos, pos; /* convert the strings to UTF-16. */ uhaystack = NULL; uhaystack_len = 0; status = U_ZERO_ERROR; intl_convert_utf8_to_utf16(&uhaystack, &uhaystack_len, (char *) haystack, haystack_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC ); efree( uhaystack ); return -1; } if ( f_ignore_case ) { intl_case_fold(&uhaystack, &uhaystack, &uhaystack_len, &status ); } /* get a pointer to the haystack taking into account the offset */ bi = NULL; status = U_ZERO_ERROR; bi = get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC ); puhaystack = get_haystack_offset(bi, uhaystack, uhaystack_len, offset); if ( NULL == puhaystack ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 TSRMLS_CC ); efree( uhaystack ); ubrk_close (bi); return -1; } uneedle = NULL; uneedle_len = 0; status = U_ZERO_ERROR; intl_convert_utf8_to_utf16(&uneedle, &uneedle_len, (char *) needle, needle_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC ); efree( uhaystack ); efree( uneedle ); ubrk_close (bi); return -1; } if ( f_ignore_case ) { intl_case_fold(&uneedle, &uneedle, &uneedle_len, &status ); } ret_pos = -1; /* -1 represents 'not found' */ /* back up until there's needle_len characters to compare */ uhaystack_end = uhaystack + uhaystack_len; pos = ubrk_last(bi); puhaystack = uhaystack + pos; while ( uhaystack_end - puhaystack < uneedle_len ) { pos = ubrk_previous(bi); if ( UBRK_DONE == pos ) { break; } puhaystack = uhaystack + pos; } /* is there enough haystack left to hold the needle? */ if ( ( uhaystack_end - puhaystack ) < uneedle_len ) { /* not enough, not found */ goto exit; } while ( UBRK_DONE != pos ) { if (!u_memcmp(uneedle, puhaystack, uneedle_len)) { /* needle_len - 1 in zend memnstr? */ /* does the grapheme in the haystack end at the same place as the last grapheme in the needle? */ if ( ubrk_isBoundary(bi, pos + uneedle_len) ) { /* found it, get grapheme count offset */ ret_pos = count_graphemes(bi, uhaystack, pos); break; } /* set position back */ ubrk_isBoundary(bi, pos); } pos = ubrk_previous(bi); puhaystack = uhaystack + pos; } exit: efree( uhaystack ); efree( uneedle ); ubrk_close (bi); return ret_pos; } /* }}} */ /* {{{ proto int grapheme_strpos(string haystack, string needle [, int offset ]) Find position of first occurrence of a string within another */ PHP_FUNCTION(grapheme_strpos) { unsigned char *haystack, *needle; int haystack_len, needle_len; unsigned char *found; int offset = 0; int ret_pos, uchar_pos; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", (char **)&haystack, &haystack_len, (char **)&needle, &needle_len, &offset) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } if ( OUTSIDE_STRING(offset, haystack_len) ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 TSRMLS_CC ); RETURN_FALSE; } /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */ if (needle_len == 0) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 TSRMLS_CC ); RETURN_FALSE; } /* 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 */ found = (unsigned char *)php_memnstr((char *)haystack + offset, (char *)needle, needle_len, (char *)haystack + haystack_len); /* if it isn't there the we are done */ if (!found) { RETURN_FALSE; } /* if it is there, and if the haystack is ascii, we are all done */ if ( ascii_check(haystack, haystack_len) >= 0 ) { RETURN_LONG(found - haystack); } /* do utf16 part of the strpos */ ret_pos = strpos_utf16(haystack, haystack_len, needle, needle_len, offset, &uchar_pos, 0 /* fIgnoreCase */ TSRMLS_CC ); if ( ret_pos >= 0 ) { RETURN_LONG(ret_pos + offset); } else { RETURN_FALSE; } } /* }}} */ /* {{{ proto int grapheme_stripos(string haystack, string needle [, int offset ]) Find position of first occurrence of a string within another, ignoring case differences */ PHP_FUNCTION(grapheme_stripos) { unsigned char *haystack, *needle, *haystack_dup, *needle_dup; int haystack_len, needle_len; unsigned char *found; int offset = 0; int ret_pos, uchar_pos; int is_ascii; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", (char **)&haystack, &haystack_len, (char **)&needle, &needle_len, &offset) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_stripos: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } if ( OUTSIDE_STRING(offset, haystack_len) ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_stripos: Offset not contained in string", 1 TSRMLS_CC ); RETURN_FALSE; } /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */ if (needle_len == 0) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_stripos: Empty delimiter", 1 TSRMLS_CC ); RETURN_FALSE; } is_ascii = ( ascii_check(haystack, haystack_len) >= 0 ); if ( is_ascii ) { needle_dup = (unsigned char *)estrndup((char *)needle, needle_len); php_strtolower((char *)needle_dup, needle_len); haystack_dup = (unsigned char *)estrndup((char *)haystack, haystack_len); php_strtolower((char *)haystack_dup, haystack_len); found = (unsigned char*) php_memnstr((char *)haystack_dup + offset, (char *)needle_dup, needle_len, (char *)haystack_dup + haystack_len); efree(haystack_dup); efree(needle_dup); if (found) { RETURN_LONG(found - haystack_dup); } /* if needle was ascii too, we are all done, otherwise we need to try using Unicode to see what we get */ if ( ascii_check(needle, needle_len) >= 0 ) { RETURN_FALSE; } } /* do utf16 part of the strpos */ ret_pos = strpos_utf16(haystack, haystack_len, needle, needle_len, offset, &uchar_pos, 1 /* fIgnoreCase */ TSRMLS_CC ); if ( ret_pos >= 0 ) { RETURN_LONG(ret_pos + offset); } else { RETURN_FALSE; } } /* }}} */ /* {{{ proto int grapheme_strrpos(string haystack, string needle [, int offset]) Find position of last occurrence of a string within another */ PHP_FUNCTION(grapheme_strrpos) { unsigned char *haystack, *needle; int haystack_len, needle_len; int32_t offset = 0; int32_t ret_pos; int is_ascii; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", (char **)&haystack, &haystack_len, (char **)&needle, &needle_len, &offset) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strrpos: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } if ( OUTSIDE_STRING(offset, haystack_len) ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 TSRMLS_CC ); RETURN_FALSE; } /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */ if (needle_len == 0) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 TSRMLS_CC ); RETURN_FALSE; } is_ascii = ascii_check(haystack, haystack_len) >= 0; if ( is_ascii ) { ret_pos = strrpos_ascii(haystack, haystack_len, needle, needle_len, offset); if ( ret_pos >= 0 ) { RETURN_LONG(ret_pos); } /* if the needle was ascii too, we are done */ if ( ascii_check(needle, needle_len) >= 0 ) { RETURN_FALSE; } /* else we need to continue via utf16 */ } ret_pos = strrpos_utf16(haystack, haystack_len, needle, needle_len, offset, 0 /* f_ignore_case */ TSRMLS_CC); if ( ret_pos >= 0 ) { RETURN_LONG(ret_pos); } else { RETURN_FALSE; } } /* }}} */ /* {{{ proto int grapheme_strripos(string haystack, string needle [, int offset]) Find position of last occurrence of a string within another, ignoring case */ PHP_FUNCTION(grapheme_strripos) { unsigned char *haystack, *needle; int haystack_len, needle_len; int32_t offset = 0; int32_t ret_pos; int is_ascii; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", (char **)&haystack, &haystack_len, (char **)&needle, &needle_len, &offset) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strrpos: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } if ( OUTSIDE_STRING(offset, haystack_len) ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 TSRMLS_CC ); RETURN_FALSE; } /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */ if (needle_len == 0) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 TSRMLS_CC ); RETURN_FALSE; } is_ascii = ascii_check(haystack, haystack_len) >= 0; if ( is_ascii ) { unsigned char *needle_dup, *haystack_dup; needle_dup = (unsigned char *)estrndup((char *)needle, needle_len); php_strtolower((char *)needle_dup, needle_len); haystack_dup = (unsigned char *)estrndup((char *)haystack, haystack_len); php_strtolower((char *)haystack_dup, haystack_len); ret_pos = strrpos_ascii(haystack_dup, haystack_len, needle_dup, needle_len, offset); efree(haystack_dup); efree(needle_dup); if ( ret_pos >= 0 ) { RETURN_LONG(ret_pos); } /* if the needle was ascii too, we are done */ if ( ascii_check(needle, needle_len) >= 0 ) { RETURN_FALSE; } /* else we need to continue via utf16 */ } ret_pos = strrpos_utf16(haystack, haystack_len, needle, needle_len, offset, 1 /* f_ignore_case */ TSRMLS_CC); if ( ret_pos >= 0 ) { RETURN_LONG(ret_pos); } else { RETURN_FALSE; } } /* }}} */ /* {{{ substr_ascii */ static void substr_ascii(char *str, int str_len, int f, int l, int argc, char **sub_str, int *sub_str_len) { *sub_str = NULL; if (argc > 2) { if ((l < 0 && -l > str_len)) { return; } else if (l > str_len) { l = str_len; } } else { l = str_len; } if (f > str_len || (f < 0 && -f > str_len)) { return; } if (l < 0 && (l + str_len - f) < 0) { return; } /* if "from" position is negative, count start position from the end * of the string */ if (f < 0) { f = str_len + f; if (f < 0) { f = 0; } } /* if "length" position is negative, set it to the length * needed to stop that many chars from the end of the string */ if (l < 0) { l = (str_len - f) + l; if (l < 0) { l = 0; } } if (f >= str_len) { return; } if ((f + l) > str_len) { l = str_len - f; } *sub_str = str + f; *sub_str_len = l; return; } /* }}} */ /* {{{ proto string grapheme_substr(string str, int start [, int length]) Returns part of a string */ PHP_FUNCTION(grapheme_substr) { unsigned char *str, *sub_str; UChar *ustr; int str_len, sub_str_len, ustr_len, start = 0, length = 0; int iter_val; UErrorCode status; unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE]; UBreakIterator* bi = NULL; int sub_str_start_pos, sub_str_end_pos; int32_t (*iter_func)(UBreakIterator *); if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|l", (char **)&str, &str_len, &start, &length) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_substr: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } if ( OUTSIDE_STRING(start, str_len) ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_substr: start not contained in string", 1 TSRMLS_CC ); RETURN_FALSE; } /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */ if ( ascii_check(str, str_len) >= 0 ) { substr_ascii((char *)str, str_len, start, length, ZEND_NUM_ARGS(), (char **) &sub_str, &sub_str_len); if ( NULL == sub_str ) { RETURN_FALSE; } RETURN_STRINGL(((char *)sub_str), sub_str_len, 1); } ustr = NULL; ustr_len = 0; status = U_ZERO_ERROR; intl_convert_utf8_to_utf16(&ustr, &ustr_len, (char *)str, str_len, &status); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC ); efree( ustr ); RETURN_FALSE; } bi = get_break_iterator((void*)u_break_iterator_buffer, &status TSRMLS_CC ); if( U_FAILURE(status) ) { RETURN_FALSE; } ubrk_setText(bi, ustr, ustr_len, &status); if ( start < 0 ) { iter_func = ubrk_previous; ubrk_last(bi); iter_val = 1; } else { iter_func = ubrk_next; iter_val = -1; } sub_str_start_pos = 0; while ( start ) { sub_str_start_pos = iter_func(bi); if ( UBRK_DONE == sub_str_start_pos ) { break; } start += iter_val; } if ( 0 != start || sub_str_start_pos >= ustr_len ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_substr: start not contained in string", 1 TSRMLS_CC ); efree(ustr); ubrk_close(bi); RETURN_FALSE; } if (ZEND_NUM_ARGS() <= 2) { /* no length supplied, return the rest of the string */ sub_str = NULL; sub_str_len = 0; status = U_ZERO_ERROR; intl_convert_utf16_to_utf8((char **)&sub_str, &sub_str_len, ustr + sub_str_start_pos, ustr_len - sub_str_start_pos, &status); efree( ustr ); ubrk_close( bi ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 1 TSRMLS_CC ); efree( sub_str ); RETURN_FALSE; } /* return the allocated string, not a duplicate */ RETURN_STRINGL(((char *)sub_str), sub_str_len, 0); } /* find the end point of the string to return */ if ( length < 0 ) { iter_func = ubrk_previous; ubrk_last(bi); iter_val = 1; } else { iter_func = ubrk_next; iter_val = -1; } sub_str_end_pos = ustr_len; while ( length ) { sub_str_end_pos = iter_func(bi); if ( UBRK_DONE == sub_str_end_pos ) { break; } length += iter_val; } if ( UBRK_DONE == sub_str_end_pos ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_substr: length not contained in string", 1 TSRMLS_CC ); efree(ustr); ubrk_close(bi); RETURN_FALSE; } sub_str = NULL; status = U_ZERO_ERROR; intl_convert_utf16_to_utf8((char **)&sub_str, &sub_str_len, ustr + sub_str_start_pos, ( sub_str_end_pos - sub_str_start_pos ), &status); efree( ustr ); ubrk_close( bi ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 1 TSRMLS_CC ); if ( NULL != sub_str ) efree( sub_str ); RETURN_FALSE; } /* return the allocated string, not a duplicate */ RETURN_STRINGL(((char *)sub_str), sub_str_len, 0); } /* }}} */ /* {{{ strstr_common_handler */ static void strstr_common_handler(INTERNAL_FUNCTION_PARAMETERS, int f_ignore_case) { unsigned char *haystack, *needle, *found; int haystack_len, needle_len; int ret_pos, uchar_pos; zend_bool part = 0; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", (char **)&haystack, &haystack_len, (char **)&needle, &needle_len, &part) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strstr: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } if (needle_len == 0) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 TSRMLS_CC ); RETURN_FALSE; } if ( !f_ignore_case ) { /* ASCII optimization: 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 */ found = (unsigned char *)php_memnstr((char *)haystack, (char *)needle, needle_len, (char *)haystack + haystack_len); /* if it isn't there the we are done */ if ( !found ) { RETURN_FALSE; } /* if it is there, and if the haystack is ascii, we are all done */ if ( ascii_check(haystack, haystack_len) >= 0 ) { size_t found_offset = found - haystack; if (part) { RETURN_STRINGL(((char *)haystack) , found_offset, 1); } else { RETURN_STRINGL(((char *)found), haystack_len - found_offset, 1); } } } /* need to work in utf16 */ ret_pos = strpos_utf16(haystack, haystack_len, needle, needle_len, 0, &uchar_pos, f_ignore_case TSRMLS_CC ); if ( ret_pos < 0 ) { RETURN_FALSE; } /* uchar_pos is the 'nth' Unicode character position of the needle */ ret_pos = 0; U8_FWD_N(haystack, ret_pos, haystack_len, uchar_pos); if (part) { RETURN_STRINGL(((char *)haystack), ret_pos, 1); } else { RETURN_STRINGL(((char *)haystack) + ret_pos, haystack_len - ret_pos, 1); } } /* }}} */ /* {{{ proto string grapheme_strstr(string haystack, string needle[, bool part]) Finds first occurrence of a string within another */ PHP_FUNCTION(grapheme_strstr) { strstr_common_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0 /* f_ignore_case */); } /* }}} */ /* {{{ proto string grapheme_stristr(string haystack, string needle[, bool part]) Finds first occurrence of a string within another */ PHP_FUNCTION(grapheme_stristr) { strstr_common_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1 /* f_ignore_case */); } /* }}} */ /* {{{ proto string grapheme_extract(string str, size[, start]) Function to extract a sequence of default grapheme clusters */ PHP_FUNCTION(grapheme_extract) { unsigned char *str, *pstr; UChar *ustr; int str_len, ustr_len; int size; /* maximum number of grapheme clusters to return */ int start = 0; /* starting position in str in bytes */ UErrorCode status; unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE]; UBreakIterator* bi = NULL; int ret_pos, pos, next_pos; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|l", (char **)&str, &str_len, &size, &start) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strstr: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } pstr = str + start; /* just in case pstr points in the middle of a character, move forward to the start of the next char */ if ( !UTF8_IS_SINGLE(*pstr) && !U8_IS_LEAD(*pstr) ) { unsigned char *str_end = str + str_len; while ( !UTF8_IS_SINGLE(*pstr) && !U8_IS_LEAD(*pstr) ) { pstr++; if ( pstr >= str_end ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_extract: invalid input string", 0 TSRMLS_CC ); RETURN_FALSE; } } } str_len -= (pstr - str); /* convert the strings to UTF-16. */ ustr = NULL; ustr_len = 0; status = U_ZERO_ERROR; intl_convert_utf8_to_utf16(&ustr, &ustr_len, (char *)pstr, str_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC ); if ( NULL != ustr ) efree( ustr ); RETURN_FALSE; } bi = NULL; status = U_ZERO_ERROR; bi = get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC ); ubrk_setText(bi, ustr, ustr_len, &status); /* if the caller put us in the middle of a grapheme, we can't detect it in all cases since we can't back up. So, we will not do anything. */ /* now we need to find the end of the chunk the user wants us to return */ pos = 0; while ( size ) { next_pos = ubrk_next(bi); if ( UBRK_DONE == next_pos ) { break; } pos = next_pos; size--; } efree(ustr); ubrk_close(bi); /* pos is one past the last UChar - and represent the number of code units to advance in the utf-8 buffer */ ret_pos = 0; U8_FWD_N(pstr, ret_pos, str_len, pos); RETURN_STRINGL(((char *)pstr), ret_pos, 1); } /* }}} */ /* {{{ proto string grapheme_extractB(string str, bsize[, start]) Function to extract a sequence of default grapheme clusters up to a byte limit */ PHP_FUNCTION(grapheme_extractB) { char *str, *pstr; UChar *ustr; int str_len, ustr_len; int bsize; /* maximum number of bytes to return */ int start = 0; /* starting position in str in bytes */ UErrorCode status; unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE]; UBreakIterator* bi = NULL; int ret_pos, pos, prev_pos, prev_ret_pos; if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|l", (char **)&str, &str_len, &bsize, &start) == FAILURE) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_extractB: unable to parse input param", 0 TSRMLS_CC ); RETURN_FALSE; } pstr = str + start; /* just in case pstr points in the middle of a character, move forward to the start of the next char */ if ( !UTF8_IS_SINGLE(*pstr) && !U8_IS_LEAD(*pstr) ) { char *str_end = str + str_len; while ( !UTF8_IS_SINGLE(*pstr) && !U8_IS_LEAD(*pstr) ) { pstr++; if ( pstr >= str_end ) { intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_extract: invalid input string", 0 TSRMLS_CC ); RETURN_FALSE; } } } str_len -= (pstr - str); /* convert the strings to UTF-16. */ ustr = NULL; ustr_len = 0; status = U_ZERO_ERROR; intl_convert_utf8_to_utf16(&ustr, &ustr_len, pstr, str_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ intl_error_set_code( NULL, status TSRMLS_CC ); /* Set error messages. */ intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 1 TSRMLS_CC ); if ( NULL != ustr ) efree( ustr ); RETURN_FALSE; } bi = NULL; status = U_ZERO_ERROR; bi = get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC ); ubrk_setText(bi, ustr, ustr_len, &status); pos = 0; prev_pos = 0; ret_pos = 0; prev_ret_pos = 0; while ( 1 ) { pos = ubrk_next(bi); if ( UBRK_DONE == pos ) { break; } prev_ret_pos = ret_pos; U8_FWD_N(pstr, ret_pos, str_len, pos - prev_pos); if ( ret_pos > bsize ) { ret_pos = prev_ret_pos; break; } if ( prev_ret_pos == ret_pos ) { /* something wrong - malformed utf8? */ break; } prev_pos = pos; } efree(ustr); ubrk_close(bi); RETURN_STRINGL(pstr, ret_pos, 1); } /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: fdm=marker * vim: noet sw=4 ts=4 */ http://cvs.php.net/viewvc.cgi/pecl/unicode/grapheme/grapheme_util.h?view=markup&rev=1.1 Index: pecl/unicode/grapheme/grapheme_util.h +++ pecl/unicode/grapheme/grapheme_util.h /* +----------------------------------------------------------------------+ | PHP Version 5 | +----------------------------------------------------------------------+ | This source file is subject to version 3.01 of the PHP license, | | that is bundled with this package in the file LICENSE, and is | | available through the world-wide-web at the following url: | | http://www.php.net/license/3_01.txt | | If you did not receive a copy of the PHP license and are unable to | | obtain it through the world-wide-web, please send a note to | | license@php.net so we can mail you a copy immediately. | +----------------------------------------------------------------------+ | Authors: Ed Batutis <ed@batutis.com> | +----------------------------------------------------------------------+ */ #ifndef GRAPHEME_GRAPHEME_UTIL_H #define GRAPHEME_GRAPHEME_UTIL_H #include "php_unicode.h" #include "unicode_convert.h" static UBreakIterator* get_break_iterator(void *stack_buffer, UErrorCode *status TSRMLS_DC ); /* the following are brought over from the pecl/intl package. They are not renamed but remapped so this code can be easily moved back to pecl/intl if necessary */ /* we don't have an error code mechanism in the unicode package */ #ifndef intl_error_set # define intl_error_set( err, code, msg, copyMsg ) unicode_error_set #endif #ifndef unicode_error_set # define unicode_error_set #endif #ifndef intl_error_set_code # define intl_error_set_code( x, y ) #endif #ifndef intl_error_set_custom_msg # define intl_error_set_custom_msg( x, y, z ) #endif /* memory allocation macros borrowed from pecl/intl */ #ifndef eumalloc # define eumalloc(size) (UChar*)safe_emalloc(size, sizeof(UChar), 0) #endif #ifndef eurealloc # define eurealloc(ptr, size) (UChar*)erealloc((ptr), size * sizeof(UChar)) #endif /* map intl_ routines onto unicode_ routines */ #ifndef intl_convert_utf8_to_utf16 # define intl_convert_utf8_to_utf16 unicode_convert_utf8_to_utf16 #endif #ifndef intl_convert_utf16_to_utf8 # define intl_convert_utf16_to_utf8 unicode_convert_utf16_to_utf8 #endif #endif // GRAPHEME_GRAPHEME_UTIL_H http://cvs.php.net/viewvc.cgi/pecl/unicode/tests/grapheme.phpt?view=markup&rev=1.1 Index: pecl/unicode/tests/grapheme.phpt +++ pecl/unicode/tests/grapheme.phpt --TEST-- grapheme() --SKIPIF-- <?php if( !extension_loaded( 'intl' ) || !extension_loaded( 'unicode' ) ) print 'skip'; ?> --FILE-- <?php /* * Test grapheme functions (procedural only) */ function ut_main() { $res_str = ''; $char_a_diaeresis = "\xC3\xA4"; // 'LATIN SMALL LETTER A WITH DIAERESIS' (U+00E4) $char_a_ring = "\xC3\xA5"; // 'LATIN SMALL LETTER A WITH RING ABOVE' (U+00E5) $char_o_diaeresis = "\xC3\xB6"; // 'LATIN SMALL LETTER O WITH DIAERESIS' (U+00F6) $char_O_diaeresis = "\xC3\x96"; // 'LATIN CAPITAL LETTER O WITH DIAERESIS' (U+00D6) $char_angstrom_sign = "\xE2\x84\xAB"; // 'ANGSTROM SIGN' (U+212B) $char_A_ring = "\xC3\x85"; // 'LATIN CAPITAL LETTER A WITH RING ABOVE' (U+00C5) $char_ohm_sign = "\xE2\x84\xA6"; // 'OHM SIGN' (U+2126) $char_omega = "\xCE\xA9"; // 'GREEK CAPITAL LETTER OMEGA' (U+03A9) $char_combining_ring_above = "\xCC\x8A"; // 'COMBINING RING ABOVE' (U+030A) $char_fi_ligature = "\xEF\xAC\x81"; // 'LATIN SMALL LIGATURE FI' (U+FB01) $char_long_s_dot = "\xE1\xBA\x9B"; // 'LATIN SMALL LETTER LONG S WITH DOT ABOVE' (U+1E9B) // the word 'hindi' using Devanagari characters: $hindi = "\xe0\xa4\xb9\xe0\xa4\xbf\xe0\xa4\xa8\xe0\xa5\x8d\xe0\xa4\xa6\xe0\xa5\x80"; $char_a_ring_nfd = "a\xCC\x8A"; $char_A_ring_nfd = "A\xCC\x8A"; $char_o_diaeresis_nfd = "o\xCC\x88"; $char_O_diaeresis_nfd = "O\xCC\x88"; $char_diaeresis = "\xCC\x88"; //===================================================================================== $res_str .= "\n" . 'function grapheme_strlen($string) {}' . "\n\n"; $res_str .= "\"hindi\" in devanagari strlen " . grapheme_strlen($hindi) . "\n"; $res_str .= "\"ab\" + \"hindi\" + \"cde\" strlen " . grapheme_strlen('ab' . $hindi . 'cde') . "\n"; $res_str .= "\"\" strlen " . grapheme_strlen("") . "\n"; $res_str .= "char_a_ring_nfd strlen " . grapheme_strlen($char_a_ring_nfd) . "\n"; $res_str .= "char_a_ring_nfd + \"bc\" strlen " . grapheme_strlen($char_a_ring_nfd . 'bc') . "\n"; $res_str .= "\"abc\" strlen " . grapheme_strlen('abc') . "\n"; //===================================================================================== $res_str .= "\n" . 'function grapheme_strpos($haystack, $needle, $offset = 0) {}' . "\n\n"; $tests = array( array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o", "o", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "o", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, $char_o_diaeresis_nfd, 4 ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, 2 ), array( "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, 1 ), array( "abc", $char_a_ring_nfd, "false" ), array( $char_a_ring_nfd . "bc", "a", "false" ), array( "abc", "d", "false" ), array( "abc", "c", 2 ), array( "abc", "b", 1 ), array( "abc", "a", 0 ), array( "abc", "a", 0, 0 ), array( "abc", "a", 1, "false" ), array( "ababc", "a", 1, 2 ), array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o", "o", 2, 6 ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, 2, 3 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "opq", "op", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "opq", "opq", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "abc", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "bc" . $char_o_diaeresis_nfd, $char_o_diaeresis_nfd . "bc" . $char_o_diaeresis_nfd, 4 ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd . "bc", 2 ), array( "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd . "bc", 1 ), array( "abc", $char_a_ring_nfd . "bc", "false" ), array( $char_a_ring_nfd . "bc", "abcdefg", "false" ), array( "abc", "defghijklmnopq", "false" ), array( "abc", "ab", 0 ), array( "abc", "bc", 1 ), array( "abc", "abc", 0 ), array( "abc", "abcd", "false" ), array( "abc", "ab", 0, 0 ), array( "abc", "abc", 0, 0 ), array( "abc", "abc", 1, "false" ), array( "ababc", "ab", 1, 2 ), array( "ababc", "abc", 1, 2 ), array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o" . $char_a_ring_nfd . "bc", "o" . $char_a_ring_nfd . "bc", 2, 6 ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_a_ring_nfd . "bc" . $char_a_ring_nfd . "def", $char_a_ring_nfd . "bc" . $char_a_ring_nfd, 2, 3 ), ); foreach( $tests as $test ) { $arg1 = urlencode($test[1]); $arg0 = urlencode($test[0]); $res_str .= "find \"$arg1\" in \"$arg0\" - grapheme_strpos"; if ( 3 == count( $test ) ) { $result = grapheme_strpos($test[0], $test[1]); } else { $res_str .= " from $test[2]"; $result = grapheme_strpos($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= $result; } $res_str .= " == " . $test[count($test)-1] . "\n"; } //===================================================================================== $res_str .= "\n" . 'function grapheme_stripos($haystack, $needle, $offset = 0) {}' . "\n\n"; $tests = array( array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", "o", 2, 6 ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_A_ring_nfd . "bc", $char_a_ring_nfd, 2, 3 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", "o", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "O", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_O_diaeresis_nfd, $char_o_diaeresis_nfd, 4 ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_A_ring_nfd, 2 ), array( "a" . $char_A_ring_nfd . "bc", $char_a_ring_nfd, 1 ), array( "Abc", $char_a_ring_nfd, "false" ), array( $char_a_ring_nfd . "bc", "A", "false" ), array( "abc", "D", "false" ), array( "abC", "c", 2 ), array( "abc", "B", 1 ), array( "Abc", "a", 0 ), array( "abc", "A", 0, 0 ), array( "Abc", "a", 1, "false" ), array( "ababc", "A", 1, 2 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", "oP", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", "opQ", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "abc", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "bC" . $char_o_diaeresis_nfd, $char_O_diaeresis_nfd . "bc" . $char_o_diaeresis_nfd, 4 ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "Bc", $char_A_ring_nfd . "bc", 2 ), array( "a" . $char_a_ring_nfd . "BC", $char_a_ring_nfd . "bc", 1 ), array( "abc", $char_a_ring_nfd . "BC", "false" ), array( $char_a_ring_nfd . "BC", "aBCdefg", "false" ), array( "aBC", "Defghijklmnopq", "false" ), array( "abC", "Ab", 0 ), array( "aBC", "bc", 1 ), array( "abC", "Abc", 0 ), array( "abC", "aBcd", "false" ), array( "ABc", "ab", 0, 0 ), array( "aBc", "abC", 0, 0 ), array( "abc", "aBc", 1, "false" ), array( "ABabc", "AB", 1, 2 ), array( "abaBc", "aBc", 1, 2 ), array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o" . $char_A_ring_nfd . "bC", "O" . $char_a_ring_nfd . "bC", 2, 6 ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_A_ring_nfd . "bC" . $char_a_ring_nfd . "def", $char_a_ring_nfd . "Bc" . $char_a_ring_nfd, 2, 3 ), ); foreach( $tests as $test ) { $arg1 = urlencode($test[1]); $arg0 = urlencode($test[0]); $res_str .= "find \"$arg1\" in \"$arg0\" - grapheme_stripos"; if ( 3 == count( $test ) ) { $result = grapheme_stripos($test[0], $test[1]); } else { $res_str .= " from $test[2]"; $result = grapheme_stripos($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= $result; } $res_str .= " == " . $test[count($test)-1] . "\n"; } //===================================================================================== $res_str .= "\n" . 'function grapheme_strrpos($haystack, $needle, $offset = 0) {}' . "\n\n"; $tests = array( array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o", "o", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "o", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, $char_o_diaeresis_nfd, 4 ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, 2 ), array( "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, 1 ), array( "abc", $char_a_ring_nfd, "false" ), array( $char_a_ring_nfd . "bc", "a", "false" ), array( "abc", "d", "false" ), array( "abc", "c", 2 ), array( "abc", "b", 1 ), array( "abc", "a", 0 ), array( "abc", "a", 0, 0 ), array( "abc", "a", 1, "false" ), array( "ababc", "a", 1, 2 ), array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o", "o", 2, 6 ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, 2, 3 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "opq", "op", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "opq", "opq", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "abc", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "bc" . $char_o_diaeresis_nfd, $char_o_diaeresis_nfd . "bc" . $char_o_diaeresis_nfd, 4 ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd . "bc", 2 ), array( "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd . "bc", 1 ), array( "abc", $char_a_ring_nfd . "bc", "false" ), array( $char_a_ring_nfd . "bc", "abcdefg", "false" ), array( "abc", "defghijklmnopq", "false" ), array( "abc", "ab", 0 ), array( "abc", "bc", 1 ), array( "abc", "abc", 0 ), array( "abc", "abcd", "false" ), array( "abc", "ab", 0, 0 ), array( "abc", "abc", 0, 0 ), array( "abc", "abc", 1, "false" ), array( "ababc", "ab", 1, 2 ), array( "ababc", "abc", 1, 2 ), array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o" . $char_a_ring_nfd . "bc", "o" . $char_a_ring_nfd . "bc", 2, 6 ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_a_ring_nfd . "bc" . $char_a_ring_nfd . "def", $char_a_ring_nfd . "bc" . $char_a_ring_nfd, 2, 3 ), ); foreach( $tests as $test ) { $arg1 = urlencode($test[1]); $arg0 = urlencode($test[0]); $res_str .= "find \"$arg1\" in \"$arg0\" - grapheme_strrpos"; if ( 3 == count( $test ) ) { $result = grapheme_strrpos($test[0], $test[1]); } else { $res_str .= " from $test[2]"; $result = grapheme_strrpos($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= $result; } $res_str .= " == " . $test[count($test)-1] . "\n"; } //===================================================================================== $res_str .= "\n" . 'function grapheme_strripos($haystack, $needle, $offset = 0) {}' . "\n\n"; $tests = array( array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", "o", 2, 6 ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_A_ring_nfd . "bc", $char_a_ring_nfd, 2, 3 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", "o", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "O", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_O_diaeresis_nfd, $char_o_diaeresis_nfd, 4 ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_A_ring_nfd, 2 ), array( "a" . $char_A_ring_nfd . "bc", $char_a_ring_nfd, 1 ), array( "Abc", $char_a_ring_nfd, "false" ), array( $char_a_ring_nfd . "bc", "A", "false" ), array( "abc", "D", "false" ), array( "abC", "c", 2 ), array( "abc", "B", 1 ), array( "Abc", "a", 0 ), array( "abc", "A", 0, 0 ), array( "Abc", "a", 1, "false" ), array( "ababc", "A", 1, 2 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", "oP", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", "opQ", 5 ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "abc", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "bC" . $char_o_diaeresis_nfd, $char_O_diaeresis_nfd . "bc" . $char_o_diaeresis_nfd, 4 ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "Bc", $char_A_ring_nfd . "bc", 2 ), array( "a" . $char_a_ring_nfd . "BC", $char_a_ring_nfd . "bc", 1 ), array( "abc", $char_a_ring_nfd . "BC", "false" ), array( $char_a_ring_nfd . "BC", "aBCdefg", "false" ), array( "aBC", "Defghijklmnopq", "false" ), array( "abC", "Ab", 0 ), array( "aBC", "bc", 1 ), array( "abC", "Abc", 0 ), array( "abC", "aBcd", "false" ), array( "ABc", "ab", 0, 0 ), array( "aBc", "abC", 0, 0 ), array( "abc", "aBc", 1, "false" ), array( "ABabc", "AB", 1, 2 ), array( "abaBc", "aBc", 1, 2 ), array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o" . $char_A_ring_nfd . "bC", "O" . $char_a_ring_nfd . "bC", 2, 6 ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_A_ring_nfd . "bC" . $char_a_ring_nfd . "def", $char_a_ring_nfd . "Bc" . $char_a_ring_nfd, 2, 3 ), ); foreach( $tests as $test ) { $arg1 = urlencode($test[1]); $arg0 = urlencode($test[0]); $res_str .= "find \"$arg1\" in \"$arg0\" - grapheme_strripos"; if ( 3 == count( $test ) ) { $result = grapheme_strripos($test[0], $test[1]); } else { $res_str .= " from $test[2]"; $result = grapheme_strripos($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= $result; } $res_str .= " == " . $test[count($test)-1] . "\n"; } //===================================================================================== $res_str .= "\n" . 'function grapheme_substr($string, $start, $length = -1) {}' . "\n\n"; $tests = array( array( "abc", 3, "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, 5, "false" ), array( "ao" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", 2, $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O" ), array( $char_o_diaeresis_nfd . $char_a_ring_nfd . "a" . $char_A_ring_nfd . "bc", 2, "a" . $char_A_ring_nfd . "bc" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", 5, "O" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, 5, "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_O_diaeresis_nfd, 4, $char_O_diaeresis_nfd ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", 2, $char_a_ring_nfd . "bc" ), array( "a" . $char_A_ring_nfd . "bc", 1, $char_A_ring_nfd . "bc" ), array( "Abc", -5, "false" ), array( $char_a_ring_nfd . "bc", 3, "false" ), array( "abc", 4, "false" ), array( "abC", 2, "C" ), array( "abc", 1, "bc" ), array( "Abc", 1, 1, "b" ), array( "abc", 0, 2, "ab" ), array( "Abc", -4, 1, "false" ), array( "ababc", 1, 2, "ba" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 5, "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 5, -1, "Op" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 5, -2, "O" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 5, -3, "" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 5, -4, "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -1, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Op" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -2, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -3, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -4, "a" . $char_a_ring_nfd . "bc" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -5, "a" . $char_a_ring_nfd . "b" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -6, "a" . $char_a_ring_nfd ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -7, "a" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -8, "" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", 0, -9, "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -7, $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -6, "bc" . $char_o_diaeresis_nfd . "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -5, "c" . $char_o_diaeresis_nfd . "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -4, $char_o_diaeresis_nfd . "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -3, "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -2, "pq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -1, "q" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -999, "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 8, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 7, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Op" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 6, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 5, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 4, "a" . $char_a_ring_nfd . "bc" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 3, "a" . $char_a_ring_nfd . "b" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 2, "a" . $char_a_ring_nfd ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 1, "a" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, 0, "" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -999, "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -1, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Op" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -2, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -3, "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -4, "a" . $char_a_ring_nfd . "bc" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -5, "a" . $char_a_ring_nfd . "b" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -6, "a" . $char_a_ring_nfd ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -7, "a" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -8, "" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "Opq", -8, -9, "false" ), ); foreach( $tests as $test ) { $arg0 = urlencode($test[0]); $res_str .= "substring of \"$arg0\" from \"$test[1]\" - grapheme_substr"; if ( 3 == count( $test ) ) { $result = grapheme_substr($test[0], $test[1]); } else { $res_str .= " with length $test[2]"; $result = grapheme_substr($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= urlencode($result); } $res_str .= " == " . urlencode($test[count($test)-1]) . "\n"; } //===================================================================================== $res_str .= "\n" . 'function grapheme_strstr($haystack, $needle, $before_needle = FALSE) {}' . "\n\n"; $tests = array( array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "o", "o", "o" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "o", "false" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, $char_o_diaeresis_nfd, $char_o_diaeresis_nfd ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, $char_a_ring_nfd . "bc"), array( "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, $char_a_ring_nfd . "bc"), array( "abc", $char_a_ring_nfd, "false" ), array( $char_a_ring_nfd . "bc", "a", "false" ), array( "abc", "d", "false" ), array( "abc", "c", "c" ), array( "abc", "b", "bc" ), array( "abc", "a", "abc" ), array( "abc", "ab", "abc" ), array( "abc", "abc", "abc" ), array( "abc", "bc", "bc" ), array( "abc", "a", FALSE, "abc" ), array( "abc", "a", TRUE, "" ), array( "abc", "b", TRUE, "a" ), array( "abc", "c", TRUE, "ab" ), array( "ababc", "bab", TRUE, "a" ), array( "ababc", "abc", TRUE, "ab" ), array( "ababc", "abc", FALSE, "abc" ), array( "ab" . $char_a_ring_nfd . "c", "d", "false" ), array( "bc" . $char_a_ring_nfd . "a", "a", "a" ), array( "a" . $char_a_ring_nfd . "bc", "b", "bc" ), array( $char_a_ring_nfd . "bc", "a", "false" ), array( $char_a_ring_nfd . "abc", "ab", "abc" ), array( "abc" . $char_a_ring_nfd, "abc", "abc" . $char_a_ring_nfd), array( "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd . "bc", $char_a_ring_nfd . "bc" ), array( "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, FALSE, $char_a_ring_nfd . "bc"), array( "a" . $char_a_ring_nfd . "bc", "a", TRUE, "" ), array( $char_a_ring_nfd . "abc", "b", TRUE, $char_a_ring_nfd . "a" ), array( "ab" . $char_a_ring_nfd . "c", "c", TRUE, "ab" . $char_a_ring_nfd ), array( "aba" . $char_a_ring_nfd . "bc", "ba" . $char_a_ring_nfd . "b", TRUE, "a" ), array( "ababc" . $char_a_ring_nfd, "abc" . $char_a_ring_nfd, TRUE, "ab" ), array( "abab" . $char_a_ring_nfd . "c", "ab" . $char_a_ring_nfd . "c", FALSE, "ab" . $char_a_ring_nfd . "c" ), ); foreach( $tests as $test ) { $arg1 = urlencode($test[1]); $arg0 = urlencode($test[0]); $res_str .= "find \"$arg1\" in \"$arg0\" - grapheme_strstr"; if ( 3 == count( $test ) ) { $result = grapheme_strstr($test[0], $test[1]); } else { $res_str .= " before flag is " . ( $test[2] ? "TRUE" : "FALSE" ); $result = grapheme_strstr($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= urlencode($result); } $res_str .= " == " . urlencode($test[count($test)-1]) . "\n"; } //===================================================================================== $res_str .= "\n" . 'function grapheme_stristr($haystack, $needle, $before_needle = FALSE) {}' . "\n\n"; $tests = array( array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, $char_O_diaeresis_nfd, $char_o_diaeresis_nfd ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd . "O", "o", "O" ), array( "a" . $char_a_ring_nfd . "bc" . $char_o_diaeresis_nfd, "o", "false" ), array( $char_o_diaeresis_nfd . "a" . $char_a_ring_nfd . "bc", $char_a_ring_nfd, $char_a_ring_nfd . "bc"), array( "a" . $char_a_ring_nfd . "bc", $char_A_ring_nfd, $char_a_ring_nfd . "bc"), array( "abc", $char_a_ring_nfd, "false" ), array( $char_a_ring_nfd . "bc", "A", "false" ), array( "abc", "d", "false" ), array( "abc", "C", "c" ), array( "aBc", "b", "Bc" ), array( "abc", "A", "abc" ), array( "abC", "ab", "abC" ), array( "abc", "aBc", "abc" ), array( "abC", "bc", "bC" ), array( "abc", "A", FALSE, "abc" ), array( "abc", "a", TRUE, "" ), array( "aBc", "b", TRUE, "a" ), array( "abc", "C", TRUE, "ab" ), array( "aBabc", "bab", TRUE, "a" ), array( "ababc", "aBc", TRUE, "ab" ), array( "ababc", "abC", FALSE, "abc" ), array( "ab" . $char_a_ring_nfd . "c", "d", "false" ), array( "bc" . $char_a_ring_nfd . "A", "a", "A" ), array( "a" . $char_a_ring_nfd . "bc", "B", "bc" ), array( $char_A_ring_nfd . "bc", "a", "false" ), array( $char_a_ring_nfd . "abc", "Ab", "abc" ), array( "abc" . $char_A_ring_nfd, "abc", "abc" . $char_A_ring_nfd), array( "a" . $char_a_ring_nfd . "bc", $char_A_ring_nfd . "bc", $char_a_ring_nfd . "bc" ), array( "a" . $char_A_ring_nfd . "bc", $char_a_ring_nfd, FALSE, $char_A_ring_nfd . "bc" ), array( "a" . $char_a_ring_nfd . "bc", "A", TRUE, "" ), array( $char_a_ring_nfd . "aBc", "b", TRUE, $char_a_ring_nfd . "a" ), array( "ab" . $char_a_ring_nfd . "c", "C", TRUE, "ab" . $char_a_ring_nfd ), array( "aba" . $char_A_ring_nfd . "bc", "ba" . $char_a_ring_nfd . "b", TRUE, "a" ), array( "ababc" . $char_a_ring_nfd, "aBc" . $char_A_ring_nfd, TRUE, "ab" ), array( "abAB" . $char_A_ring_nfd . "c", "ab" . $char_a_ring_nfd . "c", FALSE, "AB" . $char_A_ring_nfd . "c" ), ); foreach( $tests as $test ) { $arg1 = urlencode($test[1]); $arg0 = urlencode($test[0]); $res_str .= "find \"$arg1\" in \"$arg0\" - grapheme_stristr"; if ( 3 == count( $test ) ) { $result = grapheme_stristr($test[0], $test[1]); } else { $res_str .= " before flag is " . ( $test[2] ? "TRUE" : "FALSE" ); $result = grapheme_stristr($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= urlencode($result); } $res_str .= " == " . urlencode($test[count($test)-1]) . "\n"; } //===================================================================================== $res_str .= "\n" . 'function grapheme_extract($haystack, $size, $start = 0)' . "\n\n"; $tests = array( array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 1, 4, $char_o_diaeresis_nfd), array( "abc", 3, "abc" ), array( "abc", 2, "ab" ), array( "abc", 1, "a" ), array( "abc", 0, "" ), array( $char_a_ring_nfd . "bc", 3, $char_a_ring_nfd . "bc" ), array( $char_a_ring_nfd . "bc", 2, $char_a_ring_nfd . "b" ), array( $char_a_ring_nfd . "bc", 1, $char_a_ring_nfd . "" ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 3, $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, $char_a_ring_nfd . $char_o_diaeresis_nfd ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . "c", 1, $char_a_ring_nfd . "" ), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 1, 0, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 1, 2, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 1, 3, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 1, 4, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 0, $char_o_diaeresis_nfd . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 2, $char_o_diaeresis_nfd . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 3, $char_o_diaeresis_nfd . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 4, $char_diaeresis . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 7, $char_diaeresis . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 8, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 10, $char_diaeresis), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 11, "false"), ); foreach( $tests as $test ) { $arg0 = urlencode($test[0]); $res_str .= "extract from \"$arg0\" \"$test[1]\" graphemes - grapheme_extract"; if ( 3 == count( $test ) ) { $result = grapheme_extract($test[0], $test[1]); } else { $res_str .= " starting at byte position $test[2]"; $result = grapheme_extract($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= urlencode($result); } $res_str .= " == " . urlencode($test[count($test)-1]) . "\n"; } //===================================================================================== $res_str .= "\n" . 'function grapheme_extractB($haystack, $size, $start = 0)' . "\n\n"; $tests = array( array( "abc", 3, "abc" ), array( "abc", 2, "ab" ), array( "abc", 1, "a" ), array( "abc", 0, "" ), array( $char_a_ring_nfd . "bc", 5, $char_a_ring_nfd . "bc" ), array( $char_a_ring_nfd . "bc", 4, $char_a_ring_nfd . "b" ), array( $char_a_ring_nfd . "bc", 1, "" ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 9, $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 10, $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 11, $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 6, $char_a_ring_nfd . $char_o_diaeresis_nfd ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . "c", 3, $char_a_ring_nfd . "" ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . "c", 4, $char_a_ring_nfd . "" ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . "c", 5, $char_a_ring_nfd . "" ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . "c", 6, $char_a_ring_nfd . $char_o_diaeresis_nfd ), array( $char_a_ring_nfd . $char_o_diaeresis_nfd . "c", 7, $char_a_ring_nfd . $char_o_diaeresis_nfd . "c" ), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 3, 0, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 3, 2, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 3, 3, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 3, 4, $char_diaeresis), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 6, 0, $char_o_diaeresis_nfd . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 6, 2, $char_o_diaeresis_nfd . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 6, 3, $char_o_diaeresis_nfd . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 5, 4, $char_diaeresis . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 5, 7, $char_diaeresis . $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 3, 8, $char_o_diaeresis_nfd), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 10, $char_diaeresis), array( $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd . $char_o_diaeresis_nfd, 2, 11, "false"), ); foreach( $tests as $test ) { $arg0 = urlencode($test[0]); $res_str .= "extract from \"$arg0\" \"$test[1]\" graphemes - grapheme_extractB"; if ( 3 == count( $test ) ) { $result = grapheme_extractB($test[0], $test[1]); } else { $res_str .= " starting at byte position $test[2]"; $result = grapheme_extractB($test[0], $test[1], $test[2]); } $res_str .= " = "; if ( $result === false ) { $res_str .= 'false'; } else { $res_str .= urlencode($result); } $res_str .= " == " . urlencode($test[count($test)-1]) . "\n"; } //===================================================================================== return $res_str; } echo ut_main(); ?> --EXPECT-- function grapheme_strlen($string) {} "hindi" in devanagari strlen 5 "ab" + "hindi" + "cde" strlen 10 "" strlen 0 char_a_ring_nfd strlen 1 char_a_ring_nfd + "bc" strlen 3 "abc" strlen 3 function grapheme_strpos($haystack, $needle, $offset = 0) {} find "o" in "aa%CC%8Abco%CC%88o" - grapheme_strpos = 5 == 5 find "o" in "aa%CC%8Abco%CC%88" - grapheme_strpos = false == false find "o%CC%88" in "aa%CC%8Abco%CC%88" - grapheme_strpos = 4 == 4 find "a%CC%8A" in "o%CC%88aa%CC%8Abc" - grapheme_strpos = 2 == 2 find "a%CC%8A" in "aa%CC%8Abc" - grapheme_strpos = 1 == 1 find "a%CC%8A" in "abc" - grapheme_strpos = false == false find "a" in "a%CC%8Abc" - grapheme_strpos = false == false find "d" in "abc" - grapheme_strpos = false == false find "c" in "abc" - grapheme_strpos = 2 == 2 find "b" in "abc" - grapheme_strpos = 1 == 1 find "a" in "abc" - grapheme_strpos = 0 == 0 find "a" in "abc" - grapheme_strpos from 0 = 0 == 0 find "a" in "abc" - grapheme_strpos from 1 = false == false find "a" in "ababc" - grapheme_strpos from 1 = 2 == 2 find "o" in "aoa%CC%8Abco%CC%88o" - grapheme_strpos from 2 = 6 == 6 find "a%CC%8A" in "o%CC%88a%CC%8Aaa%CC%8Abc" - grapheme_strpos from 2 = 3 == 3 find "op" in "aa%CC%8Abco%CC%88opq" - grapheme_strpos = 5 == 5 find "opq" in "aa%CC%8Abco%CC%88opq" - grapheme_strpos = 5 == 5 find "abc" in "aa%CC%8Abco%CC%88" - grapheme_strpos = false == false find "o%CC%88bco%CC%88" in "aa%CC%8Abco%CC%88bco%CC%88" - grapheme_strpos = 4 == 4 find "a%CC%8Abc" in "o%CC%88aa%CC%8Abc" - grapheme_strpos = 2 == 2 find "a%CC%8Abc" in "aa%CC%8Abc" - grapheme_strpos = 1 == 1 find "a%CC%8Abc" in "abc" - grapheme_strpos = false == false find "abcdefg" in "a%CC%8Abc" - grapheme_strpos = false == false find "defghijklmnopq" in "abc" - grapheme_strpos = false == false find "ab" in "abc" - grapheme_strpos = 0 == 0 find "bc" in "abc" - grapheme_strpos = 1 == 1 find "abc" in "abc" - grapheme_strpos = 0 == 0 find "abcd" in "abc" - grapheme_strpos = false == false find "ab" in "abc" - grapheme_strpos from 0 = 0 == 0 find "abc" in "abc" - grapheme_strpos from 0 = 0 == 0 find "abc" in "abc" - grapheme_strpos from 1 = false == false find "ab" in "ababc" - grapheme_strpos from 1 = 2 == 2 find "abc" in "ababc" - grapheme_strpos from 1 = 2 == 2 find "oa%CC%8Abc" in "aoa%CC%8Abco%CC%88oa%CC%8Abc" - grapheme_strpos from 2 = 6 == 6 find "a%CC%8Abca%CC%8A" in "o%CC%88a%CC%8Aaa%CC%8Abca%CC%8Adef" - grapheme_strpos from 2 = 3 == 3 function grapheme_stripos($haystack, $needle, $offset = 0) {} find "o" in "aoa%CC%8Abco%CC%88O" - grapheme_stripos from 2 = 6 == 6 find "a%CC%8A" in "o%CC%88a%CC%8AaA%CC%8Abc" - grapheme_stripos from 2 = 3 == 3 find "o" in "aa%CC%8Abco%CC%88O" - grapheme_stripos = 5 == 5 find "O" in "aa%CC%8Abco%CC%88" - grapheme_stripos = false == false find "o%CC%88" in "aa%CC%8AbcO%CC%88" - grapheme_stripos = 4 == 4 find "A%CC%8A" in "o%CC%88aa%CC%8Abc" - grapheme_stripos = 2 == 2 find "a%CC%8A" in "aA%CC%8Abc" - grapheme_stripos = 1 == 1 find "a%CC%8A" in "Abc" - grapheme_stripos = false == false find "A" in "a%CC%8Abc" - grapheme_stripos = false == false find "D" in "abc" - grapheme_stripos = false == false find "c" in "abC" - grapheme_stripos = 2 == 2 find "B" in "abc" - grapheme_stripos = 1 == 1 find "a" in "Abc" - grapheme_stripos = 0 == 0 find "A" in "abc" - grapheme_stripos from 0 = 0 == 0 find "a" in "Abc" - grapheme_stripos from 1 = false == false find "A" in "ababc" - grapheme_stripos from 1 = 2 == 2 find "oP" in "aa%CC%8Abco%CC%88Opq" - grapheme_stripos = 5 == 5 find "opQ" in "aa%CC%8Abco%CC%88Opq" - grapheme_stripos = 5 == 5 find "abc" in "aa%CC%8Abco%CC%88" - grapheme_stripos = false == false find "O%CC%88bco%CC%88" in "aa%CC%8Abco%CC%88bCo%CC%88" - grapheme_stripos = 4 == 4 find "A%CC%8Abc" in "o%CC%88aa%CC%8ABc" - grapheme_stripos = 2 == 2 find "a%CC%8Abc" in "aa%CC%8ABC" - grapheme_stripos = 1 == 1 find "a%CC%8ABC" in "abc" - grapheme_stripos = false == false find "aBCdefg" in "a%CC%8ABC" - grapheme_stripos = false == false find "Defghijklmnopq" in "aBC" - grapheme_stripos = false == false find "Ab" in "abC" - grapheme_stripos = 0 == 0 find "bc" in "aBC" - grapheme_stripos = 1 == 1 find "Abc" in "abC" - grapheme_stripos = 0 == 0 find "aBcd" in "abC" - grapheme_stripos = false == false find "ab" in "ABc" - grapheme_stripos from 0 = 0 == 0 find "abC" in "aBc" - grapheme_stripos from 0 = 0 == 0 find "aBc" in "abc" - grapheme_stripos from 1 = false == false find "AB" in "ABabc" - grapheme_stripos from 1 = 2 == 2 find "aBc" in "abaBc" - grapheme_stripos from 1 = 2 == 2 find "Oa%CC%8AbC" in "aoa%CC%8Abco%CC%88oA%CC%8AbC" - grapheme_stripos from 2 = 6 == 6 find "a%CC%8ABca%CC%8A" in "o%CC%88a%CC%8AaA%CC%8AbCa%CC%8Adef" - grapheme_stripos from 2 = 3 == 3 function grapheme_strrpos($haystack, $needle, $offset = 0) {} find "o" in "aa%CC%8Abco%CC%88o" - grapheme_strrpos = 5 == 5 find "o" in "aa%CC%8Abco%CC%88" - grapheme_strrpos = false == false find "o%CC%88" in "aa%CC%8Abco%CC%88" - grapheme_strrpos = 4 == 4 find "a%CC%8A" in "o%CC%88aa%CC%8Abc" - grapheme_strrpos = 2 == 2 find "a%CC%8A" in "aa%CC%8Abc" - grapheme_strrpos = 1 == 1 find "a%CC%8A" in "abc" - grapheme_strrpos = false == false find "a" in "a%CC%8Abc" - grapheme_strrpos = false == false find "d" in "abc" - grapheme_strrpos = false == false find "c" in "abc" - grapheme_strrpos = 2 == 2 find "b" in "abc" - grapheme_strrpos = 1 == 1 find "a" in "abc" - grapheme_strrpos = 0 == 0 find "a" in "abc" - grapheme_strrpos from 0 = 0 == 0 find "a" in "abc" - grapheme_strrpos from 1 = false == false find "a" in "ababc" - grapheme_strrpos from 1 = 2 == 2 find "o" in "aoa%CC%8Abco%CC%88o" - grapheme_strrpos from 2 = 6 == 6 find "a%CC%8A" in "o%CC%88a%CC%8Aaa%CC%8Abc" - grapheme_strrpos from 2 = 3 == 3 find "op" in "aa%CC%8Abco%CC%88opq" - grapheme_strrpos = 5 == 5 find "opq" in "aa%CC%8Abco%CC%88opq" - grapheme_strrpos = 5 == 5 find "abc" in "aa%CC%8Abco%CC%88" - grapheme_strrpos = false == false find "o%CC%88bco%CC%88" in "aa%CC%8Abco%CC%88bco%CC%88" - grapheme_strrpos = 4 == 4 find "a%CC%8Abc" in "o%CC%88aa%CC%8Abc" - grapheme_strrpos = 2 == 2 find "a%CC%8Abc" in "aa%CC%8Abc" - grapheme_strrpos = 1 == 1 find "a%CC%8Abc" in "abc" - grapheme_strrpos = false == false find "abcdefg" in "a%CC%8Abc" - grapheme_strrpos = false == false find "defghijklmnopq" in "abc" - grapheme_strrpos = false == false find "ab" in "abc" - grapheme_strrpos = 0 == 0 find "bc" in "abc" - grapheme_strrpos = 1 == 1 find "abc" in "abc" - grapheme_strrpos = 0 == 0 find "abcd" in "abc" - grapheme_strrpos = false == false find "ab" in "abc" - grapheme_strrpos from 0 = 0 == 0 find "abc" in "abc" - grapheme_strrpos from 0 = 0 == 0 find "abc" in "abc" - grapheme_strrpos from 1 = false == false find "ab" in "ababc" - grapheme_strrpos from 1 = 2 == 2 find "abc" in "ababc" - grapheme_strrpos from 1 = 2 == 2 find "oa%CC%8Abc" in "aoa%CC%8Abco%CC%88oa%CC%8Abc" - grapheme_strrpos from 2 = 6 == 6 find "a%CC%8Abca%CC%8A" in "o%CC%88a%CC%8Aaa%CC%8Abca%CC%8Adef" - grapheme_strrpos from 2 = 3 == 3 function grapheme_strripos($haystack, $needle, $offset = 0) {} find "o" in "aoa%CC%8Abco%CC%88O" - grapheme_strripos from 2 = 6 == 6 find "a%CC%8A" in "o%CC%88a%CC%8AaA%CC%8Abc" - grapheme_strripos from 2 = 3 == 3 find "o" in "aa%CC%8Abco%CC%88O" - grapheme_strripos = 5 == 5 find "O" in "aa%CC%8Abco%CC%88" - grapheme_strripos = false == false find "o%CC%88" in "aa%CC%8AbcO%CC%88" - grapheme_strripos = 4 == 4 find "A%CC%8A" in "o%CC%88aa%CC%8Abc" - grapheme_strripos = 2 == 2 find "a%CC%8A" in "aA%CC%8Abc" - grapheme_strripos = 1 == 1 find "a%CC%8A" in "Abc" - grapheme_strripos = false == false find "A" in "a%CC%8Abc" - grapheme_strripos = false == false find "D" in "abc" - grapheme_strripos = false == false find "c" in "abC" - grapheme_strripos = 2 == 2 find "B" in "abc" - grapheme_strripos = 1 == 1 find "a" in "Abc" - grapheme_strripos = 0 == 0 find "A" in "abc" - grapheme_strripos from 0 = 0 == 0 find "a" in "Abc" - grapheme_strripos from 1 = false == false find "A" in "ababc" - grapheme_strripos from 1 = 2 == 2 find "oP" in "aa%CC%8Abco%CC%88Opq" - grapheme_strripos = 5 == 5 find "opQ" in "aa%CC%8Abco%CC%88Opq" - grapheme_strripos = 5 == 5 find "abc" in "aa%CC%8Abco%CC%88" - grapheme_strripos = false == false find "O%CC%88bco%CC%88" in "aa%CC%8Abco%CC%88bCo%CC%88" - grapheme_strripos = 4 == 4 find "A%CC%8Abc" in "o%CC%88aa%CC%8ABc" - grapheme_strripos = 2 == 2 find "a%CC%8Abc" in "aa%CC%8ABC" - grapheme_strripos = 1 == 1 find "a%CC%8ABC" in "abc" - grapheme_strripos = false == false find "aBCdefg" in "a%CC%8ABC" - grapheme_strripos = false == false find "Defghijklmnopq" in "aBC" - grapheme_strripos = false == false find "Ab" in "abC" - grapheme_strripos = 0 == 0 find "bc" in "aBC" - grapheme_strripos = 1 == 1 find "Abc" in "abC" - grapheme_strripos = 0 == 0 find "aBcd" in "abC" - grapheme_strripos = false == false find "ab" in "ABc" - grapheme_strripos from 0 = 0 == 0 find "abC" in "aBc" - grapheme_strripos from 0 = 0 == 0 find "aBc" in "abc" - grapheme_strripos from 1 = false == false find "AB" in "ABabc" - grapheme_strripos from 1 = 2 == 2 find "aBc" in "abaBc" - grapheme_strripos from 1 = 2 == 2 find "Oa%CC%8AbC" in "aoa%CC%8Abco%CC%88oA%CC%8AbC" - grapheme_strripos from 2 = 6 == 6 find "a%CC%8ABca%CC%8A" in "o%CC%88a%CC%8AaA%CC%8AbCa%CC%8Adef" - grapheme_strripos from 2 = 3 == 3 function grapheme_substr($string, $start, $length = -1) {} substring of "abc" from "3" - grapheme_substr = false == false substring of "aa%CC%8Abco%CC%88" from "5" - grapheme_substr = false == false substring of "aoa%CC%8Abco%CC%88O" from "2" - grapheme_substr = a%CC%8Abco%CC%88O == a%CC%8Abco%CC%88O substring of "o%CC%88a%CC%8AaA%CC%8Abc" from "2" - grapheme_substr = aA%CC%8Abc == aA%CC%8Abc substring of "aa%CC%8Abco%CC%88O" from "5" - grapheme_substr = O == O substring of "aa%CC%8Abco%CC%88" from "5" - grapheme_substr = false == false substring of "aa%CC%8AbcO%CC%88" from "4" - grapheme_substr = O%CC%88 == O%CC%88 substring of "o%CC%88aa%CC%8Abc" from "2" - grapheme_substr = a%CC%8Abc == a%CC%8Abc substring of "aA%CC%8Abc" from "1" - grapheme_substr = A%CC%8Abc == A%CC%8Abc substring of "Abc" from "-5" - grapheme_substr = false == false substring of "a%CC%8Abc" from "3" - grapheme_substr = false == false substring of "abc" from "4" - grapheme_substr = false == false substring of "abC" from "2" - grapheme_substr = C == C substring of "abc" from "1" - grapheme_substr = bc == bc substring of "Abc" from "1" - grapheme_substr with length 1 = b == b substring of "abc" from "0" - grapheme_substr with length 2 = ab == ab substring of "Abc" from "-4" - grapheme_substr with length 1 = false == false substring of "ababc" from "1" - grapheme_substr with length 2 = ba == ba substring of "aa%CC%8Abco%CC%88Opq" from "5" - grapheme_substr = Opq == Opq substring of "aa%CC%8Abco%CC%88Opq" from "5" - grapheme_substr with length -1 = Op == Op substring of "aa%CC%8Abco%CC%88Opq" from "5" - grapheme_substr with length -2 = O == O substring of "aa%CC%8Abco%CC%88Opq" from "5" - grapheme_substr with length -3 = == substring of "aa%CC%8Abco%CC%88Opq" from "5" - grapheme_substr with length -4 = false == false substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr = aa%CC%8Abco%CC%88Opq == aa%CC%8Abco%CC%88Opq substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -1 = aa%CC%8Abco%CC%88Op == aa%CC%8Abco%CC%88Op substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -2 = aa%CC%8Abco%CC%88O == aa%CC%8Abco%CC%88O substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -3 = aa%CC%8Abco%CC%88 == aa%CC%8Abco%CC%88 substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -4 = aa%CC%8Abc == aa%CC%8Abc substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -5 = aa%CC%8Ab == aa%CC%8Ab substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -6 = aa%CC%8A == aa%CC%8A substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -7 = a == a substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -8 = == substring of "aa%CC%8Abco%CC%88Opq" from "0" - grapheme_substr with length -9 = false == false substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr = aa%CC%8Abco%CC%88Opq == aa%CC%8Abco%CC%88Opq substring of "aa%CC%8Abco%CC%88Opq" from "-7" - grapheme_substr = a%CC%8Abco%CC%88Opq == a%CC%8Abco%CC%88Opq substring of "aa%CC%8Abco%CC%88Opq" from "-6" - grapheme_substr = bco%CC%88Opq == bco%CC%88Opq substring of "aa%CC%8Abco%CC%88Opq" from "-5" - grapheme_substr = co%CC%88Opq == co%CC%88Opq substring of "aa%CC%8Abco%CC%88Opq" from "-4" - grapheme_substr = o%CC%88Opq == o%CC%88Opq substring of "aa%CC%8Abco%CC%88Opq" from "-3" - grapheme_substr = Opq == Opq substring of "aa%CC%8Abco%CC%88Opq" from "-2" - grapheme_substr = pq == pq substring of "aa%CC%8Abco%CC%88Opq" from "-1" - grapheme_substr = q == q substring of "aa%CC%8Abco%CC%88Opq" from "-999" - grapheme_substr = false == false substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 8 = aa%CC%8Abco%CC%88Opq == aa%CC%8Abco%CC%88Opq substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 7 = aa%CC%8Abco%CC%88Op == aa%CC%8Abco%CC%88Op substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 6 = aa%CC%8Abco%CC%88O == aa%CC%8Abco%CC%88O substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 5 = aa%CC%8Abco%CC%88 == aa%CC%8Abco%CC%88 substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 4 = aa%CC%8Abc == aa%CC%8Abc substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 3 = aa%CC%8Ab == aa%CC%8Ab substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 2 = aa%CC%8A == aa%CC%8A substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 1 = a == a substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length 0 = aa%CC%8Abco%CC%88Opq == substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -999 = false == false substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -1 = aa%CC%8Abco%CC%88Op == aa%CC%8Abco%CC%88Op substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -2 = aa%CC%8Abco%CC%88O == aa%CC%8Abco%CC%88O substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -3 = aa%CC%8Abco%CC%88 == aa%CC%8Abco%CC%88 substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -4 = aa%CC%8Abc == aa%CC%8Abc substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -5 = aa%CC%8Ab == aa%CC%8Ab substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -6 = aa%CC%8A == aa%CC%8A substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -7 = a == a substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -8 = == substring of "aa%CC%8Abco%CC%88Opq" from "-8" - grapheme_substr with length -9 = false == false function grapheme_strstr($haystack, $needle, $before_needle = FALSE) {} find "o" in "aa%CC%8Abco%CC%88o" - grapheme_strstr = o == o find "o" in "aa%CC%8Abco%CC%88" - grapheme_strstr = false == false find "o%CC%88" in "aa%CC%8Abco%CC%88" - grapheme_strstr = o%CC%88 == o%CC%88 find "a%CC%8A" in "o%CC%88aa%CC%8Abc" - grapheme_strstr = a%CC%8Abc == a%CC%8Abc find "a%CC%8A" in "aa%CC%8Abc" - grapheme_strstr = a%CC%8Abc == a%CC%8Abc find "a%CC%8A" in "abc" - grapheme_strstr = false == false find "a" in "a%CC%8Abc" - grapheme_strstr = false == false find "d" in "abc" - grapheme_strstr = false == false find "c" in "abc" - grapheme_strstr = c == c find "b" in "abc" - grapheme_strstr = bc == bc find "a" in "abc" - grapheme_strstr = abc == abc find "ab" in "abc" - grapheme_strstr = abc == abc find "abc" in "abc" - grapheme_strstr = abc == abc find "bc" in "abc" - grapheme_strstr = bc == bc find "a" in "abc" - grapheme_strstr before flag is FALSE = abc == abc find "a" in "abc" - grapheme_strstr before flag is TRUE = == find "b" in "abc" - grapheme_strstr before flag is TRUE = a == a find "c" in "abc" - grapheme_strstr before flag is TRUE = ab == ab find "bab" in "ababc" - grapheme_strstr before flag is TRUE = a == a find "abc" in "ababc" - grapheme_strstr before flag is TRUE = ab == ab find "abc" in "ababc" - grapheme_strstr before flag is FALSE = abc == abc find "d" in "aba%CC%8Ac" - grapheme_strstr = false == false find "a" in "bca%CC%8Aa" - grapheme_strstr = a == a find "b" in "aa%CC%8Abc" - grapheme_strstr = bc == bc find "a" in "a%CC%8Abc" - grapheme_strstr = false == false find "ab" in "a%CC%8Aabc" - grapheme_strstr = abc == abc find "abc" in "abca%CC%8A" - grapheme_strstr = abca%CC%8A == abca%CC%8A find "a%CC%8Abc" in "aa%CC%8Abc" - grapheme_strstr = a%CC%8Abc == a%CC%8Abc find "a%CC%8A" in "aa%CC%8Abc" - grapheme_strstr before flag is FALSE = a%CC%8Abc == a%CC%8Abc find "a" in "aa%CC%8Abc" - grapheme_strstr before flag is TRUE = == find "b" in "a%CC%8Aabc" - grapheme_strstr before flag is TRUE = a%CC%8Aa == a%CC%8Aa find "c" in "aba%CC%8Ac" - grapheme_strstr before flag is TRUE = aba%CC%8A == aba%CC%8A find "baa%CC%8Ab" in "abaa%CC%8Abc" - grapheme_strstr before flag is TRUE = a == a find "abca%CC%8A" in "ababca%CC%8A" - grapheme_strstr before flag is TRUE = ab == ab find "aba%CC%8Ac" in "ababa%CC%8Ac" - grapheme_strstr before flag is FALSE = aba%CC%8Ac == aba%CC%8Ac function grapheme_stristr($haystack, $needle, $before_needle = FALSE) {} find "O%CC%88" in "aa%CC%8Abco%CC%88" - grapheme_stristr = o%CC%88 == o%CC%88 find "o" in "aa%CC%8Abco%CC%88O" - grapheme_stristr = O == O find "o" in "aa%CC%8Abco%CC%88" - grapheme_stristr = false == false find "a%CC%8A" in "o%CC%88aa%CC%8Abc" - grapheme_stristr = a%CC%8Abc == a%CC%8Abc find "A%CC%8A" in "aa%CC%8Abc" - grapheme_stristr = a%CC%8Abc == a%CC%8Abc find "a%CC%8A" in "abc" - grapheme_stristr = false == false find "A" in "a%CC%8Abc" - grapheme_stristr = false == false find "d" in "abc" - grapheme_stristr = false == false find "C" in "abc" - grapheme_stristr = c == c find "b" in "aBc" - grapheme_stristr = Bc == Bc find "A" in "abc" - grapheme_stristr = abc == abc find "ab" in "abC" - grapheme_stristr = abC == abC find "aBc" in "abc" - grapheme_stristr = abc == abc find "bc" in "abC" - grapheme_stristr = bC == bC find "A" in "abc" - grapheme_stristr before flag is FALSE = abc == abc find "a" in "abc" - grapheme_stristr before flag is TRUE = == find "b" in "aBc" - grapheme_stristr before flag is TRUE = a == a find "C" in "abc" - grapheme_stristr before flag is TRUE = ab == ab find "bab" in "aBabc" - grapheme_stristr before flag is TRUE = a == a find "aBc" in "ababc" - grapheme_stristr before flag is TRUE = ab == ab find "abC" in "ababc" - grapheme_stristr before flag is FALSE = abc == abc find "d" in "aba%CC%8Ac" - grapheme_stristr = false == false find "a" in "bca%CC%8AA" - grapheme_stristr = A == A find "B" in "aa%CC%8Abc" - grapheme_stristr = bc == bc find "a" in "A%CC%8Abc" - grapheme_stristr = false == false find "Ab" in "a%CC%8Aabc" - grapheme_stristr = abc == abc find "abc" in "abcA%CC%8A" - grapheme_stristr = abcA%CC%8A == abcA%CC%8A find "A%CC%8Abc" in "aa%CC%8Abc" - grapheme_stristr = a%CC%8Abc == a%CC%8Abc find "a%CC%8A" in "aA%CC%8Abc" - grapheme_stristr before flag is FALSE = A%CC%8Abc == A%CC%8Abc find "A" in "aa%CC%8Abc" - grapheme_stristr before flag is TRUE = == find "b" in "a%CC%8AaBc" - grapheme_stristr before flag is TRUE = a%CC%8Aa == a%CC%8Aa find "C" in "aba%CC%8Ac" - grapheme_stristr before flag is TRUE = aba%CC%8A == aba%CC%8A find "baa%CC%8Ab" in "abaA%CC%8Abc" - grapheme_stristr before flag is TRUE = a == a find "aBcA%CC%8A" in "ababca%CC%8A" - grapheme_stristr before flag is TRUE = ab == ab find "aba%CC%8Ac" in "abABA%CC%8Ac" - grapheme_stristr before flag is FALSE = ABA%CC%8Ac == ABA%CC%8Ac function grapheme_extract($haystack, $size, $start = 0) extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "1" graphemes - grapheme_extract starting at byte position 4 = %CC%88 == o%CC%88 extract from "abc" "3" graphemes - grapheme_extract = abc == abc extract from "abc" "2" graphemes - grapheme_extract = ab == ab extract from "abc" "1" graphemes - grapheme_extract = a == a extract from "abc" "0" graphemes - grapheme_extract = == extract from "a%CC%8Abc" "3" graphemes - grapheme_extract = a%CC%8Abc == a%CC%8Abc extract from "a%CC%8Abc" "2" graphemes - grapheme_extract = a%CC%8Ab == a%CC%8Ab extract from "a%CC%8Abc" "1" graphemes - grapheme_extract = a%CC%8A == a%CC%8A extract from "a%CC%8Ao%CC%88o%CC%88" "3" graphemes - grapheme_extract = a%CC%8Ao%CC%88o%CC%88 == a%CC%8Ao%CC%88o%CC%88 extract from "a%CC%8Ao%CC%88o%CC%88" "2" graphemes - grapheme_extract = a%CC%8Ao%CC%88 == a%CC%8Ao%CC%88 extract from "a%CC%8Ao%CC%88c" "1" graphemes - grapheme_extract = a%CC%8A == a%CC%8A extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "1" graphemes - grapheme_extract starting at byte position 0 = o%CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "1" graphemes - grapheme_extract starting at byte position 2 = o%CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "1" graphemes - grapheme_extract starting at byte position 3 = o%CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "1" graphemes - grapheme_extract starting at byte position 4 = %CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extract starting at byte position 0 = o%CC%88o%CC%88 == o%CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extract starting at byte position 2 = o%CC%88o%CC%88 == o%CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extract starting at byte position 3 = o%CC%88o%CC%88 == o%CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extract starting at byte position 4 = %CC%88o%CC%88 == %CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extract starting at byte position 7 = %CC%88o%CC%88 == %CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extract starting at byte position 8 = o%CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extract starting at byte position 10 = %CC%88 == %CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extract starting at byte position 11 = false == false function grapheme_extractB($haystack, $size, $start = 0) extract from "abc" "3" graphemes - grapheme_extractB = abc == abc extract from "abc" "2" graphemes - grapheme_extractB = ab == ab extract from "abc" "1" graphemes - grapheme_extractB = a == a extract from "abc" "0" graphemes - grapheme_extractB = == extract from "a%CC%8Abc" "5" graphemes - grapheme_extractB = a%CC%8Abc == a%CC%8Abc extract from "a%CC%8Abc" "4" graphemes - grapheme_extractB = a%CC%8Ab == a%CC%8Ab extract from "a%CC%8Abc" "1" graphemes - grapheme_extractB = == extract from "a%CC%8Ao%CC%88o%CC%88" "9" graphemes - grapheme_extractB = a%CC%8Ao%CC%88o%CC%88 == a%CC%8Ao%CC%88o%CC%88 extract from "a%CC%8Ao%CC%88o%CC%88" "10" graphemes - grapheme_extractB = a%CC%8Ao%CC%88o%CC%88 == a%CC%8Ao%CC%88o%CC%88 extract from "a%CC%8Ao%CC%88o%CC%88" "11" graphemes - grapheme_extractB = a%CC%8Ao%CC%88o%CC%88 == a%CC%8Ao%CC%88o%CC%88 extract from "a%CC%8Ao%CC%88o%CC%88" "6" graphemes - grapheme_extractB = a%CC%8Ao%CC%88 == a%CC%8Ao%CC%88 extract from "a%CC%8Ao%CC%88c" "3" graphemes - grapheme_extractB = a%CC%8A == a%CC%8A extract from "a%CC%8Ao%CC%88c" "4" graphemes - grapheme_extractB = a%CC%8A == a%CC%8A extract from "a%CC%8Ao%CC%88c" "5" graphemes - grapheme_extractB = a%CC%8A == a%CC%8A extract from "a%CC%8Ao%CC%88c" "6" graphemes - grapheme_extractB = a%CC%8Ao%CC%88 == a%CC%8Ao%CC%88 extract from "a%CC%8Ao%CC%88c" "7" graphemes - grapheme_extractB = a%CC%8Ao%CC%88c == a%CC%8Ao%CC%88c extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "3" graphemes - grapheme_extractB starting at byte position 0 = o%CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "3" graphemes - grapheme_extractB starting at byte position 2 = o%CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "3" graphemes - grapheme_extractB starting at byte position 3 = o%CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "3" graphemes - grapheme_extractB starting at byte position 4 = %CC%88 == %CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "6" graphemes - grapheme_extractB starting at byte position 0 = o%CC%88o%CC%88 == o%CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "6" graphemes - grapheme_extractB starting at byte position 2 = o%CC%88o%CC%88 == o%CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "6" graphemes - grapheme_extractB starting at byte position 3 = o%CC%88o%CC%88 == o%CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "5" graphemes - grapheme_extractB starting at byte position 4 = %CC%88o%CC%88 == %CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "5" graphemes - grapheme_extractB starting at byte position 7 = %CC%88o%CC%88 == %CC%88o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "3" graphemes - grapheme_extractB starting at byte position 8 = o%CC%88 == o%CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extractB starting at byte position 10 = %CC%88 == %CC%88 extract from "o%CC%88o%CC%88o%CC%88o%CC%88" "2" graphemes - grapheme_extractB starting at byte position 11 = false == false