diff options
author | Nicolas Oelgart <nicolas.oelgart@atrapalo.com> | 2020-02-17 14:29:12 +0100 |
---|---|---|
committer | Nikita Popov <nikita.ppv@gmail.com> | 2020-02-25 10:26:03 +0100 |
commit | aa79a22d32f31dbc5343f8191e925aa07447c3ec (patch) | |
tree | 85a3cbefa474cb428161242ad54c1631a654c980 /ext/pcre/php_pcre.c | |
parent | bb6e2a1615a54bc2986c782a2541289fd33a1bbb (diff) | |
download | php-git-aa79a22d32f31dbc5343f8191e925aa07447c3ec.tar.gz |
Add preg_last_error_msg() function
Provides the last PCRE error as a human-readable message, similar
to functionality existing in other extensions, such as
json_last_error_msg().
Closes GH-5185.
Diffstat (limited to 'ext/pcre/php_pcre.c')
-rw-r--r-- | ext/pcre/php_pcre.c | 49 |
1 files changed, 38 insertions, 11 deletions
diff --git a/ext/pcre/php_pcre.c b/ext/pcre/php_pcre.c index edf48ca1f4..fdc1b1b856 100644 --- a/ext/pcre/php_pcre.c +++ b/ext/pcre/php_pcre.c @@ -52,17 +52,6 @@ struct _pcre_cache_entry { uint32_t refcount; }; -enum { - PHP_PCRE_NO_ERROR = 0, - PHP_PCRE_INTERNAL_ERROR, - PHP_PCRE_BACKTRACK_LIMIT_ERROR, - PHP_PCRE_RECURSION_LIMIT_ERROR, - PHP_PCRE_BAD_UTF8_ERROR, - PHP_PCRE_BAD_UTF8_OFFSET_ERROR, - PHP_PCRE_JIT_STACKLIMIT_ERROR -}; - - PHPAPI ZEND_DECLARE_MODULE_GLOBALS(pcre) #ifdef HAVE_PCRE_JIT_SUPPORT @@ -138,6 +127,33 @@ static void pcre_handle_exec_error(int pcre_code) /* {{{ */ } /* }}} */ +static const char *php_pcre_get_error_msg(php_pcre_error_code error_code) /* {{{ */ +{ + switch (error_code) { + case PHP_PCRE_NO_ERROR: + return "No error"; + case PHP_PCRE_INTERNAL_ERROR: + return "Internal error"; + case PHP_PCRE_BAD_UTF8_ERROR: + return "Malformed UTF-8 characters, possibly incorrectly encoded"; + case PHP_PCRE_BAD_UTF8_OFFSET_ERROR: + return "The offset did not correspond to the beginning of a valid UTF-8 code point"; + case PHP_PCRE_BACKTRACK_LIMIT_ERROR: + return "Backtrack limit exhausted"; + case PHP_PCRE_RECURSION_LIMIT_ERROR: + return "Recursion limit exhausted"; + +#ifdef HAVE_PCRE_JIT_SUPPORT + case PHP_PCRE_JIT_STACKLIMIT_ERROR: + return "JIT stack limit exhausted"; +#endif + + default: + return "Unknown error"; + } +} +/* }}} */ + static void php_free_pcre_cache(zval *data) /* {{{ */ { pcre_cache_entry *pce = (pcre_cache_entry *) Z_PTR_P(data); @@ -2957,6 +2973,16 @@ static PHP_FUNCTION(preg_last_error) } /* }}} */ +/* {{{ proto string preg_last_error_msg() + Returns the error message of the last regexp execution. */ +static PHP_FUNCTION(preg_last_error_msg) +{ + ZEND_PARSE_PARAMETERS_NONE(); + + RETURN_STRING(php_pcre_get_error_msg(PCRE_G(error_code))); +} +/* }}} */ + /* {{{ module definition structures */ static const zend_function_entry pcre_functions[] = { @@ -2970,6 +2996,7 @@ static const zend_function_entry pcre_functions[] = { PHP_FE(preg_quote, arginfo_preg_quote) PHP_FE(preg_grep, arginfo_preg_grep) PHP_FE(preg_last_error, arginfo_preg_last_error) + PHP_FE(preg_last_error_msg, arginfo_preg_last_error_msg) PHP_FE_END }; |