Skip to content

Commit aa93ab7

Browse files
committed
Use string interpolation and clean up redundant code.
1 parent d634aab commit aa93ab7

File tree

4 files changed

+40
-29
lines changed

4 files changed

+40
-29
lines changed

Exception/KaitaiStructError.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class KaitaiStructError : Exception
1212
protected string srcPath;
1313

1414
public KaitaiStructError(string msg, string srcPath)
15-
: base(srcPath + ": " + msg)
15+
: base($"srcPath: {msg}")
1616
{
1717
this.srcPath = srcPath;
1818
}

Exception/ValidationFailedError.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public class ValidationFailedError : KaitaiStructError
1111
protected KaitaiStream io;
1212

1313
public ValidationFailedError(string msg, KaitaiStream io, string srcPath)
14-
: base("at pos " + io.Pos + ": validation failed: " + msg, srcPath)
14+
: base($"at pos {io.Pos}: validation failed: {msg}", srcPath)
1515
{
1616
this.io = io;
1717
}
@@ -26,7 +26,7 @@ protected static string ByteArrayToHex(byte[] arr)
2626
sb.Append(' ');
2727
}
2828

29-
sb.Append(string.Format("{0:X2}", arr[i]));
29+
sb.Append($"{arr[i]:X2}");
3030
}
3131

3232
sb.Append(']');

Exception/ValidationNotEqualError.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,15 @@ public class ValidationNotEqualError : ValidationFailedError
1313
protected Object expected;
1414

1515
public ValidationNotEqualError(byte[] expected, byte[] actual, KaitaiStream io, string srcPath)
16-
: base("not equal, expected " + ByteArrayToHex(expected) + ", but got " + ByteArrayToHex(actual), io,
16+
: base($"not equal, expected {ByteArrayToHex(expected)}, but got {ByteArrayToHex(actual)}", io,
1717
srcPath)
1818
{
1919
this.expected = expected;
2020
this.actual = actual;
2121
}
2222

2323
public ValidationNotEqualError(Object expected, Object actual, KaitaiStream io, string srcPath)
24-
: base("not equal, expected " + expected + ", but got " + actual, io, srcPath)
24+
: base($"not equal, expected {expected}, but got {actual}", io, srcPath)
2525
{
2626
this.expected = expected;
2727
this.actual = actual;

KaitaiStream.cs

Lines changed: 35 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Collections.Generic;
33
using System.IO;
44
using System.IO.Compression;
5-
using System.Linq;
65

76
namespace Kaitai
87
{
@@ -16,8 +15,8 @@ public partial class KaitaiStream : IKaitaiStream
1615

1716
static readonly bool IsLittleEndian = BitConverter.IsLittleEndian;
1817

19-
private ulong Bits = 0;
20-
private int BitsLeft = 0;
18+
private ulong Bits;
19+
private int BitsLeft;
2120
private BinaryReader m_binaryReader;
2221

2322
protected Stream BaseStream;
@@ -40,7 +39,7 @@ public KaitaiStream(string file) : this(File.Open(file, FileMode.Open, FileAcces
4039
public KaitaiStream(byte[] bytes) : this(new MemoryStream(bytes))
4140
{
4241
}
43-
42+
4443
protected BinaryReader BinaryReader
4544
{
4645
get => m_binaryReader ?? (BinaryReader = new BinaryReader(BaseStream));
@@ -304,7 +303,7 @@ public ulong ReadBitsInt(int n)
304303
// 1 bit => 1 byte
305304
// 8 bits => 1 byte
306305
// 9 bits => 2 bytes
307-
int bytesNeeded = ((bitsNeeded - 1) / 8) + 1;
306+
int bytesNeeded = (bitsNeeded - 1) / 8 + 1;
308307
byte[] buf = ReadBytes(bytesNeeded);
309308
for (int i = 0; i < buf.Length; i++)
310309
{
@@ -339,11 +338,11 @@ public ulong ReadBitsIntLe(int n)
339338
// 1 bit => 1 byte
340339
// 8 bits => 1 byte
341340
// 9 bits => 2 bytes
342-
int bytesNeeded = ((bitsNeeded - 1) / 8) + 1;
341+
int bytesNeeded = (bitsNeeded - 1) / 8 + 1;
343342
byte[] buf = ReadBytes(bytesNeeded);
344343
for (int i = 0; i < buf.Length; i++)
345344
{
346-
ulong v = (ulong)((ulong)buf[i] << BitsLeft);
345+
ulong v = (ulong)buf[i] << BitsLeft;
347346
Bits |= v;
348347
BitsLeft += 8;
349348
}
@@ -353,7 +352,7 @@ public ulong ReadBitsIntLe(int n)
353352
ulong mask = GetMaskOnes(n);
354353

355354
// derive reading result
356-
ulong res = (Bits & mask);
355+
ulong res = Bits & mask;
357356

358357
// remove bottom bits that we've just read by shifting
359358
Bits >>= n;
@@ -379,10 +378,10 @@ private static ulong GetMaskOnes(int n)
379378
public byte[] ReadBytes(long count)
380379
{
381380
if (count < 0 || count > Int32.MaxValue)
382-
throw new ArgumentOutOfRangeException("requested " + count + " bytes, while only non-negative int32 amount of bytes possible");
383-
byte[] bytes = BinaryReader.ReadBytes((int) count);
381+
throw new ArgumentOutOfRangeException($"requested {count} bytes, while only non-negative int32 amount of bytes possible");
382+
byte[] bytes = BinaryReader.ReadBytes((int)count);
384383
if (bytes.Length < count)
385-
throw new EndOfStreamException("requested " + count + " bytes, but got only " + bytes.Length + " bytes");
384+
throw new EndOfStreamException($"requested {count} bytes, but got only {bytes.Length} bytes");
386385
return bytes;
387386
}
388387

@@ -394,10 +393,10 @@ public byte[] ReadBytes(long count)
394393
public byte[] ReadBytes(ulong count)
395394
{
396395
if (count > Int32.MaxValue)
397-
throw new ArgumentOutOfRangeException("requested " + count + " bytes, while only non-negative int32 amount of bytes possible");
396+
throw new ArgumentOutOfRangeException($"requested {count} bytes, while only non-negative int32 amount of bytes possible");
398397
byte[] bytes = BinaryReader.ReadBytes((int)count);
399398
if (bytes.Length < (int)count)
400-
throw new EndOfStreamException("requested " + count + " bytes, but got only " + bytes.Length + " bytes");
399+
throw new EndOfStreamException($"requested {count} bytes, but got only {bytes.Length} bytes");
401400
return bytes;
402401
}
403402

@@ -449,7 +448,8 @@ public byte[] ReadBytesTerm(byte terminator, bool includeTerminator, bool consum
449448
{
450449
if (IsEof)
451450
{
452-
if (eosError) throw new EndOfStreamException(string.Format("End of stream reached, but no terminator `{0}` found", terminator));
451+
if (eosError)
452+
throw new EndOfStreamException($"End of stream reached, but no terminator `{terminator}` found");
453453
break;
454454
}
455455

@@ -460,8 +460,10 @@ public byte[] ReadBytesTerm(byte terminator, bool includeTerminator, bool consum
460460
if (!consumeTerminator) Seek(Pos - 1);
461461
break;
462462
}
463+
463464
bytes.Add(b);
464465
}
466+
465467
return bytes.ToArray();
466468
}
467469

@@ -476,13 +478,14 @@ public byte[] EnsureFixedContents(byte[] expected)
476478

477479
if (bytes.Length != expected.Length)
478480
{
479-
throw new Exception(string.Format("Expected bytes: {0} ({1} bytes), Instead got: {2} ({3} bytes)", Convert.ToBase64String(expected), expected.Length, Convert.ToBase64String(bytes), bytes.Length));
481+
throw new Exception($"Expected bytes: {Convert.ToBase64String(expected)} ({expected.Length} bytes), Instead got: {Convert.ToBase64String(bytes)} ({bytes.Length} bytes)");
480482
}
483+
481484
for (int i = 0; i < bytes.Length; i++)
482485
{
483486
if (bytes[i] != expected[i])
484487
{
485-
throw new Exception(string.Format("Expected bytes: {0} ({1} bytes), Instead got: {2} ({3} bytes)", Convert.ToBase64String(expected), expected.Length, Convert.ToBase64String(bytes), bytes.Length));
488+
throw new Exception($"Expected bytes: {Convert.ToBase64String(expected)} ({expected.Length} bytes), Instead got: {Convert.ToBase64String(bytes)} ({bytes.Length} bytes)");
486489
}
487490
}
488491

@@ -533,6 +536,7 @@ public byte[] ProcessXor(byte[] value, int key)
533536
{
534537
result[i] = (byte)(value[i] ^ key);
535538
}
539+
536540
return result;
537541
}
538542

@@ -551,6 +555,7 @@ public byte[] ProcessXor(byte[] value, byte[] key)
551555
{
552556
result[i] = (byte)(value[i] ^ key[j]);
553557
}
558+
554559
return result;
555560
}
556561

@@ -564,7 +569,8 @@ public byte[] ProcessXor(byte[] value, byte[] key)
564569
/// <returns></returns>
565570
public byte[] ProcessRotateLeft(byte[] data, int amount, int groupSize)
566571
{
567-
if (amount > 7 || amount < -7) throw new ArgumentException("Rotation of more than 7 cannot be performed.", "amount");
572+
if (amount > 7 || amount < -7)
573+
throw new ArgumentException("Rotation of more than 7 cannot be performed.", "amount");
568574
if (amount < 0) amount += 8; // Rotation of -2 is the same as rotation of +6
569575

570576
byte[] r = new byte[data.Length];
@@ -575,12 +581,14 @@ public byte[] ProcessRotateLeft(byte[] data, int amount, int groupSize)
575581
{
576582
byte bits = data[i];
577583
// http://stackoverflow.com/a/812039
578-
r[i] = (byte) ((bits << amount) | (bits >> (8 - amount)));
584+
r[i] = (byte)((bits << amount) | (bits >> (8 - amount)));
579585
}
586+
580587
break;
581588
default:
582-
throw new NotImplementedException(string.Format("Unable to rotate a group of {0} bytes yet", groupSize));
589+
throw new NotImplementedException($"Unable to rotate a group of {groupSize} bytes yet");
583590
}
591+
584592
return r;
585593
}
586594

@@ -597,7 +605,8 @@ public byte[] ProcessZlib(byte[] data)
597605
// There's also 4 checksum bytes at the end of the stream.
598606

599607
byte zlibCmf = data[0];
600-
if ((zlibCmf & 0x0F) != 0x08) throw new NotSupportedException("Only the DEFLATE algorithm is supported for zlib data.");
608+
if ((zlibCmf & 0x0F) != 0x08)
609+
throw new NotSupportedException("Only the DEFLATE algorithm is supported for zlib data.");
601610

602611
const int zlibFooter = 4;
603612
int zlibHeader = 2;
@@ -674,18 +683,20 @@ public static int ByteArrayCompare(byte[] a, byte[] b)
674683
int al = a.Length;
675684
int bl = b.Length;
676685
int minLen = al < bl ? al : bl;
677-
for (int i = 0; i < minLen; i++) {
686+
for (int i = 0; i < minLen; i++)
687+
{
678688
int cmp = a[i] - b[i];
679689
if (cmp != 0)
680690
return cmp;
681691
}
682692

683693
// Reached the end of at least one of the arrays
684-
if (al == bl) {
694+
if (al == bl)
695+
{
685696
return 0;
686-
} else {
687-
return al - bl;
688697
}
698+
699+
return al - bl;
689700
}
690701

691702
#endregion

0 commit comments

Comments
 (0)