| | 1 | | using System.Collections.Generic; |
| | 2 | | using System.Diagnostics.CodeAnalysis; |
| | 3 | | using System.Text.RegularExpressions; |
| | 4 | |
|
| | 5 | | namespace Emby.Naming.Video |
| | 6 | | { |
| | 7 | | /// <summary> |
| | 8 | | /// <see href="http://kodi.wiki/view/Advancedsettings.xml#video" />. |
| | 9 | | /// </summary> |
| | 10 | | public static class CleanStringParser |
| | 11 | | { |
| | 12 | | /// <summary> |
| | 13 | | /// Attempts to extract clean name with regular expressions. |
| | 14 | | /// </summary> |
| | 15 | | /// <param name="name">Name of file.</param> |
| | 16 | | /// <param name="expressions">List of regex to parse name and year from.</param> |
| | 17 | | /// <param name="newName">Parsing result string.</param> |
| | 18 | | /// <returns>True if parsing was successful.</returns> |
| | 19 | | public static bool TryClean([NotNullWhen(true)] string? name, IReadOnlyList<Regex> expressions, out string newNa |
| | 20 | | { |
| 300 | 21 | | if (string.IsNullOrEmpty(name)) |
| | 22 | | { |
| 9 | 23 | | newName = string.Empty; |
| 9 | 24 | | return false; |
| | 25 | | } |
| | 26 | |
|
| | 27 | | // Iteratively apply the regexps to clean the string. |
| 291 | 28 | | bool cleaned = false; |
| 4074 | 29 | | for (int i = 0; i < expressions.Count; i++) |
| | 30 | | { |
| 1746 | 31 | | if (TryClean(name, expressions[i], out newName)) |
| | 32 | | { |
| 100 | 33 | | cleaned = true; |
| 100 | 34 | | name = newName; |
| | 35 | | } |
| | 36 | | } |
| | 37 | |
|
| 291 | 38 | | newName = cleaned ? name : string.Empty; |
| 291 | 39 | | return cleaned; |
| | 40 | | } |
| | 41 | |
|
| | 42 | | private static bool TryClean(string name, Regex expression, out string newName) |
| | 43 | | { |
| 1746 | 44 | | var match = expression.Match(name); |
| 1746 | 45 | | if (match.Success && match.Groups.TryGetValue("cleaned", out var cleaned)) |
| | 46 | | { |
| 100 | 47 | | newName = cleaned.Value; |
| 100 | 48 | | return true; |
| | 49 | | } |
| | 50 | |
|
| 1646 | 51 | | newName = string.Empty; |
| 1646 | 52 | | return false; |
| | 53 | | } |
| | 54 | | } |
| | 55 | | } |