From 0fd77aa397b36ece2cc823e8867157f4491763dd Mon Sep 17 00:00:00 2001 From: Mark Baker Date: Thu, 10 Feb 2011 12:39:34 +0000 Subject: [PATCH] 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 --- Classes/PHPExcel/Calculation.php | 14 ++++++++------ Classes/PHPExcel/Reader/Excel2003XML.php | 5 +++-- Classes/PHPExcel/Reader/OOCalc.php | 5 +++-- Classes/PHPExcel/Reader/SYLK.php | 5 +++-- Classes/PHPExcel/ReferenceHelper.php | 7 ++++--- 5 files changed, 21 insertions(+), 15 deletions(-) diff --git a/Classes/PHPExcel/Calculation.php b/Classes/PHPExcel/Calculation.php index 2afd2ccb..21ff92aa 100644 --- a/Classes/PHPExcel/Calculation.php +++ b/Classes/PHPExcel/Calculation.php @@ -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 // the formula $temp = explode('"',$formula); - foreach($temp as $i => &$value) { - // Only count/replace in alternate array entries - if (($i % 2) == 0) { + $i = false; + foreach($temp as &$value) { + // Only count/replace in alternating array entries + if ($i = !$i) { $value = preg_replace($from,$to,$value); $value = self::_translateSeparator($fromSeparator,$toSeparator,$value,$inBraces); } @@ -2553,9 +2554,10 @@ class PHPExcel_Calculation { $temp = explode('"',$formula); // Open and Closed counts used for trapping mismatched braces in the formula $openCount = $closeCount = 0; - foreach($temp as $i => &$value) { - // Only count/replace in alternate array entries - if (($i % 2) == 0) { + $i = false; + foreach($temp as &$value) { + // Only count/replace in alternating array entries + if ($i = !$i) { $openCount += substr_count($value,'{'); $closeCount += substr_count($value,'}'); $value = str_replace($matrixReplaceFrom,$matrixReplaceTo,$value); diff --git a/Classes/PHPExcel/Reader/Excel2003XML.php b/Classes/PHPExcel/Reader/Excel2003XML.php index 27d31216..ec31c0f4 100644 --- a/Classes/PHPExcel/Reader/Excel2003XML.php +++ b/Classes/PHPExcel/Reader/Excel2003XML.php @@ -663,9 +663,10 @@ class PHPExcel_Reader_Excel2003XML implements PHPExcel_Reader_IReader $columnNumber = PHPExcel_Cell::columnIndexFromString($columnID); // Convert R1C1 style references to A1 style references (but only when not quoted) $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) - if (($key % 2) == 0) { + if ($key = !$key) { 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 // through the formula from left to right. Reversing means that we work right to left.through diff --git a/Classes/PHPExcel/Reader/OOCalc.php b/Classes/PHPExcel/Reader/OOCalc.php index 7aab6de0..c66241ec 100644 --- a/Classes/PHPExcel/Reader/OOCalc.php +++ b/Classes/PHPExcel/Reader/OOCalc.php @@ -493,9 +493,10 @@ class PHPExcel_Reader_OOCalc implements PHPExcel_Reader_IReader // echo 'Formula: '.$cellDataFormula.'
'; $cellDataFormula = substr($cellDataFormula,strpos($cellDataFormula,':=')+1); $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) - if (($key % 2) == 0) { + if ($tKey = !$tKey) { $value = preg_replace('/\[\.(.*):\.(.*)\]/Ui','$1:$2',$value); $value = preg_replace('/\[\.(.*)\]/Ui','$1',$value); $value = PHPExcel_Calculation::_translateSeparator(';',',',$value,$inBraces); diff --git a/Classes/PHPExcel/Reader/SYLK.php b/Classes/PHPExcel/Reader/SYLK.php index bf287484..e2ce8dac 100644 --- a/Classes/PHPExcel/Reader/SYLK.php +++ b/Classes/PHPExcel/Reader/SYLK.php @@ -270,9 +270,10 @@ class PHPExcel_Reader_SYLK implements PHPExcel_Reader_IReader case 'E' : $cellDataFormula = '='.substr($rowDatum,1); // Convert R1C1 style references to A1 style references (but only when not quoted) $temp = explode('"',$cellDataFormula); - foreach($temp as $key => &$value) { + $key = false; + foreach($temp as &$value) { // 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); // 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 diff --git a/Classes/PHPExcel/ReferenceHelper.php b/Classes/PHPExcel/ReferenceHelper.php index 08c034c5..63a000fe 100644 --- a/Classes/PHPExcel/ReferenceHelper.php +++ b/Classes/PHPExcel/ReferenceHelper.php @@ -348,9 +348,10 @@ class PHPExcel_ReferenceHelper public function updateFormulaReferences($pFormula = '', $pBefore = 'A1', $pNumCols = 0, $pNumRows = 0, $sheetName = '') { // Update cell references in the formula $formulaBlocks = explode('"',$pFormula); - foreach($formulaBlocks as $i => &$formulaBlock) { - // Ignore blocks that were enclosed in quotes (even entries in the $formulaBlocks array after the explode) - if (($i % 2) == 0) { + $i = false; + foreach($formulaBlocks as &$formulaBlock) { + // Ignore blocks that were enclosed in quotes (alternating entries in the $formulaBlocks array after the explode) + if ($i = !$i) { $adjustCount = 0; $newCellTokens = $cellTokens = array(); // Search for row ranges (e.g. 'Sheet1'!3:5 or 3:5) with or without $ absolutes (e.g. $3:5)