cvs: php4 / TODO /ext/standard/ basic_functions.c php_string.h string.c
| From: | Andrei Zmievski | Date: | Tue, 06 Jun 2000 20:42:33 +0000 |
| Subject: | cvs: php4 / TODO /ext/standard/ basic_functions.c php_string.h string.c | ||
| Groups: | php.cvs | ||
| Request: | Send a blank email to php-cvs+get-62@lists.php.net to get a copy of this message | ||
andrei Tue Jun 6 13:42:33 2000 EDT
Modified files:
/php4 TODO
/php4/ext/standard basic_functions.c php_string.h string.c
Log:
@- 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.
Index: php4/TODO
diff -u php4/TODO:1.88 php4/TODO:1.89
--- php4/TODO:1.88 Tue Jun 6 12:46:54 2000
+++ php4/TODO Tue Jun 6 13:42:32 2000
@@ -88,7 +88,6 @@
* 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: php4/ext/standard/basic_functions.c
diff -u php4/ext/standard/basic_functions.c:1.207 php4/ext/standard/basic_functions.c:1.208
--- php4/ext/standard/basic_functions.c:1.207 Tue Jun 6 11:58:15 2000
+++ php4/ext/standard/basic_functions.c Tue Jun 6 13:42:33 2000
@@ -169,6 +169,7 @@
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: php4/ext/standard/php_string.h
diff -u php4/ext/standard/php_string.h:1.20 php4/ext/standard/php_string.h:1.21
--- php4/ext/standard/php_string.h:1.20 Tue Jun 6 11:58:15 2000
+++ php4/ext/standard/php_string.h Tue Jun 6 13:42:33 2000
@@ -29,7 +29,7 @@
*/
-/* $Id: php_string.h,v 1.20 2000/06/06 18:58:15 ccollie Exp $ */
+/* $Id: php_string.h,v 1.21 2000/06/06 20:42:33 andrei Exp $ */
/* Synced with php 3.0 revision 1.43 1999-06-16 [ssb] */
@@ -86,6 +86,7 @@
PHP_FUNCTION(strnatcmp);
PHP_FUNCTION(strnatcasecmp);
PHP_FUNCTION(substr_count);
+PHP_FUNCTION(str_pad);
PHP_FUNCTION(sscanf);
#define strnatcmp(a, b) \
Index: php4/ext/standard/string.c
diff -u php4/ext/standard/string.c:1.124 php4/ext/standard/string.c:1.125
--- php4/ext/standard/string.c:1.124 Tue Jun 6 11:58:15 2000
+++ php4/ext/standard/string.c Tue Jun 6 13:42:33 2000
@@ -18,7 +18,7 @@
+----------------------------------------------------------------------+
*/
-/* $Id: string.c,v 1.124 2000/06/06 18:58:15 ccollie Exp $ */
+/* $Id: string.c,v 1.125 2000/06/06 20:42:33 andrei Exp $ */
/* Synced with php 3.0 revision 1.193 1999-06-16 [ssb] */
@@ -2561,6 +2561,75 @@
}
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);
}
/* }}} */