diff options
| author | dkanada <dkanada@users.noreply.github.com> | 2020-01-19 00:08:37 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2020-01-19 00:08:37 +0900 |
| commit | c618f3f8eb4ab58004dea94cd475438e555ce461 (patch) | |
| tree | 03a219a548485b0c5ad6afcdcb4fa389c6dcee2c /Emby.Naming/Video/CleanStringParser.cs | |
| parent | 1e65cc469539da0f31aaa93251bf3e0ab2e30a1e (diff) | |
| parent | 9f5bbb126e55b23243d22bcfd85fbbb17e10ec17 (diff) | |
Merge pull request #2257 from Bond-009/cleantests
Fix a couple of tests
Diffstat (limited to 'Emby.Naming/Video/CleanStringParser.cs')
| -rw-r--r-- | Emby.Naming/Video/CleanStringParser.cs | 41 |
1 files changed, 17 insertions, 24 deletions
diff --git a/Emby.Naming/Video/CleanStringParser.cs b/Emby.Naming/Video/CleanStringParser.cs index fcd4b65c7..b7b65d822 100644 --- a/Emby.Naming/Video/CleanStringParser.cs +++ b/Emby.Naming/Video/CleanStringParser.cs @@ -1,6 +1,8 @@ #pragma warning disable CS1591 #pragma warning disable SA1600 +#nullable enable +using System; using System.Collections.Generic; using System.Text.RegularExpressions; @@ -9,44 +11,35 @@ namespace Emby.Naming.Video /// <summary> /// <see href="http://kodi.wiki/view/Advancedsettings.xml#video" />. /// </summary> - public class CleanStringParser + public static class CleanStringParser { - public CleanStringResult Clean(string name, IEnumerable<Regex> expressions) + public static bool TryClean(string name, IReadOnlyList<Regex> expressions, out ReadOnlySpan<char> newName) { - var hasChanged = false; - - foreach (var exp in expressions) + var len = expressions.Count; + for (int i = 0; i < len; i++) { - var result = Clean(name, exp); - - if (!string.IsNullOrEmpty(result.Name)) + if (TryClean(name, expressions[i], out newName)) { - name = result.Name; - hasChanged = hasChanged || result.HasChanged; + return true; } } - return new CleanStringResult - { - Name = name, - HasChanged = hasChanged - }; + newName = ReadOnlySpan<char>.Empty; + return false; } - private static CleanStringResult Clean(string name, Regex expression) + private static bool TryClean(string name, Regex expression, out ReadOnlySpan<char> newName) { - var result = new CleanStringResult(); - var match = expression.Match(name); - - if (match.Success) + int index = match.Index; + if (match.Success && index != 0) { - result.HasChanged = true; - name = name.Substring(0, match.Index); + newName = name.AsSpan().Slice(0, match.Index); + return true; } - result.Name = name; - return result; + newName = string.Empty; + return false; } } } |
