-
Notifications
You must be signed in to change notification settings - Fork 1
/
Extensions.cs
275 lines (250 loc) · 11.1 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Text;
namespace Supay.Bot
{
internal static partial class Extensions
{
/// <summary>
/// Concatenates a specified separator string between each element of a specified string array, yielding a single concatenated string. </summary>
/// <param name="startIndex">
/// The first array element in value to use. </param>
/// <param name="separator">
/// A System.String. </param>
public static string Join(this string[] self, int startIndex, string separator)
{
if (self.Length == startIndex + 1)
{
return self[startIndex];
}
return string.Join(separator, self, startIndex, self.Length - startIndex);
}
/// <summary>
/// Concatenates a space between each element of a specified string array, yielding a single concatenated string. </summary>
/// <param name="startIndex">
/// The first array element in value to use. </param>
public static string Join(this string[] self, int startIndex)
{
return self.Join(startIndex, " ");
}
/// <summary>
/// Concatenates a space between each element of a specified string array, yielding a single concatenated string. </summary>
public static string Join(this string[] self)
{
return self.Join(0);
}
/// <summary>
/// Converts the specified string representation of a date (and time) to its DateTime equivalent. </summary>
public static DateTime ToDateTime(this string self)
{
if (!string.IsNullOrEmpty(self))
{
switch (self.Length)
{
case 8:
return DateTime.ParseExact(self, "yyyyMMdd", CultureInfo.InvariantCulture);
case 12:
return DateTime.ParseExact(self, "yyyyMMddHHmm", CultureInfo.InvariantCulture);
case 14:
return DateTime.ParseExact(self, "yyyyMMddHHmmss", CultureInfo.InvariantCulture);
}
}
return DateTime.MinValue;
}
/// <summary>
/// Converts the numeric value of this instance to its equivalent short string (k/m/b) representation up to a maximum number of decimals. </summary>
/// <param name="decimals">
/// Max number of decimals allowed. </param>
public static string ToShortString(this double self, int decimals)
{
string format = "#,##0." + new string('#', decimals);
if (self >= 1000000000 || self <= -1000000000)
{
return Math.Round(self / 1000000000, decimals).ToStringI(format) + "b";
}
if (self >= 1000000 || self <= -1000000)
{
return Math.Round(self / 1000000, decimals).ToStringI(format) + "m";
}
if (self >= 1000 || self <= -1000)
{
return Math.Round(self / 1000, decimals).ToStringI(format) + "k";
}
return Math.Round(self, decimals).ToStringI(format);
}
/// <summary>
/// Converts the numeric value of this instance to its equivalent short string (k/m/b) representation up to a maximum number of decimals. </summary>
/// <param name="decimals">
/// Max number of decimals allowed. </param>
public static string ToShortString(this long self, int decimals)
{
return ToShortString((double) self, decimals);
}
/// <summary>
/// Converts the numeric value of this instance to its equivalent short string (k/m/b) representation up to a maximum number of decimals. </summary>
/// <param name="decimals">
/// Max number of decimals allowed. </param>
public static string ToShortString(this int self, int decimals)
{
return ToShortString((double) self, decimals);
}
/// <summary>
/// Returns the string representation of the value of this instance in the format: ##days ##hours ##mins ##secs. </summary>
public static string ToLongString(this TimeSpan self)
{
var result = new StringBuilder(30);
if (self.Days > 0)
{
result.Append(self.Days + "day" + (self.Days == 1 ? " " : "s "));
}
if (self.Hours > 0)
{
result.Append(self.Hours + "hour" + (self.Hours == 1 ? " " : "s "));
}
if (self.Minutes > 0)
{
result.Append(self.Minutes + "min" + (self.Minutes == 1 ? " " : "s "));
}
if (self.Seconds > 0 || (self.Days == 0 && self.Hours == 0 && self.Minutes == 0))
{
result.Append(self.Seconds + "sec" + (self.Seconds == 1 ? string.Empty : "s"));
}
return result.ToString();
}
/// <summary>
/// Returns a value indicating whether the specified System.String object occurs within this string. (case insensitive) </summary>
/// <param name="value">
/// The System.String object to seek. </param>
public static bool ContainsI(this string self, string value)
{
return self.IndexOf(value, StringComparison.OrdinalIgnoreCase) != -1;
}
/// <summary>
/// Determines whether the beginning of this instance matches the specified string. (case insensitive) </summary>
/// <param name="value">
/// The System.String object to seek. </param>
public static bool StartsWithI(this string self, string value)
{
return self.StartsWith(value, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Determines whether the ending of this instance matches the specified string. (case insensitive) </summary>
/// <param name="value">
/// The System.String object to seek. </param>
public static bool EndsWithI(this string self, string value)
{
return self.EndsWith(value, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Determines whether this instance and another specified String object have the same value. (case insensitive)</summary>
public static bool EqualsI(this string self, string value)
{
return self.Equals(value, StringComparison.OrdinalIgnoreCase);
}
/// <summary>
/// Replaces the format items of this instance with the text equivalent of the value of a corresponding Object instance in a specified array. </summary>
/// <param name="args">
/// An Object array containing zero or more objects to format. </param>
public static string FormatWith(this string self, params object[] args)
{
return string.Format(CultureInfo.InvariantCulture, self, args);
}
/// <summary>
/// Converts the value of this instance to its equivalent string representation. </summary>
public static string ToStringI(this char self)
{
return self.ToString(CultureInfo.InvariantCulture);
}
/// <summary>
/// Converts the value of this instance to its equivalent string representation. </summary>
public static string ToStringI<T>(this T self) where T : IFormattable
{
return self.ToString(null, CultureInfo.InvariantCulture);
}
/// <summary>
/// Formats the value of the current instance using the specified format. </summary>
/// <param name="format">
/// The System.String specifying the format to use. </param>
public static string ToStringI<T>(this T self, string format) where T : IFormattable
{
return self.ToString(format, CultureInfo.InvariantCulture);
}
/// <summary>
/// Converts the string representation of a number to it's 32-bit signed integer equivalent. </summary>
public static int ToInt32(this string self)
{
string number = self.TrimEnd();
int multiplier = 1;
switch (number[number.Length - 1])
{
case 'b':
case 'B':
number = number.Substring(0, number.Length - 1);
multiplier = 1000000000;
break;
case 'm':
case 'M':
number = number.Substring(0, number.Length - 1);
multiplier = 1000000;
break;
case 'k':
case 'K':
number = number.Substring(0, number.Length - 1);
multiplier = 1000;
break;
}
return (int) (multiplier * double.Parse(number, NumberStyles.AllowLeadingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture));
}
/// <summary>
/// Returns true if a string can be converted to a 64-bit signed integer. </summary>
public static bool TryInt64(this string self, out long value)
{
string number = self.TrimEnd();
int multiplier = 1;
switch (number[number.Length - 1])
{
case 'm':
case 'M':
number = number.Substring(0, number.Length - 1);
multiplier = 1000000;
break;
case 'k':
case 'K':
number = number.Substring(0, number.Length - 1);
multiplier = 1000;
break;
}
double result;
if (double.TryParse(number, NumberStyles.AllowLeadingWhite | NumberStyles.AllowLeadingSign | NumberStyles.AllowThousands | NumberStyles.AllowDecimalPoint, CultureInfo.InvariantCulture, out result))
{
value = (long) (result * multiplier);
return true;
}
value = 0;
return false;
}
/// <summary>
/// Returns true if a string can be converted to a 32-bit signed integer. </summary>
public static bool TryInt32(this string self, out int value)
{
long resultValue;
bool result = TryInt64(self, out resultValue);
value = (int) resultValue;
return result;
}
public static int FindIndex<T>(this IEnumerable<T> source, Func<T, bool> match)
{
var i = 0;
foreach (var item in source)
{
if (match(item))
{
return i;
}
i++;
}
return -1;
}
}
}