Skip to content

Commit 24e49b3

Browse files
committed
Some minor code cleanup.
1 parent 42df86e commit 24e49b3

38 files changed

+1686
-2136
lines changed

src/ExcelDataReader/Core/BinaryFormat/XlsBiffBlankCell.cs

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -36,22 +36,10 @@ internal XlsBiffBlankCell(byte[] bytes)
3636
/// Gets a value indicating whether the cell's record identifier is BIFF2-specific.
3737
/// The shared binary layout of BIFF2 cells are different from BIFF3+.
3838
/// </summary>
39-
public bool IsBiff2Cell
39+
public bool IsBiff2Cell => Id switch
4040
{
41-
get
42-
{
43-
switch (Id)
44-
{
45-
case BIFFRECORDTYPE.NUMBER_OLD:
46-
case BIFFRECORDTYPE.INTEGER_OLD:
47-
case BIFFRECORDTYPE.LABEL_OLD:
48-
case BIFFRECORDTYPE.BLANK_OLD:
49-
case BIFFRECORDTYPE.BOOLERR_OLD:
50-
return true;
51-
default:
52-
return false;
53-
}
54-
}
55-
}
41+
BIFFRECORDTYPE.NUMBER_OLD or BIFFRECORDTYPE.INTEGER_OLD or BIFFRECORDTYPE.LABEL_OLD or BIFFRECORDTYPE.BLANK_OLD or BIFFRECORDTYPE.BOOLERR_OLD => true,
42+
_ => false,
43+
};
5644
}
5745
}

src/ExcelDataReader/Core/BinaryFormat/XlsBiffFormatString.cs

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -34,19 +34,11 @@ internal XlsBiffFormatString(byte[] bytes, int biffVersion)
3434
}
3535
}
3636

37-
public ushort Index
37+
public ushort Index => Id switch
3838
{
39-
get
40-
{
41-
switch (Id)
42-
{
43-
case BIFFRECORDTYPE.FORMAT_V23:
44-
throw new NotSupportedException("Index is not available for BIFF2 and BIFF3 FORMAT records.");
45-
default:
46-
return ReadUInt16(0);
47-
}
48-
}
49-
}
39+
BIFFRECORDTYPE.FORMAT_V23 => throw new NotSupportedException("Index is not available for BIFF2 and BIFF3 FORMAT records."),
40+
_ => ReadUInt16(0),
41+
};
5042

5143
/// <summary>
5244
/// Gets the string value.

src/ExcelDataReader/Core/BinaryFormat/XlsBiffMergeCells.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public XlsBiffMergeCells(byte[] bytes)
1313
{
1414
var count = ReadUInt16(0);
1515

16-
MergeCells = new List<CellRange>();
16+
MergeCells = new();
1717
for (int i = 0; i < count; i++)
1818
{
1919
var fromRow = ReadInt16(2 + i * 8 + 0);
2020
var toRow = ReadInt16(2 + i * 8 + 2);
2121
var fromCol = ReadInt16(2 + i * 8 + 4);
2222
var toCol = ReadInt16(2 + i * 8 + 6);
2323

24-
CellRange mergeCell = new CellRange(fromCol, fromRow, toCol, toRow);
24+
CellRange mergeCell = new(fromCol, fromRow, toCol, toRow);
2525
MergeCells.Add(mergeCell);
2626
}
2727
}

src/ExcelDataReader/Core/BinaryFormat/XlsBiffSST.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,12 @@ namespace ExcelDataReader.Core.BinaryFormat
99
/// </summary>
1010
internal sealed class XlsBiffSST : XlsBiffRecord
1111
{
12-
private readonly List<IXlsString> _strings;
13-
private readonly XlsSSTReader _reader = new XlsSSTReader();
12+
private readonly List<IXlsString> _strings = new();
13+
private readonly XlsSSTReader _reader = new();
1414

1515
internal XlsBiffSST(byte[] bytes)
1616
: base(bytes)
1717
{
18-
_strings = new List<IXlsString>();
1918
ReadSstStrings();
2019
}
2120

src/ExcelDataReader/Core/BinaryFormat/XlsWorkbook.cs

Lines changed: 22 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -17,29 +17,27 @@ internal XlsWorkbook(Stream stream, string password, Encoding fallbackEncoding)
1717
{
1818
Stream = stream;
1919

20-
using (var biffStream = new XlsBiffStream(stream, password: password))
21-
{
22-
if (biffStream.BiffVersion == 0)
23-
throw new ExcelReaderException(Errors.ErrorWorkbookGlobalsInvalidData);
20+
using var biffStream = new XlsBiffStream(stream, password: password);
21+
if (biffStream.BiffVersion == 0)
22+
throw new ExcelReaderException(Errors.ErrorWorkbookGlobalsInvalidData);
2423

25-
BiffVersion = biffStream.BiffVersion;
26-
SecretKey = biffStream.SecretKey;
27-
Encryption = biffStream.Encryption;
28-
Encoding = biffStream.BiffVersion == 8 ? Encoding.Unicode : fallbackEncoding;
24+
BiffVersion = biffStream.BiffVersion;
25+
SecretKey = biffStream.SecretKey;
26+
Encryption = biffStream.Encryption;
27+
Encoding = biffStream.BiffVersion == 8 ? Encoding.Unicode : fallbackEncoding;
2928

30-
if (biffStream.BiffType == BIFFTYPE.WorkbookGlobals)
31-
{
32-
ReadWorkbookGlobals(biffStream);
33-
}
34-
else if (biffStream.BiffType == BIFFTYPE.Worksheet)
35-
{
36-
// set up 'virtual' bound sheet pointing at this
37-
Sheets.Add(new XlsBiffBoundSheet(0, XlsBiffBoundSheet.SheetType.Worksheet, XlsBiffBoundSheet.SheetVisibility.Visible, "Sheet"));
38-
}
39-
else
40-
{
41-
throw new ExcelReaderException(Errors.ErrorWorkbookGlobalsInvalidData);
42-
}
29+
if (biffStream.BiffType == BIFFTYPE.WorkbookGlobals)
30+
{
31+
ReadWorkbookGlobals(biffStream);
32+
}
33+
else if (biffStream.BiffType == BIFFTYPE.Worksheet)
34+
{
35+
// set up 'virtual' bound sheet pointing at this
36+
Sheets.Add(new XlsBiffBoundSheet(0, XlsBiffBoundSheet.SheetType.Worksheet, XlsBiffBoundSheet.SheetVisibility.Visible, "Sheet"));
37+
}
38+
else
39+
{
40+
throw new ExcelReaderException(Errors.ErrorWorkbookGlobalsInvalidData);
4341
}
4442
}
4543

@@ -156,7 +154,7 @@ private void ReadWorkbookGlobals(XlsBiffStream biffStream)
156154
var formats = new Dictionary<int, XlsBiffFormatString>();
157155

158156
XlsBiffRecord rec;
159-
while ((rec = biffStream.Read()) != null && !(rec is XlsBiffEof))
157+
while ((rec = biffStream.Read()) != null && rec is not XlsBiffEof)
160158
{
161159
switch (rec)
162160
{
@@ -196,11 +194,7 @@ private void ReadWorkbookGlobals(XlsBiffStream biffStream)
196194
SST = sst;
197195
break;
198196
case XlsBiffContinue sstContinue:
199-
if (SST != null)
200-
{
201-
SST.ReadContinueStrings(sstContinue);
202-
}
203-
197+
SST?.ReadContinueStrings(sstContinue);
204198
break;
205199
case XlsBiffRecord _ when rec.Id == BIFFRECORDTYPE.MMS:
206200
Mms = rec;
@@ -222,10 +216,7 @@ private void ReadWorkbookGlobals(XlsBiffStream biffStream)
222216
}
223217
}
224218

225-
if (SST != null)
226-
{
227-
SST.Flush();
228-
}
219+
SST?.Flush();
229220

230221
foreach (var format in formats)
231222
{

0 commit comments

Comments
 (0)