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

View File

@ -201,8 +201,14 @@ mbfl_strlen(const mbfl_string *string);
MBFLAPI extern size_t MBFLAPI extern size_t
mbfl_oddlen(mbfl_string *string); 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 MBFLAPI extern size_t
mbfl_strpos(mbfl_string *haystack, mbfl_string *needle, ssize_t offset, int reverse); 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)) { if (!mbfl_is_error(n)) {
RETVAL_LONG(n); RETVAL_LONG(n);
} else { } else {
switch (-n) { switch (n) {
case 1: case MBFL_ERROR_NOT_FOUND:
break; break;
case 2: case MBFL_ERROR_ENCODING:
php_error_docref(NULL, E_WARNING, "Needle has not positive length");
break;
case 4:
php_error_docref(NULL, E_WARNING, "Unknown encoding or conversion error"); php_error_docref(NULL, E_WARNING, "Unknown encoding or conversion error");
break; break;
case 8: case MBFL_ERROR_OFFSET:
php_error_docref(NULL, E_NOTICE, "Argument is empty"); php_error_docref(NULL, E_WARNING, "Offset not contained in string");
break; break;
default: default:
php_error_docref(NULL, E_WARNING, "Unknown error in mb_strpos"); php_error_docref(NULL, E_WARNING, "Unknown error in mb_strpos");