More perforan version of stringFromColumnIndex that takes advantage of the PHP 5.3 modified ternary

This commit is contained in:
MarkBaker 2017-09-10 22:51:05 +01:00
parent d576855031
commit 77b3c12fbc
1 changed files with 13 additions and 20 deletions

View File

@ -856,32 +856,25 @@ class Cell
/** /**
* String from columnindex. * String from columnindex.
* *
* @param int $pColumnIndex Column index (A = 0) * @param int $columnIndex Column index (A = 0)
* *
* @return string * @return string
*/ */
public static function stringFromColumnIndex($pColumnIndex) public static function stringFromColumnIndex($columnIndex)
{ {
// Using a lookup cache adds a slight memory overhead, but boosts speed static $indexCache = array();
// caching using a static within the method is faster than a class static,
// though it's additional memory overhead
static $_indexCache = [];
if (!isset($_indexCache[$pColumnIndex])) { if (!isset($indexCache[$columnIndex])) {
// Determine column string $indexValue = $columnIndex + 1;
if ($pColumnIndex < 26) { $base26 = null;
$_indexCache[$pColumnIndex] = chr(65 + $pColumnIndex); do {
} elseif ($pColumnIndex < 702) { $characterValue = ($indexValue % 26) ?: 26;
$_indexCache[$pColumnIndex] = chr(64 + ($pColumnIndex / 26)) . $indexValue = ($indexValue - $characterValue) / 26;
chr(65 + $pColumnIndex % 26); $base26 = chr($characterValue + 64).($base26 ?: '');
} else { } while ($indexValue > 0);
$_indexCache[$pColumnIndex] = chr(64 + (($pColumnIndex - 26) / 676)) . $indexCache[$columnIndex] = $base26;
chr(65 + ((($pColumnIndex - 26) % 676) / 26)) .
chr(65 + $pColumnIndex % 26);
} }
} return $indexCache[$columnIndex];
return $_indexCache[$pColumnIndex];
} }
/** /**