diff --git a/NzbDrone.Common.Test/EnsureTest/PathExtensionFixture.cs b/NzbDrone.Common.Test/EnsureTest/PathExtensionFixture.cs new file mode 100644 index 000000000..7d75b62b9 --- /dev/null +++ b/NzbDrone.Common.Test/EnsureTest/PathExtensionFixture.cs @@ -0,0 +1,19 @@ +using System; +using FluentAssertions; +using Moq; +using NUnit.Framework; +using NzbDrone.Common.EnsureThat; +using NzbDrone.Test.Common; + +namespace NzbDrone.Common.Test.EnsureTest +{ + [TestFixture] + public class PathExtensionFixture : TestBase + { + [TestCase(@"p:\TV Shows\file with, comma.mkv")] + public void EnsureWindowsPath(string path) + { + Ensure.That(() => path).IsValidPath(); + } + } +} diff --git a/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj b/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj index 7c23c4306..16a8b7a4e 100644 --- a/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj +++ b/NzbDrone.Common.Test/NzbDrone.Common.Test.csproj @@ -81,6 +81,7 @@ + diff --git a/NzbDrone.Common/EnsureThat/EnsureStringExtensions.cs b/NzbDrone.Common/EnsureThat/EnsureStringExtensions.cs index d9def3064..734b5cb96 100644 --- a/NzbDrone.Common/EnsureThat/EnsureStringExtensions.cs +++ b/NzbDrone.Common/EnsureThat/EnsureStringExtensions.cs @@ -96,7 +96,7 @@ namespace NzbDrone.Common.EnsureThat return param; } - private static readonly Regex windowsInvalidPathRegex = new Regex(@"[/,*,<,>,"",|]", RegexOptions.Compiled | RegexOptions.IgnoreCase); + private static readonly Regex windowsInvalidPathRegex = new Regex(@"[/*<>""|]", RegexOptions.Compiled | RegexOptions.IgnoreCase); private static readonly Regex windowsPathRegex = new Regex(@"^[a-z]:\\", RegexOptions.Compiled | RegexOptions.IgnoreCase); [DebuggerStepThrough] diff --git a/NzbDrone.Common/NzbDrone.Common.csproj b/NzbDrone.Common/NzbDrone.Common.csproj index 236910802..2429b32a2 100644 --- a/NzbDrone.Common/NzbDrone.Common.csproj +++ b/NzbDrone.Common/NzbDrone.Common.csproj @@ -103,7 +103,7 @@ - + diff --git a/NzbDrone.Common/StringExtensions.cs b/NzbDrone.Common/StringExtensions.cs new file mode 100644 index 000000000..59dd08478 --- /dev/null +++ b/NzbDrone.Common/StringExtensions.cs @@ -0,0 +1,39 @@ +using System.Text.RegularExpressions; +using System.Linq; + +namespace NzbDrone.Common +{ + public static class StringExtensions + { + public static string Inject(this string format, params object[] formattingArgs) + { + return string.Format(format, formattingArgs); + } + + public static string Inject(this string format, params string[] formattingArgs) + { + return string.Format(format, formattingArgs.Cast()); + } + + + private static readonly Regex InvalidCharRegex = new Regex(@"[^a-z0-9\s-]", RegexOptions.Compiled | RegexOptions.IgnoreCase); + private static readonly Regex CollapseSpace = new Regex(@"\s+", RegexOptions.Compiled | RegexOptions.IgnoreCase); + + public static string ToSlug(this string phrase) + { + phrase = phrase.RemoveAccent().ToLower(); + + phrase = InvalidCharRegex.Replace(phrase, string.Empty); + phrase = CollapseSpace.Replace(phrase, " ").Trim(); + phrase = phrase.Replace(" ", "-"); + + return phrase; + } + + public static string RemoveAccent(this string txt) + { + var bytes = System.Text.Encoding.GetEncoding("Cyrillic").GetBytes(txt); + return System.Text.Encoding.ASCII.GetString(bytes); + } + } +} \ No newline at end of file