From: Andrei Zmievski Date: Tue, 6 Jun 2000 20:42:33 +0000 (+0000) Subject: @- Added str_pad() for padding a string with an arbitrary string on left or X-Git-Tag: PRE_EIGHT_BYTE_ALLOC_PATCH~59 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=b928bdceb8d3cc3bbb548c5ad4091d10963a2850;p=php @- Added str_pad() for padding a string with an arbitrary string on left or @ 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. --- diff --git a/TODO b/TODO index b64d16ac8f..8afd0b51c0 100644 --- 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(). diff --git a/ext/standard/basic_functions.c b/ext/standard/basic_functions.c index 334cee2a1c..c0eb332275 100644 --- a/ext/standard/basic_functions.c +++ b/ext/standard/basic_functions.c @@ -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) diff --git a/ext/standard/php_string.h b/ext/standard/php_string.h index ea03855cdc..983d098ca3 100644 --- a/ext/standard/php_string.h +++ b/ext/standard/php_string.h @@ -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) \ diff --git a/ext/standard/string.c b/ext/standard/string.c index bc9103c088..cd68ac1116 100644 --- a/ext/standard/string.c +++ b/ext/standard/string.c @@ -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); +} /* }}} */