Free cookie consent management tool by TermsFeed Policy Generator

source: trunk/sources/HeuristicLab.ExtLibs/HeuristicLab.EPPlus/4.0.3/HeuristicLab.EPPlus-4.0.3/epplus-4.0.3 memory improvement.diff @ 12715

Last change on this file since 12715 was 12715, checked in by mkommend, 9 years ago

#2341: Reverse merged r12714 because one file change relates to another ticket (#2399).

File size: 10.1 KB
RevLine 
[12179]1diff --git a/EPPlus-4.0.3/ExcelCellBase.cs b/EPPlus-4.0.3/ExcelCellBase.cs
2--- a/EPPlus-4.0.3/ExcelCellBase.cs
3+++ b/EPPlus-4.0.3/ExcelCellBase.cs
4@@ -480,17 +480,15 @@
5             bool fixedRow, fixedCol;
6             return GetRowCol(address, out row, out col, throwException, out fixedRow, out fixedCol);
7         }
8-        internal static bool GetRowCol(string address, out int row, out int col, bool throwException, out bool fixedRow, out bool fixedCol)
9-        {
10+        internal static bool GetRowCol(string address, out int row, out int col, bool throwException, out bool fixedRow, out bool fixedCol) {
11             bool colPart = true;
12-            string sRow = "", sCol = "";
13+            int colStartIx = 0;
14+            int colLength = 0;
15             col = 0;
16+            row = 0;
17             fixedRow = false;
18             fixedCol = false;
19-            if (address.IndexOf(':') > 0)  //If it is a mult-cell address use
20-            {
21-                address = address.Substring(0, address.IndexOf(':'));
22-            }
23+
24             if (address.EndsWith("#REF!"))
25             {
26                 row = 0;
27@@ -501,71 +499,56 @@
28             int sheetMarkerIndex = address.IndexOf('!');
29             if (sheetMarkerIndex >= 0)
30             {
31-                address = address.Substring(sheetMarkerIndex + 1);
32+                colStartIx = sheetMarkerIndex + 1;
33             }
34 
35-            address = address.ToUpper(CultureInfo.InvariantCulture);
36-            for (int i = 0; i < address.Length; i++)
37+            for (int i = colStartIx; i < address.Length; i++)
38             {
39-                if ((address[i] >= 'A' && address[i] <= 'Z') && colPart && sCol.Length <= 3)
40+                char c = address[i];
41+                if (colPart && ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) && colLength <= 3)
42                 {
43-                    sCol += address[i];
44+                    col *= 26;
45+                    col += ((int)c) - 64;
46+                    colLength++;
47                 }
48-                else if (address[i] >= '0' && address[i] <= '9')
49+                else if (c >= '0' && c <= '9')
50                 {
51-                    sRow += address[i];
52+                    row *= 10;
53+                    row += ((int)c) - 48;
54                     colPart = false;
55                 }
56-                else if (address[i] == '$')
57+                else if (c == '$')
58                 {
59-                    if (i == 0)
60-                        fixedCol = true;
61-                    else
62+                    if (i == colStartIx)
63+                    {
64+                        colStartIx++;
65+                        fixedCol = true;
66+                    }
67+                    else
68+                    {
69+                        colPart = false;
70                         fixedRow = true;
71+                    }
72+                }
73+                else if (c == ':')
74+                {
75+                    break;
76                 }
77                 else
78                 {
79+                    row = 0;
80+                    col = 0;
81                     if (throwException)
82                     {
83                         throw (new Exception(string.Format("Invalid Address format {0}", address)));
84                     }
85                     else
86                     {
87-                        row = 0;
88-                        col = 0;
89                         return false;
90                     }
91                 }
92             }
93-
94-            // Get the column number
95-            if (sCol != "")
96-            {
97-                col = GetColumn(sCol);
98-            }
99-            else
100-            {
101-                col = 0;
102-                int.TryParse(sRow, out row);
103-                return row>0;
104-            }
105-            // Get the row number
106-            if (sRow == "") //Blank, fullRow
107-            {
108-                //if (throwException)
109-                //{
110-                //    throw (new Exception(string.Format("Invalid Address format {0}", address)));
111-                //}
112-                //else
113-                //{                   
114-                row = 0;
115-                return col > 0;
116-                //}
117-            }
118-            else
119-            {
120-                return int.TryParse(sRow, out row);
121-            }
122+            return row != 0 || col != 0;
123         }
124 
125         private static int GetColumn(string sCol)
126diff --git a/EPPlus-4.0.3/ExcelRangeBase.cs b/EPPlus-4.0.3/ExcelRangeBase.cs
127--- a/EPPlus-4.0.3/ExcelRangeBase.cs
128+++ b/EPPlus-4.0.3/ExcelRangeBase.cs
129@@ -58,7 +58,7 @@
130     /// <summary>
131  /// A range of cells
132  /// </summary>
133- public class ExcelRangeBase : ExcelAddress, IExcelCell, IDisposable, IEnumerable<ExcelRangeBase>, IEnumerator<ExcelRangeBase>
134+ public class ExcelRangeBase : ExcelAddress, IExcelCell, IEnumerable<ExcelRangeBase>
135  {
136    /// <summary>
137    /// Reference to the worksheet
138@@ -2757,69 +2757,84 @@
139         CellsStoreEnumerator<object> cellEnum;
140    public IEnumerator<ExcelRangeBase> GetEnumerator()
141    {
142-     Reset();
143-     return this;
144+            return new ExcelRangeBaseEnumerator(this);
145    }
146 
147    IEnumerator IEnumerable.GetEnumerator()
148    {
149-     Reset();
150-     return this;
151+            return new ExcelRangeBaseEnumerator(this);
152    }
153 
154    /// <summary>
155    /// The current range when enumerating
156    /// </summary>
157-   public ExcelRangeBase Current
158-   {
159-     get
160-     {
161-       return new ExcelRangeBase(_worksheet, ExcelAddressBase.GetAddress(cellEnum.Row, cellEnum.Column));
162-     }
163-   }
164+        public class ExcelRangeBaseEnumerator : IEnumerator<ExcelRangeBase> {
165+            private CellsStoreEnumerator<object> _cellEnum;
166+            private int _enumAddressIx = -1;
167+            private ExcelRangeBase _range;
168+            private ExcelRangeBase _current;
169 
170-   /// <summary>
171-   /// The current range when enumerating
172-   /// </summary>
173-   object IEnumerator.Current
174-   {
175-     get
176-     {
177-       return ((object)(new ExcelRangeBase(_worksheet, ExcelAddressBase.GetAddress(cellEnum.Row, cellEnum.Column))));
178-     }
179-   }
180-
181-   int _enumAddressIx = -1;
182-        public bool MoveNext()
183-   {
184-            if (cellEnum.Next())
185-            {
186-                return true;
187-            }
188-            else if (_addresses!=null)
189-            {
190-                _enumAddressIx++;
191-                if (_enumAddressIx < _addresses.Count)
192-                {
193-                    cellEnum = new CellsStoreEnumerator<object>(_worksheet._values,
194-                        _addresses[_enumAddressIx]._fromRow,
195-                        _addresses[_enumAddressIx]._fromCol,
196-                        _addresses[_enumAddressIx]._toRow,
197-                        _addresses[_enumAddressIx]._toCol);
198-                    return MoveNext();
199-                }
200-                else
201-                {
202-                    return false;
203+            /// <summary>
204+            /// The current range when enumerating
205+            /// </summary>
206+            public ExcelRangeBase Current {
207+                get {
208+                    return _current;
209                 }
210             }
211-            return false;
212-   }
213 
214-   public void Reset()
215-   {
216-            _enumAddressIx = -1;
217-            cellEnum = new CellsStoreEnumerator<object>(_worksheet._values, _fromRow, _fromCol, _toRow, _toCol);
218+            /// <summary>
219+            /// The current range when enumerating
220+            /// </summary>
221+            object IEnumerator.Current {
222+                get {
223+                    return _current;
224+                }
225+            }
226+
227+            public ExcelRangeBaseEnumerator(ExcelRangeBase range) {
228+                this._range = range;
229+                Reset();
230+            }
231+            public bool MoveNext() {
232+                if (_cellEnum.Next()) {
233+                    _current._fromCol = _cellEnum.Column;
234+                    _current._fromRow = _cellEnum.Row;
235+                    _current._toCol = _cellEnum.Column;
236+                    _current._toRow = _cellEnum.Row;
237+                    _current.Address = GetAddress(_current._fromRow, _current._fromCol);
238+                    return true;
239+                }
240+                else if (_range._addresses != null) {
241+                    _enumAddressIx++;
242+                    if (_enumAddressIx < _range._addresses.Count) {
243+                        _cellEnum = new CellsStoreEnumerator<object>(_range._worksheet._values,
244+                            _range._addresses[_enumAddressIx]._fromRow,
245+                            _range._addresses[_enumAddressIx]._fromCol,
246+                            _range._addresses[_enumAddressIx]._toRow,
247+                            _range._addresses[_enumAddressIx]._toCol);
248+                        return MoveNext();
249+                    }
250+                    else {
251+                        return false;
252+                    }
253+                }
254+                return false;
255+            }
256+
257+            public void Reset() {
258+                _enumAddressIx = -1;
259+                _cellEnum = new CellsStoreEnumerator<object>(_range._worksheet._values, _range._fromRow, _range._fromCol, _range._toRow, _range._toCol);
260+                _current = new ExcelRangeBase(_range._worksheet, ExcelAddressBase.GetAddress(_cellEnum.Row, _cellEnum.Column));
261+            }
262+
263+            public void Dispose() {
264+                if (_cellEnum != null) {
265+                    _cellEnum.Dispose();
266+                    _cellEnum = null;
267+                }
268+            }
269+
270         }
271 
272         //private void GetNextIndexEnum(int fromRow, int fromCol, int toRow, int toCol)
273diff --git a/EPPlus-4.0.3/Table/PivotTable/ExcelPivotTable.cs b/EPPlus-4.0.3/Table/PivotTable/ExcelPivotTable.cs
274--- a/EPPlus-4.0.3/Table/PivotTable/ExcelPivotTable.cs
275+++ b/EPPlus-4.0.3/Table/PivotTable/ExcelPivotTable.cs
276@@ -150,10 +150,7 @@
277 
278             LoadFields();
279 
280-            using (var r=sheet.Cells[address.Address])
281-            {
282-                r.Clear();
283-            }
284+            sheet.Cells[address.Address].Clear();
285         }
286         private void init()
287         {
Note: See TracBrowser for help on using the repository browser.