]> granicus.if.org Git - php/commitdiff
@- Added str_pad() for padding a string with an arbitrary string on left or
authorAndrei Zmievski <andrei@php.net>
Tue, 6 Jun 2000 20:42:33 +0000 (20:42 +0000)
committerAndrei Zmievski <andrei@php.net>
Tue, 6 Jun 2000 20:42:33 +0000 (20:42 +0000)
@  right. (Andrei)

Added str_pad() for padding a string with an arbitrary string on left or right.

# With all those macros, the code is starting to eerily resemble Perl's guts.

TODO
ext/standard/basic_functions.c
ext/standard/php_string.h
ext/standard/string.c

diff --git a/TODO b/TODO
index b64d16ac8f3b6d3fb235075817ff8802b41c5000..8afd0b51c03bdf0f07d637148bade179fc9f4e35 100644 (file)
--- a/TODO
+++ b/TODO
@@ -88,7 +88,6 @@ ext/standard
     * possibly modify parsing of GPC data to automatically create arrays if
       variable name is seen more than once.
     * implement regex-cache for url-functions.
-    * strpad(). (Andrei)
     * strcase_replace(). (Andrei)
     * move socket related functions to fsock.c.
     * get_defined_funcs(), get_defined_vars().
index 334cee2a1ce4f86e321d2b029e0a6225d68e2d22..c0eb3322750dedffb119a2f952250369703bcc02 100644 (file)
@@ -169,6 +169,7 @@ function_entry basic_functions[] = {
        PHP_FE(chr,                                                                             NULL)
        PHP_FE(ord,                                                                             NULL)
        PHP_FE(parse_str,                                                               NULL)
+       PHP_FE(str_pad,                                                                 NULL)
        PHP_FALIAS(rtrim,                       chop,                           NULL)
        PHP_FALIAS(strchr,                      strstr,                         NULL)
        PHP_NAMED_FE(sprintf,           PHP_FN(user_sprintf),   NULL)
index ea03855cdcf091465800501d6e8f79e85ba18b8b..983d098ca390b61eeb18cfa4e4a2059da6efbf34 100644 (file)
@@ -86,6 +86,7 @@ PHP_FUNCTION(substr_replace);
 PHP_FUNCTION(strnatcmp);
 PHP_FUNCTION(strnatcasecmp);
 PHP_FUNCTION(substr_count);
+PHP_FUNCTION(str_pad);
 PHP_FUNCTION(sscanf);
 
 #define strnatcmp(a, b) \
index bc9103c08834e8e018abee36d0a1fdcc8250ffdb..cd68ac111650bf260bdb2154e2dfba8e566ac4d8 100644 (file)
@@ -2562,6 +2562,75 @@ PHP_FUNCTION(substr_count)
 
        RETURN_LONG(count);
 }
+/* }}} */      
+
+
+/* {{{ proto string str_pad(string input, int pad_length [, string pad_string])
+   Returns input string padded on the left or right to specified length with pad_string */
+PHP_FUNCTION(str_pad)
+{
+       zval **input,                           /* Input string */
+                **pad_length,                  /* Length to pad to (positive/negative) */
+                **pad_string;                  /* Padding string */
+       int        pad_length_abs;              /* Absolute padding length */
+       char  *result = NULL;           /* Resulting string */
+       int        result_len = 0;              /* Length of the resulting string */
+       char  *pad_str_val = " ";       /* Pointer to padding string */
+       int    pad_str_len = 1;         /* Length of the padding string */
+       int        i;
+
+       if (ZEND_NUM_ARGS() < 2 || ZEND_NUM_ARGS() > 3 ||
+               zend_get_parameters_ex(ZEND_NUM_ARGS(), &input, &pad_length, &pad_string) == FAILURE) {
+               WRONG_PARAM_COUNT;
+       }
+
+       /* Perform initial conversion to expected data types. */
+       convert_to_string_ex(input);
+       convert_to_long_ex(pad_length);
+
+       pad_length_abs = abs(Z_LVAL_PP(pad_length));
+
+       /* If resulting string turns out to be shorter than input string,
+          we simply copy the input and return. */
+       if (pad_length_abs <= Z_STRLEN_PP(input)) {
+               *return_value = **input;
+               zval_copy_ctor(return_value);
+               return;
+       }
+
+       /* Setup the padding string values if specified. */
+       if (ZEND_NUM_ARGS() > 2) {
+               convert_to_string_ex(pad_string);
+               if (Z_STRLEN_PP(pad_string) == 0) {
+                       php_error(E_WARNING, "Padding string cannot be empty in %s()",
+                                         get_active_function_name());
+                       return;
+               }
+               pad_str_val = Z_STRVAL_PP(pad_string);
+               pad_str_len = Z_STRLEN_PP(pad_string);
+       }
+
+       result = (char *)emalloc(pad_length_abs);
+
+       /* If positive, we pad on the right and copy the input now. */
+       if (Z_LVAL_PP(pad_length) > 0) {
+               memcpy(result, Z_STRVAL_PP(input), Z_STRLEN_PP(input));
+               result_len = Z_STRLEN_PP(input);
+       }
+
+       /* Loop through pad string, copying it into result. */
+       for (i = 0; i < pad_length_abs - Z_STRLEN_PP(input); i++) {
+               result[result_len++] = pad_str_val[i % pad_str_len];
+       }
+
+       /* If negative, we've padded on the left, and copy the input now. */
+       if (Z_LVAL_PP(pad_length) < 0) {
+               memcpy(result + result_len, Z_STRVAL_PP(input), Z_STRLEN_PP(input));
+               result_len += Z_STRLEN_PP(input);
+       }
+
+       RETURN_STRINGL(result, result_len, 0);
+}
 /* }}} */