cvs: pecl /unicode config.m4 config.w32 /unicode/grapheme grapheme_string.c grapheme_util.c grapheme_util.h /unicode/tests grapheme.phpt

From: Date: Mon, 04 Feb 2008 15:37:32 +0000
Subject: cvs: pecl /unicode config.m4 config.w32 /unicode/grapheme grapheme_string.c grapheme_util.c grapheme_util.h /unicode/tests grapheme.phpt
Groups: php.pecl.cvs 
Request: Send a blank email to pecl-cvs+get-9666@lists.php.net to get a copy of this message
ebatutis Mon Feb 4 15:37:32 2008 UTC Added files: /pecl/unicode/grapheme grapheme_util.c Modified files: /pecl/unicode config.m4 config.w32 /pecl/unicode/grapheme grapheme_string.c grapheme_util.h /pecl/unicode/tests grapheme.phpt Log: Minor reorganization of sources and two small bug-fixes

http://cvs.php.net/viewvc.cgi/pecl/unicode/config.m4?r1=1.1&r2=1.2&diff_format=u Index: pecl/unicode/config.m4 diff -u pecl/unicode/config.m4:1.1 pecl/unicode/config.m4:1.2 --- pecl/unicode/config.m4:1.1 Mon Jan 14 21:50:07 2008 +++ pecl/unicode/config.m4 Mon Feb 4 15:37:31 2008 @@ -14,6 +14,7 @@ php_unicode.c \ unicode_convert.c \ grapheme/grapheme_string.c \ + grapheme/grapheme_util.c \ ,$ext_shared,,$ICU_INCS) PHP_ADD_BUILD_DIR($ext_builddir/grapheme) http://cvs.php.net/viewvc.cgi/pecl/unicode/config.w32?r1=1.1&r2=1.2&diff_format=u Index: pecl/unicode/config.w32 diff -u pecl/unicode/config.w32:1.1 pecl/unicode/config.w32:1.2 --- pecl/unicode/config.w32:1.1 Mon Jan 14 21:50:07 2008 +++ pecl/unicode/config.w32 Mon Feb 4 15:37:31 2008 @@ -1,4 +1,4 @@ -// $Id: config.w32,v 1.1 2008/01/14 21:50:07 ebatutis Exp $ +// $Id: config.w32,v 1.2 2008/02/04 15:37:31 ebatutis Exp $ // vim:ft=javascript ARG_ENABLE("unicode", "Enable unicode support", "no"); @@ -10,7 +10,7 @@ EXTENSION("unicode", "php_unicode.c unicode_convert.c", true, "/I \"" + configure_module_dirname + "\""); ADD_SOURCES(configure_module_dirname + "/grapheme", "\ - grapheme_string.c \ + grapheme_string.c grapheme_util.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 http://cvs.php.net/viewvc.cgi/pecl/unicode/grapheme/grapheme_string.c?r1=1.1&r2=1.2&diff_format=u Index: pecl/unicode/grapheme/grapheme_string.c diff -u pecl/unicode/grapheme/grapheme_string.c:1.1 pecl/unicode/grapheme/grapheme_string.c:1.2 --- pecl/unicode/grapheme/grapheme_string.c:1.1 Mon Jan 14 21:50:07 2008 +++ pecl/unicode/grapheme/grapheme_string.c Mon Feb 4 15:37:32 2008 @@ -30,8 +30,6 @@ #include "ext/standard/php_string.h" -/*#include "zend_API.h"*/ - /* }}} */ /* {{{ grapheme_register_constants @@ -43,312 +41,6 @@ } /* }}} */ -/* {{{ 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) @@ -368,14 +60,14 @@ RETURN_FALSE; } - ret_len = ascii_check(string, string_len); + ret_len = grapheme_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 ); + unicode_convert_utf8_to_utf16(&ustring, &ustring_len, (char*) string, string_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ @@ -387,7 +79,7 @@ RETURN_NULL(); } - ret_len = split_string(ustring, ustring_len, NULL, 0 TSRMLS_CC ); + ret_len = grapheme_split_string(ustring, ustring_len, NULL, 0 TSRMLS_CC ); efree( ustring ); @@ -399,215 +91,6 @@ } /* }}} */ -/* {{{ 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) @@ -654,13 +137,13 @@ } /* if it is there, and if the haystack is ascii, we are all done */ - if ( ascii_check(haystack, haystack_len) >= 0 ) { + if ( grapheme_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 ); + ret_pos = grapheme_strpos_utf16(haystack, haystack_len, needle, needle_len, offset, &uchar_pos, 0 /* fIgnoreCase */ TSRMLS_CC ); if ( ret_pos >= 0 ) { RETURN_LONG(ret_pos + offset); @@ -707,7 +190,7 @@ } - is_ascii = ( ascii_check(haystack, haystack_len) >= 0 ); + is_ascii = ( grapheme_ascii_check(haystack, haystack_len) >= 0 ); if ( is_ascii ) { needle_dup = (unsigned char *)estrndup((char *)needle, needle_len); @@ -725,13 +208,13 @@ } /* 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 ) { + if ( grapheme_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 ); + ret_pos = grapheme_strpos_utf16(haystack, haystack_len, needle, needle_len, offset, &uchar_pos, 1 /* fIgnoreCase */ TSRMLS_CC ); if ( ret_pos >= 0 ) { RETURN_LONG(ret_pos + offset); @@ -776,11 +259,11 @@ RETURN_FALSE; } - is_ascii = ascii_check(haystack, haystack_len) >= 0; + is_ascii = grapheme_ascii_check(haystack, haystack_len) >= 0; if ( is_ascii ) { - ret_pos = strrpos_ascii(haystack, haystack_len, needle, needle_len, offset); + ret_pos = grapheme_strrpos_ascii(haystack, haystack_len, needle, needle_len, offset); if ( ret_pos >= 0 ) { @@ -789,14 +272,14 @@ /* if the needle was ascii too, we are done */ - if ( ascii_check(needle, needle_len) >= 0 ) { + if ( grapheme_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); + ret_pos = grapheme_strrpos_utf16(haystack, haystack_len, needle, needle_len, offset, 0 /* f_ignore_case */ TSRMLS_CC); if ( ret_pos >= 0 ) { RETURN_LONG(ret_pos); @@ -842,7 +325,7 @@ RETURN_FALSE; } - is_ascii = ascii_check(haystack, haystack_len) >= 0; + is_ascii = grapheme_ascii_check(haystack, haystack_len) >= 0; if ( is_ascii ) { unsigned char *needle_dup, *haystack_dup; @@ -852,7 +335,7 @@ 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); + ret_pos = grapheme_strrpos_ascii(haystack_dup, haystack_len, needle_dup, needle_len, offset); efree(haystack_dup); efree(needle_dup); @@ -863,14 +346,14 @@ /* if the needle was ascii too, we are done */ - if ( ascii_check(needle, needle_len) >= 0 ) { + if ( grapheme_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); + ret_pos = grapheme_strrpos_utf16(haystack, haystack_len, needle, needle_len, offset, 1 /* f_ignore_case */ TSRMLS_CC); if ( ret_pos >= 0 ) { RETURN_LONG(ret_pos); @@ -882,65 +365,6 @@ } /* }}} */ -/* {{{ 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) @@ -972,8 +396,8 @@ /* 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 ( grapheme_ascii_check(str, str_len) >= 0 ) { + grapheme_substr_ascii((char *)str, str_len, start, length, ZEND_NUM_ARGS(), (char **) &sub_str, &sub_str_len); if ( NULL == sub_str ) { RETURN_FALSE; @@ -985,7 +409,7 @@ ustr = NULL; ustr_len = 0; status = U_ZERO_ERROR; - intl_convert_utf8_to_utf16(&ustr, &ustr_len, (char *)str, str_len, &status); + unicode_convert_utf8_to_utf16(&ustr, &ustr_len, (char *)str, str_len, &status); if ( U_FAILURE( status ) ) { /* Set global error code. */ @@ -997,7 +421,7 @@ RETURN_FALSE; } - bi = get_break_iterator((void*)u_break_iterator_buffer, &status TSRMLS_CC ); + bi = grapheme_get_break_iterator((void*)u_break_iterator_buffer, &status TSRMLS_CC ); if( U_FAILURE(status) ) { RETURN_FALSE; @@ -1043,7 +467,7 @@ 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); + unicode_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 ); @@ -1076,7 +500,7 @@ iter_val = -1; } - sub_str_end_pos = ustr_len; + sub_str_end_pos = 0; while ( length ) { sub_str_end_pos = iter_func(bi); @@ -1099,7 +523,7 @@ 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); + unicode_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 ); @@ -1160,7 +584,7 @@ } /* if it is there, and if the haystack is ascii, we are all done */ - if ( ascii_check(haystack, haystack_len) >= 0 ) { + if ( grapheme_ascii_check(haystack, haystack_len) >= 0 ) { size_t found_offset = found - haystack; if (part) { @@ -1173,7 +597,7 @@ } /* need to work in utf16 */ - ret_pos = strpos_utf16(haystack, haystack_len, needle, needle_len, 0, &uchar_pos, f_ignore_case TSRMLS_CC ); + ret_pos = grapheme_strpos_utf16(haystack, haystack_len, needle, needle_len, 0, &uchar_pos, f_ignore_case TSRMLS_CC ); if ( ret_pos < 0 ) { RETURN_FALSE; @@ -1232,6 +656,13 @@ RETURN_FALSE; } + if ( start > str_len ) { + + intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_extract: start not contained in string", 1 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 */ @@ -1255,7 +686,7 @@ ustr = NULL; ustr_len = 0; status = U_ZERO_ERROR; - intl_convert_utf8_to_utf16(&ustr, &ustr_len, (char *)pstr, str_len, &status ); + unicode_convert_utf8_to_utf16(&ustr, &ustr_len, (char *)pstr, str_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ @@ -1272,7 +703,7 @@ bi = NULL; status = U_ZERO_ERROR; - bi = get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC ); + bi = grapheme_get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC ); ubrk_setText(bi, ustr, ustr_len, &status); @@ -1306,7 +737,6 @@ /* }}} */ - /* {{{ 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) @@ -1329,6 +759,13 @@ RETURN_FALSE; } + if ( start > str_len ) { + + intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_extractB: start not contained in string", 1 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 */ @@ -1352,7 +789,7 @@ ustr = NULL; ustr_len = 0; status = U_ZERO_ERROR; - intl_convert_utf8_to_utf16(&ustr, &ustr_len, pstr, str_len, &status ); + unicode_convert_utf8_to_utf16(&ustr, &ustr_len, pstr, str_len, &status ); if ( U_FAILURE( status ) ) { /* Set global error code. */ @@ -1369,7 +806,7 @@ bi = NULL; status = U_ZERO_ERROR; - bi = get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC ); + bi = grapheme_get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC ); ubrk_setText(bi, ustr, ustr_len, &status); @@ -1417,3 +854,4 @@ * vim600: fdm=marker * vim: noet sw=4 ts=4 */ + http://cvs.php.net/viewvc.cgi/pecl/unicode/grapheme/grapheme_util.h?r1=1.1&r2=1.2&diff_format=u Index: pecl/unicode/grapheme/grapheme_util.h diff -u pecl/unicode/grapheme/grapheme_util.h:1.1 pecl/unicode/grapheme/grapheme_util.h:1.2 --- pecl/unicode/grapheme/grapheme_util.h:1.1 Mon Jan 14 21:50:07 2008 +++ pecl/unicode/grapheme/grapheme_util.h Mon Feb 4 15:37:32 2008 @@ -20,7 +20,41 @@ #include "php_unicode.h" #include "unicode_convert.h" -static UBreakIterator* get_break_iterator(void *stack_buffer, UErrorCode *status TSRMLS_DC ); +/* get_break_interator: get a break iterator from the global structure */ +UBreakIterator* grapheme_get_break_iterator(void *stack_buffer, UErrorCode *status TSRMLS_DC ); + +void +grapheme_substr_ascii(char *str, int str_len, int f, int l, int argc, char **sub_str, int *sub_str_len); + +int +grapheme_strrpos_utf16(unsigned char *haystack, int haystack_len, unsigned char*needle, int needle_len, int offset, int f_ignore_case TSRMLS_DC); + +int +grapheme_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); + +inline int grapheme_ascii_check(const unsigned char *day, size_t len); + +int grapheme_split_string(const UChar *text, int32_t text_length, int boundary_array[], int boundary_array_len TSRMLS_DC ); + +inline int32_t +grapheme_count_graphemes(UBreakIterator *bi, UChar *string, int32_t string_len); + +inline int32_t +grapheme_memnstr_grapheme(UBreakIterator *bi, UChar *haystack, UChar *needle, int32_t needle_len, UChar *end); + +inline void *grapheme_memrchr_grapheme(const void *s, int c, size_t n); + +UChar * +grapheme_get_haystack_offset(UBreakIterator* bi, UChar *uhaystack, int32_t uhaystack_len, int32_t offset); + +int32_t +grapheme_strrpos_ascii(unsigned char *haystack, int32_t haystack_len, unsigned char *needle, int32_t needle_len, int32_t offset); + +UBreakIterator* +grapheme_get_break_iterator(void *stack_buffer, UErrorCode *status TSRMLS_DC ); + +/* OUTSIDE_STRING: check if (possibly negative) offset is outside the string */ +#define OUTSIDE_STRING(offset, max_len) ( offset < 0 ? -offset > max_len : offset >= max_len ) /* 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 */ http://cvs.php.net/viewvc.cgi/pecl/unicode/tests/grapheme.phpt?r1=1.1&r2=1.2&diff_format=u Index: pecl/unicode/tests/grapheme.phpt diff -u pecl/unicode/tests/grapheme.phpt:1.1 pecl/unicode/tests/grapheme.phpt:1.2 --- pecl/unicode/tests/grapheme.phpt:1.1 Mon Jan 14 21:50:07 2008 +++ pecl/unicode/tests/grapheme.phpt Mon Feb 4 15:37:32 2008 @@ -112,7 +112,7 @@ else { $res_str .= $result; } - $res_str .= " == " . $test[count($test)-1] . "\n"; + $res_str .= " == " . $test[count($test)-1] . check_result($result, $test[count($test)-1]) . "\n"; } //===================================================================================== @@ -176,7 +176,7 @@ else { $res_str .= $result; } - $res_str .= " == " . $test[count($test)-1] . "\n"; + $res_str .= " == " . $test[count($test)-1] . check_result($result, $test[count($test)-1]) . "\n"; } @@ -242,7 +242,7 @@ else { $res_str .= $result; } - $res_str .= " == " . $test[count($test)-1] . "\n"; + $res_str .= " == " . $test[count($test)-1] . check_result($result, $test[count($test)-1]) . "\n"; } @@ -307,7 +307,7 @@ else { $res_str .= $result; } - $res_str .= " == " . $test[count($test)-1] . "\n"; + $res_str .= " == " . $test[count($test)-1] . check_result($result, $test[count($test)-1]) . "\n"; } @@ -315,6 +315,7 @@ $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" ), @@ -401,7 +402,7 @@ else { $res_str .= urlencode($result); } - $res_str .= " == " . urlencode($test[count($test)-1]) . "\n"; + $res_str .= " == " . urlencode($test[count($test)-1]) . check_result($result, $test[count($test)-1]) . "\n"; } @@ -466,7 +467,7 @@ else { $res_str .= urlencode($result); } - $res_str .= " == " . urlencode($test[count($test)-1]) . "\n"; + $res_str .= " == " . urlencode($test[count($test)-1]) . check_result($result, $test[count($test)-1]) . "\n"; } @@ -531,7 +532,7 @@ else { $res_str .= urlencode($result); } - $res_str .= " == " . urlencode($test[count($test)-1]) . "\n"; + $res_str .= " == " . urlencode($test[count($test)-1]) . check_result($result, $test[count($test)-1]) . "\n"; } @@ -539,7 +540,6 @@ $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" ), @@ -554,7 +554,7 @@ 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, 1, 4, $char_diaeresis), 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), @@ -584,7 +584,7 @@ else { $res_str .= urlencode($result); } - $res_str .= " == " . urlencode($test[count($test)-1]) . "\n"; + $res_str .= " == " . urlencode($test[count($test)-1]) . check_result($result, $test[count($test)-1]) . "\n"; } @@ -642,7 +642,7 @@ else { $res_str .= urlencode($result); } - $res_str .= " == " . urlencode($test[count($test)-1]) . "\n"; + $res_str .= " == " . urlencode($test[count($test)-1]) . check_result($result, $test[count($test)-1]) . "\n"; } //===================================================================================== @@ -652,6 +652,19 @@ echo ut_main(); +function check_result($result, $expected) { + + if ( $result === false ) { + $result = 'false'; + } + + if ( strcmp($result, $expected) != 0 ) { + return " **FAILED** "; + } + + return ""; +} + ?> --EXPECT-- @@ -872,7 +885,7 @@ 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 0 = == 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 @@ -962,7 +975,6 @@ 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 @@ -976,7 +988,7 @@ 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" "1" graphemes - grapheme_extract starting at byte position 4 = %CC%88 == %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 @@ -1016,3 +1028,4 @@ 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/grapheme/grapheme_util.c?view=markup&rev=1.1 Index: pecl/unicode/grapheme/grapheme_util.c +++ pecl/unicode/grapheme/grapheme_util.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_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); } } /* }}} */ /* {{{ grapheme_intl_case_fold: convert string to lowercase */ void grapheme_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; } /* }}} */ /* {{{ grapheme_substr_ascii */ void grapheme_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; } /* }}} */ /* {{{ grapheme_strrpos_utf16 - strrpos using utf16 */ int grapheme_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; unicode_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 ) { grapheme_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 = grapheme_get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC ); puhaystack = grapheme_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; unicode_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 ) { grapheme_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 = grapheme_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; } /* }}} */ /* {{{ grapheme_strpos_utf16 - strrpos using utf16*/ int grapheme_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; unicode_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 = grapheme_get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC ); puhaystack = grapheme_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 ) { grapheme_intl_case_fold(&uhaystack, &puhaystack, &uhaystack_len, &status ); } uneedle = NULL; uneedle_len = 0; status = U_ZERO_ERROR; unicode_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 ) { grapheme_intl_case_fold(&uneedle, &uneedle, &uneedle_len, &status ); } ret_pos = grapheme_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; } /* }}} */ /* {{{ grapheme_ascii_check: ASCII check */ inline int grapheme_ascii_check(const unsigned char *day, size_t len) { int ret_len = len; while ( len-- ) { if ( *day++ > 0x7f ) return -1; } return ret_len; } /* }}} */ /* {{{ grapheme_split_string: find and optionally return grapheme boundaries */ int grapheme_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 = grapheme_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; } /* }}} */ /* {{{ grapheme_count_graphemes */ inline int32_t grapheme_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; } /* }}} */ /* {{{ grapheme_memnstr_grapheme: find needle in haystack using grapheme boundaries */ inline int32_t grapheme_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 = grapheme_count_graphemes(bi, haystack, (p - haystack)); return grapheme_offset; } } } if (p == NULL) { return -1; } p++; } return -1; } /* }}} */ /* {{{ grapheme_memrstr_grapheme: reverse find needle in haystack using grapheme boundaries */ inline void *grapheme_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; } /* }}} */ /* {{{ grapheme_get_haystack_offset - bump the haystack pointer based on the grapheme count offset */ UChar * grapheme_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; } /* }}} */ /* {{{ grapheme_strrpos_ascii: borrowed from the php ext/standard/string.c */ int32_t grapheme_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; } /* }}} */ /* {{{ grapheme_get_break_iterator: get a clone of the global character break iterator */ UBreakIterator* grapheme_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); } /* }}} */ /* * Local variables: * tab-width: 4 * c-basic-offset: 4 * End: * vim600: fdm=marker * vim: noet sw=4 ts=4 */
« previous php.pecl.cvs (#9666) next »