11// Copyright (c) Six Labors.
22// Licensed under the Six Labors Split License.
3- #nullable disable
43
54using System . Collections . Concurrent ;
5+ using System . Diagnostics . CodeAnalysis ;
66
77namespace SixLabors . ImageSharp . Formats ;
88
@@ -92,8 +92,9 @@ public void AddImageFormat(IImageFormat format)
9292 /// For the specified file extensions type find the e <see cref="IImageFormat"/>.
9393 /// </summary>
9494 /// <param name="extension">The extension to discover</param>
95- /// <returns>The <see cref="IImageFormat"/> if found otherwise null</returns>
96- public IImageFormat FindFormatByFileExtension ( string extension )
95+ /// <param name="format">The <see cref="IImageFormat"/> if found otherwise null</param>
96+ /// <returns>False if no format was found</returns>
97+ public bool TryFindFormatByFileExtension ( string extension , [ NotNullWhen ( true ) ] out IImageFormat ? format )
9798 {
9899 Guard . NotNullOrWhiteSpace ( extension , nameof ( extension ) ) ;
99100
@@ -102,15 +103,18 @@ public IImageFormat FindFormatByFileExtension(string extension)
102103 extension = extension [ 1 ..] ;
103104 }
104105
105- return this . imageFormats . FirstOrDefault ( x => x . FileExtensions . Contains ( extension , StringComparer . OrdinalIgnoreCase ) ) ;
106+ format = this . imageFormats . FirstOrDefault ( x =>
107+ x . FileExtensions . Contains ( extension , StringComparer . OrdinalIgnoreCase ) ) ;
108+
109+ return format != null ;
106110 }
107111
108112 /// <summary>
109113 /// For the specified mime type find the <see cref="IImageFormat"/>.
110114 /// </summary>
111115 /// <param name="mimeType">The mime-type to discover</param>
112116 /// <returns>The <see cref="IImageFormat"/> if found; otherwise null</returns>
113- public IImageFormat FindFormatByMimeType ( string mimeType )
117+ public IImageFormat ? FindFormatByMimeType ( string mimeType )
114118 => this . imageFormats . FirstOrDefault ( x => x . MimeTypes . Contains ( mimeType , StringComparer . OrdinalIgnoreCase ) ) ;
115119
116120 /// <summary>
@@ -160,11 +164,11 @@ public void AddImageFormatDetector(IImageFormatDetector detector)
160164 /// </summary>
161165 /// <param name="format">The format to discover</param>
162166 /// <returns>The <see cref="IImageDecoder"/> if found otherwise null</returns>
163- public IImageDecoder FindDecoder ( IImageFormat format )
167+ public IImageDecoder ? FindDecoder ( IImageFormat format )
164168 {
165169 Guard . NotNull ( format , nameof ( format ) ) ;
166170
167- return this . mimeTypeDecoders . TryGetValue ( format , out IImageDecoder decoder )
171+ return this . mimeTypeDecoders . TryGetValue ( format , out IImageDecoder ? decoder )
168172 ? decoder
169173 : null ;
170174 }
@@ -174,11 +178,11 @@ public IImageDecoder FindDecoder(IImageFormat format)
174178 /// </summary>
175179 /// <param name="format">The format to discover</param>
176180 /// <returns>The <see cref="IImageEncoder"/> if found otherwise null</returns>
177- public IImageEncoder FindEncoder ( IImageFormat format )
181+ public IImageEncoder ? FindEncoder ( IImageFormat format )
178182 {
179183 Guard . NotNull ( format , nameof ( format ) ) ;
180184
181- return this . mimeTypeEncoders . TryGetValue ( format , out IImageEncoder encoder )
185+ return this . mimeTypeEncoders . TryGetValue ( format , out IImageEncoder ? encoder )
182186 ? encoder
183187 : null ;
184188 }
0 commit comments