@@ -2079,6 +2079,22 @@ PHP_FUNCTION(mb_strlen)
20792079}
20802080/* }}} */
20812081
2082+ static void handle_strpos_error (size_t error ) {
2083+ switch (error ) {
2084+ case MBFL_ERROR_NOT_FOUND :
2085+ break ;
2086+ case MBFL_ERROR_ENCODING :
2087+ php_error_docref (NULL , E_WARNING , "Unknown encoding or conversion error" );
2088+ break ;
2089+ case MBFL_ERROR_OFFSET :
2090+ php_error_docref (NULL , E_WARNING , "Offset not contained in string" );
2091+ break ;
2092+ default :
2093+ php_error_docref (NULL , E_WARNING , "Unknown error in mb_strpos" );
2094+ break ;
2095+ }
2096+ }
2097+
20822098/* {{{ proto int mb_strpos(string haystack, string needle [, int offset [, string encoding]])
20832099 Find position of first occurrence of a string within another */
20842100PHP_FUNCTION (mb_strpos )
@@ -2099,34 +2115,11 @@ PHP_FUNCTION(mb_strpos)
20992115 RETURN_FALSE ;
21002116 }
21012117
2102- if (offset != 0 ) {
2103- size_t slen = mbfl_strlen (& haystack );
2104- if (offset < 0 ) {
2105- offset += slen ;
2106- }
2107- if (offset < 0 || offset > slen ) {
2108- php_error_docref (NULL , E_WARNING , "Offset not contained in string" );
2109- RETURN_FALSE ;
2110- }
2111- }
2112-
21132118 n = mbfl_strpos (& haystack , & needle , offset , reverse );
21142119 if (!mbfl_is_error (n )) {
21152120 RETVAL_LONG (n );
21162121 } else {
2117- switch (n ) {
2118- case MBFL_ERROR_NOT_FOUND :
2119- break ;
2120- case MBFL_ERROR_ENCODING :
2121- php_error_docref (NULL , E_WARNING , "Unknown encoding or conversion error" );
2122- break ;
2123- case MBFL_ERROR_OFFSET :
2124- php_error_docref (NULL , E_WARNING , "Offset not contained in string" );
2125- break ;
2126- default :
2127- php_error_docref (NULL , E_WARNING , "Unknown error in mb_strpos" );
2128- break ;
2129- }
2122+ handle_strpos_error (n );
21302123 RETVAL_FALSE ;
21312124 }
21322125}
@@ -2150,19 +2143,11 @@ PHP_FUNCTION(mb_strrpos)
21502143 RETURN_FALSE ;
21512144 }
21522145
2153- if (offset != 0 ) {
2154- size_t haystack_char_len = mbfl_strlen (& haystack );
2155- if ((offset > 0 && offset > haystack_char_len ) ||
2156- (offset < 0 && - offset > haystack_char_len )) {
2157- php_error_docref (NULL , E_WARNING , "Offset is greater than the length of haystack string" );
2158- RETURN_FALSE ;
2159- }
2160- }
2161-
21622146 n = mbfl_strpos (& haystack , & needle , offset , 1 );
21632147 if (!mbfl_is_error (n )) {
21642148 RETVAL_LONG (n );
21652149 } else {
2150+ handle_strpos_error (n );
21662151 RETVAL_FALSE ;
21672152 }
21682153}
0 commit comments