[TASK] Improve performance of trimExplode 13/38513/9
authorStefan Froemken <froemken@gmail.com>
Wed, 8 Apr 2015 06:37:10 +0000 (08:37 +0200)
committerJigal van Hemert <jigal.van.hemert@typo3.org>
Wed, 8 Apr 2015 06:46:31 +0000 (08:46 +0200)
Preg_split replaces the trim action and the removal of empty values.
Performance varies per system and parameter values. Most used cases show
significant improvement on various systems and no performance loss in other
cases.

Resolves: #66307
Releases: master
Change-Id: Icf3c8dff93c934071e10eae7155e3798d57b3d5a
Reviewed-on: http://review.typo3.org/38513
Reviewed-by: Xavier Perseguers <xavier@typo3.org>
Tested-by: Xavier Perseguers <xavier@typo3.org>
Reviewed-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
Tested-by: Jigal van Hemert <jigal.van.hemert@typo3.org>
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php

index 24e60b0..75cd4a7 100755 (executable)
@@ -1471,19 +1471,9 @@ class GeneralUtility {
         * @return array Exploded values
         */
        static public function trimExplode($delim, $string, $removeEmptyValues = FALSE, $limit = 0) {
-               $result = array_map('trim', explode($delim, $string));
-               if ($removeEmptyValues) {
-                       $temp = array();
-                       foreach ($result as $value) {
-                               if ($value !== '') {
-                                       $temp[] = $value;
-                               }
-                       }
-                       $result = $temp;
-               }
+               $result = preg_split('/\s*+' . preg_quote($delim, '/') . '\s*+/', trim($string), NULL, $removeEmptyValues ? PREG_SPLIT_NO_EMPTY : NULL);
                if ($limit > 0 && count($result) > $limit) {
-                       $lastElements = array_splice($result, $limit - 1);
-                       $result[] = implode($delim, $lastElements);
+                       $result = explode($delim, implode($delim, $result), $limit);
                } elseif ($limit < 0) {
                        $result = array_slice($result, 0, $limit);
                }
index 7a010f1..46f70d4 100644 (file)
@@ -1305,6 +1305,26 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
                $this->assertEquals($expectedArray, $actualArray);
        }
 
+       /**
+        * @test
+        */
+       public function checkTrimExplodeKeepsWhitespaceInsideElements() {
+               $testString = 'a , b , c , ,d ,, ,e,f, g h ,';
+               $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f', 'g h');
+               $actualArray = Utility\GeneralUtility::trimExplode(',', $testString, TRUE);
+               $this->assertEquals($expectedArray, $actualArray);
+       }
+
+       /**
+        * @test
+        */
+       public function checkTrimExplodeCanUseInternalRegexDelimiterAsExplodeDelimiter() {
+               $testString = 'a / b / c / /d // /e/f/ g h /';
+               $expectedArray = array('a', 'b', 'c', 'd', 'e', 'f', 'g h');
+               $actualArray = Utility\GeneralUtility::trimExplode('/', $testString, TRUE);
+               $this->assertEquals($expectedArray, $actualArray);
+       }
+
        //////////////////////////////////
        // Tests concerning getBytesFromSizeMeasurement
        //////////////////////////////////