using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
namespace Emby.Naming.Video
{
///
/// .
///
public static class CleanStringParser
{
///
/// Attempts to extract clean name with regular expressions.
///
/// Name of file.
/// List of regex to parse name and year from.
/// Parsing result string.
/// True if parsing was successful.
public static bool TryClean(string name, IReadOnlyList expressions, [NotNullWhen(true)] out ReadOnlySpan newName)
{
var len = expressions.Count;
for (int i = 0; i < len; i++)
{
if (TryClean(name, expressions[i], out newName))
{
return true;
}
}
newName = ReadOnlySpan.Empty;
return false;
}
private static bool TryClean(string name, Regex expression, [NotNullWhen(true)] out ReadOnlySpan newName)
{
if (string.IsNullOrEmpty(name))
{
newName = null;
return false;
}
var match = expression.Match(name);
int index = match.Index;
if (match.Success && index != 0)
{
newName = name.AsSpan().Slice(0, match.Index);
return true;
}
newName = string.Empty;
return false;
}
}
}