xref: /PHP-7.4/ext/intl/grapheme/grapheme_util.c (revision 28c93763)
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, last ? 0 : offset_pos, &status);
183 		STRPOS_CHECK_STATUS(status, "Invalid search offset");
184 	}
185 
186 
187 	if(last) {
188 		if (offset >= 0) {
189 			char_pos = usearch_last(src, &status);
190 			if(char_pos < offset_pos) {
191 				/* last one is beyond our start offset */
192 				char_pos = USEARCH_DONE;
193 			}
194 		} else {
195 			/* searching backwards is broken, so we search forwards, albeit it's less efficient */
196 			int32_t prev_pos = USEARCH_DONE;
197 			do {
198 				char_pos = usearch_next(src, &status);
199 				if (char_pos == USEARCH_DONE || char_pos > offset_pos) {
200 					char_pos = prev_pos;
201 					break;
202 				}
203 				prev_pos = char_pos;
204 			} while(1);
205 		}
206 	} else {
207 		char_pos = usearch_next(src, &status);
208 	}
209 	STRPOS_CHECK_STATUS(status, "Error looking up string");
210 	if(char_pos != USEARCH_DONE && ubrk_isBoundary(bi, char_pos)) {
211 		ret_pos = grapheme_count_graphemes(bi, uhaystack,char_pos);
212 		if(puchar_pos) {
213 			*puchar_pos = char_pos;
214 		}
215 	} else {
216 		ret_pos = -1;
217 	}
218 
219 	if (uhaystack) {
220 		efree( uhaystack );
221 	}
222 	if (uneedle) {
223 		efree( uneedle );
224 	}
225 	ubrk_close (bi);
226 	usearch_close (src);
227 
228 	return ret_pos;
229 }
230 
231 /* }}} */
232 
233 /* {{{ grapheme_ascii_check: ASCII check */
grapheme_ascii_check(const unsigned char * day,size_t len)234 zend_long grapheme_ascii_check(const unsigned char *day, size_t len)
235 {
236 	int ret_len = len;
237 	while ( len-- ) {
238 	if ( *day++ > 0x7f || (*day == '\n' && *(day - 1) == '\r') )
239 		return -1;
240 	}
241 
242 	return ret_len;
243 }
244 
245 /* }}} */
246 
247 /* {{{ 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)248 int32_t grapheme_split_string(const UChar *text, int32_t text_length, int boundary_array[], int boundary_array_len )
249 {
250 	unsigned char u_break_iterator_buffer[U_BRK_SAFECLONE_BUFFERSIZE];
251 	UErrorCode		status = U_ZERO_ERROR;
252 	int ret_len, pos;
253 	UBreakIterator* bi;
254 
255 	bi = grapheme_get_break_iterator((void*)u_break_iterator_buffer, &status );
256 
257 	if( U_FAILURE(status) ) {
258 		return -1;
259 	}
260 
261 	ubrk_setText(bi, text, text_length,	&status);
262 
263 	pos = 0;
264 
265 	for ( ret_len = 0; pos != UBRK_DONE; ) {
266 
267 		pos = ubrk_next(bi);
268 
269 		if ( pos != UBRK_DONE ) {
270 
271 			if ( NULL != boundary_array && ret_len < boundary_array_len ) {
272 				boundary_array[ret_len] = pos;
273 			}
274 
275 			ret_len++;
276 		}
277 	}
278 
279 	ubrk_close(bi);
280 
281 	return ret_len;
282 }
283 /* }}} */
284 
285 /* {{{ grapheme_count_graphemes */
grapheme_count_graphemes(UBreakIterator * bi,UChar * string,int32_t string_len)286 int32_t grapheme_count_graphemes(UBreakIterator *bi, UChar *string, int32_t string_len)
287 {
288 	int ret_len = 0;
289 	int pos = 0;
290 	UErrorCode		status = U_ZERO_ERROR;
291 
292 	ubrk_setText(bi, string, string_len, &status);
293 
294 	do {
295 
296 		pos = ubrk_next(bi);
297 
298 		if ( UBRK_DONE != pos ) {
299 			ret_len++;
300 		}
301 
302 	} while ( UBRK_DONE != pos );
303 
304 	return ret_len;
305 }
306 /* }}} */
307 
308 
309 /* {{{ 	grapheme_get_haystack_offset - bump the haystack pointer based on the grapheme count offset */
grapheme_get_haystack_offset(UBreakIterator * bi,int32_t offset)310 int32_t grapheme_get_haystack_offset(UBreakIterator* bi, int32_t offset)
311 {
312 	int32_t pos;
313 	int32_t (*iter_op)(UBreakIterator* bi);
314 	int iter_incr;
315 
316 	if ( 0 == offset ) {
317 		return 0;
318 	}
319 
320 	if ( offset < 0 ) {
321 		iter_op = ubrk_previous;
322 		ubrk_last(bi); /* one past the end */
323 		iter_incr = 1;
324 	}
325 	else {
326 		iter_op = ubrk_next;
327 		iter_incr = -1;
328 	}
329 
330 	pos = 0;
331 
332 	while ( pos != UBRK_DONE && offset != 0 ) {
333 
334 		pos = iter_op(bi);
335 
336 		if ( UBRK_DONE != pos ) {
337 			offset += iter_incr;
338 		}
339 	}
340 
341 	if ( offset != 0 ) {
342 		return -1;
343 	}
344 
345 	return pos;
346 }
347 /* }}} */
348 
349 /* {{{ grapheme_strrpos_ascii: borrowed from the php ext/standard/string.c */
350  zend_long
grapheme_strrpos_ascii(char * haystack,size_t haystack_len,char * needle,size_t needle_len,int32_t offset)351 grapheme_strrpos_ascii(char *haystack, size_t haystack_len, char *needle, size_t needle_len, int32_t offset)
352 {
353 	char *p, *e;
354 
355 	if (offset >= 0) {
356 		p = haystack + offset;
357 		e = haystack + haystack_len - needle_len;
358 	} else {
359 		p = haystack;
360 		if (needle_len > (size_t)-offset) {
361 			e = haystack + haystack_len - needle_len;
362 		} else {
363 			e = haystack + haystack_len + offset;
364 		}
365 	}
366 
367 	if (needle_len == 1) {
368 		/* Single character search can shortcut memcmps */
369 		while (e >= p) {
370 			if (*e == *needle) {
371 				return (e - p + (offset > 0 ? offset : 0));
372 			}
373 			e--;
374 		}
375 		return -1;
376 	}
377 
378 	while (e >= p) {
379 		if (memcmp(e, needle, needle_len) == 0) {
380 			return (e - p + (offset > 0 ? offset : 0));
381 		}
382 		e--;
383 	}
384 
385 	return -1;
386 }
387 
388 /* }}} */
389 
390 /* {{{ grapheme_get_break_iterator: get a clone of the global character break iterator */
grapheme_get_break_iterator(void * stack_buffer,UErrorCode * status)391 UBreakIterator* grapheme_get_break_iterator(void *stack_buffer, UErrorCode *status )
392 {
393 	int32_t buffer_size;
394 
395 	UBreakIterator *global_break_iterator = INTL_G( grapheme_iterator );
396 
397 	if ( NULL == global_break_iterator ) {
398 
399 		global_break_iterator = ubrk_open(UBRK_CHARACTER,
400 											NULL,	/* icu default locale - locale has no effect on this iterator */
401 											NULL,	/* text not set in global iterator */
402 											0,		/* text length = 0 */
403 											status);
404 
405 		INTL_G(grapheme_iterator) = global_break_iterator;
406 	}
407 
408 	buffer_size = U_BRK_SAFECLONE_BUFFERSIZE;
409 
410 	return ubrk_safeClone(global_break_iterator, stack_buffer, &buffer_size, status);
411 }
412 /* }}} */
413