You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
jellyfin/Emby.Naming/Video/CleanStringParser.cs

56 lines
1.7 KiB

using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Text.RegularExpressions;
namespace Emby.Naming.Video
{
/// <summary>
/// <see href="http://kodi.wiki/view/Advancedsettings.xml#video" />.
/// </summary>
public static class CleanStringParser
{
/// <summary>
/// Attempts to extract clean name with regular expressions.
/// </summary>
/// <param name="name">Name of file.</param>
/// <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([NotNullWhen(true)] string? name, IReadOnlyList<Regex> expressions, out ReadOnlySpan<char> newName)
{
if (string.IsNullOrEmpty(name))
{
newName = ReadOnlySpan<char>.Empty;
return false;
}
var len = expressions.Count;
for (int i = 0; i < len; i++)
{
if (TryClean(name, expressions[i], out newName))
{
return true;
}
}
newName = ReadOnlySpan<char>.Empty;
return false;
}
private static bool TryClean(string name, Regex expression, out ReadOnlySpan<char> newName)
{
var match = expression.Match(name);
int index = match.Index;
if (match.Success && index != 0)
{
newName = name.AsSpan().Slice(0, match.Index);
return true;
}
newName = ReadOnlySpan<char>.Empty;
return false;
}
}
}