From 677054dd0a7c2e65cc9f4930f2325179bd0a8f18 Mon Sep 17 00:00:00 2001 From: MarkBaker Date: Mon, 9 May 2022 12:32:47 +0200 Subject: [PATCH] Performance tweaks to cell address validation --- src/PhpSpreadsheet/Cell/AddressHelper.php | 8 ++++---- src/PhpSpreadsheet/Cell/CellAddress.php | 4 +--- src/PhpSpreadsheet/Cell/Coordinate.php | 4 ++-- src/PhpSpreadsheet/Worksheet/Validations.php | 5 ++--- 4 files changed, 9 insertions(+), 12 deletions(-) diff --git a/src/PhpSpreadsheet/Cell/AddressHelper.php b/src/PhpSpreadsheet/Cell/AddressHelper.php index 499d248c..535bdee0 100644 --- a/src/PhpSpreadsheet/Cell/AddressHelper.php +++ b/src/PhpSpreadsheet/Cell/AddressHelper.php @@ -118,17 +118,17 @@ class AddressHelper throw new Exception('Invalid A1-format Cell Reference'); } - $columnId = Coordinate::columnIndexFromString($cellReference['col_ref']); - if ($cellReference['absolute_col'] === '$') { + if ($cellReference['col'][0] === '$') { // Column must be absolute address $currentColumnNumber = null; } + $columnId = Coordinate::columnIndexFromString(ltrim($cellReference['col'], '$')); - $rowId = (int) $cellReference['row_ref']; - if ($cellReference['absolute_row'] === '$') { + if ($cellReference['row'][0] === '$') { // Row must be absolute address $currentRowNumber = null; } + $rowId = (int) ltrim($cellReference['row'], '$'); if ($currentRowNumber !== null) { if ($rowId === $currentRowNumber) { diff --git a/src/PhpSpreadsheet/Cell/CellAddress.php b/src/PhpSpreadsheet/Cell/CellAddress.php index 069c9ac9..a0c544f2 100644 --- a/src/PhpSpreadsheet/Cell/CellAddress.php +++ b/src/PhpSpreadsheet/Cell/CellAddress.php @@ -35,9 +35,7 @@ class CellAddress public function __construct(string $cellAddress, ?Worksheet $worksheet = null) { $this->cellAddress = str_replace('$', '', $cellAddress); - [$this->columnName, $rowId] = Coordinate::coordinateFromString($cellAddress); - $this->rowId = (int) $rowId; - $this->columnId = Coordinate::columnIndexFromString($this->columnName); + [$this->columnId, $this->rowId, $this->columnName] = Coordinate::indexesFromString($this->cellAddress); $this->worksheet = $worksheet; } diff --git a/src/PhpSpreadsheet/Cell/Coordinate.php b/src/PhpSpreadsheet/Cell/Coordinate.php index fac3c629..7a5e665d 100644 --- a/src/PhpSpreadsheet/Cell/Coordinate.php +++ b/src/PhpSpreadsheet/Cell/Coordinate.php @@ -13,7 +13,7 @@ use PhpOffice\PhpSpreadsheet\Worksheet\Worksheet; */ abstract class Coordinate { - public const A1_COORDINATE_REGEX = '/^(?\$?)(?[A-Z]{1,3})(?\$?)(?\d{1,7})$/i'; + public const A1_COORDINATE_REGEX = '/^(?\$?[A-Z]{1,3})(?\$?\d{1,7})$/i'; /** * Default range variable constant. @@ -32,7 +32,7 @@ abstract class Coordinate public static function coordinateFromString($cellAddress) { if (preg_match(self::A1_COORDINATE_REGEX, $cellAddress, $matches)) { - return [$matches['absolute_col'] . $matches['col_ref'], $matches['absolute_row'] . $matches['row_ref']]; + return [$matches['col'], $matches['row']]; } elseif (self::coordinateIsRange($cellAddress)) { throw new Exception('Cell coordinate string can not be a range of cells'); } elseif ($cellAddress == '') { diff --git a/src/PhpSpreadsheet/Worksheet/Validations.php b/src/PhpSpreadsheet/Worksheet/Validations.php index b31b0813..cd76d2a4 100644 --- a/src/PhpSpreadsheet/Worksheet/Validations.php +++ b/src/PhpSpreadsheet/Worksheet/Validations.php @@ -42,9 +42,8 @@ class Validations public static function validateCellOrCellRange($cellRange): string { if (is_string($cellRange) || is_numeric($cellRange)) { - $cellRange = (string) $cellRange; // Convert a single column reference like 'A' to 'A:A' - $cellRange = (string) preg_replace('/^([A-Z]+)$/', '${1}:${1}', $cellRange); + $cellRange = (string) preg_replace('/^([A-Z]+)$/', '${1}:${1}', (string) $cellRange); // Convert a single row reference like '1' to '1:1' $cellRange = (string) preg_replace('/^(\d+)$/', '${1}:${1}', $cellRange); } elseif (is_object($cellRange) && $cellRange instanceof CellAddress) { @@ -85,7 +84,7 @@ class Validations public static function definedNameToCoordinate(string $coordinate, Worksheet $worksheet): string { // Uppercase coordinate - $testCoordinate = strtoupper($coordinate); + $coordinate = strtoupper($coordinate); // Eliminate leading equal sign $testCoordinate = (string) preg_replace('/^=/', '', $coordinate); $defined = $worksheet->getParent()->getDefinedName($testCoordinate, $worksheet);