1 : /*
2 : +----------------------------------------------------------------------+
3 : | PHP Version 5 |
4 : +----------------------------------------------------------------------+
5 : | Copyright (c) 1997-2009 The PHP Group |
6 : +----------------------------------------------------------------------+
7 : | This source file is subject to version 3.01 of the PHP license, |
8 : | that is bundled with this package in the file LICENSE, and is |
9 : | available through the world-wide-web at the following url: |
10 : | http://www.php.net/license/3_01.txt |
11 : | If you did not receive a copy of the PHP license and are unable to |
12 : | obtain it through the world-wide-web, please send a note to |
13 : | license@php.net so we can mail you a copy immediately. |
14 : +----------------------------------------------------------------------+
15 : | Author: Tsukada Takuya <tsukada@fminn.nagano.nagano.jp> |
16 : +----------------------------------------------------------------------+
17 : */
18 :
19 : /* $Id: php_mbregex.c 272374 2008-12-31 11:17:49Z sebastian $ */
20 :
21 :
22 : #ifdef HAVE_CONFIG_H
23 : #include "config.h"
24 : #endif
25 :
26 : #include "php.h"
27 : #include "php_ini.h"
28 :
29 : #if HAVE_MBREGEX
30 :
31 : #include "ext/standard/php_smart_str.h"
32 : #include "php_mbregex.h"
33 : #include "mbstring.h"
34 :
35 : ZEND_EXTERN_MODULE_GLOBALS(mbstring)
36 :
37 : /* {{{ static void php_mb_regex_free_cache() */
38 : static void php_mb_regex_free_cache(php_mb_regex_t **pre)
39 145 : {
40 145 : onig_free(*pre);
41 145 : }
42 : /* }}} */
43 :
44 : /* {{{ _php_mb_regex_globals_ctor */
45 : void _php_mb_regex_globals_ctor(zend_mbstring_globals *pglobals TSRMLS_DC)
46 13565 : {
47 13565 : MBSTRG(default_mbctype) = ONIG_ENCODING_EUC_JP;
48 13565 : MBSTRG(current_mbctype) = ONIG_ENCODING_EUC_JP;
49 13565 : zend_hash_init(&(MBSTRG(ht_rc)), 0, NULL, (void (*)(void *)) php_mb_regex_free_cache, 1);
50 13565 : MBSTRG(search_str) = (zval*) NULL;
51 13565 : MBSTRG(search_re) = (php_mb_regex_t*)NULL;
52 13565 : MBSTRG(search_pos) = 0;
53 13565 : MBSTRG(search_regs) = (OnigRegion*)NULL;
54 13565 : MBSTRG(regex_default_options) = ONIG_OPTION_MULTILINE | ONIG_OPTION_SINGLELINE;
55 13565 : MBSTRG(regex_default_syntax) = ONIG_SYNTAX_RUBY;
56 13565 : }
57 : /* }}} */
58 :
59 : /* {{{ _php_mb_regex_globals_dtor */
60 : void _php_mb_regex_globals_dtor(zend_mbstring_globals *pglobals TSRMLS_DC)
61 13597 : {
62 13597 : zend_hash_destroy(&MBSTRG(ht_rc));
63 13597 : }
64 : /* }}} */
65 :
66 : /* {{{ PHP_MINIT_FUNCTION(mb_regex) */
67 : PHP_MINIT_FUNCTION(mb_regex)
68 13565 : {
69 13565 : onig_init();
70 13565 : return SUCCESS;
71 : }
72 : /* }}} */
73 :
74 : /* {{{ PHP_MSHUTDOWN_FUNCTION(mb_regex) */
75 : PHP_MSHUTDOWN_FUNCTION(mb_regex)
76 13597 : {
77 13597 : onig_end();
78 13597 : return SUCCESS;
79 : }
80 : /* }}} */
81 :
82 : /* {{{ PHP_RINIT_FUNCTION(mb_regex) */
83 : PHP_RINIT_FUNCTION(mb_regex)
84 13551 : {
85 13551 : return SUCCESS;
86 : }
87 : /* }}} */
88 :
89 : /* {{{ PHP_RSHUTDOWN_FUNCTION(mb_regex) */
90 : PHP_RSHUTDOWN_FUNCTION(mb_regex)
91 13584 : {
92 13584 : MBSTRG(current_mbctype) = MBSTRG(default_mbctype);
93 :
94 13584 : if (MBSTRG(search_str) != NULL) {
95 2 : zval_ptr_dtor(&MBSTRG(search_str));
96 2 : MBSTRG(search_str) = (zval *)NULL;
97 : }
98 13584 : MBSTRG(search_pos) = 0;
99 :
100 13584 : if (MBSTRG(search_regs) != NULL) {
101 0 : onig_region_free(MBSTRG(search_regs), 1);
102 0 : MBSTRG(search_regs) = (OnigRegion *)NULL;
103 : }
104 13584 : zend_hash_clean(&MBSTRG(ht_rc));
105 :
106 13584 : return SUCCESS;
107 : }
108 : /* }}} */
109 :
110 : /*
111 : * encoding name resolver
112 : */
113 :
114 : /* {{{ encoding name map */
115 : typedef struct _php_mb_regex_enc_name_map_t {
116 : const char *names;
117 : OnigEncoding code;
118 : } php_mb_regex_enc_name_map_t;
119 :
120 : php_mb_regex_enc_name_map_t enc_name_map[] ={
121 : {
122 : "EUC-JP\0EUCJP\0X-EUC-JP\0UJIS\0EUCJP\0EUCJP-WIN\0",
123 : ONIG_ENCODING_EUC_JP
124 : },
125 : {
126 : "UTF-8\0UTF8\0",
127 : ONIG_ENCODING_UTF8
128 : },
129 : {
130 : "UTF-16\0UTF-16BE\0",
131 : ONIG_ENCODING_UTF16_BE
132 : },
133 : {
134 : "UTF-16LE\0",
135 : ONIG_ENCODING_UTF16_LE
136 : },
137 : {
138 : "UCS-4\0UTF-32\0UTF-32BE\0",
139 : ONIG_ENCODING_UTF32_BE
140 : },
141 : {
142 : "UCS-4LE\0UTF-32LE\0",
143 : ONIG_ENCODING_UTF32_LE
144 : },
145 : {
146 : "SJIS\0CP932\0MS932\0SHIFT_JIS\0SJIS-WIN\0WINDOWS-31J\0",
147 : ONIG_ENCODING_SJIS
148 : },
149 : {
150 : "BIG5\0BIG-5\0BIGFIVE\0CN-BIG5\0BIG-FIVE\0",
151 : ONIG_ENCODING_BIG5
152 : },
153 : {
154 : "EUC-CN\0EUCCN\0EUC_CN\0GB-2312\0GB2312\0",
155 : ONIG_ENCODING_EUC_CN
156 : },
157 : {
158 : "EUC-TW\0EUCTW\0EUC_TW\0",
159 : ONIG_ENCODING_EUC_TW
160 : },
161 : {
162 : "EUC-KR\0EUCKR\0EUC_KR\0",
163 : ONIG_ENCODING_EUC_KR
164 : },
165 : {
166 : "KOI8\0KOI-8\0",
167 : ONIG_ENCODING_KOI8
168 : },
169 : {
170 : "KOI8R\0KOI8-R\0KOI-8R\0",
171 : ONIG_ENCODING_KOI8_R
172 : },
173 : {
174 : "ISO-8859-1\0ISO8859-1\0ISO_8859_1\0ISO8859_1\0",
175 : ONIG_ENCODING_ISO_8859_1
176 : },
177 : {
178 : "ISO-8859-2\0ISO8859-2\0ISO_8859_2\0ISO8859_2\0",
179 : ONIG_ENCODING_ISO_8859_2
180 : },
181 : {
182 : "ISO-8859-3\0ISO8859-3\0ISO_8859_3\0ISO8859_3\0",
183 : ONIG_ENCODING_ISO_8859_3
184 : },
185 : {
186 : "ISO-8859-4\0ISO8859-4\0ISO_8859_4\0ISO8859_4\0",
187 : ONIG_ENCODING_ISO_8859_4
188 : },
189 : {
190 : "ISO-8859-5\0ISO8859-5\0ISO_8859_5\0ISO8859_5\0",
191 : ONIG_ENCODING_ISO_8859_5
192 : },
193 : {
194 : "ISO-8859-6\0ISO8859-6\0ISO_8859_6\0ISO8859_6\0",
195 : ONIG_ENCODING_ISO_8859_6
196 : },
197 : {
198 : "ISO-8859-7\0ISO8859-7\0ISO_8859_7\0ISO8859_7\0",
199 : ONIG_ENCODING_ISO_8859_7
200 : },
201 : {
202 : "ISO-8859-8\0ISO8859-8\0ISO_8859_8\0ISO8859_8\0",
203 : ONIG_ENCODING_ISO_8859_8
204 : },
205 : {
206 : "ISO-8859-9\0ISO8859-9\0ISO_8859_9\0ISO8859_9\0",
207 : ONIG_ENCODING_ISO_8859_9
208 : },
209 : {
210 : "ISO-8859-10\0ISO8859-10\0ISO_8859_10\0ISO8859_10\0",
211 : ONIG_ENCODING_ISO_8859_10
212 : },
213 : {
214 : "ISO-8859-11\0ISO8859-11\0ISO_8859_11\0ISO8859_11\0",
215 : ONIG_ENCODING_ISO_8859_11
216 : },
217 : {
218 : "ISO-8859-13\0ISO8859-13\0ISO_8859_13\0ISO8859_13\0",
219 : ONIG_ENCODING_ISO_8859_13
220 : },
221 : {
222 : "ISO-8859-14\0ISO8859-14\0ISO_8859_14\0ISO8859_14\0",
223 : ONIG_ENCODING_ISO_8859_14
224 : },
225 : {
226 : "ISO-8859-15\0ISO8859-15\0ISO_8859_15\0ISO8859_15\0",
227 : ONIG_ENCODING_ISO_8859_15
228 : },
229 : {
230 : "ISO-8859-16\0ISO8859-16\0ISO_8859_16\0ISO8859_16\0",
231 : ONIG_ENCODING_ISO_8859_16
232 : },
233 : {
234 : "ASCII\0US-ASCII\0US_ASCII\0ISO646\0",
235 : ONIG_ENCODING_ASCII
236 : },
237 : { NULL, ONIG_ENCODING_UNDEF }
238 : };
239 : /* }}} */
240 :
241 : /* {{{ php_mb_regex_name2mbctype */
242 : OnigEncoding php_mb_regex_name2mbctype(const char *pname)
243 40816 : {
244 : const char *p;
245 : php_mb_regex_enc_name_map_t *mapping;
246 :
247 40816 : if (pname == NULL) {
248 0 : return ONIG_ENCODING_UNDEF;
249 : }
250 :
251 571406 : for (mapping = enc_name_map; mapping->names != NULL; mapping++) {
252 2327478 : for (p = mapping->names; *p != '\0'; p += (strlen(p) + 1)) {
253 1796888 : if (strcasecmp(p, pname) == 0) {
254 40729 : return mapping->code;
255 : }
256 : }
257 : }
258 :
259 87 : return ONIG_ENCODING_UNDEF;
260 : }
261 : /* }}} */
262 :
263 : /* {{{ php_mbregex_mbctype2name */
264 : const char *php_mb_regex_mbctype2name(OnigEncoding mbctype)
265 240 : {
266 : php_mb_regex_enc_name_map_t *mapping;
267 :
268 3189 : for (mapping = enc_name_map; mapping->names != NULL; mapping++) {
269 3189 : if (mapping->code == mbctype) {
270 240 : return mapping->names;
271 : }
272 : }
273 :
274 0 : return NULL;
275 : }
276 : /* }}} */
277 :
278 : /*
279 : * regex cache
280 : */
281 : /* {{{ php_mbregex_compile_pattern */
282 : static php_mb_regex_t *php_mbregex_compile_pattern(const char *pattern, int patlen, OnigOptionType options, OnigEncoding enc, OnigSyntaxType *syntax TSRMLS_DC)
283 332 : {
284 332 : int err_code = 0;
285 332 : int found = 0;
286 332 : php_mb_regex_t *retval = NULL, **rc = NULL;
287 : OnigErrorInfo err_info;
288 : OnigUChar err_str[ONIG_MAX_ERROR_MESSAGE_LEN];
289 :
290 332 : found = zend_hash_find(&MBSTRG(ht_rc), (char *)pattern, patlen+1, (void **) &rc);
291 477 : if (found == FAILURE || (*rc)->options != options || (*rc)->enc != enc || (*rc)->syntax != syntax) {
292 145 : if ((err_code = onig_new(&retval, (OnigUChar *)pattern, (OnigUChar *)(pattern + patlen), options, enc, syntax, &err_info)) != ONIG_NORMAL) {
293 0 : onig_error_code_to_str(err_str, err_code, err_info);
294 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "mbregex compile err: %s", err_str);
295 0 : retval = NULL;
296 0 : goto out;
297 : }
298 145 : zend_hash_update(&MBSTRG(ht_rc), (char *) pattern, patlen + 1, (void *) &retval, sizeof(retval), NULL);
299 187 : } else if (found == SUCCESS) {
300 187 : retval = *rc;
301 : }
302 332 : out:
303 332 : return retval;
304 : }
305 : /* }}} */
306 :
307 : /* {{{ _php_mb_regex_get_option_string */
308 : static size_t _php_mb_regex_get_option_string(char *str, size_t len, OnigOptionType option, OnigSyntaxType *syntax)
309 6 : {
310 6 : size_t len_left = len;
311 6 : size_t len_req = 0;
312 6 : char *p = str;
313 : char c;
314 :
315 6 : if ((option & ONIG_OPTION_IGNORECASE) != 0) {
316 0 : if (len_left > 0) {
317 0 : --len_left;
318 0 : *(p++) = 'i';
319 : }
320 0 : ++len_req;
321 : }
322 :
323 6 : if ((option & ONIG_OPTION_EXTEND) != 0) {
324 1 : if (len_left > 0) {
325 1 : --len_left;
326 1 : *(p++) = 'x';
327 : }
328 1 : ++len_req;
329 : }
330 :
331 6 : if ((option & (ONIG_OPTION_MULTILINE | ONIG_OPTION_SINGLELINE)) ==
332 : (ONIG_OPTION_MULTILINE | ONIG_OPTION_SINGLELINE)) {
333 0 : if (len_left > 0) {
334 0 : --len_left;
335 0 : *(p++) = 'p';
336 : }
337 0 : ++len_req;
338 : } else {
339 6 : if ((option & ONIG_OPTION_MULTILINE) != 0) {
340 0 : if (len_left > 0) {
341 0 : --len_left;
342 0 : *(p++) = 'm';
343 : }
344 0 : ++len_req;
345 : }
346 :
347 6 : if ((option & ONIG_OPTION_SINGLELINE) != 0) {
348 0 : if (len_left > 0) {
349 0 : --len_left;
350 0 : *(p++) = 's';
351 : }
352 0 : ++len_req;
353 : }
354 : }
355 6 : if ((option & ONIG_OPTION_FIND_LONGEST) != 0) {
356 0 : if (len_left > 0) {
357 0 : --len_left;
358 0 : *(p++) = 'l';
359 : }
360 0 : ++len_req;
361 : }
362 6 : if ((option & ONIG_OPTION_FIND_NOT_EMPTY) != 0) {
363 0 : if (len_left > 0) {
364 0 : --len_left;
365 0 : *(p++) = 'n';
366 : }
367 0 : ++len_req;
368 : }
369 :
370 6 : c = 0;
371 :
372 6 : if (syntax == ONIG_SYNTAX_JAVA) {
373 0 : c = 'j';
374 6 : } else if (syntax == ONIG_SYNTAX_GNU_REGEX) {
375 0 : c = 'u';
376 6 : } else if (syntax == ONIG_SYNTAX_GREP) {
377 0 : c = 'g';
378 6 : } else if (syntax == ONIG_SYNTAX_EMACS) {
379 0 : c = 'c';
380 6 : } else if (syntax == ONIG_SYNTAX_RUBY) {
381 6 : c = 'r';
382 0 : } else if (syntax == ONIG_SYNTAX_PERL) {
383 0 : c = 'z';
384 0 : } else if (syntax == ONIG_SYNTAX_POSIX_BASIC) {
385 0 : c = 'b';
386 0 : } else if (syntax == ONIG_SYNTAX_POSIX_EXTENDED) {
387 0 : c = 'd';
388 : }
389 :
390 6 : if (c != 0) {
391 6 : if (len_left > 0) {
392 6 : --len_left;
393 6 : *(p++) = c;
394 : }
395 6 : ++len_req;
396 : }
397 :
398 :
399 6 : if (len_left > 0) {
400 6 : --len_left;
401 6 : *(p++) = '\0';
402 : }
403 6 : ++len_req;
404 6 : if (len < len_req) {
405 0 : return len_req;
406 : }
407 :
408 6 : return 0;
409 : }
410 : /* }}} */
411 :
412 : /* {{{ _php_mb_regex_init_options */
413 : static void
414 : _php_mb_regex_init_options(const char *parg, int narg, OnigOptionType *option, OnigSyntaxType **syntax, int *eval)
415 117 : {
416 : int n;
417 : char c;
418 117 : int optm = 0;
419 :
420 117 : *syntax = ONIG_SYNTAX_RUBY;
421 :
422 117 : if (parg != NULL) {
423 117 : n = 0;
424 321 : while(n < narg) {
425 87 : c = parg[n++];
426 87 : switch (c) {
427 : case 'i':
428 1 : optm |= ONIG_OPTION_IGNORECASE;
429 1 : break;
430 : case 'x':
431 9 : optm |= ONIG_OPTION_EXTEND;
432 9 : break;
433 : case 'm':
434 0 : optm |= ONIG_OPTION_MULTILINE;
435 0 : break;
436 : case 's':
437 0 : optm |= ONIG_OPTION_SINGLELINE;
438 0 : break;
439 : case 'p':
440 0 : optm |= ONIG_OPTION_MULTILINE | ONIG_OPTION_SINGLELINE;
441 0 : break;
442 : case 'l':
443 0 : optm |= ONIG_OPTION_FIND_LONGEST;
444 0 : break;
445 : case 'n':
446 0 : optm |= ONIG_OPTION_FIND_NOT_EMPTY;
447 0 : break;
448 : case 'j':
449 0 : *syntax = ONIG_SYNTAX_JAVA;
450 0 : break;
451 : case 'u':
452 0 : *syntax = ONIG_SYNTAX_GNU_REGEX;
453 0 : break;
454 : case 'g':
455 0 : *syntax = ONIG_SYNTAX_GREP;
456 0 : break;
457 : case 'c':
458 0 : *syntax = ONIG_SYNTAX_EMACS;
459 0 : break;
460 : case 'r':
461 2 : *syntax = ONIG_SYNTAX_RUBY;
462 2 : break;
463 : case 'z':
464 0 : *syntax = ONIG_SYNTAX_PERL;
465 0 : break;
466 : case 'b':
467 0 : *syntax = ONIG_SYNTAX_POSIX_BASIC;
468 0 : break;
469 : case 'd':
470 0 : *syntax = ONIG_SYNTAX_POSIX_EXTENDED;
471 0 : break;
472 : case 'e':
473 1 : if (eval != NULL) *eval = 1;
474 : break;
475 : default:
476 : break;
477 : }
478 : }
479 117 : if (option != NULL) *option|=optm;
480 : }
481 117 : }
482 : /* }}} */
483 :
484 : /*
485 : * php funcions
486 : */
487 :
488 : /* {{{ proto string mb_regex_encoding([string encoding])
489 : Returns the current encoding for regex as a string. */
490 : PHP_FUNCTION(mb_regex_encoding)
491 235 : {
492 : zval **arg1;
493 : OnigEncoding mbctype;
494 :
495 235 : if (ZEND_NUM_ARGS() == 0) {
496 114 : const char *retval = php_mb_regex_mbctype2name(MBSTRG(current_mbctype));
497 114 : if ( retval != NULL ) {
498 114 : RETVAL_STRING((char *)retval, 1);
499 : } else {
500 0 : RETVAL_FALSE;
501 : }
502 241 : } else if (ZEND_NUM_ARGS() == 1 &&
503 : zend_get_parameters_ex(1, &arg1) != FAILURE) {
504 120 : convert_to_string_ex(arg1);
505 120 : mbctype = php_mb_regex_name2mbctype(Z_STRVAL_PP(arg1));
506 120 : if (mbctype == ONIG_ENCODING_UNDEF) {
507 45 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", Z_STRVAL_PP(arg1));
508 45 : RETVAL_FALSE;
509 : } else {
510 75 : MBSTRG(current_mbctype) = mbctype;
511 75 : RETVAL_TRUE;
512 : }
513 : } else {
514 1 : WRONG_PARAM_COUNT;
515 : }
516 : }
517 : /* }}} */
518 :
519 : /* {{{ _php_mb_regex_ereg_exec */
520 : static void _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAMETERS, int icase)
521 136 : {
522 : zval **arg_pattern, *array;
523 : char *string;
524 : int string_len;
525 : php_mb_regex_t *re;
526 136 : OnigRegion *regs = NULL;
527 : int i, match_len, beg, end;
528 : OnigOptionType options;
529 : char *str;
530 :
531 136 : array = NULL;
532 :
533 136 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zs|z", &arg_pattern, &string, &string_len, &array) == FAILURE) {
534 4 : RETURN_FALSE;
535 : }
536 :
537 132 : options = MBSTRG(regex_default_options);
538 132 : if (icase) {
539 1 : options |= ONIG_OPTION_IGNORECASE;
540 : }
541 :
542 : /* compile the regular expression from the supplied regex */
543 132 : if (Z_TYPE_PP(arg_pattern) != IS_STRING) {
544 : /* we convert numbers to integers and treat them as a string */
545 30 : if (Z_TYPE_PP(arg_pattern) == IS_DOUBLE) {
546 5 : convert_to_long_ex(arg_pattern); /* get rid of decimal places */
547 : }
548 30 : convert_to_string_ex(arg_pattern);
549 : /* don't bother doing an extended regex with just a number */
550 : }
551 :
552 132 : if (!Z_STRVAL_PP(arg_pattern) || Z_STRLEN_PP(arg_pattern) == 0) {
553 17 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "empty pattern");
554 17 : RETVAL_FALSE;
555 17 : goto out;
556 : }
557 :
558 115 : re = php_mbregex_compile_pattern(Z_STRVAL_PP(arg_pattern), Z_STRLEN_PP(arg_pattern), options, MBSTRG(current_mbctype), MBSTRG(regex_default_syntax) TSRMLS_CC);
559 115 : if (re == NULL) {
560 0 : RETVAL_FALSE;
561 0 : goto out;
562 : }
563 :
564 115 : regs = onig_region_new();
565 :
566 : /* actually execute the regular expression */
567 115 : if (onig_search(re, (OnigUChar *)string, (OnigUChar *)(string + string_len), string, (OnigUChar *)(string + string_len), regs, 0) < 0) {
568 48 : RETVAL_FALSE;
569 48 : goto out;
570 : }
571 :
572 67 : match_len = 1;
573 67 : str = string;
574 67 : if (array != NULL) {
575 61 : match_len = regs->end[0] - regs->beg[0];
576 61 : zval_dtor(array);
577 61 : array_init(array);
578 179 : for (i = 0; i < regs->num_regs; i++) {
579 118 : beg = regs->beg[i];
580 118 : end = regs->end[i];
581 232 : if (beg >= 0 && beg < end && end <= string_len) {
582 114 : add_index_stringl(array, i, (char *)&str[beg], end - beg, 1);
583 : } else {
584 4 : add_index_bool(array, i, 0);
585 : }
586 : }
587 : }
588 :
589 67 : if (match_len == 0) {
590 4 : match_len = 1;
591 : }
592 67 : RETVAL_LONG(match_len);
593 132 : out:
594 132 : if (regs != NULL) {
595 115 : onig_region_free(regs, 1);
596 : }
597 : }
598 : /* }}} */
599 :
600 : /* {{{ proto int mb_ereg(string pattern, string string [, array registers])
601 : Regular expression match for multibyte string */
602 : PHP_FUNCTION(mb_ereg)
603 135 : {
604 135 : _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
605 135 : }
606 : /* }}} */
607 :
608 : /* {{{ proto int mb_eregi(string pattern, string string [, array registers])
609 : Case-insensitive regular expression match for multibyte string */
610 : PHP_FUNCTION(mb_eregi)
611 1 : {
612 1 : _php_mb_regex_ereg_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
613 1 : }
614 : /* }}} */
615 :
616 : /* {{{ _php_mb_regex_ereg_replace_exec */
617 : static void _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAMETERS, OnigOptionType options)
618 126 : {
619 : zval **arg_pattern_zval;
620 :
621 : char *arg_pattern;
622 : int arg_pattern_len;
623 :
624 : char *replace;
625 : int replace_len;
626 :
627 : char *string;
628 : int string_len;
629 :
630 : char *p;
631 : php_mb_regex_t *re;
632 : OnigSyntaxType *syntax;
633 126 : OnigRegion *regs = NULL;
634 126 : smart_str out_buf = { 0 };
635 126 : smart_str eval_buf = { 0 };
636 : smart_str *pbuf;
637 : int i, err, eval, n;
638 : OnigUChar *pos;
639 : OnigUChar *string_lim;
640 126 : char *description = NULL;
641 : char pat_buf[2];
642 :
643 : const mbfl_encoding *enc;
644 :
645 : {
646 : const char *current_enc_name;
647 126 : current_enc_name = php_mb_regex_mbctype2name(MBSTRG(current_mbctype));
648 126 : if (current_enc_name == NULL ||
649 : (enc = mbfl_name2encoding(current_enc_name)) == NULL) {
650 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown error");
651 0 : RETURN_FALSE;
652 : }
653 : }
654 126 : eval = 0;
655 : {
656 126 : char *option_str = NULL;
657 126 : int option_str_len = 0;
658 :
659 126 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "Zss|s",
660 : &arg_pattern_zval,
661 : &replace, &replace_len,
662 : &string, &string_len,
663 : &option_str, &option_str_len) == FAILURE) {
664 5 : RETURN_FALSE;
665 : }
666 :
667 121 : if (option_str != NULL) {
668 93 : _php_mb_regex_init_options(option_str, option_str_len, &options, &syntax, &eval);
669 : } else {
670 28 : options |= MBSTRG(regex_default_options);
671 28 : syntax = MBSTRG(regex_default_syntax);
672 : }
673 : }
674 121 : if (Z_TYPE_PP(arg_pattern_zval) == IS_STRING) {
675 103 : arg_pattern = Z_STRVAL_PP(arg_pattern_zval);
676 103 : arg_pattern_len = Z_STRLEN_PP(arg_pattern_zval);
677 : } else {
678 : /* FIXME: this code is not multibyte aware! */
679 18 : convert_to_long_ex(arg_pattern_zval);
680 18 : pat_buf[0] = (char)Z_LVAL_PP(arg_pattern_zval);
681 18 : pat_buf[1] = '\0';
682 :
683 18 : arg_pattern = pat_buf;
684 18 : arg_pattern_len = 1;
685 : }
686 : /* create regex pattern buffer */
687 121 : re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, options, MBSTRG(current_mbctype), syntax TSRMLS_CC);
688 121 : if (re == NULL) {
689 0 : RETURN_FALSE;
690 : }
691 :
692 121 : if (eval) {
693 1 : pbuf = &eval_buf;
694 1 : description = zend_make_compiled_string_description("mbregex replace" TSRMLS_CC);
695 : } else {
696 120 : pbuf = &out_buf;
697 120 : description = NULL;
698 : }
699 :
700 : /* do the actual work */
701 121 : err = 0;
702 121 : pos = string;
703 121 : string_lim = (OnigUChar*)(string + string_len);
704 121 : regs = onig_region_new();
705 703 : while (err >= 0) {
706 462 : err = onig_search(re, (OnigUChar *)string, (OnigUChar *)string_lim, pos, (OnigUChar *)string_lim, regs, 0);
707 462 : if (err <= -2) {
708 : OnigUChar err_str[ONIG_MAX_ERROR_MESSAGE_LEN];
709 0 : onig_error_code_to_str(err_str, err);
710 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "mbregex search failure in php_mbereg_replace_exec(): %s", err_str);
711 0 : break;
712 : }
713 462 : if (err >= 0) {
714 : #if moriyoshi_0
715 : if (regs->beg[0] == regs->end[0]) {
716 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty regular expression");
717 : break;
718 : }
719 : #endif
720 : /* copy the part of the string before the match */
721 342 : smart_str_appendl(&out_buf, pos, (size_t)((OnigUChar *)(string + regs->beg[0]) - pos));
722 : /* copy replacement and backrefs */
723 342 : i = 0;
724 342 : p = replace;
725 1716 : while (i < replace_len) {
726 1032 : int fwd = (int) php_mb_mbchar_bytes_ex(p, enc);
727 1032 : n = -1;
728 1032 : if ((replace_len - i) >= 2 && fwd == 1 &&
729 : p[0] == '\\' && p[1] >= '0' && p[1] <= '9') {
730 14 : n = p[1] - '0';
731 : }
732 1045 : if (n >= 0 && n < regs->num_regs) {
733 13 : if (regs->beg[n] >= 0 && regs->beg[n] < regs->end[n] && regs->end[n] <= string_len) {
734 13 : smart_str_appendl(pbuf, string + regs->beg[n], regs->end[n] - regs->beg[n]);
735 : }
736 13 : p += 2;
737 13 : i += 2;
738 : } else {
739 1019 : smart_str_appendl(pbuf, p, fwd);
740 1019 : p += fwd;
741 1019 : i += fwd;
742 : }
743 : }
744 342 : if (eval) {
745 : zval v;
746 : /* null terminate buffer */
747 1 : smart_str_appendc(&eval_buf, '\0');
748 : /* do eval */
749 1 : if (zend_eval_string(eval_buf.c, &v, description TSRMLS_CC) == FAILURE) {
750 1 : efree(description);
751 1 : php_error_docref(NULL TSRMLS_CC,E_ERROR, "Failed evaluating code: %s%s", PHP_EOL, eval_buf.c);
752 : /* zend_error() does not return in this case */
753 : }
754 :
755 : /* result of eval */
756 0 : convert_to_string(&v);
757 0 : smart_str_appendl(&out_buf, Z_STRVAL(v), Z_STRLEN(v));
758 : /* Clean up */
759 0 : eval_buf.len = 0;
760 0 : zval_dtor(&v);
761 : }
762 341 : n = regs->end[0];
763 341 : if ((size_t)(pos - (OnigUChar *)string) < n) {
764 315 : pos = string + n;
765 : } else {
766 26 : if (pos < string_lim) {
767 21 : smart_str_appendl(&out_buf, pos, 1);
768 : }
769 26 : pos++;
770 : }
771 : } else { /* nomatch */
772 : /* stick that last bit of string on our output */
773 120 : if (string_lim - pos > 0) {
774 70 : smart_str_appendl(&out_buf, pos, string_lim - pos);
775 : }
776 : }
777 461 : onig_region_free(regs, 0);
778 : }
779 :
780 120 : if (description) {
781 0 : efree(description);
782 : }
783 120 : if (regs != NULL) {
784 120 : onig_region_free(regs, 1);
785 : }
786 120 : smart_str_free(&eval_buf);
787 :
788 120 : if (err <= -2) {
789 0 : smart_str_free(&out_buf);
790 0 : RETVAL_FALSE;
791 : } else {
792 120 : smart_str_appendc(&out_buf, '\0');
793 120 : RETVAL_STRINGL((char *)out_buf.c, out_buf.len - 1, 0);
794 : }
795 : }
796 : /* }}} */
797 :
798 : /* {{{ proto string mb_ereg_replace(string pattern, string replacement, string string [, string option])
799 : Replace regular expression for multibyte string */
800 : PHP_FUNCTION(mb_ereg_replace)
801 126 : {
802 126 : _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
803 125 : }
804 : /* }}} */
805 :
806 : /* {{{ proto string mb_eregi_replace(string pattern, string replacement, string string)
807 : Case insensitive replace regular expression for multibyte string */
808 : PHP_FUNCTION(mb_eregi_replace)
809 0 : {
810 0 : _php_mb_regex_ereg_replace_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, ONIG_OPTION_IGNORECASE);
811 0 : }
812 : /* }}} */
813 :
814 : /* {{{ proto array mb_split(string pattern, string string [, int limit])
815 : split multibyte string into array by regular expression */
816 : PHP_FUNCTION(mb_split)
817 86 : {
818 : char *arg_pattern;
819 : int arg_pattern_len;
820 : php_mb_regex_t *re;
821 86 : OnigRegion *regs = NULL;
822 : char *string;
823 : OnigUChar *pos;
824 : int string_len;
825 :
826 : int n, err;
827 86 : long count = -1;
828 :
829 86 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|l", &arg_pattern, &arg_pattern_len, &string, &string_len, &count) == FAILURE) {
830 11 : RETURN_FALSE;
831 : }
832 :
833 75 : if (count == 0) {
834 10 : count = 1;
835 : }
836 :
837 : /* create regex pattern buffer */
838 75 : if ((re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, MBSTRG(regex_default_options), MBSTRG(current_mbctype), MBSTRG(regex_default_syntax) TSRMLS_CC)) == NULL) {
839 0 : RETURN_FALSE;
840 : }
841 :
842 75 : array_init(return_value);
843 :
844 75 : pos = (OnigUChar *)string;
845 75 : err = 0;
846 75 : regs = onig_region_new();
847 : /* churn through str, generating array entries as we go */
848 222 : while ((--count != 0) &&
849 : (err = onig_search(re, (OnigUChar *)string, (OnigUChar *)(string + string_len), pos, (OnigUChar *)(string + string_len), regs, 0)) >= 0) {
850 80 : if (regs->beg[0] == regs->end[0]) {
851 8 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty regular expression");
852 8 : break;
853 : }
854 :
855 : /* add it to the array */
856 72 : if (regs->beg[0] < string_len && regs->beg[0] >= (size_t)(pos - (OnigUChar *)string)) {
857 72 : add_next_index_stringl(return_value, pos, ((OnigUChar *)(string + regs->beg[0]) - pos), 1);
858 : } else {
859 0 : err = -2;
860 0 : break;
861 : }
862 : /* point at our new starting point */
863 72 : n = regs->end[0];
864 72 : if ((pos - (OnigUChar *)string) < n) {
865 72 : pos = (OnigUChar *)string + n;
866 : }
867 72 : if (count < 0) {
868 42 : count = 0;
869 : }
870 72 : onig_region_free(regs, 0);
871 : }
872 :
873 75 : onig_region_free(regs, 1);
874 :
875 : /* see if we encountered an error */
876 75 : if (err <= -2) {
877 : OnigUChar err_str[ONIG_MAX_ERROR_MESSAGE_LEN];
878 0 : onig_error_code_to_str(err_str, err);
879 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "mbregex search failure in mbsplit(): %s", err_str);
880 0 : zval_dtor(return_value);
881 0 : RETURN_FALSE;
882 : }
883 :
884 : /* otherwise we just have one last element to add to the array */
885 75 : n = ((OnigUChar *)(string + string_len) - pos);
886 75 : if (n > 0) {
887 61 : add_next_index_stringl(return_value, pos, n, 1);
888 : } else {
889 14 : add_next_index_stringl(return_value, "", 0, 1);
890 : }
891 : }
892 : /* }}} */
893 :
894 : /* {{{ proto bool mb_ereg_match(string pattern, string string [,string option])
895 : Regular expression match for multibyte string */
896 : PHP_FUNCTION(mb_ereg_match)
897 7 : {
898 : char *arg_pattern;
899 : int arg_pattern_len;
900 :
901 : char *string;
902 : int string_len;
903 :
904 : php_mb_regex_t *re;
905 : OnigSyntaxType *syntax;
906 7 : int option = 0, err;
907 :
908 : {
909 7 : char *option_str = NULL;
910 7 : int option_str_len = 0;
911 :
912 7 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|s",
913 : &arg_pattern, &arg_pattern_len, &string, &string_len,
914 : &option_str, &option_str_len)==FAILURE) {
915 3 : RETURN_FALSE;
916 : }
917 :
918 4 : if (option_str != NULL) {
919 1 : _php_mb_regex_init_options(option_str, option_str_len, &option, &syntax, NULL);
920 : } else {
921 3 : option |= MBSTRG(regex_default_options);
922 3 : syntax = MBSTRG(regex_default_syntax);
923 : }
924 : }
925 :
926 4 : if ((re = php_mbregex_compile_pattern(arg_pattern, arg_pattern_len, option, MBSTRG(current_mbctype), syntax TSRMLS_CC)) == NULL) {
927 0 : RETURN_FALSE;
928 : }
929 :
930 : /* match */
931 4 : err = onig_match(re, (OnigUChar *)string, (OnigUChar *)(string + string_len), (OnigUChar *)string, NULL, 0);
932 4 : if (err >= 0) {
933 2 : RETVAL_TRUE;
934 : } else {
935 2 : RETVAL_FALSE;
936 : }
937 : }
938 : /* }}} */
939 :
940 : /* regex search */
941 : /* {{{ _php_mb_regex_ereg_search_exec */
942 : static void
943 : _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAMETERS, int mode)
944 60 : {
945 : zval **arg_pattern, **arg_options;
946 : int n, i, err, pos, len, beg, end, option;
947 : OnigUChar *str;
948 : OnigSyntaxType *syntax;
949 :
950 60 : option = MBSTRG(regex_default_options);
951 60 : switch (ZEND_NUM_ARGS()) {
952 : case 0:
953 60 : break;
954 : case 1:
955 0 : if (zend_get_parameters_ex(1, &arg_pattern) == FAILURE) {
956 0 : WRONG_PARAM_COUNT;
957 : }
958 0 : break;
959 : case 2:
960 0 : if (zend_get_parameters_ex(2, &arg_pattern, &arg_options) == FAILURE) {
961 0 : WRONG_PARAM_COUNT;
962 : }
963 0 : convert_to_string_ex(arg_options);
964 0 : option = 0;
965 0 : _php_mb_regex_init_options(Z_STRVAL_PP(arg_options), Z_STRLEN_PP(arg_options), &option, &syntax, NULL);
966 0 : break;
967 : default:
968 0 : WRONG_PARAM_COUNT;
969 : break;
970 : }
971 60 : if (ZEND_NUM_ARGS() > 0) {
972 : /* create regex pattern buffer */
973 0 : convert_to_string_ex(arg_pattern);
974 :
975 0 : if ((MBSTRG(search_re) = php_mbregex_compile_pattern(Z_STRVAL_PP(arg_pattern), Z_STRLEN_PP(arg_pattern), option, MBSTRG(current_mbctype), MBSTRG(regex_default_syntax) TSRMLS_CC)) == NULL) {
976 0 : RETURN_FALSE;
977 : }
978 : }
979 :
980 60 : pos = MBSTRG(search_pos);
981 60 : str = NULL;
982 60 : len = 0;
983 60 : if (MBSTRG(search_str) != NULL && Z_TYPE_P(MBSTRG(search_str)) == IS_STRING){
984 60 : str = (OnigUChar *)Z_STRVAL_P(MBSTRG(search_str));
985 60 : len = Z_STRLEN_P(MBSTRG(search_str));
986 : }
987 :
988 60 : if (MBSTRG(search_re) == NULL) {
989 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "No regex given");
990 0 : RETURN_FALSE;
991 : }
992 :
993 60 : if (str == NULL) {
994 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "No string given");
995 0 : RETURN_FALSE;
996 : }
997 :
998 60 : if (MBSTRG(search_regs)) {
999 44 : onig_region_free(MBSTRG(search_regs), 1);
1000 : }
1001 60 : MBSTRG(search_regs) = onig_region_new();
1002 :
1003 60 : err = onig_search(MBSTRG(search_re), str, str + len, str + pos, str + len, MBSTRG(search_regs), 0);
1004 60 : if (err == ONIG_MISMATCH) {
1005 16 : MBSTRG(search_pos) = len;
1006 16 : RETVAL_FALSE;
1007 44 : } else if (err <= -2) {
1008 : OnigUChar err_str[ONIG_MAX_ERROR_MESSAGE_LEN];
1009 0 : onig_error_code_to_str(err_str, err);
1010 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "mbregex search failure in mbregex_search(): %s", err_str);
1011 0 : RETVAL_FALSE;
1012 : } else {
1013 44 : if (MBSTRG(search_regs)->beg[0] == MBSTRG(search_regs)->end[0]) {
1014 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty regular expression");
1015 : }
1016 44 : switch (mode) {
1017 : case 1:
1018 44 : array_init(return_value);
1019 44 : beg = MBSTRG(search_regs)->beg[0];
1020 44 : end = MBSTRG(search_regs)->end[0];
1021 44 : add_next_index_long(return_value, beg);
1022 44 : add_next_index_long(return_value, end - beg);
1023 44 : break;
1024 : case 2:
1025 0 : array_init(return_value);
1026 0 : n = MBSTRG(search_regs)->num_regs;
1027 0 : for (i = 0; i < n; i++) {
1028 0 : beg = MBSTRG(search_regs)->beg[i];
1029 0 : end = MBSTRG(search_regs)->end[i];
1030 0 : if (beg >= 0 && beg <= end && end <= len) {
1031 0 : add_index_stringl(return_value, i, (char *)&str[beg], end - beg, 1);
1032 : } else {
1033 0 : add_index_bool(return_value, i, 0);
1034 : }
1035 : }
1036 0 : break;
1037 : default:
1038 0 : RETVAL_TRUE;
1039 : break;
1040 : }
1041 44 : end = MBSTRG(search_regs)->end[0];
1042 44 : if (pos < end) {
1043 44 : MBSTRG(search_pos) = end;
1044 : } else {
1045 0 : MBSTRG(search_pos) = pos + 1;
1046 : }
1047 : }
1048 :
1049 60 : if (err < 0) {
1050 16 : onig_region_free(MBSTRG(search_regs), 1);
1051 16 : MBSTRG(search_regs) = (OnigRegion *)NULL;
1052 : }
1053 : }
1054 : /* }}} */
1055 :
1056 : /* {{{ proto bool mb_ereg_search([string pattern[, string option]])
1057 : Regular expression search for multibyte string */
1058 : PHP_FUNCTION(mb_ereg_search)
1059 0 : {
1060 0 : _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 0);
1061 0 : }
1062 : /* }}} */
1063 :
1064 : /* {{{ proto array mb_ereg_search_pos([string pattern[, string option]])
1065 : Regular expression search for multibyte string */
1066 : PHP_FUNCTION(mb_ereg_search_pos)
1067 60 : {
1068 60 : _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 1);
1069 60 : }
1070 : /* }}} */
1071 :
1072 : /* {{{ proto array mb_ereg_search_regs([string pattern[, string option]])
1073 : Regular expression search for multibyte string */
1074 : PHP_FUNCTION(mb_ereg_search_regs)
1075 0 : {
1076 0 : _php_mb_regex_ereg_search_exec(INTERNAL_FUNCTION_PARAM_PASSTHRU, 2);
1077 0 : }
1078 : /* }}} */
1079 :
1080 : /* {{{ proto bool mb_ereg_search_init(string string [, string pattern[, string option]])
1081 : Initialize string and regular expression for search. */
1082 : PHP_FUNCTION(mb_ereg_search_init)
1083 17 : {
1084 : zval **arg_str, **arg_pattern, **arg_options;
1085 17 : OnigSyntaxType *syntax = NULL;
1086 : int option;
1087 :
1088 17 : option = MBSTRG(regex_default_options);
1089 17 : syntax = MBSTRG(regex_default_syntax);
1090 17 : switch (ZEND_NUM_ARGS()) {
1091 : case 1:
1092 0 : if (zend_get_parameters_ex(1, &arg_str) == FAILURE) {
1093 0 : WRONG_PARAM_COUNT;
1094 : }
1095 0 : break;
1096 : case 2:
1097 0 : if (zend_get_parameters_ex(2, &arg_str, &arg_pattern) == FAILURE) {
1098 0 : WRONG_PARAM_COUNT;
1099 : }
1100 0 : break;
1101 : case 3:
1102 17 : if (zend_get_parameters_ex(3, &arg_str, &arg_pattern, &arg_options) == FAILURE) {
1103 0 : WRONG_PARAM_COUNT;
1104 : }
1105 17 : convert_to_string_ex(arg_options);
1106 17 : option = 0;
1107 17 : _php_mb_regex_init_options(Z_STRVAL_PP(arg_options), Z_STRLEN_PP(arg_options), &option, &syntax, NULL);
1108 17 : break;
1109 : default:
1110 0 : WRONG_PARAM_COUNT;
1111 : break;
1112 : }
1113 17 : convert_to_string_ex(arg_str);
1114 17 : if (ZEND_NUM_ARGS() > 1) {
1115 : /* create regex pattern buffer */
1116 17 : convert_to_string_ex(arg_pattern);
1117 :
1118 17 : if ((MBSTRG(search_re) = php_mbregex_compile_pattern(Z_STRVAL_PP(arg_pattern), Z_STRLEN_PP(arg_pattern), option, MBSTRG(current_mbctype), syntax TSRMLS_CC)) == NULL) {
1119 0 : RETURN_FALSE;
1120 : }
1121 : }
1122 :
1123 17 : if (MBSTRG(search_str) != NULL) {
1124 15 : zval_ptr_dtor(&MBSTRG(search_str));
1125 15 : MBSTRG(search_str) = (zval *)NULL;
1126 : }
1127 :
1128 17 : MBSTRG(search_str) = *arg_str;
1129 17 : ZVAL_ADDREF(MBSTRG(search_str));
1130 17 : SEPARATE_ZVAL_IF_NOT_REF(&MBSTRG(search_str));
1131 :
1132 17 : MBSTRG(search_pos) = 0;
1133 :
1134 17 : if (MBSTRG(search_regs) != NULL) {
1135 0 : onig_region_free(MBSTRG(search_regs), 1);
1136 0 : MBSTRG(search_regs) = (OnigRegion *) NULL;
1137 : }
1138 :
1139 17 : RETURN_TRUE;
1140 : }
1141 : /* }}} */
1142 :
1143 : /* {{{ proto array mb_ereg_search_getregs(void)
1144 : Get matched substring of the last time */
1145 : PHP_FUNCTION(mb_ereg_search_getregs)
1146 44 : {
1147 : int n, i, len, beg, end;
1148 : OnigUChar *str;
1149 :
1150 88 : if (MBSTRG(search_regs) != NULL && Z_TYPE_P(MBSTRG(search_str)) == IS_STRING && Z_STRVAL_P(MBSTRG(search_str)) != NULL) {
1151 44 : array_init(return_value);
1152 :
1153 44 : str = (OnigUChar *)Z_STRVAL_P(MBSTRG(search_str));
1154 44 : len = Z_STRLEN_P(MBSTRG(search_str));
1155 44 : n = MBSTRG(search_regs)->num_regs;
1156 132 : for (i = 0; i < n; i++) {
1157 88 : beg = MBSTRG(search_regs)->beg[i];
1158 88 : end = MBSTRG(search_regs)->end[i];
1159 176 : if (beg >= 0 && beg <= end && end <= len) {
1160 88 : add_index_stringl(return_value, i, (char *)&str[beg], end - beg, 1);
1161 : } else {
1162 0 : add_index_bool(return_value, i, 0);
1163 : }
1164 : }
1165 : } else {
1166 0 : RETVAL_FALSE;
1167 : }
1168 44 : }
1169 : /* }}} */
1170 :
1171 : /* {{{ proto int mb_ereg_search_getpos(void)
1172 : Get search start position */
1173 : PHP_FUNCTION(mb_ereg_search_getpos)
1174 44 : {
1175 44 : RETVAL_LONG(MBSTRG(search_pos));
1176 44 : }
1177 : /* }}} */
1178 :
1179 : /* {{{ proto bool mb_ereg_search_setpos(int position)
1180 : Set search start position */
1181 : PHP_FUNCTION(mb_ereg_search_setpos)
1182 0 : {
1183 : zval **arg_pos;
1184 : int n;
1185 :
1186 0 : if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(1, &arg_pos) == FAILURE) {
1187 0 : WRONG_PARAM_COUNT;
1188 : }
1189 0 : convert_to_long_ex(arg_pos);
1190 0 : n = Z_LVAL_PP(arg_pos);
1191 0 : if (n < 0 || (MBSTRG(search_str) != NULL && Z_TYPE_P(MBSTRG(search_str)) == IS_STRING && n >= Z_STRLEN_P(MBSTRG(search_str)))) {
1192 0 : php_error_docref(NULL TSRMLS_CC, E_WARNING, "Position is out of range");
1193 0 : MBSTRG(search_pos) = 0;
1194 0 : RETVAL_FALSE;
1195 : } else {
1196 0 : MBSTRG(search_pos) = n;
1197 0 : RETVAL_TRUE;
1198 : }
1199 : }
1200 : /* }}} */
1201 :
1202 : /* {{{ php_mb_regex_set_options */
1203 : void php_mb_regex_set_options(OnigOptionType options, OnigSyntaxType *syntax, OnigOptionType *prev_options, OnigSyntaxType **prev_syntax TSRMLS_DC)
1204 6 : {
1205 6 : if (prev_options != NULL) {
1206 0 : *prev_options = MBSTRG(regex_default_options);
1207 : }
1208 6 : if (prev_syntax != NULL) {
1209 0 : *prev_syntax = MBSTRG(regex_default_syntax);
1210 : }
1211 6 : MBSTRG(regex_default_options) = options;
1212 6 : MBSTRG(regex_default_syntax) = syntax;
1213 6 : }
1214 : /* }}} */
1215 :
1216 : /* {{{ proto string mb_regex_set_options([string options])
1217 : Set or get the default options for mbregex functions */
1218 : PHP_FUNCTION(mb_regex_set_options)
1219 6 : {
1220 : OnigOptionType opt;
1221 : OnigSyntaxType *syntax;
1222 6 : char *string = NULL;
1223 : int string_len;
1224 : char buf[16];
1225 :
1226 6 : if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "|s",
1227 : &string, &string_len) == FAILURE) {
1228 0 : RETURN_FALSE;
1229 : }
1230 6 : if (string != NULL) {
1231 6 : opt = 0;
1232 6 : syntax = NULL;
1233 6 : _php_mb_regex_init_options(string, string_len, &opt, &syntax, NULL);
1234 6 : php_mb_regex_set_options(opt, syntax, NULL, NULL TSRMLS_CC);
1235 : } else {
1236 0 : opt = MBSTRG(regex_default_options);
1237 0 : syntax = MBSTRG(regex_default_syntax);
1238 : }
1239 6 : _php_mb_regex_get_option_string(buf, sizeof(buf), opt, syntax);
1240 :
1241 6 : RETVAL_STRING(buf, 1);
1242 : }
1243 : /* }}} */
1244 :
1245 : #endif /* HAVE_MBREGEX */
1246 :
1247 : /*
1248 : * Local variables:
1249 : * tab-width: 4
1250 : * c-basic-offset: 4
1251 : * End:
1252 : * vim600: fdm=marker
1253 : * vim: noet sw=4 ts=4
1254 : */
|