2020-01-11 20:16:36 +00:00
|
|
|
using System;
|
2019-01-06 20:50:43 +00:00
|
|
|
using System.Collections.Generic;
|
2021-03-09 00:28:21 +00:00
|
|
|
using System.Diagnostics.CodeAnalysis;
|
2018-09-12 17:26:21 +00:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
namespace Emby.Naming.Video
|
|
|
|
{
|
|
|
|
/// <summary>
|
2019-12-06 19:40:06 +00:00
|
|
|
/// <see href="http://kodi.wiki/view/Advancedsettings.xml#video" />.
|
2018-09-12 17:26:21 +00:00
|
|
|
/// </summary>
|
2020-01-11 19:25:06 +00:00
|
|
|
public static class CleanStringParser
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2020-11-10 16:11:48 +00: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-10-26 12:47:34 +00:00
|
|
|
public static bool TryClean([NotNullWhen(true)] string? name, IReadOnlyList<Regex> expressions, out string newName)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2021-03-09 00:28:21 +00:00
|
|
|
if (string.IsNullOrEmpty(name))
|
|
|
|
{
|
2021-10-26 12:47:34 +00:00
|
|
|
newName = string.Empty;
|
2021-03-09 00:28:21 +00:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2021-08-05 06:25:54 +00:00
|
|
|
// Iteratively apply the regexps to clean the string.
|
|
|
|
bool cleaned = false;
|
|
|
|
for (int i = 0; i < expressions.Count; i++)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2021-08-05 06:25:54 +00:00
|
|
|
if (TryClean(name, expressions[i], out newName))
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2021-08-05 06:25:54 +00:00
|
|
|
cleaned = true;
|
2021-10-26 12:47:34 +00:00
|
|
|
name = newName;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-26 12:47:34 +00:00
|
|
|
newName = cleaned ? name : string.Empty;
|
2021-08-05 20:09:42 +00:00
|
|
|
return cleaned;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 12:47:34 +00:00
|
|
|
private static bool TryClean(string name, Regex expression, out string newName)
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
|
|
|
var match = expression.Match(name);
|
2021-10-26 12:47:34 +00:00
|
|
|
if (match.Success && match.Groups.TryGetValue("cleaned", out var cleaned))
|
2018-09-12 17:26:21 +00:00
|
|
|
{
|
2021-10-26 12:47:34 +00:00
|
|
|
newName = cleaned.Value;
|
2020-01-11 20:16:36 +00:00
|
|
|
return true;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
|
2021-10-26 12:47:34 +00:00
|
|
|
newName = string.Empty;
|
2020-01-11 20:16:36 +00:00
|
|
|
return false;
|
2018-09-12 17:26:21 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|