xref: /PHP-7.3/ext/intl/grapheme/grapheme_util.c (revision 1c850bfc)
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 
ZEND_EXTERN_MODULE_GLOBALS(intl)35 ZEND_EXTERN_MODULE_GLOBALS( intl )
36 
37 /* }}} */
38 
39 /* {{{ grapheme_close_global_iterator - clean up */
40 void
41 grapheme_close_global_iterator( void )
42 {
43 	UBreakIterator *global_break_iterator = INTL_G( grapheme_iterator );
44 
45 	if ( NULL != global_break_iterator ) {
46 		ubrk_close(global_break_iterator);
47 	}
48 }
49 /* }}} */
50 
51 /* {{{ grapheme_substr_ascii f='from' - starting point, l='length' */
grapheme_substr_ascii(char * str,size_t str_len,int32_t f,int32_t l,char ** sub_str,int32_t * sub_str_len)52 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 	int32_t str_len2 = (int32_t)str_len; /* in order to avoid signed/unsigned problems */
55     *sub_str = NULL;
56 
57     if(str_len > INT32_MAX) {
58     	/* We can not return long strings from ICU functions, so we won't here too */
59     	return;
60     }
61 
62     if ((l < 0 && -l > str_len2)) {
63         return;
64     } else if (l > 0 && l > str_len2) {
65         l = str_len2;
66     }
67 
68     if (f > str_len2 || (f < 0 && -f > str_len2)) {
69         return;
70     }
71 
72     if (l < 0 && str_len2 < f - l) {
73         return;
74     }
75 
76     /* if "from" position is negative, count start position from the end
77      * of the string
78      */
79     if (f < 0) {
80         f = str_len2 + f;
81         if (f < 0) {
82             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     if (l < 0) {
91         l = (str_len2 - f) + l;
92         if (l < 0) {
93             l = 0;
94         }
95     }
96 
97     if (f >= str_len2) {
98         return;
99     }
100 
101     if ((f + l) > str_len2) {
102         l = str_len - f;
103     }
104 
105     *sub_str = str + f;
106     *sub_str_len = l;
107 
108     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*/
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)133 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 	UChar *uhaystack = NULL, *uneedle = NULL;
136 	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 	UBreakIterator* bi = NULL;
139 	UErrorCode status;
140 	UStringSearch* src = NULL;
141 
142 	if(puchar_pos) {
143 		*puchar_pos = -1;
144 	}
145 	/* convert the strings to UTF-16. */
146 
147 	status = U_ZERO_ERROR;
148 	intl_convert_utf8_to_utf16(&uhaystack, &uhaystack_len, haystack, haystack_len, &status );
149 	STRPOS_CHECK_STATUS(status, "Error converting input string to UTF-16");
150 
151 	status = U_ZERO_ERROR;
152 	intl_convert_utf8_to_utf16(&uneedle, &uneedle_len, needle, needle_len, &status );
153 	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 	status = U_ZERO_ERROR;
157 	bi = grapheme_get_break_iterator(u_break_iterator_buffer, &status );
158 	STRPOS_CHECK_STATUS(status, "Failed to get iterator");
159 	status = U_ZERO_ERROR;
160 	ubrk_setText(bi, uhaystack, uhaystack_len, &status);
161 	STRPOS_CHECK_STATUS(status, "Failed to set up iterator");
162 
163 	status = U_ZERO_ERROR;
164 	src = usearch_open(uneedle, uneedle_len, uhaystack, uhaystack_len, "", bi, &status);
165 	STRPOS_CHECK_STATUS(status, "Error creating search object");
166 
167 	if(f_ignore_case) {
168 		UCollator *coll = usearch_getCollator(src);
169 		status = U_ZERO_ERROR;
170 		ucol_setAttribute(coll, UCOL_STRENGTH, UCOL_SECONDARY, &status);
171 		STRPOS_CHECK_STATUS(status, "Error setting collation strength");
172 		usearch_reset(src);
173 	}
174 
175 	if(offset != 0) {
176 		offset_pos = grapheme_get_haystack_offset(bi, offset);
177 		if(offset_pos == -1) {
178 			status = U_ILLEGAL_ARGUMENT_ERROR;
179 			STRPOS_CHECK_STATUS(status, "Invalid search offset");
180 		}
181 		status = U_ZERO_ERROR;
182 		usearch_setOffset(src, offset_pos, &status);
183 		STRPOS_CHECK_STATUS(status, "Invalid search offset");
184 	}
185 
186 
187 	if(last) {
188 		char_pos = usearch_last(src, &status);
189 		if(char_pos < offset_pos) {
190 			/* last one is beyound our start offset */
191 			char_pos = USEARCH_DONE;
192 		}
193 	} else {
194 		char_pos = usearch_next(src, &status);
195 	}
196 	STRPOS_CHECK_STATUS(status, "Error looking up string");
197 	if(char_pos != USEARCH_DONE && ubrk_isBoundary(bi, char_pos)) {
198 		ret_pos = grapheme_count_graphemes(bi, uhaystack,char_pos);
199 		if(puchar_pos) {
200 			*puchar_pos = char_pos;
201 		}
202 	} else {
203 		ret_pos = -1;
204 	}
205 
206 	if (uhaystack) {
207 		efree( uhaystack );
208 	}
209 	if (uneedle) {
210 		efree( uneedle );
211 	}
212 	ubrk_close (bi);
213 	usearch_close (src);
214 
215 	return ret_pos;
216 }
217 
218 /* }}} */
219 
220 /* {{{ grapheme_ascii_check: ASCII check */
grapheme_ascii_check(const unsigned char * day,size_t len)221 zend_long grapheme_ascii_check(const unsigned char *day, size_t len)
222 {
223 	int ret_len = len;
224 	while ( len-- ) {
225 	if ( *day++ > 0x7f || (*day == '\n' && *(day - 1) == '\r') )
226 		return -1;
227 	}
228 
229 	return ret_len;
230 }
231 
232 /* }}} */
233 
234 /* {{{ grapheme_split_string: find and optionally return grapheme boundaries */
grapheme_split_string(const UChar * text,int32_t text_length,int boundary_array[],int boundary_array_len)235 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 	UErrorCode		status = U_ZERO_ERROR;
239 	int ret_len, pos;
240 	UBreakIterator* bi;
241 
242 	bi = grapheme_get_break_iterator((void*)u_break_iterator_buffer, &status );
243 
244 	if( U_FAILURE(status) ) {
245 		return -1;
246 	}
247 
248 	ubrk_setText(bi, text, text_length,	&status);
249 
250 	pos = 0;
251 
252 	for ( ret_len = 0; pos != UBRK_DONE; ) {
253 
254 		pos = ubrk_next(bi);
255 
256 		if ( pos != UBRK_DONE ) {
257 
258 			if ( NULL != boundary_array && ret_len < boundary_array_len ) {
259 				boundary_array[ret_len] = pos;
260 			}
261 
262 			ret_len++;
263 		}
264 	}
265 
266 	ubrk_close(bi);
267 
268 	return ret_len;
269 }
270 /* }}} */
271 
272 /* {{{ grapheme_count_graphemes */
grapheme_count_graphemes(UBreakIterator * bi,UChar * string,int32_t string_len)273 int32_t grapheme_count_graphemes(UBreakIterator *bi, UChar *string, int32_t string_len)
274 {
275 	int ret_len = 0;
276 	int pos = 0;
277 	UErrorCode		status = U_ZERO_ERROR;
278 
279 	ubrk_setText(bi, string, string_len, &status);
280 
281 	do {
282 
283 		pos = ubrk_next(bi);
284 
285 		if ( UBRK_DONE != pos ) {
286 			ret_len++;
287 		}
288 
289 	} while ( UBRK_DONE != pos );
290 
291 	return ret_len;
292 }
293 /* }}} */
294 
295 
296 /* {{{ 	grapheme_get_haystack_offset - bump the haystack pointer based on the grapheme count offset */
grapheme_get_haystack_offset(UBreakIterator * bi,int32_t offset)297 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 	if ( 0 == offset ) {
304 		return 0;
305 	}
306 
307 	if ( offset < 0 ) {
308 		iter_op = ubrk_previous;
309 		ubrk_last(bi); /* one past the end */
310 		iter_incr = 1;
311 	}
312 	else {
313 		iter_op = ubrk_next;
314 		iter_incr = -1;
315 	}
316 
317 	pos = 0;
318 
319 	while ( pos != UBRK_DONE && offset != 0 ) {
320 
321 		pos = iter_op(bi);
322 
323 		if ( UBRK_DONE != pos ) {
324 			offset += iter_incr;
325 		}
326 	}
327 
328 	if ( offset != 0 ) {
329 		return -1;
330 	}
331 
332 	return pos;
333 }
334 /* }}} */
335 
336 /* {{{ grapheme_strrpos_ascii: borrowed from the php ext/standard/string.c */
337  zend_long
grapheme_strrpos_ascii(char * haystack,size_t haystack_len,char * needle,size_t needle_len,int32_t offset)338 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 	if (offset >= 0) {
343 		p = haystack + offset;
344 		e = haystack + haystack_len - needle_len;
345 	} else {
346 		p = haystack;
347 		if (needle_len > (size_t)-offset) {
348 			e = haystack + haystack_len - needle_len;
349 		} else {
350 			e = haystack + haystack_len + offset;
351 		}
352 	}
353 
354 	if (needle_len == 1) {
355 		/* Single character search can shortcut memcmps */
356 		while (e >= p) {
357 			if (*e == *needle) {
358 				return (e - p + (offset > 0 ? offset : 0));
359 			}
360 			e--;
361 		}
362 		return -1;
363 	}
364 
365 	while (e >= p) {
366 		if (memcmp(e, needle, needle_len) == 0) {
367 			return (e - p + (offset > 0 ? offset : 0));
368 		}
369 		e--;
370 	}
371 
372 	return -1;
373 }
374 
375 /* }}} */
376 
377 /* {{{ grapheme_get_break_iterator: get a clone of the global character break iterator */
grapheme_get_break_iterator(void * stack_buffer,UErrorCode * status)378 UBreakIterator* grapheme_get_break_iterator(void *stack_buffer, UErrorCode *status )
379 {
380 	int32_t buffer_size;
381 
382 	UBreakIterator *global_break_iterator = INTL_G( grapheme_iterator );
383 
384 	if ( NULL == global_break_iterator ) {
385 
386 		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 		INTL_G(grapheme_iterator) = global_break_iterator;
393 	}
394 
395 	buffer_size = U_BRK_SAFECLONE_BUFFERSIZE;
396 
397 	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  */
409