php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login | |
Patch feature-49007.diff for Strings related Bug #49007Patch version 2011-02-26 20:52 UTC Return to Bug #49007 | Download this patchThis patch is obsolete Obsoleted by patches: Patch Revisions:Developer: jthijssen@noxlogic.nlIndex: ext/standard/string.c =================================================================== --- ext/standard/string.c (revision 308566) +++ ext/standard/string.c (working copy) @@ -715,11 +715,12 @@ * mode 3 : trim left and right * what indicates which chars are to be trimmed. NULL->default (' \t\n\r\v\0') */ -PHPAPI char *php_trim(char *c, int len, char *what, int what_len, zval *return_value, int mode TSRMLS_DC) +PHPAPI char *php_trim(char *c, int len, char *what, int what_len, int limit, zval *return_value, int mode TSRMLS_DC) { register int i; int trimmed = 0; char mask[256]; + int tlimit; if (what) { php_charmask((unsigned char*)what, what_len, mask TSRMLS_CC); @@ -728,9 +729,10 @@ } if (mode & 1) { - for (i = 0; i < len; i++) { + for (tlimit = limit, i = 0; tlimit && i < len; i++) { if (mask[(unsigned char)c[i]]) { trimmed++; + tlimit--; } else { break; } @@ -739,9 +741,10 @@ c += trimmed; } if (mode & 2) { - for (i = len - 1; i >= 0; i--) { + for (tlimit = limit, i = len - 1; tlimit && i >= 0; i--) { if (mask[(unsigned char)c[i]]) { len--; + tlimit--; } else { break; } @@ -765,12 +768,13 @@ char *str; char *what = NULL; int str_len, what_len = 0; + int limit = -1; - if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|s", &str, &str_len, &what, &what_len) == FAILURE) { + if (zend_parse_parameters(ZEND_NUM_ARGS() TSRMLS_CC, "s|sl", &str, &str_len, &what, &what_len, &limit) == FAILURE) { return; } - php_trim(str, str_len, what, what_len, return_value, mode TSRMLS_CC); + php_trim(str, str_len, what, what_len, limit, return_value, mode TSRMLS_CC); } /* }}} */ Index: ext/standard/php_string.h =================================================================== --- ext/standard/php_string.h (revision 308566) +++ ext/standard/php_string.h (working copy) @@ -132,7 +132,7 @@ int needle_len, char *str, int str_len, int *_new_length, int case_sensitivity, int *replace_count); PHPAPI char *php_str_to_str(char *haystack, int length, char *needle, int needle_len, char *str, int str_len, int *_new_length); -PHPAPI char *php_trim(char *c, int len, char *what, int what_len, zval *return_value, int mode TSRMLS_DC); +PHPAPI char *php_trim(char *c, int len, char *what, int what_len, int limit, zval *return_value, int mode TSRMLS_DC); PHPAPI size_t php_strip_tags(char *rbuf, int len, int *state, char *allow, int allow_len); PHPAPI size_t php_strip_tags_ex(char *rbuf, int len, int *stateptr, char *allow, int allow_len, zend_bool allow_tag_spaces); PHPAPI int php_char_to_str_ex(char *str, uint len, char from, char *to, int to_len, zval *result, int case_sensitivity, int *replace_count); Index: ext/standard/tests/strings/trim1.phpt =================================================================== Binary files ext/standard/tests/strings/trim1.phpt (revision 308566) and ext/standard/tests/strings/trim1.phpt (working copy) differ Index: ext/standard/tests/strings/trim_basic.phpt =================================================================== --- ext/standard/tests/strings/trim_basic.phpt (revision 308566) +++ ext/standard/tests/strings/trim_basic.phpt (working copy) @@ -3,7 +3,7 @@ --FILE-- <?php -/* Prototype : string trim ( string $str [, string $charlist ] ) +/* Prototype : string trim ( string $str [, string $charlist [, int limit = -1 ] ] ) * Description: Strip whitespace (or other characters) from the beginning and end of a string. * Source code: ext/standard/string.c */ Index: ext/standard/tests/strings/trim_error.phpt =================================================================== --- ext/standard/tests/strings/trim_error.phpt (revision 308566) +++ ext/standard/tests/strings/trim_error.phpt (working copy) @@ -3,7 +3,7 @@ --FILE-- <?php -/* Prototype : string trim ( string $str [, string $charlist ] ) +/* Prototype : string trim ( string $str [, string $charlist [, int limit = -1 ] ] ) * Description: Strip whitespace (or other characters) from the begining and end of a string. * Source code: ext/standard/string.c */ @@ -16,7 +16,7 @@ echo "\n-- Testing trim() function with more than expected no. of arguments --\n"; $extra_arg = 10; -var_dump( trim("Hello World", "Heo", $extra_arg) ); +var_dump( trim("Hello World", "Heo", -1, $extra_arg) ); $hello = " Hello World\n"; @@ -38,7 +38,7 @@ -- Testing trim() function with more than expected no. of arguments -- -Warning: trim() expects at most 2 parameters, 3 given in %s on line %d +Warning: trim() expects at most 3 parameters, 4 given in %s on line %d NULL -- Test trim function with various invalid charlists -- Index: ext/standard/tests/strings/trim_variation1.phpt =================================================================== --- ext/standard/tests/strings/trim_variation1.phpt (revision 308566) +++ ext/standard/tests/strings/trim_variation1.phpt (working copy) @@ -3,7 +3,7 @@ --FILE-- <?php -/* Prototype : string trim ( string $str [, string $charlist ] ) +/* Prototype : string trim ( string $str [, string $charlist [, int limit = -1 ] ] ) * Description: Strip whitespace (or other characters) from the begining and end of a string. * Source code: ext/standard/string.c */ Index: ext/standard/tests/strings/trim_variation2.phpt =================================================================== --- ext/standard/tests/strings/trim_variation2.phpt (revision 308566) +++ ext/standard/tests/strings/trim_variation2.phpt (working copy) @@ -3,7 +3,7 @@ --FILE-- <?php -/* Prototype : string trim ( string $str [, string $charlist ] ) +/* Prototype : string trim ( string $str [, string $charlist [, int limit = -1 ] ] ) * Description: Strip whitespace (or other characters) from the begining and end of a string. * Source code: ext/standard/string.c */ Index: ext/standard/tests/strings/trim_variation3.phpt =================================================================== --- ext/standard/tests/strings/trim_variation3.phpt (revision 0) +++ ext/standard/tests/strings/trim_variation3.phpt (revision 0) @@ -0,0 +1,46 @@ +--TEST-- +Test trim() function : usage variations - test limit argument +--FILE-- +<?php + +/* Prototype : string trim ( string $str [, string $charlist [, int limit = -1 ] ] ) + * Description: Strip whitespace (or other characters) from the begining and end of a string. + * Source code: ext/standard/string.c +*/ + +echo "*** Testing trim() function: with different limit arguments ***\n"; + +var_dump (trim(" hello ", " \n\r\t\v\0", 1)); +var_dump (trim(" hello ", " \n\r\t\v\0", 5)); +var_dump (trim(" hello ", " \n\r\t\v\0", 0)); +var_dump (trim(" hello ", " \n\r\t\v\0", -1)); + +var_dump (ltrim(" hello ", " \n\r\t\v\0", 1)); +var_dump (ltrim(" hello ", " \n\r\t\v\0", 5)); +var_dump (ltrim(" hello ", " \n\r\t\v\0", 0)); +var_dump (ltrim(" hello ", " \n\r\t\v\0", -1)); + +var_dump (rtrim(" hello ", " \n\r\t\v\0", 1)); +var_dump (rtrim(" hello ", " \n\r\t\v\0", 5)); +var_dump (rtrim(" hello ", " \n\r\t\v\0", 0)); +var_dump (rtrim(" hello ", " \n\r\t\v\0", -1)); + + + +?> +===DONE=== +--EXPECT-- +*** Testing trim() function: with different limit arguments *** +string(7) " hello " +string(5) "hello" +string(9) " hello " +string(5) "hello" +string(8) " hello " +string(7) "hello " +string(9) " hello " +string(7) "hello " +string(8) " hello " +string(7) " hello" +string(9) " hello " +string(7) " hello" +===DONE=== \ No newline at end of file |
Copyright © 2001-2025 The PHP Group All rights reserved. |
Last updated: Sat Jan 04 22:01:28 2025 UTC |