Correct Praveen's performance optimization to correctly handle deleted rows at the end of the worksheet. Previously we included the deleted row in the worksheet because we did not take the 'RowCount' into account. When the final worksheet row is deleted, the 'RowCount' is the only thing changed.
diff --git a/EPPlus/CellStore.cs b/EPPlus/CellStore.cs
index 0f8a600..a4dd683 100644
--- a/EPPlus/CellStore.cs
+++ b/EPPlus/CellStore.cs
@@ -68,7 +68,7 @@
{
var page = (short)(Row >> CellStore<int>.pageBits);
_searchIx.Index = page;
- var res = (_pages!=null && page>=0 && page<_pages.Length && _pages[page]!=null && _pages[page].Index == page) ? page : Array.BinarySearch(_pages, 0, PageCount, _searchIx);
+ var res = (_pages != null && page >= 0 && page < PageCount && page < _pages.Length && _pages[page] != null && _pages[page].Index == page) ? page : Array.BinarySearch(_pages, 0, PageCount, _searchIx);
if (res >= 0)
{
GetPage(Row, ref res);
@@ -263,7 +263,7 @@
internal int GetPosition(int offset)
{
_searchIx.Index = (short)offset;
- return (Rows!=null && offset>0 && offset-1<Rows.Length && Rows[offset-1]!=null && Rows[offset-1].Index == offset) ? offset-1 : Array.BinarySearch(Rows, 0, RowCount, _searchIx);
+ return (Rows != null && offset > 0 && offset - 1 < RowCount && offset - 1 < Rows.Length && Rows[offset - 1] != null && Rows[offset - 1].Index == offset) ? offset - 1 : Array.BinarySearch(Rows, 0, RowCount, _searchIx);
}
internal int GetNextRow(int row)
{
@@ -355,7 +355,7 @@
internal int GetPosition(int Column)
{
_searchIx.Index = (short)Column;
- return (_columnIndex!=null && Column>0 && Column-1<_columnIndex.Length && _columnIndex[Column-1]!=null && _columnIndex[Column-1].Index == Column) ? Column-1 : Array.BinarySearch(_columnIndex, 0, ColumnCount, _searchIx);
+ return (_columnIndex != null && Column > 0 && Column - 1 < ColumnCount && Column - 1 < _columnIndex.Length && _columnIndex[Column - 1] != null && _columnIndex[Column - 1].Index == Column) ? Column - 1 : Array.BinarySearch(_columnIndex, 0, ColumnCount, _searchIx);
}
internal CellStore<T> Clone()
{
@@ -568,7 +568,7 @@
}
short ix = (short)(Row - pageItem.IndexOffset);
_searchIx.Index = ix;
- var cellPos = (pageItem.Rows!=null && ix>0 && ix-1<pageItem.Rows.Length && pageItem.Rows[ix-1]!=null && pageItem.Rows[ix-1].Index == ix) ? ix-1 : Array.BinarySearch(pageItem.Rows, 0, pageItem.RowCount, _searchIx);
+ var cellPos = (pageItem.Rows != null && ix > 0 && ix - 1 < pageItem.RowCount && ix - 1 < pageItem.Rows.Length && pageItem.Rows[ix - 1] != null && pageItem.Rows[ix - 1].Index == ix) ? ix - 1 : Array.BinarySearch(pageItem.Rows, 0, pageItem.RowCount, _searchIx);
if (cellPos >= 0)
{
return pageItem.Rows[cellPos].IndexPointer;
@@ -609,7 +609,7 @@
{
lock (_columnIndex)
{
- var col = (_columnIndex!=null && Column>0 && Column-1 < _columnIndex.Length && _columnIndex[Column-1]!=null && _columnIndex[Column-1].Index == Column) ? Column-1 : Array.BinarySearch(_columnIndex, 0, ColumnCount, new IndexBase() { Index = (short)(Column) });
+ var col = (_columnIndex != null && Column > 0 && Column - 1 < ColumnCount && Column - 1 < _columnIndex.Length && _columnIndex[Column - 1] != null && _columnIndex[Column - 1].Index == Column) ? Column - 1 : Array.BinarySearch(_columnIndex, 0, ColumnCount, new IndexBase() { Index = (short)(Column) });
var page = (short)(Row >> pageBits);
if (col >= 0)
{
@@ -645,7 +645,7 @@
short ix = (short)(Row - ((pageItem.Index << pageBits) + pageItem.Offset));
_searchIx.Index = ix;
- var cellPos = (pageItem.Rows!=null && ix>=0 && ix<pageItem.Rows.Length && pageItem.Rows[ix]!=null && pageItem.Rows[ix].Index == ix) ? ix : Array.BinarySearch(pageItem.Rows, 0, pageItem.RowCount, _searchIx);
+ var cellPos = (pageItem.Rows != null && ix >= 0 && ix < pageItem.RowCount && ix < pageItem.Rows.Length && pageItem.Rows[ix] != null && pageItem.Rows[ix].Index == ix) ? ix : Array.BinarySearch(pageItem.Rows, 0, pageItem.RowCount, _searchIx);
if (cellPos < 0)
{
cellPos = ~cellPos;