Skip to content

Commit 7af24ea

Browse files
Christian Schneidernikic
Christian Schneider
authored andcommitted
Allow empty needle in grapheme_str*pos, grapheme_str*str
For consistency with str* and mb_str* functions. Closes GH-6245. Closes php/php-tasks#20.
1 parent 2c1b5c4 commit 7af24ea

File tree

3 files changed

+59
-101
lines changed

3 files changed

+59
-101
lines changed

ext/intl/grapheme/grapheme_string.c

Lines changed: 0 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -124,11 +124,6 @@ PHP_FUNCTION(grapheme_strpos)
124124

125125
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
126126

127-
if (needle_len == 0) {
128-
zend_argument_value_error(2, "cannot be empty");
129-
RETURN_THROWS();
130-
}
131-
132127
if (offset >= 0 && grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0) {
133128
/* quick check to see if the string might be there
134129
* I realize that 'offset' is 'grapheme count offset' but will work in spite of that
@@ -178,11 +173,6 @@ PHP_FUNCTION(grapheme_stripos)
178173

179174
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
180175

181-
if (needle_len == 0) {
182-
zend_argument_value_error(2, "cannot be empty");
183-
RETURN_THROWS();
184-
}
185-
186176
is_ascii = ( grapheme_ascii_check((unsigned char*)haystack, haystack_len) >= 0 );
187177

188178
if ( is_ascii ) {
@@ -244,11 +234,6 @@ PHP_FUNCTION(grapheme_strrpos)
244234

245235
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
246236

247-
if (needle_len == 0) {
248-
zend_argument_value_error(2, "cannot be empty");
249-
RETURN_THROWS();
250-
}
251-
252237
is_ascii = grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0;
253238

254239
if ( is_ascii ) {
@@ -304,11 +289,6 @@ PHP_FUNCTION(grapheme_strripos)
304289

305290
/* the offset is 'grapheme count offset' so it still might be invalid - we'll check it later */
306291

307-
if (needle_len == 0) {
308-
zend_argument_value_error(2, "cannot be empty");
309-
RETURN_THROWS();
310-
}
311-
312292
is_ascii = grapheme_ascii_check((unsigned char *)haystack, haystack_len) >= 0;
313293

314294
if ( is_ascii ) {
@@ -572,11 +552,6 @@ static void strstr_common_handler(INTERNAL_FUNCTION_PARAMETERS, int f_ignore_cas
572552
RETURN_THROWS();
573553
}
574554

575-
if (needle_len == 0) {
576-
zend_argument_value_error(2, "cannot be empty");
577-
RETURN_THROWS();
578-
}
579-
580555
if ( !f_ignore_case ) {
581556

582557
/* ASCII optimization: quick check to see if the string might be there */

ext/intl/grapheme/grapheme_util.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,17 @@ int32_t grapheme_strpos_utf16(char *haystack, size_t haystack_len, char *needle,
126126
ubrk_setText(bi, uhaystack, uhaystack_len, &status);
127127
STRPOS_CHECK_STATUS(status, "Failed to set up iterator");
128128

129+
if (uneedle_len == 0) {
130+
offset_pos = grapheme_get_haystack_offset(bi, last && offset >= 0 ? uhaystack_len : offset);
131+
if (offset_pos == -1) {
132+
zend_argument_value_error(3, "must be contained in argument #1 ($haystack)");
133+
ret_pos = -1;
134+
goto finish;
135+
}
136+
ret_pos = offset_pos;
137+
goto finish;
138+
}
139+
129140
status = U_ZERO_ERROR;
130141
src = usearch_open(uneedle, uneedle_len, uhaystack, uhaystack_len, "", bi, &status);
131142
STRPOS_CHECK_STATUS(status, "Error creating search object");

ext/intl/tests/grapheme_empty.phpt

Lines changed: 48 additions & 76 deletions
Original file line numberDiff line numberDiff line change
@@ -7,82 +7,54 @@ Test grapheme_strpos-alike functions with empty needle
77

88
ini_set("intl.error_level", E_WARNING);
99

10-
try {
11-
var_dump(grapheme_strpos("abc", "", -1));
12-
} catch (ValueError $exception) {
13-
echo $exception->getMessage() . "\n";
14-
}
15-
16-
try {
17-
var_dump(grapheme_strpos("abc", ""));
18-
} catch (ValueError $exception) {
19-
echo $exception->getMessage() . "\n";
20-
}
21-
22-
try {
23-
var_dump(grapheme_strpos("abc", "", -1));
24-
} catch (ValueError $exception) {
25-
echo $exception->getMessage() . "\n";
26-
}
27-
28-
try {
29-
var_dump(grapheme_stripos("abc", ""));
30-
} catch (ValueError $exception) {
31-
echo $exception->getMessage() . "\n";
32-
}
33-
34-
try {
35-
var_dump(grapheme_stripos("abc", "", -1));
36-
} catch (ValueError $exception) {
37-
echo $exception->getMessage() . "\n";
38-
}
39-
40-
try {
41-
var_dump(grapheme_strrpos("abc", ""));
42-
} catch (ValueError $exception) {
43-
echo $exception->getMessage() . "\n";
44-
}
45-
46-
try {
47-
var_dump(grapheme_strrpos("abc", "", -1));
48-
} catch (ValueError $exception) {
49-
echo $exception->getMessage() . "\n";
50-
}
51-
52-
try {
53-
var_dump(grapheme_strripos("abc", ""));
54-
} catch (ValueError $exception) {
55-
echo $exception->getMessage() . "\n";
56-
}
57-
58-
try {
59-
var_dump(grapheme_strripos("abc", "", 1));
60-
} catch (ValueError $exception) {
61-
echo $exception->getMessage() . "\n";
62-
}
63-
64-
try {
65-
var_dump(grapheme_strstr("abc", ""));
66-
} catch (ValueError $exception) {
67-
echo $exception->getMessage() . "\n";
68-
}
69-
70-
try {
71-
var_dump(grapheme_stristr("abc", ""));
72-
} catch (ValueError $exception) {
73-
echo $exception->getMessage() . "\n";
74-
}
10+
var_dump(grapheme_strpos("abc", ""));
11+
var_dump(grapheme_strpos("abc", "", -1));
12+
var_dump(grapheme_stripos("abc", ""));
13+
var_dump(grapheme_stripos("abc", "", -1));
14+
var_dump(grapheme_strrpos("abc", ""));
15+
var_dump(grapheme_strrpos("abc", "", -1));
16+
var_dump(grapheme_strripos("abc", ""));
17+
var_dump(grapheme_strripos("abc", "", 1));
18+
var_dump(grapheme_strstr("abc", ""));
19+
var_dump(grapheme_strstr("abc", "", true));
20+
var_dump(grapheme_stristr("abc", ""));
21+
var_dump(grapheme_stristr("abc", "", true));
22+
var_dump(grapheme_strpos("äbc", ""));
23+
var_dump(grapheme_strpos("äbc", "", -1));
24+
var_dump(grapheme_stripos("äbc", ""));
25+
var_dump(grapheme_stripos("äbc", "", -1));
26+
var_dump(grapheme_strrpos("äbc", ""));
27+
var_dump(grapheme_strrpos("äbc", "", -1));
28+
var_dump(grapheme_strripos("äbc", ""));
29+
var_dump(grapheme_strripos("äbc", "", 1));
30+
var_dump(grapheme_strstr("äbc", ""));
31+
var_dump(grapheme_strstr("äbc", "", true));
32+
var_dump(grapheme_stristr("äbc", ""));
33+
var_dump(grapheme_stristr("äbc", "", true));
7534

7635
?>
7736
--EXPECT--
78-
grapheme_strpos(): Argument #2 ($needle) cannot be empty
79-
grapheme_strpos(): Argument #2 ($needle) cannot be empty
80-
grapheme_strpos(): Argument #2 ($needle) cannot be empty
81-
grapheme_stripos(): Argument #2 ($needle) cannot be empty
82-
grapheme_stripos(): Argument #2 ($needle) cannot be empty
83-
grapheme_strrpos(): Argument #2 ($needle) cannot be empty
84-
grapheme_strrpos(): Argument #2 ($needle) cannot be empty
85-
grapheme_strripos(): Argument #2 ($needle) cannot be empty
86-
grapheme_strripos(): Argument #2 ($needle) cannot be empty
87-
grapheme_strstr(): Argument #2 ($needle) cannot be empty
88-
grapheme_stristr(): Argument #2 ($needle) cannot be empty
37+
int(0)
38+
int(2)
39+
int(0)
40+
int(2)
41+
int(3)
42+
int(2)
43+
int(3)
44+
int(3)
45+
string(3) "abc"
46+
string(0) ""
47+
string(3) "abc"
48+
string(0) ""
49+
int(0)
50+
int(2)
51+
int(0)
52+
int(2)
53+
int(3)
54+
int(2)
55+
int(3)
56+
int(3)
57+
string(4) "äbc"
58+
string(0) ""
59+
string(4) "äbc"
60+
string(0) ""

0 commit comments

Comments
 (0)