Minor performance tweaks when working with alternating blocks in a string

git-svn-id: https://phpexcel.svn.codeplex.com/svn/trunk@68309 2327b42d-5241-43d6-9e2a-de5ac946f064
This commit is contained in:
Mark Baker 2011-02-10 12:39:34 +00:00
parent 8218d04b0f
commit 0fd77aa397
5 changed files with 21 additions and 15 deletions

View File

@ -1960,9 +1960,10 @@ class PHPExcel_Calculation {
// So instead we skip replacing in any quoted strings by only replacing in every other array element after we've exploded // So instead we skip replacing in any quoted strings by only replacing in every other array element after we've exploded
// the formula // the formula
$temp = explode('"',$formula); $temp = explode('"',$formula);
foreach($temp as $i => &$value) { $i = false;
// Only count/replace in alternate array entries foreach($temp as &$value) {
if (($i % 2) == 0) { // Only count/replace in alternating array entries
if ($i = !$i) {
$value = preg_replace($from,$to,$value); $value = preg_replace($from,$to,$value);
$value = self::_translateSeparator($fromSeparator,$toSeparator,$value,$inBraces); $value = self::_translateSeparator($fromSeparator,$toSeparator,$value,$inBraces);
} }
@ -2553,9 +2554,10 @@ class PHPExcel_Calculation {
$temp = explode('"',$formula); $temp = explode('"',$formula);
// Open and Closed counts used for trapping mismatched braces in the formula // Open and Closed counts used for trapping mismatched braces in the formula
$openCount = $closeCount = 0; $openCount = $closeCount = 0;
foreach($temp as $i => &$value) { $i = false;
// Only count/replace in alternate array entries foreach($temp as &$value) {
if (($i % 2) == 0) { // Only count/replace in alternating array entries
if ($i = !$i) {
$openCount += substr_count($value,'{'); $openCount += substr_count($value,'{');
$closeCount += substr_count($value,'}'); $closeCount += substr_count($value,'}');
$value = str_replace($matrixReplaceFrom,$matrixReplaceTo,$value); $value = str_replace($matrixReplaceFrom,$matrixReplaceTo,$value);

View File

@ -663,9 +663,10 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader
$columnNumber = PHPExcel_Cell::columnIndexFromString($columnID); $columnNumber = PHPExcel_Cell::columnIndexFromString($columnID);
// Convert R1C1 style references to A1 style references (but only when not quoted) // Convert R1C1 style references to A1 style references (but only when not quoted)
$temp = explode('"',$cellDataFormula); $temp = explode('"',$cellDataFormula);
foreach($temp as $key => &$value) { $key = false;
foreach($temp as &$value) {
// Only replace in alternate array entries (i.e. non-quoted blocks) // Only replace in alternate array entries (i.e. non-quoted blocks)
if (($key % 2) == 0) { if ($key = !$key) {
preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/',$value, $cellReferences,PREG_SET_ORDER+PREG_OFFSET_CAPTURE); preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/',$value, $cellReferences,PREG_SET_ORDER+PREG_OFFSET_CAPTURE);
// Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way // Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way
// through the formula from left to right. Reversing means that we work right to left.through // through the formula from left to right. Reversing means that we work right to left.through

View File

@ -493,9 +493,10 @@ class PHPExcel_Reader_OOCalc implements PHPExcel_Reader_IReader
// echo 'Formula: '.$cellDataFormula.'<br />'; // echo 'Formula: '.$cellDataFormula.'<br />';
$cellDataFormula = substr($cellDataFormula,strpos($cellDataFormula,':=')+1); $cellDataFormula = substr($cellDataFormula,strpos($cellDataFormula,':=')+1);
$temp = explode('"',$cellDataFormula); $temp = explode('"',$cellDataFormula);
foreach($temp as $key => &$value) { $tKey = false;
foreach($temp as &$value) {
// Only replace in alternate array entries (i.e. non-quoted blocks) // Only replace in alternate array entries (i.e. non-quoted blocks)
if (($key % 2) == 0) { if ($tKey = !$tKey) {
$value = preg_replace('/\[\.(.*):\.(.*)\]/Ui','$1:$2',$value); $value = preg_replace('/\[\.(.*):\.(.*)\]/Ui','$1:$2',$value);
$value = preg_replace('/\[\.(.*)\]/Ui','$1',$value); $value = preg_replace('/\[\.(.*)\]/Ui','$1',$value);
$value = PHPExcel_Calculation::_translateSeparator(';',',',$value,$inBraces); $value = PHPExcel_Calculation::_translateSeparator(';',',',$value,$inBraces);

View File

@ -270,9 +270,10 @@ class PHPExcel_Reader_SYLK implements PHPExcel_Reader_IReader
case 'E' : $cellDataFormula = '='.substr($rowDatum,1); case 'E' : $cellDataFormula = '='.substr($rowDatum,1);
// Convert R1C1 style references to A1 style references (but only when not quoted) // Convert R1C1 style references to A1 style references (but only when not quoted)
$temp = explode('"',$cellDataFormula); $temp = explode('"',$cellDataFormula);
foreach($temp as $key => &$value) { $key = false;
foreach($temp as &$value) {
// Only count/replace in alternate array entries // Only count/replace in alternate array entries
if (($key % 2) == 0) { if ($key = !$key) {
preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/',$value, $cellReferences,PREG_SET_ORDER+PREG_OFFSET_CAPTURE); preg_match_all('/(R(\[?-?\d*\]?))(C(\[?-?\d*\]?))/',$value, $cellReferences,PREG_SET_ORDER+PREG_OFFSET_CAPTURE);
// Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way // Reverse the matches array, otherwise all our offsets will become incorrect if we modify our way
// through the formula from left to right. Reversing means that we work right to left.through // through the formula from left to right. Reversing means that we work right to left.through

View File

@ -348,9 +348,10 @@ class PHPExcel_ReferenceHelper
public function updateFormulaReferences($pFormula = '', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, $sheetName = '') { public function updateFormulaReferences($pFormula = '', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, $sheetName = '') {
// Update cell references in the formula // Update cell references in the formula
$formulaBlocks = explode('"',$pFormula); $formulaBlocks = explode('"',$pFormula);
foreach($formulaBlocks as $i => &$formulaBlock) { $i = false;
// Ignore blocks that were enclosed in quotes (even entries in the $formulaBlocks array after the explode) foreach($formulaBlocks as &$formulaBlock) {
if (($i % 2) == 0) { // Ignore blocks that were enclosed in quotes (alternating entries in the $formulaBlocks array after the explode)
if ($i = !$i) {
$adjustCount = 0; $adjustCount = 0;
$newCellTokens = $cellTokens = array(); $newCellTokens = $cellTokens = array();
// Search for row ranges (e.g. 'Sheet1'!3:5 or 3:5) with or without $ absolutes (e.g. $3:5) // Search for row ranges (e.g. 'Sheet1'!3:5 or 3:5) with or without $ absolutes (e.g. $3:5)