2020-01-11 13:16:36 -07:00
|
|
|
using System;
|
2019-01-06 13:50:43 -07:00
|
|
|
using System.Collections.Generic;
|
2021-03-07 14:49:31 -07:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2018-09-12 10:26:21 -07:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
namespace Emby.Naming.Video
|
|
|
|
{
|
|
|
|
/// <summary>
|
2019-12-06 12:40:06 -07:00
|
|
|
/// <see href="http://kodi.wiki/view/Advancedsettings.xml#video" />.
|
2018-09-12 10:26:21 -07:00
|
|
|
/// </summary>
|
2020-01-11 12:25:06 -07:00
|
|
|
public static class CleanStringParser
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2020-11-10 09:11:48 -07:00
|
|
|
/// <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>
|
2021-03-07 14:49:31 -07:00
|
|
|
public static bool TryClean(string name, IReadOnlyList<Regex> expressions, [NotNullWhen(true)] out ReadOnlySpan<char> newName)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2020-01-11 13:16:36 -07:00
|
|
|
var len = expressions.Count;
|
|
|
|
for (int i = 0; i < len; i++)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2020-01-11 13:16:36 -07:00
|
|
|
if (TryClean(name, expressions[i], out newName))
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2020-01-11 13:16:36 -07:00
|
|
|
return true;
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-11 13:16:36 -07:00
|
|
|
newName = ReadOnlySpan<char>.Empty;
|
|
|
|
return false;
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
|
2021-03-07 14:49:31 -07:00
|
|
|
private static bool TryClean(string name, Regex expression, [NotNullWhen(true)] out ReadOnlySpan<char> newName)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2021-03-07 14:49:31 -07:00
|
|
|
if (string.IsNullOrEmpty(name))
|
|
|
|
{
|
|
|
|
newName = null;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2018-09-12 10:26:21 -07:00
|
|
|
var match = expression.Match(name);
|
2020-01-11 13:16:36 -07:00
|
|
|
int index = match.Index;
|
|
|
|
if (match.Success && index != 0)
|
2018-09-12 10:26:21 -07:00
|
|
|
{
|
2020-01-11 13:16:36 -07:00
|
|
|
newName = name.AsSpan().Slice(0, match.Index);
|
|
|
|
return true;
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
|
2020-01-11 13:16:36 -07:00
|
|
|
newName = string.Empty;
|
|
|
|
return false;
|
2018-09-12 10:26:21 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|