From b8ecfa18c1cdf1f6c598908edb66c47580dc41fb Mon Sep 17 00:00:00 2001 From: Andrei Zmievski Date: Wed, 27 Oct 1999 22:06:05 +0000 Subject: [PATCH] (PHP str_repeat) New function. --- ext/standard/basic_functions.c | 1 + ext/standard/php3_string.h | 1 + ext/standard/string.c | 44 ++++++++++++++++++++++++++++++++++ 3 files changed, 46 insertions(+) diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index c6dbbcdd50..5b4f0ba49c 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -166,6 +166,7 @@ function_entry basic_functions[] = { PHP_FE(addcslashes, NULL) PHP_FE(chop, NULL) PHP_FE(str_replace, NULL) + PHP_FE(str_repeat, NULL) PHP_FE(chunk_split, NULL) PHP_FE(trim, NULL) PHP_FE(ltrim, NULL) diff --git a/ext/standard/php3_string.h b/ext/standard/php3_string.h index 7ee76fdd28..6d39de002e 100644 --- a/ext/standard/php3_string.h +++ b/ext/standard/php3_string.h @@ -84,6 +84,7 @@ PHP_FUNCTION(parse_str); PHP_FUNCTION(bin2hex); PHP_FUNCTION(similar_text); PHP_FUNCTION(strip_tags); +PHP_FUNCTION(str_repeat); extern PHPAPI char *php_strtoupper(char *s); extern PHPAPI char *php_strtolower(char *s); diff --git a/ext/standard/string.c b/ext/standard/string.c index 4fb14edb28..d208c6ad9b 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -2053,6 +2053,50 @@ PHPAPI void php_strip_tags(char *rbuf, int len, int state, char *allow) { if(allow) efree(tbuf); } +/* {{{ proto string str_repeat(string input, int mult) + Returns the input string repeat mult times */ +PHP_FUNCTION(str_repeat) +{ + zval **input_str; /* Input string */ + zval **mult; /* Multiplier */ + char *result; /* Resulting string */ + int result_len; /* Length of the resulting string */ + int i; + + if (ARG_COUNT(ht) != 2 || getParametersEx(2, &input_str, &mult) == FAILURE) { + WRONG_PARAM_COUNT; + } + + /* Make sure we're dealing with proper types */ + convert_to_string_ex(input_str); + convert_to_long_ex(mult); + + if ((*mult)->value.lval < 1) { + php_error(E_WARNING, "Second argument to %s() has to be greater than 0", + get_active_function_name()); + return; + } + + /* Don't waste our time if it's empty */ + if ((*input_str)->value.str.len == 0) + RETURN_STRINGL(empty_string, 0, 1); + + /* Initialize the result string */ + result_len = (*input_str)->value.str.len * (*mult)->value.lval; + result = (char *)emalloc(result_len + 1); + + /* Copy the input string into the result as many times as necessary */ + for (i=0; i<(*mult)->value.lval; i++) { + memcpy(result + (*input_str)->value.str.len * i, + (*input_str)->value.str.val, + (*input_str)->value.str.len); + } + result[result_len] = '\0'; + + RETURN_STRINGL(result, result_len + 1, 0); +} +/* }}} */ + /* * Local variables: * tab-width: 4 -- 2.40.0