1313 */
1414abstract class Coordinate
1515{
16- public const A1_COORDINATE_REGEX = '/^(?<absolute_col >\$?)(?<col_ref> [A-Z]{1,3})(?<absolute_row >\$?)(?<row_ref> \d{1,7})$/i ' ;
16+ public const A1_COORDINATE_REGEX = '/^(?<col >\$?[A-Z]{1,3})(?<row >\$?\d{1,7})$/i ' ;
1717
1818 /**
1919 * Default range variable constant.
@@ -32,7 +32,7 @@ abstract class Coordinate
3232 public static function coordinateFromString ($ cellAddress )
3333 {
3434 if (preg_match (self ::A1_COORDINATE_REGEX , $ cellAddress , $ matches )) {
35- return [$ matches ['absolute_col ' ] . $ matches [ ' col_ref ' ] , $ matches ['absolute_row ' ] . $ matches [ ' row_ref ' ]];
35+ return [$ matches ['col ' ] , $ matches ['row ' ]];
3636 } elseif (self ::coordinateIsRange ($ cellAddress )) {
3737 throw new Exception ('Cell coordinate string can not be a range of cells ' );
3838 } elseif ($ cellAddress == '' ) {
@@ -276,35 +276,42 @@ public static function columnIndexFromString($columnAddress)
276276 if (isset ($ indexCache [$ columnAddress ])) {
277277 return $ indexCache [$ columnAddress ];
278278 }
279- // It's surprising how costly the strtoupper() and ord() calls actually are, so we use a lookup array rather than use ord()
280- // and make it case insensitive to get rid of the strtoupper() as well. Because it's a static, there's no significant
281- // memory overhead either
279+ // It's surprising how costly the strtoupper() and ord() calls actually are, so we use a lookup array
280+ // rather than use ord() and make it case insensitive to get rid of the strtoupper() as well.
281+ // Because it's a static, there's no significant memory overhead either.
282282 static $ columnLookup = [
283- 'A ' => 1 , 'B ' => 2 , 'C ' => 3 , 'D ' => 4 , 'E ' => 5 , 'F ' => 6 , 'G ' => 7 , 'H ' => 8 , 'I ' => 9 , 'J ' => 10 , 'K ' => 11 , 'L ' => 12 , 'M ' => 13 ,
284- 'N ' => 14 , 'O ' => 15 , 'P ' => 16 , 'Q ' => 17 , 'R ' => 18 , 'S ' => 19 , 'T ' => 20 , 'U ' => 21 , 'V ' => 22 , 'W ' => 23 , 'X ' => 24 , 'Y ' => 25 , 'Z ' => 26 ,
285- 'a ' => 1 , 'b ' => 2 , 'c ' => 3 , 'd ' => 4 , 'e ' => 5 , 'f ' => 6 , 'g ' => 7 , 'h ' => 8 , 'i ' => 9 , 'j ' => 10 , 'k ' => 11 , 'l ' => 12 , 'm ' => 13 ,
286- 'n ' => 14 , 'o ' => 15 , 'p ' => 16 , 'q ' => 17 , 'r ' => 18 , 's ' => 19 , 't ' => 20 , 'u ' => 21 , 'v ' => 22 , 'w ' => 23 , 'x ' => 24 , 'y ' => 25 , 'z ' => 26 ,
283+ 'A ' => 1 , 'B ' => 2 , 'C ' => 3 , 'D ' => 4 , 'E ' => 5 , 'F ' => 6 , 'G ' => 7 , 'H ' => 8 , 'I ' => 9 , 'J ' => 10 ,
284+ 'K ' => 11 , 'L ' => 12 , 'M ' => 13 , 'N ' => 14 , 'O ' => 15 , 'P ' => 16 , 'Q ' => 17 , 'R ' => 18 , 'S ' => 19 ,
285+ 'T ' => 20 , 'U ' => 21 , 'V ' => 22 , 'W ' => 23 , 'X ' => 24 , 'Y ' => 25 , 'Z ' => 26 ,
286+ 'a ' => 1 , 'b ' => 2 , 'c ' => 3 , 'd ' => 4 , 'e ' => 5 , 'f ' => 6 , 'g ' => 7 , 'h ' => 8 , 'i ' => 9 , 'j ' => 10 ,
287+ 'k ' => 11 , 'l ' => 12 , 'm ' => 13 , 'n ' => 14 , 'o ' => 15 , 'p ' => 16 , 'q ' => 17 , 'r ' => 18 , 's ' => 19 ,
288+ 't ' => 20 , 'u ' => 21 , 'v ' => 22 , 'w ' => 23 , 'x ' => 24 , 'y ' => 25 , 'z ' => 26 ,
287289 ];
288290
289- // We also use the language construct isset() rather than the more costly strlen() function to match the length of $columnAddress
290- // for improved performance
291+ // We also use the language construct isset() rather than the more costly strlen() function to match the
292+ // length of $columnAddress for improved performance
291293 if (isset ($ columnAddress [0 ])) {
292294 if (!isset ($ columnAddress [1 ])) {
293295 $ indexCache [$ columnAddress ] = $ columnLookup [$ columnAddress ];
294296
295297 return $ indexCache [$ columnAddress ];
296298 } elseif (!isset ($ columnAddress [2 ])) {
297- $ indexCache [$ columnAddress ] = $ columnLookup [$ columnAddress [0 ]] * 26 + $ columnLookup [$ columnAddress [1 ]];
299+ $ indexCache [$ columnAddress ] = $ columnLookup [$ columnAddress [0 ]] * 26
300+ + $ columnLookup [$ columnAddress [1 ]];
298301
299302 return $ indexCache [$ columnAddress ];
300303 } elseif (!isset ($ columnAddress [3 ])) {
301- $ indexCache [$ columnAddress ] = $ columnLookup [$ columnAddress [0 ]] * 676 + $ columnLookup [$ columnAddress [1 ]] * 26 + $ columnLookup [$ columnAddress [2 ]];
304+ $ indexCache [$ columnAddress ] = $ columnLookup [$ columnAddress [0 ]] * 676
305+ + $ columnLookup [$ columnAddress [1 ]] * 26
306+ + $ columnLookup [$ columnAddress [2 ]];
302307
303308 return $ indexCache [$ columnAddress ];
304309 }
305310 }
306311
307- throw new Exception ('Column string index can not be ' . ((isset ($ columnAddress [0 ])) ? 'longer than 3 characters ' : 'empty ' ));
312+ throw new Exception (
313+ 'Column string index can not be ' . ((isset ($ columnAddress [0 ])) ? 'longer than 3 characters ' : 'empty ' )
314+ );
308315 }
309316
310317 /**
@@ -317,14 +324,15 @@ public static function columnIndexFromString($columnAddress)
317324 public static function stringFromColumnIndex ($ columnIndex )
318325 {
319326 static $ indexCache = [];
327+ static $ lookupCache = ' ABCDEFGHIJKLMNOPQRSTUVWXYZ ' ;
320328
321329 if (!isset ($ indexCache [$ columnIndex ])) {
322330 $ indexValue = $ columnIndex ;
323- $ base26 = null ;
331+ $ base26 = '' ;
324332 do {
325333 $ characterValue = ($ indexValue % 26 ) ?: 26 ;
326334 $ indexValue = ($ indexValue - $ characterValue ) / 26 ;
327- $ base26 = chr ( $ characterValue + 64 ) . ( $ base26 ?: '' ) ;
335+ $ base26 = $ lookupCache [ $ characterValue] . $ base26 ;
328336 } while ($ indexValue > 0 );
329337 $ indexCache [$ columnIndex ] = $ base26 ;
330338 }
0 commit comments