Use preg_replace() with arrays, where appropriate, because it's more efficient that caling several times

This commit is contained in:
MarkBaker 2022-05-25 13:32:06 +02:00
parent bf46ff1e0c
commit ff8f801043
3 changed files with 34 additions and 21 deletions

View File

@ -13,15 +13,23 @@ class FormulaTranslator
// Cell range 3-d reference
// As we don't support 3-d ranges, we're just going to take a quick and dirty approach
// and assume that the second worksheet reference is the same as the first
$excelAddress = (string) preg_replace('/\$?([^\.]+)\.([^\.]+):\$?([^\.]+)\.([^\.]+)/miu', '$1!$2:$4', $excelAddress);
// Cell range reference in another sheet
$excelAddress = (string) preg_replace('/\$?([^\.]+)\.([^\.]+):\.([^\.]+)/miu', '$1!$2:$3', $excelAddress);
// Cell reference in another sheet
$excelAddress = (string) preg_replace('/\$?([^\.]+)\.([^\.]+)/miu', '$1!$2', $excelAddress);
// Cell range reference
$excelAddress = (string) preg_replace('/\.([^\.]+):\.([^\.]+)/miu', '$1:$2', $excelAddress);
// Simple cell reference
$excelAddress = (string) preg_replace('/\.([^\.]+)/miu', '$1', $excelAddress);
$excelAddress = (string) preg_replace(
[
'/\$?([^\.]+)\.([^\.]+):\$?([^\.]+)\.([^\.]+)/miu',
'/\$?([^\.]+)\.([^\.]+):\.([^\.]+)/miu', // Cell range reference in another sheet
'/\$?([^\.]+)\.([^\.]+)/miu', // Cell reference in another sheet
'/\.([^\.]+):\.([^\.]+)/miu', // Cell range reference
'/\.([^\.]+)/miu', // Simple cell reference
],
[
'$1!$2:$4',
'$1!$2:$3',
'$1!$2',
'$1:$2',
'$1',
],
$excelAddress
);
return $excelAddress;
}
@ -37,14 +45,21 @@ class FormulaTranslator
// Only replace in alternate array entries (i.e. non-quoted blocks)
// so that conversion isn't done in string values
if ($tKey = !$tKey) {
// Cell range reference in another sheet
$value = (string) preg_replace('/\[\$?([^\.]+)\.([^\.]+):\.([^\.]+)\]/miu', '$1!$2:$3', $value);
// Cell reference in another sheet
$value = (string) preg_replace('/\[\$?([^\.]+)\.([^\.]+)\]/miu', '$1!$2', $value);
// Cell range reference
$value = (string) preg_replace('/\[\.([^\.]+):\.([^\.]+)\]/miu', '$1:$2', $value);
// Simple cell reference
$value = (string) preg_replace('/\[\.([^\.]+)\]/miu', '$1', $value);
$value = (string) preg_replace(
[
'/\[\$?([^\.]+)\.([^\.]+):\.([^\.]+)\]/miu', // Cell range reference in another sheet
'/\[\$?([^\.]+)\.([^\.]+)\]/miu', // Cell reference in another sheet
'/\[\.([^\.]+):\.([^\.]+)\]/miu', // Cell range reference
'/\[\.([^\.]+)\]/miu', // Simple cell reference
],
[
'$1!$2:$3',
'$1!$2',
'$1:$2',
'$1',
],
$value
);
// Convert references to defined names/formulae
$value = str_replace('$$', '', $value);

View File

@ -778,8 +778,7 @@ class Parser
*/
private function getRefIndex($ext_ref)
{
$ext_ref = (string) preg_replace("/^'/", '', $ext_ref); // Remove leading ' if any.
$ext_ref = (string) preg_replace("/'$/", '', $ext_ref); // Remove trailing ' if any.
$ext_ref = (string) preg_replace(["/^'/", "/'$/"], ['', ''], $ext_ref); // Remove leading and trailing ' if any.
$ext_ref = str_replace('\'\'', '\'', $ext_ref); // Replace escaped '' with '
// Check if there is a sheet range eg., Sheet1:Sheet2.

View File

@ -1095,8 +1095,7 @@ class Worksheet extends BIFFwriter
// Strip URL type and change Unix dir separator to Dos style (if needed)
//
$url = (string) preg_replace('/^external:/', '', $url);
$url = (string) preg_replace('/\//', '\\', $url);
$url = (string) preg_replace(['/^external:/', '/\//'], ['', '\\'], $url);
// Determine if the link is relative or absolute:
// relative if link contains no dir separator, "somefile.xls"