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

View File

@ -778,8 +778,7 @@ class Parser
*/ */
private function getRefIndex($ext_ref) private function getRefIndex($ext_ref)
{ {
$ext_ref = (string) preg_replace("/^'/", '', $ext_ref); // Remove leading ' if any. $ext_ref = (string) preg_replace(["/^'/", "/'$/"], ['', ''], $ext_ref); // Remove leading and trailing ' if any.
$ext_ref = (string) preg_replace("/'$/", '', $ext_ref); // Remove trailing ' if any.
$ext_ref = str_replace('\'\'', '\'', $ext_ref); // Replace escaped '' with ' $ext_ref = str_replace('\'\'', '\'', $ext_ref); // Replace escaped '' with '
// Check if there is a sheet range eg., Sheet1:Sheet2. // 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) // Strip URL type and change Unix dir separator to Dos style (if needed)
// //
$url = (string) preg_replace('/^external:/', '', $url); $url = (string) preg_replace(['/^external:/', '/\//'], ['', '\\'], $url);
$url = (string) preg_replace('/\//', '\\', $url);
// Determine if the link is relative or absolute: // Determine if the link is relative or absolute:
// relative if link contains no dir separator, "somefile.xls" // relative if link contains no dir separator, "somefile.xls"