]> granicus.if.org Git - php/commitdiff
(PHP str_repeat) New function.
authorAndrei Zmievski <andrei@php.net>
Wed, 27 Oct 1999 22:06:05 +0000 (22:06 +0000)
committerAndrei Zmievski <andrei@php.net>
Wed, 27 Oct 1999 22:06:05 +0000 (22:06 +0000)
ext/standard/basic_functions.c
ext/standard/php3_string.h
ext/standard/string.c

index c6dbbcdd5052fe31ab88e10ecde56068f81895a7..5b4f0ba49c2a677e6a071784db56f510d3d403d0 100644 (file)
@@ -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)
index 7ee76fdd28fc5e3fb7141402a8bb19b2bdce6d74..6d39de002eea20eef6ede94693b64c3b5d33e103 100644 (file)
@@ -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);
index 4fb14edb2804822dfe053d0d507f494fbdd7da37..d208c6ad9bdeedba8e93cdd8ca369b8bb223b290 100644 (file)
@@ -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