[BUGFIX] Wrong handling of delimiter in revExplode() 26/27926/5
authorMarkus Klein <klein.t3@mfc-linz.at>
Fri, 28 Feb 2014 01:41:30 +0000 (02:41 +0100)
committerHelmut Hummel <helmut.hummel@typo3.org>
Mon, 3 Mar 2014 16:08:14 +0000 (17:08 +0100)
The delimiter has to be reversed for the case of limit 2,
as the search is performed on the non-reversed string.
Otherwise the function will yield different results for
limits greater than 2.

Resolves: #56405
Releases: 6.2
Change-Id: I077d38918d98fbe2e5cd153c75b115e0d95734f8
Reviewed-on: https://review.typo3.org/27926
Reviewed-by: Georg Tiefenbrunn
Tested-by: Georg Tiefenbrunn
Reviewed-by: Helmut Hummel
Tested-by: Helmut Hummel
typo3/sysext/core/Classes/Utility/GeneralUtility.php
typo3/sysext/core/Tests/Unit/Utility/GeneralUtilityTest.php

index 26db8ac..fa25704 100644 (file)
@@ -1474,7 +1474,12 @@ class GeneralUtility {
 
        /**
         * Reverse explode which explodes the string counting from behind.
-        * Thus \TYPO3\CMS\Core\Utility\GeneralUtility::revExplode(':','my:words:here',2) will return array('my:words','here')
+        *
+        * Note: The delimiter has to given in the reverse order as
+        *       it is occurring within the string.
+        *
+        * GeneralUtility::revExplode('[]', '[my][words][here]', 2)
+        *   ==> array('[my][words', 'here]')
         *
         * @param string $delimiter Delimiter string to explode with
         * @param string $string The string to explode
@@ -1484,9 +1489,9 @@ class GeneralUtility {
        static public function revExplode($delimiter, $string, $count = 0) {
                // 2 is the (currently, as of 2014-02) most-used value for $count in the core, therefore we check it first
                if ($count === 2) {
-                       $position = strrpos($string, $delimiter);
+                       $position = strrpos($string, strrev($delimiter));
                        if ($position !== FALSE) {
-                               return array(substr($string, 0, $position), substr($string, $position + 1));
+                               return array(substr($string, 0, $position), substr($string, $position + strlen($delimiter)));
                        } else {
                                return array($string);
                        }
index c5b8981..4a15373 100644 (file)
@@ -1355,48 +1355,71 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
        // Tests concerning revExplode
        //////////////////////////////////
 
+       /**
+        * @return array
+        */
        public function revExplodeDataProvider() {
                return array(
                        'limit 0 should return unexploded string' => array(
+                               ':',
                                'my:words:here',
                                0,
                                array('my:words:here')
                        ),
                        'limit 1 should return unexploded string' => array(
+                               ':',
                                'my:words:here',
                                1,
                                array('my:words:here')
                        ),
                        'limit 2 should return two pieces' => array(
+                               ':',
                                'my:words:here',
                                2,
                                array('my:words', 'here')
                        ),
                        'limit 3 should return unexploded string' => array(
+                               ':',
                                'my:words:here',
                                3,
                                array('my', 'words', 'here')
                        ),
                        'limit 0 should return unexploded string if no delimiter is contained' => array(
+                               ':',
                                'mywordshere',
                                0,
                                array('mywordshere')
                        ),
                        'limit 1 should return unexploded string if no delimiter is contained' => array(
+                               ':',
                                'mywordshere',
                                1,
                                array('mywordshere')
                        ),
                        'limit 2 should return unexploded string if no delimiter is contained' => array(
+                               ':',
                                'mywordshere',
                                2,
                                array('mywordshere')
                        ),
                        'limit 3 should return unexploded string if no delimiter is contained' => array(
+                               ':',
                                'mywordshere',
                                3,
                                array('mywordshere')
                        ),
+                       'multi character delimiter is handled properly with limit 2' => array(
+                               '[]',
+                               'a[b][c][d]',
+                               2,
+                               array('a[b][c', 'd]')
+                       ),
+                       'multi character delimiter is handled properly with limit 3' => array(
+                               '[]',
+                               'a[b][c][d]',
+                               3,
+                               array('a[b', 'c', 'd]')
+                       ),
                );
        }
 
@@ -1404,8 +1427,8 @@ class GeneralUtilityTest extends \TYPO3\CMS\Core\Tests\UnitTestCase {
         * @test
         * @dataProvider revExplodeDataProvider
         */
-       public function revExplodeCorrectlyExplodesStringForGivenPartsCount($testString, $count, $expectedArray) {
-               $actualArray = Utility\GeneralUtility::revExplode(':', $testString, $count);
+       public function revExplodeCorrectlyExplodesStringForGivenPartsCount($delimiter, $testString, $count, $expectedArray) {
+               $actualArray = Utility\GeneralUtility::revExplode($delimiter, $testString, $count);
                $this->assertEquals($expectedArray, $actualArray);
        }