From c5c02f08f886b92cbd3e2dccb8e0df7c690364c2 Mon Sep 17 00:00:00 2001 From: "kay.one" Date: Sat, 17 Aug 2013 18:52:56 -0700 Subject: [PATCH] better remove RemoveAccent logic. --- NzbDrone.Common/NzbDrone.Common.csproj | 1 - NzbDrone.Common/StringExtension.cs | 24 --------- NzbDrone.Common/StringExtensions.cs | 58 +++++++++++++--------- NzbDrone.Core/MetadataSource/TraktProxy.cs | 16 +++++- 4 files changed, 48 insertions(+), 51 deletions(-) delete mode 100644 NzbDrone.Common/StringExtension.cs diff --git a/NzbDrone.Common/NzbDrone.Common.csproj b/NzbDrone.Common/NzbDrone.Common.csproj index 2d3fd449d..313cdbc0a 100644 --- a/NzbDrone.Common/NzbDrone.Common.csproj +++ b/NzbDrone.Common/NzbDrone.Common.csproj @@ -125,7 +125,6 @@ - diff --git a/NzbDrone.Common/StringExtension.cs b/NzbDrone.Common/StringExtension.cs deleted file mode 100644 index acd9aa70d..000000000 --- a/NzbDrone.Common/StringExtension.cs +++ /dev/null @@ -1,24 +0,0 @@ -using System; -using System.Linq; - -namespace NzbDrone.Common -{ - public static class StringExtension - { - public static string NullSafe(this string target) - { - return ((object)target).NullSafe().ToString(); - } - - public static object NullSafe(this object target) - { - if (target != null) return target; - return "[NULL]"; - } - - public static string FirstCharToUpper(this string input) - { - return input.First().ToString().ToUpper() + String.Join("", input.Skip(1)); - } - } -} \ No newline at end of file diff --git a/NzbDrone.Common/StringExtensions.cs b/NzbDrone.Common/StringExtensions.cs index 85db4e23d..1c4e7b54f 100644 --- a/NzbDrone.Common/StringExtensions.cs +++ b/NzbDrone.Common/StringExtensions.cs @@ -1,3 +1,6 @@ +using System; +using System.Globalization; +using System.Linq; using System.Text; using System.Text.RegularExpressions; @@ -5,42 +8,49 @@ namespace NzbDrone.Common { public static class StringExtensions { - public static string Inject(this string format, params object[] formattingArgs) + public static string NullSafe(this string target) { - return string.Format(format, formattingArgs); + return ((object)target).NullSafe().ToString(); } - private static readonly Regex InvalidCharRegex = new Regex(@"[^a-zA-Z0-9\s-]", RegexOptions.Compiled); - private static readonly Regex InvalidSearchCharRegex = new Regex(@"[^a-zA-Z0-9\s-\.]", RegexOptions.Compiled); - private static readonly Regex CollapseSpace = new Regex(@"\s+", RegexOptions.Compiled); - - public static string ToSlug(this string phrase) + public static object NullSafe(this object target) { - phrase = phrase.RemoveAccent().ToLower(); + if (target != null) return target; + return "[NULL]"; + } - phrase = InvalidCharRegex.Replace(phrase, string.Empty); - phrase = CollapseSpace.Replace(phrase, " ").Trim(); - phrase = phrase.Replace(" ", "-"); + public static string FirstCharToUpper(this string input) + { + return input.First().ToString().ToUpper() + String.Join("", input.Skip(1)); + } - return phrase; + public static string Inject(this string format, params object[] formattingArgs) + { + return string.Format(format, formattingArgs); } - public static string ToSearchTerm(this string phrase) + private static readonly Regex CollapseSpace = new Regex(@"\s+", RegexOptions.Compiled); + + public static string RemoveAccent(this string text) { - phrase = phrase.RemoveAccent().ToLower(); - - phrase = phrase.Replace("&", "and"); - phrase = InvalidSearchCharRegex.Replace(phrase, string.Empty); - phrase = CollapseSpace.Replace(phrase, " ").Trim(); - phrase = phrase.Replace(" ", "+"); - - return phrase; + var normalizedString = text.Normalize(NormalizationForm.FormD); + var stringBuilder = new StringBuilder(); + + foreach (var c in normalizedString) + { + var unicodeCategory = CharUnicodeInfo.GetUnicodeCategory(c); + if (unicodeCategory != UnicodeCategory.NonSpacingMark) + { + stringBuilder.Append(c); + } + } + + return stringBuilder.ToString().Normalize(NormalizationForm.FormC); } - public static string RemoveAccent(this string txt) + public static string CleanSpaces(this string text) { - var bytes = Encoding.GetEncoding("Cyrillic").GetBytes(txt); - return Encoding.ASCII.GetString(bytes); + return CollapseSpace.Replace(text, " ").Trim(); } } } \ No newline at end of file diff --git a/NzbDrone.Core/MetadataSource/TraktProxy.cs b/NzbDrone.Core/MetadataSource/TraktProxy.cs index 6dca25834..4a84c0cbd 100644 --- a/NzbDrone.Core/MetadataSource/TraktProxy.cs +++ b/NzbDrone.Core/MetadataSource/TraktProxy.cs @@ -2,7 +2,6 @@ using System.Collections.Generic; using System.IO; using System.Linq; -using System.Net; using System.Text.RegularExpressions; using NzbDrone.Common; using NzbDrone.Core.MediaCover; @@ -19,12 +18,25 @@ namespace NzbDrone.Core.MetadataSource public List SearchForNewSeries(string title) { var client = BuildClient("search", "shows"); - var restRequest = new RestRequest(title.ToSearchTerm()); + var restRequest = new RestRequest(GetSearchTerm(title)); var response = client.ExecuteAndValidate>(restRequest); return response.Select(MapSeries).ToList(); } + + private static readonly Regex InvalidSearchCharRegex = new Regex(@"[^a-zA-Z0-9\s-\.]", RegexOptions.Compiled); + + private static string GetSearchTerm(string phrase) + { + phrase = phrase.RemoveAccent().ToLower(); + phrase = phrase.Replace("&", "and"); + phrase = InvalidSearchCharRegex.Replace(phrase, string.Empty); + phrase = phrase.CleanSpaces().Replace(" ", "+"); + + return phrase; + } + public Tuple> GetSeriesInfo(int tvDbSeriesId) { var client = BuildClient("show", "summary");