Skip to content

Commit 6c398fd

Browse files
committed
Change Advance to return a span
Use `Skip` to skip data and completely ignore it.
1 parent b983a8f commit 6c398fd

File tree

2 files changed

+23
-35
lines changed

2 files changed

+23
-35
lines changed

MetadataExtractor/Formats/Apple/BplistReader.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ Trailer ReadTrailer()
9595
var reader = new BufferReader(bplist.AsSpan(bplist.Length - Trailer.SizeBytes), isBigEndian: true);
9696

9797
// Skip 5-byte unused values, 1-byte sort version.
98-
reader.Advance(6);
98+
reader.Skip(5 + 1);
9999

100100
return new Trailer
101101
{

MetadataExtractor/IO/BufferReader.cs

Lines changed: 22 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -25,48 +25,41 @@ public byte GetByte()
2525

2626
public void GetBytes(scoped Span<byte> bytes)
2727
{
28-
if (_position + bytes.Length > _bytes.Length)
29-
throw new IOException("End of data reached.");
30-
31-
_bytes.Slice(_position, bytes.Length).CopyTo(bytes);
32-
_position += bytes.Length;
28+
var buffer = Advance(bytes.Length);
29+
buffer.CopyTo(bytes);
3330
}
3431

3532
public byte[] GetBytes(int count)
3633
{
37-
if (_position + count > _bytes.Length)
38-
throw new IOException("End of data reached.");
39-
34+
var buffer = Advance(count);
4035
var bytes = new byte[count];
4136

42-
_bytes.Slice(_position, count).CopyTo(bytes);
43-
_position += count;
37+
buffer.CopyTo(bytes);
4438
return bytes;
4539
}
4640

47-
public void Advance(int n)
41+
private ReadOnlySpan<byte> Advance(int count)
4842
{
49-
Debug.Assert(n >= 0, "n must be zero or greater");
43+
Debug.Assert(count >= 0, "count must be zero or greater");
5044

51-
if (_position + n > _bytes.Length)
45+
if (_position + count > _bytes.Length)
5246
throw new IOException("End of data reached.");
5347

54-
_position += n;
48+
var span = _bytes.Slice(_position, count);
49+
50+
_position += count;
51+
52+
return span;
5553
}
5654

57-
public bool TryAdvance(int n)
55+
public void Skip(int count)
5856
{
59-
Debug.Assert(n >= 0, "n must be zero or greater");
57+
Debug.Assert(count >= 0, "count must be zero or greater");
6058

61-
_position += n;
62-
63-
if (_position > _bytes.Length)
64-
{
65-
_position = _bytes.Length;
66-
return false;
67-
}
59+
if (_position + count > _bytes.Length)
60+
throw new IOException("End of data reached.");
6861

69-
return true;
62+
_position += count;
7063
}
7164

7265
public sbyte GetSByte()
@@ -76,8 +69,7 @@ public sbyte GetSByte()
7669

7770
public ushort GetUInt16()
7871
{
79-
var bytes = _bytes.Slice(_position, 2);
80-
Advance(2);
72+
var bytes = Advance(2);
8173

8274
return _isBigEndian
8375
? BinaryPrimitives.ReadUInt16BigEndian(bytes)
@@ -86,8 +78,7 @@ public ushort GetUInt16()
8678

8779
public short GetInt16()
8880
{
89-
var bytes = _bytes.Slice(_position, 2);
90-
Advance(2);
81+
var bytes = Advance(2);
9182

9283
return _isBigEndian
9384
? BinaryPrimitives.ReadInt16BigEndian(bytes)
@@ -96,8 +87,7 @@ public short GetInt16()
9687

9788
public uint GetUInt32()
9889
{
99-
var bytes = _bytes.Slice(_position, 4);
100-
Advance(4);
90+
var bytes = Advance(4);
10191

10292
return _isBigEndian
10393
? BinaryPrimitives.ReadUInt32BigEndian(bytes)
@@ -106,8 +96,7 @@ public uint GetUInt32()
10696

10797
public int GetInt32()
10898
{
109-
var bytes = _bytes.Slice(_position, 4);
110-
Advance(4);
99+
var bytes = Advance(4);
111100

112101
return _isBigEndian
113102
? BinaryPrimitives.ReadInt32BigEndian(bytes)
@@ -116,8 +105,7 @@ public int GetInt32()
116105

117106
public long GetInt64()
118107
{
119-
var bytes = _bytes.Slice(_position, 8);
120-
Advance(8);
108+
var bytes = Advance(8);
121109

122110
return _isBigEndian
123111
? BinaryPrimitives.ReadInt64BigEndian(bytes)

0 commit comments

Comments
 (0)