summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Zmievski <andrei@php.net>1999-10-27 22:06:05 +0000
committerAndrei Zmievski <andrei@php.net>1999-10-27 22:06:05 +0000
commitb8ecfa18c1cdf1f6c598908edb66c47580dc41fb (patch)
treeac8c79c46a60058a250dc2801f6585cadec513e2
parentea880d2b86729f8dc68c5cb4d7572463d089d9b1 (diff)
downloadphp-git-b8ecfa18c1cdf1f6c598908edb66c47580dc41fb.tar.gz
(PHP str_repeat) New function.
-rw-r--r--ext/standard/basic_functions.c1
-rw-r--r--ext/standard/php3_string.h1
-rw-r--r--ext/standard/string.c44
3 files changed, 46 insertions, 0 deletions
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