From e42f182be3a5beb1da0bcc4c016138503bb19a54 Mon Sep 17 00:00:00 2001 From: Seiji Masugata Date: Wed, 29 Mar 2006 15:47:07 +0000 Subject: [PATCH] added mb_strrchr( ). --- ext/mbstring/mbstring.c | 65 +++++++++++++++++++++++++++++++++++++++++ ext/mbstring/mbstring.h | 1 + 2 files changed, 66 insertions(+) diff --git a/ext/mbstring/mbstring.c b/ext/mbstring/mbstring.c index e66a67ade36..e1b66e1d569 100644 --- a/ext/mbstring/mbstring.c +++ b/ext/mbstring/mbstring.c @@ -174,6 +174,7 @@ static const struct mb_overload_def mb_ovld[] = { {MB_OVERLOAD_STRING, "strpos", "mb_strpos", "mb_orig_strpos"}, {MB_OVERLOAD_STRING, "strrpos", "mb_strrpos", "mb_orig_strrpos"}, {MB_OVERLOAD_STRING, "strstr", "mb_strstr", "mb_orig_strstr"}, + {MB_OVERLOAD_STRING, "strrchr", "mb_strrchr", "mb_orig_strrchr"}, {MB_OVERLOAD_STRING, "substr", "mb_substr", "mb_orig_substr"}, {MB_OVERLOAD_STRING, "strtolower", "mb_strtolower", "mb_orig_strtolower"}, {MB_OVERLOAD_STRING, "strtoupper", "mb_strtoupper", "mb_orig_strtoupper"}, @@ -207,6 +208,7 @@ zend_function_entry mbstring_functions[] = { PHP_FE(mb_strpos, NULL) PHP_FE(mb_strrpos, NULL) PHP_FE(mb_strstr, NULL) + PHP_FE(mb_strrchr, NULL) PHP_FE(mb_substr_count, NULL) PHP_FE(mb_substr, NULL) PHP_FE(mb_strcut, NULL) @@ -1738,6 +1740,69 @@ PHP_FUNCTION(mb_strstr) } /* }}} */ +/* {{{ proto string mb_strrchr(string haystack, string needle[, bool part[, string encoding]]) + Finds the last occurrence of a character in a string within another */ +PHP_FUNCTION(mb_strrchr) +{ + int n, len, mblen; + mbfl_string haystack, needle, result, *ret = NULL;; + char *enc_name = NULL; + int enc_name_len; + zend_bool part = 0; + + mbfl_string_init(&haystack); + mbfl_string_init(&needle); + haystack.no_language = MBSTRG(current_language); + haystack.no_encoding = MBSTRG(current_internal_encoding); + needle.no_language = MBSTRG(current_language); + needle.no_encoding = MBSTRG(current_internal_encoding); + + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "ss|bs", (char **)&haystack.val, &haystack.len, (char **)&needle.val, &needle.len, &part, &enc_name, &enc_name_len) == FAILURE) { + RETURN_FALSE; + } + + if (enc_name != NULL) { + haystack.no_encoding = needle.no_encoding = mbfl_name2no_encoding(enc_name); + if (haystack.no_encoding == mbfl_no_encoding_invalid) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Unknown encoding \"%s\"", enc_name); + RETURN_FALSE; + } + } + + if (haystack.len <= 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING, "Empty haystack"); + RETURN_FALSE; + } + if (needle.len <= 0) { + php_error_docref(NULL TSRMLS_CC, E_WARNING,"Empty needle"); + RETURN_FALSE; + } + n = mbfl_strpos(&haystack, &needle, 0, 1); + if (n >= 0) { + if (part) { + mblen = mbfl_strlen(&haystack); + ret = mbfl_substr(&haystack, &result, 0, n); + if (ret != NULL) { + RETVAL_STRINGL((char *)ret->val, ret->len, 0); + } else { + RETVAL_FALSE; + } + } else { + mblen = mbfl_strlen(&haystack); + len = (mblen - n); + ret = mbfl_substr(&haystack, &result, n, len); + if (ret != NULL) { + RETVAL_STRINGL((char *)ret->val, ret->len, 0); + } else { + RETVAL_FALSE; + } + } + } else { + RETVAL_FALSE; + } +} +/* }}} */ + /* {{{ proto int mb_substr_count(string haystack, string needle [, string encoding]) Count the number of substring occurrences */ PHP_FUNCTION(mb_substr_count) diff --git a/ext/mbstring/mbstring.h b/ext/mbstring/mbstring.h index 6a60f350dff..5d6c75466d0 100644 --- a/ext/mbstring/mbstring.h +++ b/ext/mbstring/mbstring.h @@ -105,6 +105,7 @@ PHP_FUNCTION(mb_strlen); PHP_FUNCTION(mb_strpos); PHP_FUNCTION(mb_strrpos); PHP_FUNCTION(mb_strstr); +PHP_FUNCTION(mb_strrchr); PHP_FUNCTION(mb_substr_count); PHP_FUNCTION(mb_substr); PHP_FUNCTION(mb_strcut);