Files
OpenBullet/RuriLib/Extensions.cs
T

158 lines
5.7 KiB
C#

using RuriLib.Functions.Requests;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Security.Authentication;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace RuriLib
{
/// <summary>
/// Extension methods for lists.
/// </summary>
public static class ListExtensions
{
/// <summary>
/// Randomizes the elements in a list.
/// </summary>
/// <param name="list">The list to shuffle</param>
/// <param name="rng">The random number generator</param>
public static void Shuffle<T>(this IList<T> list, Random rng)
{
int n = list.Count;
while (n > 1)
{
n--;
int k = rng.Next(n + 1);
T value = list[k];
list[k] = list[n];
list[n] = value;
}
}
}
/// <summary>
/// Extension methods for strings.
/// </summary>
public static class StringExtensions
{
/// <summary>
/// Replaces literal values of \n, \r\n and \t with the actual escape codes.
/// </summary>
/// <param name="str">The string to unescape</param>
/// <param name="useEnvNewLine">Whether to unescape both \n and \r\n with the Environment.NewLine</param>
/// <returns>The string with unescaped escape sequences.</returns>
public static string Unescape(this string str, bool useEnvNewLine = false)
{
// Unescape only \n etc. not \\n
str = Regex.Replace(str, @"(?<!\\)\\r\\n", useEnvNewLine ? Environment.NewLine : "\r\n");
str = Regex.Replace(str, @"(?<!\\)\\n", useEnvNewLine ? Environment.NewLine : "\n");
str = Regex.Replace(str, @"(?<!\\)\\t", "\t");
// Replace \\n with \n
return new StringBuilder(str)
.Replace(@"\\r\\n", @"\r\n")
.Replace(@"\\n", @"\n")
.Replace(@"\\t", @"\t")
.ToString();
}
/// <summary>
/// Returns true if <paramref name="path"/> starts with the path <paramref name="baseDirPath"/>.
/// The comparison is case-insensitive, handles / and \ slashes as folder separators and
/// only matches if the base dir folder name is matched exactly ("c:\foobar\file.txt" is not a sub path of "c:\foo").
/// </summary>
public static bool IsSubPathOf(this string path, string baseDirPath)
{
string normalizedPath = Path.GetFullPath(path.Replace('/', '\\')
.WithEnding("\\"));
string normalizedBaseDirPath = Path.GetFullPath(baseDirPath.Replace('/', '\\')
.WithEnding("\\"));
return normalizedPath.StartsWith(normalizedBaseDirPath, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Returns <paramref name="str"/> with the minimal concatenation of <paramref name="ending"/> (starting from end) that
/// results in satisfying .EndsWith(ending).
/// </summary>
/// <example>"hel".WithEnding("llo") returns "hello", which is the result of "hel" + "lo".</example>
public static string WithEnding(this string str, string ending)
{
if (str == null)
return ending;
string result = str;
// Right() is 1-indexed, so include these cases
// * Append no characters
// * Append up to N characters, where N is ending length
for (int i = 0; i <= ending.Length; i++)
{
string tmp = result + ending.Right(i);
if (tmp.EndsWith(ending))
return tmp;
}
return result;
}
/// <summary>Gets the rightmost <paramref name="length" /> characters from a string.</summary>
/// <param name="value">The string to retrieve the substring from. Must not be null.</param>
/// <param name="length">The number of characters to retrieve.</param>
/// <returns>The substring.</returns>
public static string Right(this string value, int length)
{
if (value == null)
{
throw new ArgumentNullException("value");
}
if (length < 0)
{
throw new ArgumentOutOfRangeException("length", length, "Length is less than zero");
}
return (length < value.Length) ? value.Substring(value.Length - length) : value;
}
}
/// <summary>
/// Extension methods for SecurityProtocol enum.
/// </summary>
public static class SecurityProtocolExtensions
{
/// <summary>
/// Converts the SecurityProtocol to an SslProtocols enum. Multiple protocols are not supported and SystemDefault is None.
/// </summary>
/// <param name="protocol">The SecurityProtocol</param>
/// <returns>The converted SslProtocols.</returns>
public static SslProtocols ToSslProtocols(this SecurityProtocol protocol)
{
switch (protocol)
{
case SecurityProtocol.SystemDefault:
return SslProtocols.None;
case SecurityProtocol.SSL3:
return SslProtocols.Ssl3;
case SecurityProtocol.TLS10:
return SslProtocols.Tls;
case SecurityProtocol.TLS11:
return SslProtocols.Tls11;
case SecurityProtocol.TLS12:
return SslProtocols.Tls12;
default:
throw new Exception("Protocol not supported");
}
}
}
}