Skip to content

Commit

Permalink
Add implicit Span casts to ScopedBuffer
Browse files Browse the repository at this point in the history
  • Loading branch information
iamcarbon committed Feb 19, 2024
1 parent 51ff3ee commit fa1fda7
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 11 deletions.
4 changes: 2 additions & 2 deletions MetadataExtractor/Formats/Bmp/BmpReader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -357,9 +357,9 @@ private static void ReadBitmapHeader(SequentialReader reader, BmpHeaderDirectory
? new ScopedBuffer(stackalloc byte[profileSize])
: new ScopedBuffer(profileSize);

reader.GetBytes(iccBuffer.Span);
reader.GetBytes(iccBuffer);

var iccDirectory = new IccReader().Extract(iccBuffer.Span);
var iccDirectory = new IccReader().Extract(iccBuffer);
iccDirectory.Parent = directory;
directories.Add(iccDirectory);
}
Expand Down
4 changes: 2 additions & 2 deletions MetadataExtractor/Formats/Exif/ExifTiffHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,8 @@ public override bool CustomProcessTag(in TiffReaderContext context, int tagId, i
if (tagId == ExifDirectoryBase.TagInterColorProfile)
{
using var iccBuffer = new ScopedBuffer(byteCount);
context.Reader.GetBytes(valueOffset, iccBuffer.Span);
var iccDirectory = new IccReader().Extract(iccBuffer.Span);
context.Reader.GetBytes(valueOffset, iccBuffer);
var iccDirectory = new IccReader().Extract(iccBuffer);
iccDirectory.Parent = CurrentDirectory;
Directories.Add(iccDirectory);
return true;
Expand Down
4 changes: 2 additions & 2 deletions MetadataExtractor/Formats/Photoshop/PhotoshopTiffHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ public override bool CustomProcessTag(in TiffReaderContext context, int tagId, i
case TagIccProfiles:
{
using var bufferScope = new ScopedBuffer(byteCount);
context.Reader.GetBytes(valueOffset, bufferScope.Span);
Directories.Add(new IccReader().Extract(bufferScope.Span));
context.Reader.GetBytes(valueOffset, bufferScope);
Directories.Add(new IccReader().Extract(bufferScope));
return true;
}
}
Expand Down
4 changes: 2 additions & 2 deletions MetadataExtractor/IO/BufferReader.Indexed.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,9 +197,9 @@ public readonly string GetString(int index, int bytesRequested, Encoding encodin
{
using var buffer = new ScopedBuffer(bytesRequested);

GetBytes(index, buffer.Span);
GetBytes(index, buffer);

return encoding.GetString(buffer.Span);
return encoding.GetString(buffer);
}
}

Expand Down
6 changes: 3 additions & 3 deletions MetadataExtractor/IO/BufferReader.Sequential.cs
Original file line number Diff line number Diff line change
Expand Up @@ -126,13 +126,13 @@ public string GetString(int bytesRequested, Encoding encoding)
if (bytesRequested is 0)
return "";

using ScopedBuffer bytes = bytesRequested <= 256
using ScopedBuffer buffer = bytesRequested <= 256
? new ScopedBuffer(stackalloc byte[bytesRequested])
: new ScopedBuffer(bytesRequested);

GetBytes(bytes.Span);
GetBytes(buffer);

return encoding.GetString(bytes.Span);
return encoding.GetString(buffer);
}

public StringValue GetNullTerminatedStringValue(int maxLengthBytes, Encoding? encoding = null, bool moveToMaxLength = false)
Expand Down
10 changes: 10 additions & 0 deletions MetadataExtractor/IO/ScopedBuffer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ public ScopedBuffer(Span<byte> span)

public readonly Span<byte> Span => _span;

public static implicit operator Span<byte>(ScopedBuffer bufferScope)
{
return bufferScope._span;
}

public static implicit operator ReadOnlySpan<byte>(ScopedBuffer bufferScope)
{
return bufferScope._span;
}

public void Dispose()
{
if (_rentedBuffer is null) return;
Expand Down

0 comments on commit fa1fda7

Please sign in to comment.