-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathUtils.cs
92 lines (85 loc) · 3.13 KB
/
Utils.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
namespace DiscordNet
{
public static class Utils
{
public static string ResolveHtml(string html)
{
string res = WebUtility.HtmlDecode(StripTags(html.Replace('\n', ' ').Replace("</p>", "\n")));
if (res[0] == '\n')
res = res.Substring(1);
return res;
}
public static string StripTags(string source)
{
char[] array = new char[source.Length];
int arrayIndex = 0;
bool inside = false;
for (int i = 0; i < source.Length; i++)
{
char let = source[i];
if (let == '<')
{
inside = true;
continue;
}
if (let == '>')
{
inside = false;
continue;
}
if (!inside)
{
array[arrayIndex] = let;
arrayIndex++;
}
}
return new string(array, 0, arrayIndex);
}
public static IEnumerable<T> RandomShuffle<T>(this IEnumerable<T> source)
=> source.Select(t => new {Index = Guid.NewGuid(), Value = t}).OrderBy(p => p.Index).Select(p => p.Value);
public static string BuildType(Type type)
{
string typeName = type.Name, typeGeneric = "";
int idx;
if ((idx = typeName.IndexOf('`')) != -1)
{
typeName = typeName.Substring(0, idx);
var generics = type.GetGenericArguments();
if (generics.Any())
typeGeneric = string.Join(", ", generics.Select(x => BuildType(x)));
}
return GetTypeName(type, typeName, typeGeneric);
}
private static string GetTypeName(Type type, string name, string generic)
{
if (Nullable.GetUnderlyingType(type) != null)
return $"{generic}?";
if (type.IsByRef)
return BuildType(type.GetElementType());
return Aliases.ContainsKey(type) ? Aliases[type] : $"{name}{(string.IsNullOrEmpty(generic) ? "" : $"<{generic}>")}";
}
private static readonly Dictionary<Type, string> Aliases = new Dictionary<Type, string>()
{
{ typeof(byte), "byte" },
{ typeof(sbyte), "sbyte" },
{ typeof(short), "short" },
{ typeof(ushort), "ushort" },
{ typeof(int), "int" },
{ typeof(uint), "uint" },
{ typeof(long), "long" },
{ typeof(ulong), "ulong" },
{ typeof(float), "float" },
{ typeof(double), "double" },
{ typeof(decimal), "decimal" },
{ typeof(object), "object" },
{ typeof(bool), "bool" },
{ typeof(char), "char" },
{ typeof(string), "string" },
{ typeof(void), "void" }
};
}
}