Implode() FR and Patch

From: Date: Sat, 06 Dec 2008 02:21:21 +0000
Subject: Implode() FR and Patch
Groups: php.internals 
Request: Send a blank email to internals+get-42119@lists.php.net to get a copy of this message
Hello There, I would like to propose a third argument to implode(), named skip_empty, that will cause empty elements to be ignored when generating the implode string. By empty I mean everything that converts to an empty string such as '', false, null, etc. For example: <?php $a = array(1,'',2,null,3,0,4,true,5,false,false); echo implode(',', $a); // old behavior echo "\n"; echo implode(',', $a, true); // new feature echo "\n"; echo $foo; echo "\n"; ?> Will output: 1,,2,,3,0,4,1,5,, 1,2,3,0,4,1,5 Obviously the new parameter defaults to 'false' for backwards compatibility. The patch file is attached for evaluation, review, feedback etc. Please keep in mind that this is my first time playing with PHP source so expect flaws on my code. I did a few simple tests (using time, if someone has a non buggy valgrind for OS X please let me know) against 5.3 CVS (named php53 in examples) and my patched version (named php+ in examples). The PHP scripts are right below the tests. Looking at the results I would say there is almost no performance loss. Before I place a feature request at PHP.net I would love to hear some feedback here, or even a yes/no for this feature. Best Regards, Igor Feghali. === TEST 1 === $ time ./php53 /tmp/more_small.php real 0m0.257s user 0m0.223s sys 0m0.032s $ time ./php53 /tmp/more_small.php real 0m0.258s user 0m0.224s sys 0m0.032s $ time ./php53 /tmp/more_small.php real 0m0.260s user 0m0.225s sys 0m0.032s $ time ./php+ /tmp/more_small.php real 0m0.261s user 0m0.226s sys 0m0.033s $ time ./php+ /tmp/more_small.php real 0m0.258s user 0m0.224s sys 0m0.032s $ time ./php+ /tmp/more_small.php real 0m0.260s user 0m0.225s sys 0m0.033s === TEST 2 === $ time ./php53 /tmp/less_big.php real 0m0.328s user 0m0.205s sys 0m0.120s $ time ./php53 /tmp/less_big.php real 0m0.328s user 0m0.206s sys 0m0.120s $ time ./php53 /tmp/less_big.php real 0m0.326s user 0m0.205s sys 0m0.119s $ time ./php+ /tmp/less_big.php real 0m0.330s user 0m0.206s sys 0m0.122s $ time ./php+ /tmp/less_big.php real 0m0.330s user 0m0.206s sys 0m0.121s $ time ./php+ /tmp/less_big.php real 0m0.333s user 0m0.207s sys 0m0.124s === TEST 3 === $ time ./php+ /tmp/lots_null.php real 0m0.263s user 0m0.229s sys 0m0.032s $ time ./php+ /tmp/lots_null.php real 0m0.261s user 0m0.227s sys 0m0.032s $ time ./php+ /tmp/lots_null.php real 0m0.260s user 0m0.226s sys 0m0.032s $ time ./php+ /tmp/no_null.php real 0m0.259s user 0m0.226s sys 0m0.032s $ time ./php+ /tmp/no_null.php real 0m0.257s user 0m0.223s sys 0m0.031s $ time ./php+ /tmp/no_null.php real 0m0.264s user 0m0.229s sys 0m0.032s === SCRIPTS === less_big.php <?php $str = str_repeat(str_repeat('foo', 1000).',', 11345); $arr = explode(',', $str); $out = implode(',', $arr); lots_null.php <?php $str = str_repeat('foo,,', 99999); $arr = explode(',', $str); $out = implode(',', $arr); more_small.php <?php $str = str_repeat('foo,bar,', 99999); $arr = explode(',', $str); $out = implode(',', $arr); no_null.php <?php $str = str_repeat('foo,,', 99999); $arr = explode(',', $str); $out = implode(',', $arr, true);

Attachment: [text/x-diff] implode.patch
« previous php.internals (#42119) next »