-
Notifications
You must be signed in to change notification settings - Fork 2
/
Extensions.cs
139 lines (129 loc) · 4.38 KB
/
Extensions.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
namespace Homiebot
{
public static class Extensions
{
public static T[] SubArray<T>(this T[] array, int offset, int length)
{
return new ArraySegment<T>(array, offset, length)
.ToArray();
}
public static IEnumerable<string> SplitForDiscord(this string message)
{
if(message.Length < Discord.DiscordHelper.CharacterLimit){
return new string[] {message};
}else{
var charCount = 0;
return message.Split(' ',StringSplitOptions.RemoveEmptyEntries)
.GroupBy(w => (charCount+= w.Length+1) / Discord.DiscordHelper.CharacterLimit)
.Select(g => string.Join(' ',g));
}
}
public static T GetRandomUnused<T>(this IEnumerable<T> collection, Random random, ref HashSet<T> used)
{
if(used.Count == collection.Count()){
used = new ();
}
var localused = used.ToHashSet<T>();
var item = collection
.Where(item=> !localused.Contains(item))
.OrderBy(x => random.Next())
.First();
used.Add(item);
return item;
}
public static string ToMockingCase(this string message, Random random)
{
bool lastLetterwasUpper = false;
bool needsreverse = false;
StringBuilder s = new StringBuilder();
foreach (char c in message.ToLowerInvariant())
{
if(random.NextBoolean())
{
if(needsreverse && !lastLetterwasUpper)
{
s.Append(Char.ToUpperInvariant(c));
needsreverse = false;
continue;
}
else
{
s.Append(c);
}
if(!lastLetterwasUpper)
{
needsreverse = true;
}
lastLetterwasUpper = false;
}else
{
if(needsreverse && lastLetterwasUpper)
{
s.Append(c);
needsreverse = false;
continue;
}
else
{
s.Append(Char.ToUpperInvariant(c));
}
if(lastLetterwasUpper)
{
needsreverse = true;
}
lastLetterwasUpper = true;
}
}
return s.ToString();
}
public static string ToUwuCase(this string message)
{
char[] nya = {'M','N','n','m'};
StringBuilder s = new StringBuilder();
char lastchar = 'a';
foreach (char c in message)
{
switch(c)
{
case 'L':
case 'R' :
s.Append('W');
break;
case 'l':
case 'r':
s.Append('w');
break;
case 'o':
case 'a':
if(nya.Contains(lastchar)){s.Append('y'); s.Append(c);}
else{s.Append(c);}
break;
default:
s.Append(c);
break;
}
lastchar = c;
}
return s.ToString();
}
public static string ToBlockText(this string source)
{
var upper = source.ToUpperInvariant();
var retstr = upper;
for(int i = 1; i<upper.Length; i++){
retstr += "\n";
retstr += (upper.Substring(i,upper.Length-i) + upper.Substring(0,i));
}
return retstr;
}
public static bool NextBoolean(this Random random)
{
return random.Next() > (Int32.MaxValue / 2);
// Next() returns an int in the range [0..Int32.MaxValue]
}
}
}