Skip to content

Commit 8521176

Browse files
author
Moriyoshi Koizumi
committed
Changed mb_regex_set_options() more informative
1 parent 8d9963b commit 8521176

File tree

1 file changed

+83
-6
lines changed

1 file changed

+83
-6
lines changed

ext/mbstring/php_mbregex.c

Lines changed: 83 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,73 @@ php_mbregex_compile_pattern(mb_regex_t *pre, const char *pattern, int patlen, in
169169
return res;
170170
}
171171

172+
static size_t
173+
_php_mb_regex_get_option_string(char *str, size_t len, int option)
174+
{
175+
size_t len_left = len;
176+
size_t len_req = 0;
177+
char *p = str;
178+
179+
180+
if ((option & MBRE_OPTION_IGNORECASE) != 0) {
181+
if (len_left > 0) {
182+
--len_left;
183+
*(p++) = 'i';
184+
}
185+
++len_req;
186+
}
187+
188+
if ((option & MBRE_OPTION_EXTENDED) != 0) {
189+
if (len_left > 0) {
190+
--len_left;
191+
*(p++) = 'x';
192+
}
193+
++len_req;
194+
}
195+
196+
if ((option & MBRE_OPTION_POSIXLINE) == MBRE_OPTION_POSIXLINE) {
197+
if (len_left > 0) {
198+
--len_left;
199+
*(p++) = 'p';
200+
}
201+
++len_req;
202+
} else {
203+
if ((option & MBRE_OPTION_MULTILINE) != 0) {
204+
if (len_left > 0) {
205+
--len_left;
206+
*(p++) = 'm';
207+
}
208+
++len_req;
209+
}
210+
211+
if ((option & MBRE_OPTION_SINGLELINE) != 0) {
212+
if (len_left > 0) {
213+
--len_left;
214+
*(p++) = 's';
215+
}
216+
++len_req;
217+
}
218+
}
219+
if ((option & MBRE_OPTION_LONGEST) != 0) {
220+
if (len_left > 0) {
221+
--len_left;
222+
*(p++) = 'l';
223+
}
224+
++len_req;
225+
}
226+
if (len_left > 0) {
227+
--len_left;
228+
*(p++) = '\0';
229+
}
230+
231+
++len_req;
232+
if (len < len_req) {
233+
return len_req;
234+
}
235+
236+
return 0;
237+
}
238+
172239
static void
173240
_php_mb_regex_init_options(const char *parg, int narg, int *option, int *eval)
174241
{
@@ -974,18 +1041,28 @@ PHPAPI int php_mb_regex_set_options_by_string( const char *opt_str, int len TSRM
9741041
}
9751042
/* }}} */
9761043

977-
/* {{{ proto bool mb_regex_set_options([string options])
978-
Set the default options for mbregex functions */
1044+
/* {{{ proto string mb_regex_set_options([string options])
1045+
Set or get the default options for mbregex functions */
9791046
PHP_FUNCTION(mb_regex_set_options)
9801047
{
981-
char *string;
1048+
int opt;
1049+
char *string = NULL;
9821050
int string_len;
983-
if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "s",
1051+
char buf[16];
1052+
1053+
if ( zend_parse_parameters( ZEND_NUM_ARGS() TSRMLS_CC, "|s",
9841054
&string, &string_len ) == FAILURE ) {
9851055
RETURN_FALSE;
9861056
}
987-
php_mb_regex_set_options_by_string( (const char*)string, string_len TSRMLS_CC);
988-
RETURN_TRUE;
1057+
if (string != NULL) {
1058+
opt = php_mb_regex_set_options_by_string( (const char*)string,
1059+
string_len TSRMLS_CC );
1060+
} else {
1061+
opt = MBSTRG(regex_default_options);
1062+
}
1063+
_php_mb_regex_get_option_string(buf, sizeof(buf), opt);
1064+
1065+
RETVAL_STRING(buf, 1);
9891066
}
9901067
/* }}} */
9911068

0 commit comments

Comments
 (0)