Back to index

php5  5.3.10
grapheme_string.c
Go to the documentation of this file.
00001 /*
00002    +----------------------------------------------------------------------+
00003    | PHP Version 5                                                                                                |
00004    +----------------------------------------------------------------------+
00005    | This source file is subject to version 3.01 of the PHP license,    |
00006    | that is bundled with this package in the file LICENSE, and is             |
00007    | available through the world-wide-web at the following url:                       |
00008    | http://www.php.net/license/3_01.txt                                                     |
00009    | If you did not receive a copy of the PHP license and are unable to   |
00010    | obtain it through the world-wide-web, please send a note to               |
00011    | license@php.net so we can mail you a copy immediately.                           |
00012    +----------------------------------------------------------------------+
00013    | Author: Ed Batutis <ed@batutis.com>                                                     |
00014    +----------------------------------------------------------------------+
00015  */
00016 
00017 /* {{{ includes */
00018 #ifdef HAVE_CONFIG_H
00019 #include "config.h"
00020 #endif
00021 
00022 #include <php.h>
00023 #include "grapheme.h"
00024 #include "grapheme_util.h"
00025 
00026 #include <unicode/utypes.h>
00027 #include <unicode/ucol.h>
00028 #include <unicode/ustring.h>
00029 #include <unicode/ubrk.h>
00030 
00031 #include "ext/standard/php_string.h"
00032 
00033 /* }}} */
00034 
00035 #define GRAPHEME_EXTRACT_TYPE_COUNT              0
00036 #define GRAPHEME_EXTRACT_TYPE_MAXBYTES    1
00037 #define GRAPHEME_EXTRACT_TYPE_MAXCHARS    2
00038 #define GRAPHEME_EXTRACT_TYPE_MIN  GRAPHEME_EXTRACT_TYPE_COUNT
00039 #define GRAPHEME_EXTRACT_TYPE_MAX  GRAPHEME_EXTRACT_TYPE_MAXCHARS
00040 
00041 
00042 /* {{{ grapheme_register_constants
00043  * Register API constants
00044  */
00045 void grapheme_register_constants( INIT_FUNC_ARGS )
00046 {
00047        REGISTER_LONG_CONSTANT("GRAPHEME_EXTR_COUNT", GRAPHEME_EXTRACT_TYPE_COUNT, CONST_CS | CONST_PERSISTENT);
00048        REGISTER_LONG_CONSTANT("GRAPHEME_EXTR_MAXBYTES", GRAPHEME_EXTRACT_TYPE_MAXBYTES, CONST_CS | CONST_PERSISTENT);
00049        REGISTER_LONG_CONSTANT("GRAPHEME_EXTR_MAXCHARS", GRAPHEME_EXTRACT_TYPE_MAXCHARS, CONST_CS | CONST_PERSISTENT);
00050 }
00051 /* }}} */
00052 
00053 /* {{{ proto int grapheme_strlen(string str)
00054    Get number of graphemes in a string */
00055 PHP_FUNCTION(grapheme_strlen)
00056 {
00057        unsigned char* string;
00058        int string_len;
00059        UChar* ustring = NULL;
00060        int ustring_len = 0;
00061        int ret_len;
00062        UErrorCode status;
00063 
00064        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s", (char **)&string, &string_len) == FAILURE) {
00065 
00066               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00067                       "grapheme_strlen: unable to parse input param", 0 TSRMLS_CC );
00068 
00069               RETURN_FALSE;
00070        }
00071 
00072        ret_len = grapheme_ascii_check(string, string_len);
00073 
00074        if ( ret_len >= 0 )
00075               RETURN_LONG(ret_len);
00076 
00077        /* convert the string to UTF-16. */
00078        status = U_ZERO_ERROR;
00079        intl_convert_utf8_to_utf16(&ustring, &ustring_len, (char*) string, string_len, &status );
00080 
00081        if ( U_FAILURE( status ) ) {
00082               /* Set global error code. */
00083               intl_error_set_code( NULL, status TSRMLS_CC );
00084 
00085               /* Set error messages. */
00086               intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
00087               if (ustring) {
00088                      efree( ustring );
00089               }
00090               RETURN_NULL();
00091        }
00092 
00093        ret_len = grapheme_split_string(ustring, ustring_len, NULL, 0 TSRMLS_CC );
00094 
00095        if (ustring) {
00096               efree( ustring );
00097        }
00098 
00099        if (ret_len >= 0) {
00100               RETVAL_LONG(ret_len);
00101        } else {
00102               RETVAL_FALSE;
00103        }
00104 }
00105 /* }}} */
00106 
00107 /* {{{ proto int grapheme_strpos(string haystack, string needle [, int offset ])
00108    Find position of first occurrence of a string within another */
00109 PHP_FUNCTION(grapheme_strpos)
00110 {
00111        unsigned char *haystack, *needle;
00112        int haystack_len, needle_len;
00113        unsigned char *found;
00114        long loffset = 0;
00115        int32_t offset = 0;
00116        int ret_pos, uchar_pos;
00117 
00118        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", (char **)&haystack, &haystack_len, (char **)&needle, &needle_len, &loffset) == FAILURE) {
00119 
00120               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00121                       "grapheme_strpos: unable to parse input param", 0 TSRMLS_CC );
00122 
00123               RETURN_FALSE;
00124        }
00125 
00126        if ( OUTSIDE_STRING(loffset, haystack_len) ) {
00127 
00128               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 TSRMLS_CC );
00129 
00130               RETURN_FALSE;
00131        }
00132 
00133        /* we checked that it will fit: */
00134        offset = (int32_t) loffset;
00135 
00136        /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
00137 
00138        if (needle_len == 0) {
00139 
00140               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 TSRMLS_CC );
00141 
00142               RETURN_FALSE;
00143        }
00144 
00145 
00146        /* quick check to see if the string might be there
00147         * I realize that 'offset' is 'grapheme count offset' but will work in spite of that
00148        */
00149        found = (unsigned char *)php_memnstr((char *)haystack + offset, (char *)needle, needle_len, (char *)haystack + haystack_len);
00150 
00151        /* if it isn't there the we are done */
00152        if (!found) {
00153               RETURN_FALSE;
00154        }
00155 
00156        /* if it is there, and if the haystack is ascii, we are all done */
00157        if ( grapheme_ascii_check(haystack, haystack_len) >= 0 ) {
00158 
00159               RETURN_LONG(found - haystack);
00160        }
00161 
00162        /* do utf16 part of the strpos */
00163        ret_pos = grapheme_strpos_utf16(haystack, haystack_len, needle, needle_len, offset, &uchar_pos, 0 /* fIgnoreCase */ TSRMLS_CC );
00164 
00165        if ( ret_pos >= 0 ) {
00166               RETURN_LONG(ret_pos + offset);
00167        } else {
00168               RETURN_FALSE;
00169        }
00170 
00171 }
00172 /* }}} */
00173 
00174 /* {{{ proto int grapheme_stripos(string haystack, string needle [, int offset ])
00175    Find position of first occurrence of a string within another, ignoring case differences */
00176 PHP_FUNCTION(grapheme_stripos)
00177 {
00178        unsigned char *haystack, *needle, *haystack_dup, *needle_dup;
00179        int haystack_len, needle_len;
00180        unsigned char *found;
00181        long loffset = 0;
00182        int32_t offset = 0;
00183        int ret_pos, uchar_pos;
00184        int is_ascii;
00185 
00186        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", (char **)&haystack, &haystack_len, (char **)&needle, &needle_len, &loffset) == FAILURE) {
00187 
00188               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00189                       "grapheme_stripos: unable to parse input param", 0 TSRMLS_CC );
00190 
00191               RETURN_FALSE;
00192        }
00193 
00194        if ( OUTSIDE_STRING(loffset, haystack_len) ) {
00195 
00196               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_stripos: Offset not contained in string", 1 TSRMLS_CC );
00197 
00198               RETURN_FALSE;
00199        }
00200 
00201        /* we checked that it will fit: */
00202        offset = (int32_t) loffset;
00203 
00204        /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
00205 
00206        if (needle_len == 0) {
00207 
00208               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_stripos: Empty delimiter", 1 TSRMLS_CC );
00209 
00210               RETURN_FALSE;
00211        }
00212 
00213 
00214        is_ascii = ( grapheme_ascii_check(haystack, haystack_len) >= 0 );
00215 
00216        if ( is_ascii ) {
00217               needle_dup = (unsigned char *)estrndup((char *)needle, needle_len);
00218               php_strtolower((char *)needle_dup, needle_len);
00219               haystack_dup = (unsigned char *)estrndup((char *)haystack, haystack_len);
00220               php_strtolower((char *)haystack_dup, haystack_len);
00221 
00222               found = (unsigned char*) php_memnstr((char *)haystack_dup + offset, (char *)needle_dup, needle_len, (char *)haystack_dup + haystack_len);
00223 
00224               efree(haystack_dup);
00225               efree(needle_dup);
00226 
00227               if (found) {
00228                      RETURN_LONG(found - haystack_dup);
00229               }
00230 
00231               /* if needle was ascii too, we are all done, otherwise we need to try using Unicode to see what we get */
00232               if ( grapheme_ascii_check(needle, needle_len) >= 0 ) {
00233                      RETURN_FALSE;
00234               }
00235        }
00236 
00237        /* do utf16 part of the strpos */
00238        ret_pos = grapheme_strpos_utf16(haystack, haystack_len, needle, needle_len, offset, &uchar_pos, 1 /* fIgnoreCase */ TSRMLS_CC );
00239 
00240        if ( ret_pos >= 0 ) {
00241               RETURN_LONG(ret_pos + offset);
00242        } else {
00243               RETURN_FALSE;
00244        }
00245 
00246 }
00247 /* }}} */
00248 
00249 /* {{{ proto int grapheme_strrpos(string haystack, string needle [, int offset])
00250    Find position of last occurrence of a string within another */
00251 PHP_FUNCTION(grapheme_strrpos)
00252 {
00253        unsigned char *haystack, *needle;
00254        int haystack_len, needle_len;
00255        long loffset = 0;
00256        int32_t offset = 0;
00257        int32_t ret_pos;
00258        int is_ascii;
00259 
00260        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", (char **)&haystack, &haystack_len, (char **)&needle, &needle_len, &loffset) == FAILURE) {
00261 
00262               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00263                       "grapheme_strrpos: unable to parse input param", 0 TSRMLS_CC );
00264 
00265               RETURN_FALSE;
00266        }
00267 
00268        if ( OUTSIDE_STRING(loffset, haystack_len) ) {
00269 
00270               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 TSRMLS_CC );
00271 
00272               RETURN_FALSE;
00273        }
00274 
00275        /* we checked that it will fit: */
00276        offset = (int32_t) loffset;
00277 
00278        /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
00279 
00280        if (needle_len == 0) {
00281 
00282               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 TSRMLS_CC );
00283 
00284               RETURN_FALSE;
00285        }
00286 
00287        is_ascii = grapheme_ascii_check(haystack, haystack_len) >= 0;
00288 
00289        if ( is_ascii ) {
00290 
00291               ret_pos = grapheme_strrpos_ascii(haystack, haystack_len, needle, needle_len, offset);
00292 
00293 
00294               if ( ret_pos >= 0 ) {
00295                      RETURN_LONG(ret_pos);
00296               }
00297 
00298               /* if the needle was ascii too, we are done */
00299 
00300               if (  grapheme_ascii_check(needle, needle_len) >= 0 ) {
00301                      RETURN_FALSE;
00302               }
00303 
00304               /* else we need to continue via utf16 */
00305        }
00306 
00307        ret_pos = grapheme_strrpos_utf16(haystack, haystack_len, needle, needle_len, offset, 0 /* f_ignore_case */ TSRMLS_CC);
00308 
00309        if ( ret_pos >= 0 ) {
00310               RETURN_LONG(ret_pos);
00311        } else {
00312               RETURN_FALSE;
00313        }
00314 
00315 
00316 }
00317 /* }}} */
00318 
00319 /* {{{ proto int grapheme_strripos(string haystack, string needle [, int offset])
00320    Find position of last occurrence of a string within another, ignoring case */
00321 PHP_FUNCTION(grapheme_strripos)
00322 {
00323        unsigned char *haystack, *needle;
00324        int haystack_len, needle_len;
00325        long loffset = 0;
00326        int32_t offset = 0;
00327        int32_t ret_pos;
00328        int is_ascii;
00329 
00330        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", (char **)&haystack, &haystack_len, (char **)&needle, &needle_len, &loffset) == FAILURE) {
00331 
00332               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00333                       "grapheme_strrpos: unable to parse input param", 0 TSRMLS_CC );
00334 
00335               RETURN_FALSE;
00336        }
00337 
00338        if ( OUTSIDE_STRING(loffset, haystack_len) ) {
00339 
00340               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Offset not contained in string", 1 TSRMLS_CC );
00341 
00342               RETURN_FALSE;
00343        }
00344 
00345        /* we checked that it will fit: */
00346        offset = (int32_t) loffset;
00347 
00348        /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
00349 
00350        if (needle_len == 0) {
00351 
00352               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 TSRMLS_CC );
00353 
00354               RETURN_FALSE;
00355        }
00356 
00357        is_ascii = grapheme_ascii_check(haystack, haystack_len) >= 0;
00358 
00359        if ( is_ascii ) {
00360               unsigned char *needle_dup, *haystack_dup;
00361 
00362               needle_dup = (unsigned char *)estrndup((char *)needle, needle_len);
00363               php_strtolower((char *)needle_dup, needle_len);
00364               haystack_dup = (unsigned char *)estrndup((char *)haystack, haystack_len);
00365               php_strtolower((char *)haystack_dup, haystack_len);
00366 
00367               ret_pos = grapheme_strrpos_ascii(haystack_dup, haystack_len, needle_dup, needle_len, offset);
00368 
00369               efree(haystack_dup);
00370               efree(needle_dup);
00371 
00372               if ( ret_pos >= 0 ) {
00373                      RETURN_LONG(ret_pos);
00374               }
00375 
00376               /* if the needle was ascii too, we are done */
00377 
00378               if (  grapheme_ascii_check(needle, needle_len) >= 0 ) {
00379                      RETURN_FALSE;
00380               }
00381 
00382               /* else we need to continue via utf16 */
00383        }
00384 
00385        ret_pos = grapheme_strrpos_utf16(haystack, haystack_len, needle, needle_len, offset, 1 /* f_ignore_case */ TSRMLS_CC);
00386 
00387        if ( ret_pos >= 0 ) {
00388               RETURN_LONG(ret_pos);
00389        } else {
00390               RETURN_FALSE;
00391        }
00392 
00393 
00394 }
00395 /* }}} */
00396 
00397 /* {{{ proto string grapheme_substr(string str, int start [, int length])
00398    Returns part of a string */
00399 PHP_FUNCTION(grapheme_substr)
00400 {
00401        unsigned char *str, *sub_str;
00402        UChar *ustr;
00403        int str_len, sub_str_len, ustr_len;
00404        long lstart = 0, length = 0;
00405        int32_t start = 0;
00406        int iter_val;
00407        UErrorCode status;
00408        unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE];
00409        UBreakIterator* bi = NULL;
00410        int sub_str_start_pos, sub_str_end_pos;
00411        int32_t (*iter_func)(UBreakIterator *);
00412 
00413        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|l", (char **)&str, &str_len, &lstart, &length) == FAILURE) {
00414 
00415               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00416                       "grapheme_substr: unable to parse input param", 0 TSRMLS_CC );
00417 
00418               RETURN_FALSE;
00419        }
00420 
00421        if ( OUTSIDE_STRING(lstart, str_len) ) {
00422 
00423               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_substr: start not contained in string", 1 TSRMLS_CC );
00424 
00425               RETURN_FALSE;
00426        }
00427 
00428        /* we checked that it will fit: */
00429        start = (int32_t) lstart;
00430 
00431        /* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
00432 
00433        if ( grapheme_ascii_check(str, str_len) >= 0 ) {
00434               grapheme_substr_ascii((char *)str, str_len, start, length, ZEND_NUM_ARGS(), (char **) &sub_str, &sub_str_len);
00435 
00436               if ( NULL == sub_str ) {
00437                      RETURN_FALSE;
00438               }
00439 
00440               RETURN_STRINGL(((char *)sub_str), sub_str_len, 1);
00441        }
00442 
00443        ustr = NULL;
00444        ustr_len = 0;
00445        status = U_ZERO_ERROR;
00446        intl_convert_utf8_to_utf16(&ustr, &ustr_len, (char *)str, str_len, &status);
00447 
00448        if ( U_FAILURE( status ) ) {
00449               /* Set global error code. */
00450               intl_error_set_code( NULL, status TSRMLS_CC );
00451 
00452               /* Set error messages. */
00453               intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
00454               if (ustr) {
00455                      efree( ustr );
00456               }
00457               RETURN_FALSE;
00458        }
00459 
00460        bi = grapheme_get_break_iterator((void*)u_break_iterator_buffer, &status TSRMLS_CC );
00461 
00462        if( U_FAILURE(status) ) {
00463               RETURN_FALSE;
00464        }
00465 
00466        ubrk_setText(bi, ustr, ustr_len,   &status);
00467 
00468        if ( start < 0 ) {
00469               iter_func = ubrk_previous;
00470               ubrk_last(bi);
00471               iter_val = 1;
00472        }
00473        else {
00474               iter_func = ubrk_next;
00475               iter_val = -1;
00476        }
00477 
00478        sub_str_start_pos = 0;
00479 
00480        while ( start ) {
00481               sub_str_start_pos = iter_func(bi);
00482 
00483               if ( UBRK_DONE == sub_str_start_pos ) {
00484                      break;
00485               }
00486 
00487               start += iter_val;
00488        }
00489 
00490        if ( 0 != start || sub_str_start_pos >= ustr_len ) {
00491 
00492               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_substr: start not contained in string", 1 TSRMLS_CC );
00493 
00494               if (ustr) {
00495                      efree(ustr);
00496               }
00497               ubrk_close(bi);
00498               RETURN_FALSE;
00499        }
00500 
00501        if (ZEND_NUM_ARGS() <= 2) {
00502 
00503               /* no length supplied, return the rest of the string */
00504 
00505               sub_str = NULL;
00506               sub_str_len = 0;
00507               status = U_ZERO_ERROR;
00508               intl_convert_utf16_to_utf8((char **)&sub_str, &sub_str_len, ustr + sub_str_start_pos, ustr_len - sub_str_start_pos, &status);
00509 
00510               if (ustr) {
00511                      efree( ustr );
00512               }
00513               ubrk_close( bi );
00514 
00515               if ( U_FAILURE( status ) ) {
00516                      /* Set global error code. */
00517                      intl_error_set_code( NULL, status TSRMLS_CC );
00518 
00519                      /* Set error messages. */
00520                      intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 0 TSRMLS_CC );
00521 
00522                      if (sub_str) {
00523                             efree( sub_str );
00524                      }
00525 
00526                      RETURN_FALSE;
00527               }
00528 
00529               /* return the allocated string, not a duplicate */
00530               RETURN_STRINGL(((char *)sub_str), sub_str_len, 0);
00531        }
00532 
00533        /* find the end point of the string to return */
00534 
00535        if ( length < 0 ) {
00536               iter_func = ubrk_previous;
00537               ubrk_last(bi);
00538               iter_val = 1;
00539        }
00540        else {
00541               iter_func = ubrk_next;
00542               iter_val = -1;
00543        }
00544 
00545        sub_str_end_pos = 0;
00546 
00547        while ( length ) {
00548               sub_str_end_pos = iter_func(bi);
00549 
00550               if ( UBRK_DONE == sub_str_end_pos ) {
00551                      break;
00552               }
00553 
00554               length += iter_val;
00555        }
00556 
00557        if ( UBRK_DONE == sub_str_end_pos && length < 0) {
00558 
00559               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_substr: length not contained in string", 1 TSRMLS_CC );
00560 
00561               efree(ustr);
00562               ubrk_close(bi);
00563               RETURN_FALSE;
00564        }
00565 
00566        sub_str = NULL;
00567        status = U_ZERO_ERROR;
00568        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);
00569 
00570        efree( ustr );
00571        ubrk_close( bi );
00572 
00573        if ( U_FAILURE( status ) ) {
00574               /* Set global error code. */
00575               intl_error_set_code( NULL, status TSRMLS_CC );
00576 
00577               /* Set error messages. */
00578               intl_error_set_custom_msg( NULL, "Error converting output string to UTF-8", 0 TSRMLS_CC );
00579 
00580               if ( NULL != sub_str )
00581                      efree( sub_str );
00582 
00583               RETURN_FALSE;
00584        }
00585 
00586         /* return the allocated string, not a duplicate */
00587        RETURN_STRINGL(((char *)sub_str), sub_str_len, 0);
00588 
00589 }
00590 /* }}} */
00591 
00592 /* {{{ strstr_common_handler */
00593 static void strstr_common_handler(INTERNAL_FUNCTION_PARAMETERS, int f_ignore_case)
00594 {
00595        unsigned char *haystack, *needle, *found;
00596        int haystack_len, needle_len;
00597        int ret_pos, uchar_pos;
00598        zend_bool part = 0;
00599 
00600        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|b", (char **)&haystack, &haystack_len, (char **)&needle, &needle_len, &part) == FAILURE) {
00601 
00602               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00603                       "grapheme_strstr: unable to parse input param", 0 TSRMLS_CC );
00604 
00605               RETURN_FALSE;
00606        }
00607 
00608        if (needle_len == 0) {
00609 
00610               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_strpos: Empty delimiter", 1 TSRMLS_CC );
00611 
00612               RETURN_FALSE;
00613        }
00614 
00615 
00616        if ( !f_ignore_case ) {
00617 
00618               /* ASCII optimization: quick check to see if the string might be there
00619                * I realize that 'offset' is 'grapheme count offset' but will work in spite of that
00620               */
00621               found = (unsigned char *)php_memnstr((char *)haystack, (char *)needle, needle_len, (char *)haystack + haystack_len);
00622 
00623               /* if it isn't there the we are done */
00624               if ( !found ) {
00625                      RETURN_FALSE;
00626               }
00627 
00628               /* if it is there, and if the haystack is ascii, we are all done */
00629               if ( grapheme_ascii_check(haystack, haystack_len) >= 0 ) {
00630                      size_t found_offset = found - haystack;
00631 
00632                      if (part) {
00633                             RETURN_STRINGL(((char *)haystack) , found_offset, 1);
00634                      } else {
00635                             RETURN_STRINGL(((char *)found), haystack_len - found_offset, 1);
00636                      }
00637               }
00638 
00639        }
00640 
00641        /* need to work in utf16 */
00642        ret_pos = grapheme_strpos_utf16(haystack, haystack_len, needle, needle_len, 0, &uchar_pos, f_ignore_case TSRMLS_CC );
00643 
00644        if ( ret_pos < 0 ) {
00645               RETURN_FALSE;
00646        }
00647 
00648        /* uchar_pos is the 'nth' Unicode character position of the needle */
00649 
00650        ret_pos = 0;
00651        U8_FWD_N(haystack, ret_pos, haystack_len, uchar_pos);
00652 
00653        if (part) {
00654               RETURN_STRINGL(((char *)haystack), ret_pos, 1);
00655        }
00656        else {
00657               RETURN_STRINGL(((char *)haystack) + ret_pos, haystack_len - ret_pos, 1);
00658        }
00659 
00660 }
00661 /* }}} */
00662 
00663 /* {{{ proto string grapheme_strstr(string haystack, string needle[, bool part])
00664    Finds first occurrence of a string within another */
00665 PHP_FUNCTION(grapheme_strstr)
00666 {
00667        strstr_common_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0 /* f_ignore_case */);
00668 }
00669 /* }}} */
00670 
00671 /* {{{ proto string grapheme_stristr(string haystack, string needle[, bool part])
00672    Finds first occurrence of a string within another */
00673 PHP_FUNCTION(grapheme_stristr)
00674 {
00675        strstr_common_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1 /* f_ignore_case */);
00676 }
00677 /* }}} */
00678 
00679 /* {{{ grapheme_extract_charcount_iter - grapheme iterator for grapheme_extract MAXCHARS */
00680 static inline int32_t
00681 grapheme_extract_charcount_iter(UBreakIterator *bi, int32_t csize, unsigned char *pstr, int32_t str_len)
00682 {
00683        int pos = 0, prev_pos = 0;
00684        int ret_pos = 0, prev_ret_pos = 0;
00685 
00686        while ( 1 ) {
00687               pos = ubrk_next(bi);
00688 
00689               if ( UBRK_DONE == pos ) {
00690                      break;
00691               }
00692 
00693               /* if we are beyond our limit, then the loop is done */
00694               if ( pos > csize ) {
00695                      break;
00696               }
00697 
00698               /* update our pointer in the original UTF-8 buffer by as many characters
00699                  as ubrk_next iterated over */
00700 
00701               prev_ret_pos = ret_pos;
00702               U8_FWD_N(pstr, ret_pos, str_len, pos - prev_pos);
00703 
00704               if ( prev_ret_pos == ret_pos ) {
00705                      /* something wrong - malformed utf8? */
00706                      break;
00707               }
00708 
00709               prev_pos = pos;
00710        }
00711 
00712        return ret_pos;
00713 }
00714 /* }}} */
00715 
00716 /* {{{ grapheme_extract_bytecount_iter - grapheme iterator for grapheme_extract MAXBYTES */
00717 static inline int32_t
00718 grapheme_extract_bytecount_iter(UBreakIterator *bi, int32_t bsize, unsigned char *pstr, int32_t str_len)
00719 {
00720        int pos = 0, prev_pos = 0;
00721        int ret_pos = 0, prev_ret_pos = 0;
00722 
00723        while ( 1 ) {
00724               pos = ubrk_next(bi);
00725 
00726               if ( UBRK_DONE == pos ) {
00727                      break;
00728               }
00729 
00730               prev_ret_pos = ret_pos;
00731               U8_FWD_N(pstr, ret_pos, str_len, pos - prev_pos);
00732 
00733               if ( ret_pos > bsize ) {
00734                      ret_pos = prev_ret_pos;
00735                      break;
00736               }
00737 
00738               if ( prev_ret_pos == ret_pos ) {
00739                      /* something wrong - malformed utf8? */
00740                      break;
00741               }
00742 
00743               prev_pos = pos;
00744        }
00745 
00746        return ret_pos;
00747 }
00748 /* }}} */
00749 
00750 /* {{{ grapheme_extract_count_iter - grapheme iterator for grapheme_extract COUNT */
00751 static inline int32_t
00752 grapheme_extract_count_iter(UBreakIterator *bi, int32_t size, unsigned char *pstr, int32_t str_len)
00753 {
00754        int pos = 0, next_pos = 0;
00755        int ret_pos = 0;
00756 
00757        while ( size ) {
00758               next_pos = ubrk_next(bi);
00759 
00760               if ( UBRK_DONE == next_pos ) {
00761                      break;
00762               }
00763               pos = next_pos;
00764               size--;
00765        }
00766 
00767        /* pos is one past the last UChar - and represent the number of code units to
00768               advance in the utf-8 buffer
00769        */
00770 
00771        U8_FWD_N(pstr, ret_pos, str_len, pos);
00772 
00773        return ret_pos;
00774 }
00775 /* }}} */
00776 
00777 /* {{{ grapheme extract iter function pointer array */
00778 typedef int32_t (*grapheme_extract_iter)(UBreakIterator * /*bi*/, int32_t /*size*/, unsigned char * /*pstr*/, int32_t /*str_len*/);
00779 
00780 static grapheme_extract_iter grapheme_extract_iters[] = {
00781        &grapheme_extract_count_iter,
00782        &grapheme_extract_bytecount_iter,
00783        &grapheme_extract_charcount_iter,
00784 };
00785 /* }}} */
00786 
00787 /* {{{ proto string grapheme_extract(string str, int size[, int extract_type[, int start[, int next]]])
00788        Function to extract a sequence of default grapheme clusters */
00789 PHP_FUNCTION(grapheme_extract)
00790 {
00791        unsigned char *str, *pstr;
00792        UChar *ustr;
00793        int str_len, ustr_len;
00794        long size; /* maximum number of grapheme clusters, bytes, or characters (based on extract_type) to return */
00795        long lstart = 0; /* starting position in str in bytes */
00796        int32_t start = 0;
00797        long extract_type = GRAPHEME_EXTRACT_TYPE_COUNT;
00798        UErrorCode status;
00799        unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE];
00800        UBreakIterator* bi = NULL;
00801        int ret_pos;
00802        zval *next = NULL; /* return offset of next part of the string */
00803 
00804        if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "sl|llz", (char **)&str, &str_len, &size, &extract_type, &lstart, &next) == FAILURE) {
00805 
00806               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00807                       "grapheme_extract: unable to parse input param", 0 TSRMLS_CC );
00808 
00809               RETURN_FALSE;
00810        }
00811 
00812        if ( NULL != next ) {
00813               if ( !PZVAL_IS_REF(next) ) {
00814                      intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00815                              "grapheme_extract: 'next' was not passed by reference", 0 TSRMLS_CC );
00816 
00817                      RETURN_FALSE;
00818               }
00819               else {
00820                      /* initialize next */
00821             ZVAL_LONG(next, lstart);
00822               }
00823        }
00824 
00825        if ( extract_type < GRAPHEME_EXTRACT_TYPE_MIN || extract_type > GRAPHEME_EXTRACT_TYPE_MAX ) {
00826 
00827               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00828                       "grapheme_extract: unknown extract type param", 0 TSRMLS_CC );
00829 
00830               RETURN_FALSE;
00831        }
00832 
00833        if ( lstart > INT32_MAX || lstart < 0 || lstart >= str_len ) {
00834               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_extract: start not contained in string", 0 TSRMLS_CC );
00835               RETURN_FALSE;
00836        }
00837 
00838        if ( size > INT32_MAX || size < 0) {
00839               intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR, "grapheme_extract: size is invalid", 0 TSRMLS_CC );
00840               RETURN_FALSE;
00841        }
00842        if (size == 0) {
00843               RETURN_EMPTY_STRING();
00844        }
00845 
00846        /* we checked that it will fit: */
00847        start = (int32_t) lstart;
00848 
00849        pstr = str + start;
00850 
00851        /* just in case pstr points in the middle of a character, move forward to the start of the next char */
00852        if ( !UTF8_IS_SINGLE(*pstr) && !U8_IS_LEAD(*pstr) ) {
00853               unsigned char *str_end = str + str_len;
00854 
00855               while ( !UTF8_IS_SINGLE(*pstr) && !U8_IS_LEAD(*pstr) ) {
00856                      pstr++;
00857                      if ( pstr >= str_end ) {
00858                             intl_error_set( NULL, U_ILLEGAL_ARGUMENT_ERROR,
00859                                                         "grapheme_extract: invalid input string", 0 TSRMLS_CC );
00860 
00861                             RETURN_FALSE;
00862                      }
00863               }
00864        }
00865 
00866        str_len -= (pstr - str);
00867 
00868        /* if the string is all ASCII up to size+1 - or str_len whichever is first - then we are done.
00869               (size + 1 because the size-th character might be the beginning of a grapheme cluster)
00870         */
00871 
00872        if ( -1 != grapheme_ascii_check(pstr, size + 1 < str_len ? size + 1 : str_len ) ) {
00873         long nsize = ( size < str_len ? size : str_len );
00874               if ( NULL != next ) {
00875                      ZVAL_LONG(next, start+nsize);
00876               }
00877               RETURN_STRINGL(((char *)pstr), nsize, 1);
00878        }
00879 
00880        /* convert the strings to UTF-16. */
00881        ustr = NULL;
00882        ustr_len = 0;
00883        status = U_ZERO_ERROR;
00884        intl_convert_utf8_to_utf16(&ustr, &ustr_len, (char *)pstr, str_len, &status );
00885 
00886        if ( U_FAILURE( status ) ) {
00887               /* Set global error code. */
00888               intl_error_set_code( NULL, status TSRMLS_CC );
00889 
00890               /* Set error messages. */
00891               intl_error_set_custom_msg( NULL, "Error converting input string to UTF-16", 0 TSRMLS_CC );
00892 
00893               if ( NULL != ustr )
00894                      efree( ustr );
00895 
00896               RETURN_FALSE;
00897        }
00898 
00899        bi = NULL;
00900        status = U_ZERO_ERROR;
00901        bi = grapheme_get_break_iterator(u_break_iterator_buffer, &status TSRMLS_CC );
00902 
00903        ubrk_setText(bi, ustr, ustr_len, &status);
00904 
00905        /* if the caller put us in the middle of a grapheme, we can't detect it in all cases since we
00906               can't back up. So, we will not do anything. */
00907 
00908        /* now we need to find the end of the chunk the user wants us to return */
00909 
00910        ret_pos = (*grapheme_extract_iters[extract_type])(bi, size, pstr, str_len);
00911 
00912        if (ustr) {
00913               efree(ustr);
00914        }
00915        ubrk_close(bi);
00916 
00917        if ( NULL != next ) {
00918               ZVAL_LONG(next, start+ret_pos);
00919        }
00920 
00921        RETURN_STRINGL(((char *)pstr), ret_pos, 1);
00922 }
00923 
00924 /* }}} */
00925 
00926 /*
00927  * Local variables:
00928  * tab-width: 4
00929  * c-basic-offset: 4
00930  * End:
00931  * vim600: fdm=marker
00932  * vim: noet sw=4 ts=4
00933  */
00934