do not throw ArgumentNullException in TryCleanString

This commit is contained in:
cvium 2021-03-07 22:49:31 +01:00
parent 8615847a8a
commit e0db17a935
3 changed files with 12 additions and 3 deletions

View File

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
namespace Emby.Naming.Video
@ -16,7 +17,7 @@ namespace Emby.Naming.Video
/// <param name="expressions">List of regex to parse name and year from.</param>
/// <param name="newName">Parsing result string.</param>
/// <returns>True if parsing was successful.</returns>
public static bool TryClean(string name, IReadOnlyList<Regex> expressions, out ReadOnlySpan<char> newName)
public static bool TryClean(string name, IReadOnlyList<Regex> expressions, [NotNullWhen(true)] out ReadOnlySpan<char> newName)
{
var len = expressions.Count;
for (int i = 0; i < len; i++)
@ -31,8 +32,14 @@ namespace Emby.Naming.Video
return false;
}
private static bool TryClean(string name, Regex expression, out ReadOnlySpan<char> newName)
private static bool TryClean(string name, Regex expression, [NotNullWhen(true)] out ReadOnlySpan<char> newName)
{
if (string.IsNullOrEmpty(name))
{
newName = null;
return false;
}
var match = expression.Match(name);
int index = match.Index;
if (match.Success && index != 0)

View File

@ -1,4 +1,5 @@
using System;
using System.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
using Emby.Naming.Common;
@ -146,7 +147,7 @@ namespace Emby.Naming.Video
/// <param name="name">Raw name.</param>
/// <param name="newName">Clean name.</param>
/// <returns>True if cleaning of name was successful.</returns>
public bool TryCleanString(string name, out ReadOnlySpan<char> newName)
public bool TryCleanString(string name, [NotNullWhen(true)] out ReadOnlySpan<char> newName)
{
return CleanStringParser.TryClean(name, _options.CleanStringRegexes, out newName);
}

View File

@ -28,6 +28,7 @@ namespace Jellyfin.Naming.Tests.Video
[InlineData("Crouching.Tiger.Hidden.Dragon.BDrip.mkv", "Crouching.Tiger.Hidden.Dragon")]
[InlineData("Crouching.Tiger.Hidden.Dragon.BDrip-HDC.mkv", "Crouching.Tiger.Hidden.Dragon")]
[InlineData("Crouching.Tiger.Hidden.Dragon.4K.UltraHD.HDR.BDrip-HDC.mkv", "Crouching.Tiger.Hidden.Dragon")]
[InlineData(null, null)]
// FIXME: [InlineData("After The Sunset - [0004].mkv", "After The Sunset")]
public void CleanStringTest(string input, string expectedName)
{