From fa1fda729cab30a0a06362461b3dee94280cacfe Mon Sep 17 00:00:00 2001 From: Jason Nelson Date: Sun, 18 Feb 2024 18:46:03 -0800 Subject: [PATCH] Add implicit Span casts to ScopedBuffer --- MetadataExtractor/Formats/Bmp/BmpReader.cs | 4 ++-- MetadataExtractor/Formats/Exif/ExifTiffHandler.cs | 4 ++-- .../Formats/Photoshop/PhotoshopTiffHandler.cs | 4 ++-- MetadataExtractor/IO/BufferReader.Indexed.cs | 4 ++-- MetadataExtractor/IO/BufferReader.Sequential.cs | 6 +++--- MetadataExtractor/IO/ScopedBuffer.cs | 10 ++++++++++ 6 files changed, 21 insertions(+), 11 deletions(-) diff --git a/MetadataExtractor/Formats/Bmp/BmpReader.cs b/MetadataExtractor/Formats/Bmp/BmpReader.cs index dff15a04..8862b7f3 100644 --- a/MetadataExtractor/Formats/Bmp/BmpReader.cs +++ b/MetadataExtractor/Formats/Bmp/BmpReader.cs @@ -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); } diff --git a/MetadataExtractor/Formats/Exif/ExifTiffHandler.cs b/MetadataExtractor/Formats/Exif/ExifTiffHandler.cs index 2969664d..f39d51f9 100644 --- a/MetadataExtractor/Formats/Exif/ExifTiffHandler.cs +++ b/MetadataExtractor/Formats/Exif/ExifTiffHandler.cs @@ -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; diff --git a/MetadataExtractor/Formats/Photoshop/PhotoshopTiffHandler.cs b/MetadataExtractor/Formats/Photoshop/PhotoshopTiffHandler.cs index f85103fb..d74e8186 100644 --- a/MetadataExtractor/Formats/Photoshop/PhotoshopTiffHandler.cs +++ b/MetadataExtractor/Formats/Photoshop/PhotoshopTiffHandler.cs @@ -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; } } diff --git a/MetadataExtractor/IO/BufferReader.Indexed.cs b/MetadataExtractor/IO/BufferReader.Indexed.cs index 974651fb..44aa4cb1 100644 --- a/MetadataExtractor/IO/BufferReader.Indexed.cs +++ b/MetadataExtractor/IO/BufferReader.Indexed.cs @@ -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); } } diff --git a/MetadataExtractor/IO/BufferReader.Sequential.cs b/MetadataExtractor/IO/BufferReader.Sequential.cs index 1d707c31..a9f7f58b 100644 --- a/MetadataExtractor/IO/BufferReader.Sequential.cs +++ b/MetadataExtractor/IO/BufferReader.Sequential.cs @@ -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) diff --git a/MetadataExtractor/IO/ScopedBuffer.cs b/MetadataExtractor/IO/ScopedBuffer.cs index ebbb0d5b..eddae690 100644 --- a/MetadataExtractor/IO/ScopedBuffer.cs +++ b/MetadataExtractor/IO/ScopedBuffer.cs @@ -22,6 +22,16 @@ public ScopedBuffer(Span span) public readonly Span Span => _span; + public static implicit operator Span(ScopedBuffer bufferScope) + { + return bufferScope._span; + } + + public static implicit operator ReadOnlySpan(ScopedBuffer bufferScope) + { + return bufferScope._span; + } + public void Dispose() { if (_rentedBuffer is null) return;