PHP  
 PHP: Test and Code Coverage Analysis
downloads | QA | documentation | faq | getting help | mailing lists | reporting bugs | php.net sites | links | my php.net 
 

LCOV - code coverage report
Current view: top level - ext/intl/grapheme - grapheme_util.c (source / functions) Hit Total Coverage
Test: PHP Code Coverage Lines: 143 161 88.8 %
Date: 2022-01-18 Functions: 9 9 100.0 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :    +----------------------------------------------------------------------+
       3             :    | PHP Version 7                                                        |
       4             :    +----------------------------------------------------------------------+
       5             :    | This source file is subject to version 3.01 of the PHP license,      |
       6             :    | that is bundled with this package in the file LICENSE, and is        |
       7             :    | available through the world-wide-web at the following url:           |
       8             :    | http://www.php.net/license/3_01.txt                                  |
       9             :    | If you did not receive a copy of the PHP license and are unable to   |
      10             :    | obtain it through the world-wide-web, please send a note to          |
      11             :    | license@php.net so we can mail you a copy immediately.               |
      12             :    +----------------------------------------------------------------------+
      13             :    | Author: Ed Batutis <ed@batutis.com>                                  |
      14             :    +----------------------------------------------------------------------+
      15             :  */
      16             : 
      17             : /* {{{ includes */
      18             : #ifdef HAVE_CONFIG_H
      19             : #include "config.h"
      20             : #endif
      21             : 
      22             : #include <php.h>
      23             : #include "grapheme.h"
      24             : #include "grapheme_util.h"
      25             : #include "intl_common.h"
      26             : 
      27             : #include <unicode/utypes.h>
      28             : #include <unicode/ucol.h>
      29             : #include <unicode/ustring.h>
      30             : #include <unicode/ubrk.h>
      31             : #include <unicode/usearch.h>
      32             : 
      33             : #include "ext/standard/php_string.h"
      34             : 
      35             : ZEND_EXTERN_MODULE_GLOBALS( intl )
      36             : 
      37             : /* }}} */
      38             : 
      39             : /* {{{ grapheme_close_global_iterator - clean up */
      40             : void
      41           7 : grapheme_close_global_iterator( void )
      42             : {
      43           7 :         UBreakIterator *global_break_iterator = INTL_G( grapheme_iterator );
      44             : 
      45           7 :         if ( NULL != global_break_iterator ) {
      46           7 :                 ubrk_close(global_break_iterator);
      47             :         }
      48           7 : }
      49             : /* }}} */
      50             : 
      51             : /* {{{ grapheme_substr_ascii f='from' - starting point, l='length' */
      52           9 : void grapheme_substr_ascii(char *str, size_t str_len, int32_t f, int32_t l, char **sub_str, int32_t *sub_str_len)
      53             : {
      54           9 :         int32_t str_len2 = (int32_t)str_len; /* in order to avoid signed/unsigned problems */
      55           9 :     *sub_str = NULL;
      56             : 
      57           9 :     if(str_len > INT32_MAX) {
      58             :         /* We can not return long strings from ICU functions, so we won't here too */
      59           0 :         return;
      60             :     }
      61             : 
      62           9 :     if ((l < 0 && -l > str_len2)) {
      63           0 :         return;
      64           9 :     } else if (l > 0 && l > str_len2) {
      65           2 :         l = str_len2;
      66             :     }
      67             : 
      68           9 :     if (f > str_len2 || (f < 0 && -f > str_len2)) {
      69           0 :         return;
      70             :     }
      71             : 
      72           9 :     if (l < 0 && str_len2 < f - l) {
      73           1 :         return;
      74             :     }
      75             : 
      76             :     /* if "from" position is negative, count start position from the end
      77             :      * of the string
      78             :      */
      79           8 :     if (f < 0) {
      80           1 :         f = str_len2 + f;
      81           1 :         if (f < 0) {
      82           0 :             f = 0;
      83             :         }
      84             :     }
      85             : 
      86             : 
      87             :     /* if "length" position is negative, set it to the length
      88             :      * needed to stop that many chars from the end of the string
      89             :      */
      90           8 :     if (l < 0) {
      91           0 :         l = (str_len2 - f) + l;
      92           0 :         if (l < 0) {
      93           0 :             l = 0;
      94             :         }
      95             :     }
      96             : 
      97           8 :     if (f >= str_len2) {
      98           0 :         return;
      99             :     }
     100             : 
     101           8 :     if ((f + l) > str_len2) {
     102           3 :         l = str_len - f;
     103             :     }
     104             : 
     105           8 :     *sub_str = str + f;
     106           8 :     *sub_str_len = l;
     107             : 
     108           8 :     return;
     109             : }
     110             : /* }}} */
     111             : 
     112             : #define STRPOS_CHECK_STATUS(status, error)                                                      \
     113             :         if ( U_FAILURE( (status) ) ) {                                                                  \
     114             :                 intl_error_set_code( NULL, (status) );                  \
     115             :                 intl_error_set_custom_msg( NULL, (error), 0 );  \
     116             :                 if (uhaystack) {                                                                                        \
     117             :                         efree( uhaystack );                                                                     \
     118             :                 }                                                                                                                       \
     119             :                 if (uneedle) {                                                                                          \
     120             :                         efree( uneedle );                                                                               \
     121             :                 }                                                                                                                       \
     122             :                 if(bi) {                                                                                                        \
     123             :                         ubrk_close (bi);                                                                                \
     124             :                 }                                                                                                                       \
     125             :                 if(src) {                                                                                                       \
     126             :                         usearch_close(src);                                                                             \
     127             :                 }                                                                                                                       \
     128             :                 return -1;                                                                                                      \
     129             :         }
     130             : 
     131             : 
     132             : /* {{{ grapheme_strpos_utf16 - strrpos using utf16*/
     133         138 : int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle, size_t needle_len, int32_t offset, int32_t *puchar_pos, int f_ignore_case, int last)
     134             : {
     135         138 :         UChar *uhaystack = NULL, *uneedle = NULL;
     136         138 :         int32_t uhaystack_len = 0, uneedle_len = 0, char_pos, ret_pos, offset_pos = 0;
     137             :         unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE];
     138         138 :         UBreakIterator* bi = NULL;
     139             :         UErrorCode status;
     140         138 :         UStringSearch* src = NULL;
     141             : 
     142         138 :         if(puchar_pos) {
     143          55 :                 *puchar_pos = -1;
     144             :         }
     145             :         /* convert the strings to UTF-16. */
     146             : 
     147         138 :         status = U_ZERO_ERROR;
     148         138 :         intl_convert_utf8_to_utf16(&uhaystack, &uhaystack_len, haystack, haystack_len, &status );
     149         138 :         STRPOS_CHECK_STATUS(status, "Error converting input string to UTF-16");
     150             : 
     151         138 :         status = U_ZERO_ERROR;
     152         138 :         intl_convert_utf8_to_utf16(&uneedle, &uneedle_len, needle, needle_len, &status );
     153         138 :         STRPOS_CHECK_STATUS(status, "Error converting needle string to UTF-16");
     154             : 
     155             :         /* get a pointer to the haystack taking into account the offset */
     156         138 :         status = U_ZERO_ERROR;
     157         138 :         bi = grapheme_get_break_iterator(u_break_iterator_buffer, &status );
     158         138 :         STRPOS_CHECK_STATUS(status, "Failed to get iterator");
     159         138 :         status = U_ZERO_ERROR;
     160         138 :         ubrk_setText(bi, uhaystack, uhaystack_len, &status);
     161         138 :         STRPOS_CHECK_STATUS(status, "Failed to set up iterator");
     162             : 
     163         138 :         status = U_ZERO_ERROR;
     164         138 :         src = usearch_open(uneedle, uneedle_len, uhaystack, uhaystack_len, "", bi, &status);
     165         138 :         STRPOS_CHECK_STATUS(status, "Error creating search object");
     166             : 
     167         138 :         if(f_ignore_case) {
     168          78 :                 UCollator *coll = usearch_getCollator(src);
     169          78 :                 status = U_ZERO_ERROR;
     170          78 :                 ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_SECONDARY, &status);
     171          78 :                 STRPOS_CHECK_STATUS(status, "Error setting collation strength");
     172          78 :                 usearch_reset(src);
     173             :         }
     174             : 
     175         138 :         if(offset != 0) {
     176          24 :                 offset_pos = grapheme_get_haystack_offset(bi, offset);
     177          24 :                 if(offset_pos == -1) {
     178           0 :                         status = U_ILLEGAL_ARGUMENT_ERROR;
     179           0 :                         STRPOS_CHECK_STATUS(status, "Invalid search offset");
     180             :                 }
     181          24 :                 status = U_ZERO_ERROR;
     182          24 :                 usearch_setOffset(src, offset_pos, &status);
     183          24 :                 STRPOS_CHECK_STATUS(status, "Invalid search offset");
     184             :         }
     185             : 
     186             : 
     187         138 :         if(last) {
     188          39 :                 char_pos = usearch_last(src, &status);
     189          39 :                 if(char_pos < offset_pos) {
     190             :                         /* last one is beyound our start offset */
     191          12 :                         char_pos = USEARCH_DONE;
     192             :                 }
     193             :         } else {
     194          99 :                 char_pos = usearch_next(src, &status);
     195             :         }
     196         138 :         STRPOS_CHECK_STATUS(status, "Error looking up string");
     197         138 :         if(char_pos != USEARCH_DONE && ubrk_isBoundary(bi, char_pos)) {
     198         108 :                 ret_pos = grapheme_count_graphemes(bi, uhaystack,char_pos);
     199         216 :                 if(puchar_pos) {
     200          46 :                         *puchar_pos = char_pos;
     201             :                 }
     202             :         } else {
     203          30 :                 ret_pos = -1;
     204             :         }
     205             : 
     206         138 :         if (uhaystack) {
     207         138 :                 efree( uhaystack );
     208             :         }
     209         138 :         if (uneedle) {
     210         138 :                 efree( uneedle );
     211             :         }
     212         138 :         ubrk_close (bi);
     213         138 :         usearch_close (src);
     214             : 
     215         138 :         return ret_pos;
     216             : }
     217             : 
     218             : /* }}} */
     219             : 
     220             : /* {{{ grapheme_ascii_check: ASCII check */
     221         370 : zend_long grapheme_ascii_check(const unsigned char *day, size_t len)
     222             : {
     223         370 :         int ret_len = len;
     224       67131 :         while ( len-- ) {
     225       66622 :         if ( *day++ > 0x7f || (*day == '\n' && *(day - 1) == '\r') )
     226         231 :                 return -1;
     227             :         }
     228             : 
     229         139 :         return ret_len;
     230             : }
     231             : 
     232             : /* }}} */
     233             : 
     234             : /* {{{ grapheme_split_string: find and optionally return grapheme boundaries */
     235           5 : int32_t grapheme_split_string(const UChar *text, int32_t text_length, int boundary_array[], int boundary_array_len )
     236             : {
     237             :         unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE];
     238           5 :         UErrorCode              status = U_ZERO_ERROR;
     239             :         int ret_len, pos;
     240             :         UBreakIterator* bi;
     241             : 
     242           5 :         bi = grapheme_get_break_iterator((void*)u_break_iterator_buffer, &status );
     243             : 
     244           5 :         if( U_FAILURE(status) ) {
     245           0 :                 return -1;
     246             :         }
     247             : 
     248           5 :         ubrk_setText(bi, text, text_length,     &status);
     249             : 
     250           5 :         pos = 0;
     251             : 
     252          31 :         for ( ret_len = 0; pos != UBRK_DONE; ) {
     253             : 
     254          21 :                 pos = ubrk_next(bi);
     255             : 
     256          21 :                 if ( pos != UBRK_DONE ) {
     257             : 
     258          16 :                         if ( NULL != boundary_array && ret_len < boundary_array_len ) {
     259           0 :                                 boundary_array[ret_len] = pos;
     260             :                         }
     261             : 
     262          16 :                         ret_len++;
     263             :                 }
     264             :         }
     265             : 
     266           5 :         ubrk_close(bi);
     267             : 
     268           5 :         return ret_len;
     269             : }
     270             : /* }}} */
     271             : 
     272             : /* {{{ grapheme_count_graphemes */
     273         108 : int32_t grapheme_count_graphemes(UBreakIterator *bi, UChar *string, int32_t string_len)
     274             : {
     275         108 :         int ret_len = 0;
     276         108 :         int pos = 0;
     277         108 :         UErrorCode              status = U_ZERO_ERROR;
     278             : 
     279         108 :         ubrk_setText(bi, string, string_len, &status);
     280             : 
     281             :         do {
     282             : 
     283       65797 :                 pos = ubrk_next(bi);
     284             : 
     285       65797 :                 if ( UBRK_DONE != pos ) {
     286       65689 :                         ret_len++;
     287             :                 }
     288             : 
     289       65797 :         } while ( UBRK_DONE != pos );
     290             : 
     291         108 :         return ret_len;
     292             : }
     293             : /* }}} */
     294             : 
     295             : 
     296             : /* {{{  grapheme_get_haystack_offset - bump the haystack pointer based on the grapheme count offset */
     297          24 : int32_t grapheme_get_haystack_offset(UBreakIterator* bi, int32_t offset)
     298             : {
     299             :         int32_t pos;
     300             :         int32_t (*iter_op)(UBreakIterator* bi);
     301             :         int iter_incr;
     302             : 
     303          24 :         if ( 0 == offset ) {
     304           0 :                 return 0;
     305             :         }
     306             : 
     307          24 :         if ( offset < 0 ) {
     308           8 :                 iter_op = ubrk_previous;
     309           8 :                 ubrk_last(bi); /* one past the end */
     310           8 :                 iter_incr = 1;
     311             :         }
     312             :         else {
     313          16 :                 iter_op = ubrk_next;
     314          16 :                 iter_incr = -1;
     315             :         }
     316             : 
     317          24 :         pos = 0;
     318             : 
     319         307 :         while ( pos != UBRK_DONE && offset != 0 ) {
     320             : 
     321         259 :                 pos = iter_op(bi);
     322             : 
     323         259 :                 if ( UBRK_DONE != pos ) {
     324         259 :                         offset += iter_incr;
     325             :                 }
     326             :         }
     327             : 
     328          24 :         if ( offset != 0 ) {
     329           0 :                 return -1;
     330             :         }
     331             : 
     332          24 :         return pos;
     333             : }
     334             : /* }}} */
     335             : 
     336             : /* {{{ grapheme_strrpos_ascii: borrowed from the php ext/standard/string.c */
     337             :  zend_long
     338          38 : grapheme_strrpos_ascii(char *haystack, size_t haystack_len, char *needle, size_t needle_len, int32_t offset)
     339             : {
     340             :         char *p, *e;
     341             : 
     342          38 :         if (offset >= 0) {
     343          38 :                 p = haystack + offset;
     344          38 :                 e = haystack + haystack_len - needle_len;
     345             :         } else {
     346           0 :                 p = haystack;
     347           0 :                 if (needle_len > (size_t)-offset) {
     348           0 :                         e = haystack + haystack_len - needle_len;
     349             :                 } else {
     350           0 :                         e = haystack + haystack_len + offset;
     351             :                 }
     352             :         }
     353             : 
     354          38 :         if (needle_len == 1) {
     355             :                 /* Single character search can shortcut memcmps */
     356          52 :                 while (e >= p) {
     357          34 :                         if (*e == *needle) {
     358          10 :                                 return (e - p + (offset > 0 ? offset : 0));
     359             :                         }
     360          24 :                         e--;
     361             :                 }
     362           4 :                 return -1;
     363             :         }
     364             : 
     365          56 :         while (e >= p) {
     366          22 :                 if (memcmp(e, needle, needle_len) == 0) {
     367          14 :                         return (e - p + (offset > 0 ? offset : 0));
     368             :                 }
     369           8 :                 e--;
     370             :         }
     371             : 
     372          10 :         return -1;
     373             : }
     374             : 
     375             : /* }}} */
     376             : 
     377             : /* {{{ grapheme_get_break_iterator: get a clone of the global character break iterator */
     378         273 : UBreakIterator* grapheme_get_break_iterator(void *stack_buffer, UErrorCode *status )
     379             : {
     380             :         int32_t buffer_size;
     381             : 
     382         273 :         UBreakIterator *global_break_iterator = INTL_G( grapheme_iterator );
     383             : 
     384         273 :         if ( NULL == global_break_iterator ) {
     385             : 
     386           7 :                 global_break_iterator = ubrk_open(UBRK_CHARACTER,
     387             :                                                                                         NULL,   /* icu default locale - locale has no effect on this iterator */
     388             :                                                                                         NULL,   /* text not set in global iterator */
     389             :                                                                                         0,              /* text length = 0 */
     390             :                                                                                         status);
     391             : 
     392           7 :                 INTL_G(grapheme_iterator) = global_break_iterator;
     393             :         }
     394             : 
     395         273 :         buffer_size = U_BRK_SAFECLONE_BUFFERSIZE;
     396             : 
     397         273 :         return ubrk_safeClone(global_break_iterator, stack_buffer, &buffer_size, status);
     398             : }
     399             : /* }}} */
     400             : 
     401             : /*
     402             :  * Local variables:
     403             :  * tab-width: 4
     404             :  * c-basic-offset: 4
     405             :  * End:
     406             :  * vim600: fdm=marker
     407             :  * vim: noet sw=4 ts=4
     408             :  */

Generated by: LCOV version 1.10

Generated at Wed, 19 Jan 2022 00:14:12 +0000 (3 days ago)

Copyright © 2005-2022 The PHP Group
All rights reserved.