Skip to content

Commit 30ec11c

Browse files
Jean85PowerKiKi
authored andcommitted
Optimize regex using \d
1 parent cfc325a commit 30ec11c

File tree

8 files changed

+27
-27
lines changed

8 files changed

+27
-27
lines changed

src/PhpSpreadsheet/Calculation/FormulaParser.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ private function parseToTokens()
224224
// scientific notation check
225225
if (strpos(self::OPERATORS_SN, $this->formula[$index]) !== false) {
226226
if (strlen($value) > 1) {
227-
if (preg_match("/^[1-9]{1}(\.[0-9]+)?E{1}$/", $this->formula[$index]) != 0) {
227+
if (preg_match("/^[1-9]{1}(\.\d+)?E{1}$/", $this->formula[$index]) != 0) {
228228
$value .= $this->formula[$index];
229229
++$index;
230230

src/PhpSpreadsheet/Calculation/LookupRef.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ public static function ROW($cellAddress = null)
179179
if (is_array($cellAddress)) {
180180
foreach ($cellAddress as $columnKey => $rowValue) {
181181
foreach ($rowValue as $rowKey => $cellValue) {
182-
return (int) preg_replace('/[^0-9]/', '', $rowKey);
182+
return (int) preg_replace('/\D/', '', $rowKey);
183183
}
184184
}
185185
} else {
@@ -188,8 +188,8 @@ public static function ROW($cellAddress = null)
188188
}
189189
if (strpos($cellAddress, ':') !== false) {
190190
list($startAddress, $endAddress) = explode(':', $cellAddress);
191-
$startAddress = preg_replace('/[^0-9]/', '', $startAddress);
192-
$endAddress = preg_replace('/[^0-9]/', '', $endAddress);
191+
$startAddress = preg_replace('/\D/', '', $startAddress);
192+
$endAddress = preg_replace('/\D/', '', $endAddress);
193193
$returnValue = [];
194194
do {
195195
$returnValue[][] = (int) $startAddress;
@@ -199,7 +199,7 @@ public static function ROW($cellAddress = null)
199199
}
200200
list($cellAddress) = explode(':', $cellAddress);
201201

202-
return (int) preg_replace('/[^0-9]/', '', $cellAddress);
202+
return (int) preg_replace('/\D/', '', $cellAddress);
203203
}
204204
}
205205

src/PhpSpreadsheet/Cell/AdvancedValueBinder.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ public function bindValue(Cell $cell, $value = null)
4949
}
5050

5151
// Check for fraction
52-
if (preg_match('/^([+-]?)\s*([0-9]+)\s?\/\s*([0-9]+)$/', $value, $matches)) {
52+
if (preg_match('/^([+-]?)\s*(\d+)\s?\/\s*(\d+)$/', $value, $matches)) {
5353
// Convert value to number
5454
$value = $matches[2] / $matches[3];
5555
if ($matches[1] == '-') {
@@ -61,7 +61,7 @@ public function bindValue(Cell $cell, $value = null)
6161
->getNumberFormat()->setFormatCode('??/??');
6262

6363
return true;
64-
} elseif (preg_match('/^([+-]?)([0-9]*) +([0-9]*)\s?\/\s*([0-9]*)$/', $value, $matches)) {
64+
} elseif (preg_match('/^([+-]?)(\d*) +(\d*)\s?\/\s*(\d*)$/', $value, $matches)) {
6565
// Convert value to number
6666
$value = $matches[2] + ($matches[3] / $matches[4]);
6767
if ($matches[1] == '-') {
@@ -76,7 +76,7 @@ public function bindValue(Cell $cell, $value = null)
7676
}
7777

7878
// Check for percentage
79-
if (preg_match('/^\-?[0-9]*\.?[0-9]*\s?\%$/', $value)) {
79+
if (preg_match('/^\-?\d*\.?\d*\s?\%$/', $value)) {
8080
// Convert value to number
8181
$value = (float) str_replace('%', '', $value) / 100;
8282
$cell->setValueExplicit($value, DataType::TYPE_NUMERIC);

src/PhpSpreadsheet/Cell/DefaultValueBinder.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public static function dataTypeForValue($pValue)
5959
return DataType::TYPE_BOOL;
6060
} elseif (is_float($pValue) || is_int($pValue)) {
6161
return DataType::TYPE_NUMERIC;
62-
} elseif (preg_match('/^[\+\-]?([0-9]+\\.?[0-9]*|[0-9]*\\.?[0-9]+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $pValue)) {
62+
} elseif (preg_match('/^[\+\-]?(\d+\\.?\d*|\d*\\.?\d+)([Ee][\-\+]?[0-2]?\d{1,3})?$/', $pValue)) {
6363
$tValue = ltrim($pValue, '+-');
6464
if (is_string($pValue) && $tValue[0] === '0' && strlen($tValue) > 1 && $tValue[1] !== '.') {
6565
return DataType::TYPE_STRING;

src/PhpSpreadsheet/Reader/Xls.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4548,8 +4548,8 @@ private function readSelection()
45484548
}
45494549

45504550
// first column 'A' + last column 'IV' indicates that full row is selected
4551-
if (preg_match('/^(A[0-9]+\:)IV([0-9]+)$/', $selectedCells)) {
4552-
$selectedCells = preg_replace('/^(A[0-9]+\:)IV([0-9]+)$/', '${1}XFD${2}', $selectedCells);
4551+
if (preg_match('/^(A\d+\:)IV(\d+)$/', $selectedCells)) {
4552+
$selectedCells = preg_replace('/^(A\d+\:)IV(\d+)$/', '${1}XFD${2}', $selectedCells);
45534553
}
45544554

45554555
$this->phpSheet->setSelectedCells($selectedCells);

src/PhpSpreadsheet/Style/Style.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ public function applyFromArray(array $pStyles, $pAdvanced = true)
342342
// Selection type, inspect
343343
if (preg_match('/^[A-Z]+1:[A-Z]+1048576$/', $pRange)) {
344344
$selectionType = 'COLUMN';
345-
} elseif (preg_match('/^A[0-9]+:XFD[0-9]+$/', $pRange)) {
345+
} elseif (preg_match('/^A\d+:XFD\d+$/', $pRange)) {
346346
$selectionType = 'ROW';
347347
} else {
348348
$selectionType = 'CELL';

src/PhpSpreadsheet/Worksheet/Worksheet.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2424,13 +2424,13 @@ public function setSelectedCells($pCoordinate)
24242424
$pCoordinate = preg_replace('/^([A-Z]+)$/', '${1}:${1}', $pCoordinate);
24252425

24262426
// Convert '1' to '1:1'
2427-
$pCoordinate = preg_replace('/^([0-9]+)$/', '${1}:${1}', $pCoordinate);
2427+
$pCoordinate = preg_replace('/^(\d+)$/', '${1}:${1}', $pCoordinate);
24282428

24292429
// Convert 'A:C' to 'A1:C1048576'
24302430
$pCoordinate = preg_replace('/^([A-Z]+):([A-Z]+)$/', '${1}1:${2}1048576', $pCoordinate);
24312431

24322432
// Convert '1:3' to 'A1:XFD3'
2433-
$pCoordinate = preg_replace('/^([0-9]+):([0-9]+)$/', 'A${1}:XFD${2}', $pCoordinate);
2433+
$pCoordinate = preg_replace('/^(\d+):(\d+)$/', 'A${1}:XFD${2}', $pCoordinate);
24342434

24352435
if (strpos($pCoordinate, ':') !== false || strpos($pCoordinate, ',') !== false) {
24362436
list($first) = Coordinate::splitRange($pCoordinate);

src/PhpSpreadsheet/Writer/Xls/Parser.php

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1019,21 +1019,21 @@ private function match($token)
10191019
break;
10201020
default:
10211021
// if it's a reference A1 or $A$1 or $A1 or A$1
1022-
if (preg_match('/^\$?[A-Ia-i]?[A-Za-z]\$?[0-9]+$/', $token) and !preg_match('/[0-9]/', $this->lookAhead) and ($this->lookAhead != ':') and ($this->lookAhead != '.') and ($this->lookAhead != '!')) {
1022+
if (preg_match('/^\$?[A-Ia-i]?[A-Za-z]\$?\d+$/', $token) and !preg_match('/\d/', $this->lookAhead) and ($this->lookAhead != ':') and ($this->lookAhead != '.') and ($this->lookAhead != '!')) {
10231023
return $token;
1024-
} elseif (preg_match('/^' . self::REGEX_SHEET_TITLE_UNQUOTED . "(\:" . self::REGEX_SHEET_TITLE_UNQUOTED . ")?\!\\$?[A-Ia-i]?[A-Za-z]\\$?[0-9]+$/u", $token) and !preg_match('/[0-9]/', $this->lookAhead) and ($this->lookAhead != ':') and ($this->lookAhead != '.')) {
1024+
} elseif (preg_match('/^' . self::REGEX_SHEET_TITLE_UNQUOTED . "(\:" . self::REGEX_SHEET_TITLE_UNQUOTED . ")?\!\\$?[A-Ia-i]?[A-Za-z]\\$?\d+$/u", $token) and !preg_match('/\d/', $this->lookAhead) and ($this->lookAhead != ':') and ($this->lookAhead != '.')) {
10251025
// If it's an external reference (Sheet1!A1 or Sheet1:Sheet2!A1 or Sheet1!$A$1 or Sheet1:Sheet2!$A$1)
10261026
return $token;
1027-
} elseif (preg_match("/^'" . self::REGEX_SHEET_TITLE_QUOTED . "(\:" . self::REGEX_SHEET_TITLE_QUOTED . ")?'\!\\$?[A-Ia-i]?[A-Za-z]\\$?[0-9]+$/u", $token) and !preg_match('/[0-9]/', $this->lookAhead) and ($this->lookAhead != ':') and ($this->lookAhead != '.')) {
1027+
} elseif (preg_match("/^'" . self::REGEX_SHEET_TITLE_QUOTED . "(\:" . self::REGEX_SHEET_TITLE_QUOTED . ")?'\!\\$?[A-Ia-i]?[A-Za-z]\\$?\d+$/u", $token) and !preg_match('/\d/', $this->lookAhead) and ($this->lookAhead != ':') and ($this->lookAhead != '.')) {
10281028
// If it's an external reference ('Sheet1'!A1 or 'Sheet1:Sheet2'!A1 or 'Sheet1'!$A$1 or 'Sheet1:Sheet2'!$A$1)
10291029
return $token;
1030-
} elseif (preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/', $token) && !preg_match('/[0-9]/', $this->lookAhead)) {
1030+
} elseif (preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?\d+:(\$)?[A-Ia-i]?[A-Za-z](\$)?\d+$/', $token) && !preg_match('/\d/', $this->lookAhead)) {
10311031
// if it's a range A1:A2 or $A$1:$A$2
10321032
return $token;
1033-
} elseif (preg_match('/^' . self::REGEX_SHEET_TITLE_UNQUOTED . "(\:" . self::REGEX_SHEET_TITLE_UNQUOTED . ")?\!\\$?([A-Ia-i]?[A-Za-z])?\\$?[0-9]+:\\$?([A-Ia-i]?[A-Za-z])?\\$?[0-9]+$/u", $token) and !preg_match('/[0-9]/', $this->lookAhead)) {
1033+
} elseif (preg_match('/^' . self::REGEX_SHEET_TITLE_UNQUOTED . "(\:" . self::REGEX_SHEET_TITLE_UNQUOTED . ")?\!\\$?([A-Ia-i]?[A-Za-z])?\\$?\d+:\\$?([A-Ia-i]?[A-Za-z])?\\$?\d+$/u", $token) and !preg_match('/\d/', $this->lookAhead)) {
10341034
// If it's an external range like Sheet1!A1:B2 or Sheet1:Sheet2!A1:B2 or Sheet1!$A$1:$B$2 or Sheet1:Sheet2!$A$1:$B$2
10351035
return $token;
1036-
} elseif (preg_match("/^'" . self::REGEX_SHEET_TITLE_QUOTED . "(\:" . self::REGEX_SHEET_TITLE_QUOTED . ")?'\!\\$?([A-Ia-i]?[A-Za-z])?\\$?[0-9]+:\\$?([A-Ia-i]?[A-Za-z])?\\$?[0-9]+$/u", $token) and !preg_match('/[0-9]/', $this->lookAhead)) {
1036+
} elseif (preg_match("/^'" . self::REGEX_SHEET_TITLE_QUOTED . "(\:" . self::REGEX_SHEET_TITLE_QUOTED . ")?'\!\\$?([A-Ia-i]?[A-Za-z])?\\$?\d+:\\$?([A-Ia-i]?[A-Za-z])?\\$?\d+$/u", $token) and !preg_match('/\d/', $this->lookAhead)) {
10371037
// If it's an external range like 'Sheet1'!A1:B2 or 'Sheet1:Sheet2'!A1:B2 or 'Sheet1'!$A$1:$B$2 or 'Sheet1:Sheet2'!$A$1:$B$2
10381038
return $token;
10391039
} elseif (is_numeric($token) and (!is_numeric($token . $this->lookAhead) or ($this->lookAhead == '')) and ($this->lookAhead != '!') and ($this->lookAhead != ':')) {
@@ -1250,39 +1250,39 @@ private function fact()
12501250
return $result;
12511251
}
12521252
// if it's a reference
1253-
if (preg_match('/^\$?[A-Ia-i]?[A-Za-z]\$?[0-9]+$/', $this->currentToken)) {
1253+
if (preg_match('/^\$?[A-Ia-i]?[A-Za-z]\$?\d+$/', $this->currentToken)) {
12541254
$result = $this->createTree($this->currentToken, '', '');
12551255
$this->advance();
12561256

12571257
return $result;
1258-
} elseif (preg_match('/^' . self::REGEX_SHEET_TITLE_UNQUOTED . "(\:" . self::REGEX_SHEET_TITLE_UNQUOTED . ")?\!\\$?[A-Ia-i]?[A-Za-z]\\$?[0-9]+$/u", $this->currentToken)) {
1258+
} elseif (preg_match('/^' . self::REGEX_SHEET_TITLE_UNQUOTED . "(\:" . self::REGEX_SHEET_TITLE_UNQUOTED . ")?\!\\$?[A-Ia-i]?[A-Za-z]\\$?\d+$/u", $this->currentToken)) {
12591259
// If it's an external reference (Sheet1!A1 or Sheet1:Sheet2!A1 or Sheet1!$A$1 or Sheet1:Sheet2!$A$1)
12601260
$result = $this->createTree($this->currentToken, '', '');
12611261
$this->advance();
12621262

12631263
return $result;
1264-
} elseif (preg_match("/^'" . self::REGEX_SHEET_TITLE_QUOTED . "(\:" . self::REGEX_SHEET_TITLE_QUOTED . ")?'\!\\$?[A-Ia-i]?[A-Za-z]\\$?[0-9]+$/u", $this->currentToken)) {
1264+
} elseif (preg_match("/^'" . self::REGEX_SHEET_TITLE_QUOTED . "(\:" . self::REGEX_SHEET_TITLE_QUOTED . ")?'\!\\$?[A-Ia-i]?[A-Za-z]\\$?\d+$/u", $this->currentToken)) {
12651265
// If it's an external reference ('Sheet1'!A1 or 'Sheet1:Sheet2'!A1 or 'Sheet1'!$A$1 or 'Sheet1:Sheet2'!$A$1)
12661266
$result = $this->createTree($this->currentToken, '', '');
12671267
$this->advance();
12681268

12691269
return $result;
1270-
} elseif (preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+:(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/', $this->currentToken) or
1271-
preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+\.\.(\$)?[A-Ia-i]?[A-Za-z](\$)?[0-9]+$/', $this->currentToken)) {
1270+
} elseif (preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?\d+:(\$)?[A-Ia-i]?[A-Za-z](\$)?\d+$/', $this->currentToken) or
1271+
preg_match('/^(\$)?[A-Ia-i]?[A-Za-z](\$)?\d+\.\.(\$)?[A-Ia-i]?[A-Za-z](\$)?\d+$/', $this->currentToken)) {
12721272
// if it's a range A1:B2 or $A$1:$B$2
12731273
// must be an error?
12741274
$result = $this->createTree($this->currentToken, '', '');
12751275
$this->advance();
12761276

12771277
return $result;
1278-
} elseif (preg_match('/^' . self::REGEX_SHEET_TITLE_UNQUOTED . "(\:" . self::REGEX_SHEET_TITLE_UNQUOTED . ")?\!\\$?([A-Ia-i]?[A-Za-z])?\\$?[0-9]+:\\$?([A-Ia-i]?[A-Za-z])?\\$?[0-9]+$/u", $this->currentToken)) {
1278+
} elseif (preg_match('/^' . self::REGEX_SHEET_TITLE_UNQUOTED . "(\:" . self::REGEX_SHEET_TITLE_UNQUOTED . ")?\!\\$?([A-Ia-i]?[A-Za-z])?\\$?\d+:\\$?([A-Ia-i]?[A-Za-z])?\\$?\d+$/u", $this->currentToken)) {
12791279
// If it's an external range (Sheet1!A1:B2 or Sheet1:Sheet2!A1:B2 or Sheet1!$A$1:$B$2 or Sheet1:Sheet2!$A$1:$B$2)
12801280
// must be an error?
12811281
$result = $this->createTree($this->currentToken, '', '');
12821282
$this->advance();
12831283

12841284
return $result;
1285-
} elseif (preg_match("/^'" . self::REGEX_SHEET_TITLE_QUOTED . "(\:" . self::REGEX_SHEET_TITLE_QUOTED . ")?'\!\\$?([A-Ia-i]?[A-Za-z])?\\$?[0-9]+:\\$?([A-Ia-i]?[A-Za-z])?\\$?[0-9]+$/u", $this->currentToken)) {
1285+
} elseif (preg_match("/^'" . self::REGEX_SHEET_TITLE_QUOTED . "(\:" . self::REGEX_SHEET_TITLE_QUOTED . ")?'\!\\$?([A-Ia-i]?[A-Za-z])?\\$?\d+:\\$?([A-Ia-i]?[A-Za-z])?\\$?\d+$/u", $this->currentToken)) {
12861286
// If it's an external range ('Sheet1'!A1:B2 or 'Sheet1'!A1:B2 or 'Sheet1'!$A$1:$B$2 or 'Sheet1'!$A$1:$B$2)
12871287
// must be an error?
12881288
$result = $this->createTree($this->currentToken, '', '');

0 commit comments

Comments
 (0)