2013-07-24 07:35:32 +02:00
|
|
|
using System.Text;
|
2013-05-29 02:15:12 +02:00
|
|
|
using System.Text.RegularExpressions;
|
|
|
|
|
|
|
|
namespace NzbDrone.Common
|
|
|
|
{
|
|
|
|
public static class StringExtensions
|
|
|
|
{
|
|
|
|
public static string Inject(this string format, params object[] formattingArgs)
|
|
|
|
{
|
|
|
|
return string.Format(format, formattingArgs);
|
|
|
|
}
|
|
|
|
|
2013-08-08 04:07:46 +02:00
|
|
|
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);
|
2013-05-29 02:15:12 +02:00
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2013-05-30 05:26:47 +02:00
|
|
|
public static string ToSearchTerm(this string phrase)
|
|
|
|
{
|
|
|
|
phrase = phrase.RemoveAccent().ToLower();
|
|
|
|
|
|
|
|
phrase = phrase.Replace("&", "and");
|
|
|
|
phrase = InvalidSearchCharRegex.Replace(phrase, string.Empty);
|
|
|
|
phrase = CollapseSpace.Replace(phrase, " ").Trim();
|
|
|
|
phrase = phrase.Replace(" ", "+");
|
|
|
|
|
|
|
|
return phrase;
|
|
|
|
}
|
|
|
|
|
2013-05-29 02:15:12 +02:00
|
|
|
public static string RemoveAccent(this string txt)
|
|
|
|
{
|
2013-07-24 07:35:32 +02:00
|
|
|
var bytes = Encoding.GetEncoding("Cyrillic").GetBytes(txt);
|
|
|
|
return Encoding.ASCII.GetString(bytes);
|
2013-05-29 02:15:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|