Add #defines for mbfl_strpos error conditions

This commit is contained in:
Nikita Popov 2020-01-24 10:02:41 +01:00
parent e72bf63691
commit 0f6d223ddb
3 changed files with 28 additions and 40 deletions

View File

@ -819,25 +819,13 @@ mbfl_strpos(
size_t result;
mbfl_string _haystack_u8, _needle_u8;
const mbfl_string *haystack_u8, *needle_u8 = NULL;
const unsigned char *u8_tbl;
if (haystack == NULL || haystack->val == NULL || needle == NULL || needle->val == NULL) {
return (size_t) -8;
}
{
const mbfl_encoding *u8_enc = &mbfl_encoding_utf8;
if (u8_enc->mblen_table == NULL) {
return (size_t) -8;
}
u8_tbl = u8_enc->mblen_table;
}
const unsigned char *u8_tbl = mbfl_encoding_utf8.mblen_table;
if (haystack->encoding->no_encoding != mbfl_no_encoding_utf8) {
mbfl_string_init(&_haystack_u8);
haystack_u8 = mbfl_convert_encoding(haystack, &_haystack_u8, &mbfl_encoding_utf8);
if (haystack_u8 == NULL) {
result = (size_t) -4;
result = MBFL_ERROR_ENCODING;
goto out;
}
} else {
@ -848,14 +836,14 @@ mbfl_strpos(
mbfl_string_init(&_needle_u8);
needle_u8 = mbfl_convert_encoding(needle, &_needle_u8, &mbfl_encoding_utf8);
if (needle_u8 == NULL) {
result = (size_t) -4;
result = MBFL_ERROR_ENCODING;
goto out;
}
} else {
needle_u8 = needle;
}
result = (size_t) -1;
result = MBFL_ERROR_NOT_FOUND;
if (haystack_u8->len < needle_u8->len) {
goto out;
}
@ -898,7 +886,7 @@ mbfl_strpos(
p = haystack_u8_val;
while (offset-- > 0) {
if (p >= e) {
result = (size_t) -16;
result = MBFL_ERROR_OFFSET;
goto out;
}
p += u8_tbl[*p];
@ -968,7 +956,7 @@ mbfl_strpos(
while (offset < 0) {
unsigned char c;
if (p <= e) {
result = (size_t) -16;
result = MBFL_ERROR_OFFSET;
goto out;
}
c = *(--p);
@ -983,7 +971,7 @@ mbfl_strpos(
const unsigned char *ee = haystack_u8_val + haystack_u8->len;
while (offset-- > 0) {
if (e >= ee) {
result = (size_t) -16;
result = MBFL_ERROR_OFFSET;
goto out;
}
e += u8_tbl[*e];
@ -1046,9 +1034,6 @@ mbfl_substr_count(
mbfl_convert_filter *filter;
struct collector_strpos_data pc;
if (haystack == NULL || needle == NULL) {
return (size_t) -8;
}
/* needle is converted into wchar */
mbfl_wchar_device_init(&pc.needle);
filter = mbfl_convert_filter_new(
@ -1056,18 +1041,18 @@ mbfl_substr_count(
&mbfl_encoding_wchar,
mbfl_wchar_device_output, 0, &pc.needle);
if (filter == NULL) {
return (size_t) -4;
return MBFL_ERROR_ENCODING;
}
mbfl_convert_filter_feed_string(filter, needle->val, needle->len);
mbfl_convert_filter_flush(filter);
mbfl_convert_filter_delete(filter);
pc.needle_len = pc.needle.pos;
if (pc.needle.buffer == NULL) {
return (size_t) -4;
return MBFL_ERROR_ENCODING;
}
if (pc.needle_len <= 0) {
if (pc.needle_len == 0) {
mbfl_wchar_device_clear(&pc.needle);
return (size_t) -2;
return MBFL_ERROR_EMPTY;
}
/* initialize filter and collector data */
filter = mbfl_convert_filter_new(
@ -1076,13 +1061,13 @@ mbfl_substr_count(
collector_strpos, 0, &pc);
if (filter == NULL) {
mbfl_wchar_device_clear(&pc.needle);
return (size_t) -4;
return MBFL_ERROR_ENCODING;
}
pc.start = 0;
pc.output = 0;
pc.needle_pos = 0;
pc.found_pos = 0;
pc.matched_pos = (size_t) -1;
pc.matched_pos = MBFL_ERROR_NOT_FOUND;
/* feed data */
p = haystack->val;
@ -1090,12 +1075,12 @@ mbfl_substr_count(
if (p != NULL) {
while (n > 0) {
if ((*filter->filter_function)(*p++, filter) < 0) {
pc.matched_pos = (size_t) -4;
pc.matched_pos = MBFL_ERROR_ENCODING;
break;
}
if (pc.matched_pos != (size_t) -1) {
if (pc.matched_pos != MBFL_ERROR_NOT_FOUND) {
++result;
pc.matched_pos = (size_t) -1;
pc.matched_pos = MBFL_ERROR_NOT_FOUND;
pc.needle_pos = 0;
}
n--;

View File

@ -201,8 +201,14 @@ mbfl_strlen(const mbfl_string *string);
MBFLAPI extern size_t
mbfl_oddlen(mbfl_string *string);
#define MBFL_ERROR_NOT_FOUND ((size_t) -1)
#define MBFL_ERROR_ENCODING ((size_t) -4)
#define MBFL_ERROR_EMPTY ((size_t) -8)
#define MBFL_ERROR_OFFSET ((size_t) -16)
/*
* strpos
* strpos.
* Errors: MBFL_ERROR_NOT_FOUND, MBFL_ERROR_ENCODING, MBFL_ERROR_OFFSET
*/
MBFLAPI extern size_t
mbfl_strpos(mbfl_string *haystack, mbfl_string *needle, ssize_t offset, int reverse);

View File

@ -2114,17 +2114,14 @@ PHP_FUNCTION(mb_strpos)
if (!mbfl_is_error(n)) {
RETVAL_LONG(n);
} else {
switch (-n) {
case 1:
switch (n) {
case MBFL_ERROR_NOT_FOUND:
break;
case 2:
php_error_docref(NULL, E_WARNING, "Needle has not positive length");
break;
case 4:
case MBFL_ERROR_ENCODING:
php_error_docref(NULL, E_WARNING, "Unknown encoding or conversion error");
break;
case 8:
php_error_docref(NULL, E_NOTICE, "Argument is empty");
case MBFL_ERROR_OFFSET:
php_error_docref(NULL, E_WARNING, "Offset not contained in string");
break;
default:
php_error_docref(NULL, E_WARNING, "Unknown error in mb_strpos");