Project

General

Profile

Bug #20068 » simpler_faster_trimExplode_v3.diff

Administrator Admin, 2009-02-21 16:35

View differences:

t3lib/class.t3lib_div.php (working copy)
*
* @param string Delimiter string to explode with
* @param string The string to explode
* @param boolean If set, all empty values (='') will NOT be set in output
* @param integer If positive, the result will contain a maximum of limit elements,
* if negative, all components except the last -limit are returned,
* @param boolean If set, all empty values will be removed in output
* @param integer If positive, the result will contain a maximum of $limit elements,
* if negative, all components except the last -$limit are returned,
* if zero (default), the result is not limited at all
* @return array Exploded values
*/
public static function trimExplode($delim, $string, $onlyNonEmptyValues = false, $limit = 0) {
$array = (!$limit ? explode($delim, $string) : explode($delim, $string, $limit));
// for two perfomance reasons the loop is duplicated
// a) avoid check for $onlyNonEmptyValues in foreach loop
// b) avoid unnecessary code when $onlyNonEmptyValues is not set
if ($onlyNonEmptyValues) {
$new_array = array();
foreach($array as $value) {
$value = trim($value);
public static function trimExplode($delim, $string, $removeEmptyValues = false, $limit = 0) {
$explodedValues = $limit ? explode($delim, $string, $limit) : explode($delim, $string);
$result = array_map('trim', $explodedValues);
if ($removeEmptyValues) {
$temp = array();
foreach($result as $value) {
if ($value != '') {
$new_array[] = $value;
$temp[] = $value;
}
}
// direct return for perfomance reasons
return $new_array;
$result = $temp;
}
foreach($array as &$value) {
$value = trim($value);
}
return $array;
return $result;
}
/**
tests/t3lib/t3lib_div_testcase.php (revision 0)
<?php
/***************************************************************
* Copyright notice
*
* (c) 2009 Ingo Renner <ingo@typo3.org>
* All rights reserved
*
* This script is part of the TYPO3 project. The TYPO3 project is
* free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* The GNU General Public License can be found at
* http://www.gnu.org/copyleft/gpl.html.
*
* This script is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* This copyright notice MUST APPEAR in all copies of the script!
***************************************************************/
/**
* Testcase for class t3lib_div
*
* @author Ingo Renner <ingo@typo3.org>
* @package TYPO3
* @subpackage t3lib
*/
class t3lib_div_testcase extends tx_phpunit_testcase {
/**
* @test
*/
public function checkTrimExplodeTrimsSpacesAtElementStartAndEnd() {
$testString = ' a , b , c ,d ,, e,f,';
$expectedArray = array('a', 'b', 'c', 'd', '', 'e', 'f', '');
$actualArray = t3lib_div::trimExplode(',', $testString);
$this->assertEquals($expectedArray, $actualArray);
}
/**
* @test
*/
public function checkTrimExplodeRemovesNewLines() {
$testString = ' a , b , ' . chr(10) . ' ,d ,, e,f,';
$expectedArray = array('a', 'b', 'd', 'e', 'f');
$actualArray = t3lib_div::trimExplode(',', $testString, true);
$this->assertEquals($expectedArray, $actualArray);
}
/**
* @test
*/
public function checkTrimExplodeRemovesEmptyElements() {
$testString = 'a , b , c , ,d ,, ,e,f,';
$expectedArray = array('a', 'b', 'c', 'd', 'e', 'f');
$actualArray = t3lib_div::trimExplode(',', $testString, true);
$this->assertEquals($expectedArray, $actualArray);
}
/**
* @test
*/
public function checkTrimExplodeLimitsResults() {
$testString = ' a , b , c , , d,, ,e ';
$expectedArray = array('a', 'b', 'c , , d,, ,e'); // limiting returns the rest of the string as the last element
$actualArray = t3lib_div::trimExplode(',', $testString, false, 3);
$this->assertEquals($expectedArray, $actualArray);
}
}
?>
(3-3/3)