|
4 | 4 | using System.Linq; |
5 | 5 | using System.Text; |
6 | 6 | using System.Text.RegularExpressions; |
| 7 | +using Oxide.Pooling; |
7 | 8 |
|
8 | | -namespace Oxide.Core |
| 9 | +namespace Oxide |
9 | 10 | { |
10 | 11 | /// <summary> |
11 | 12 | /// Useful extension methods which are added to base types |
@@ -175,178 +176,26 @@ public static string ToSentence<T>(this IEnumerable<T> items) |
175 | 176 | /// <param name="max"></param> |
176 | 177 | /// <returns></returns> |
177 | 178 | public static string Truncate(this string text, int max) => text.Length <= max ? text : text.Substring(0, max) + " ..."; |
178 | | - } |
179 | | -} |
180 | 179 |
|
181 | | -namespace Oxide.Plugins |
182 | | -{ |
183 | | - /// <summary> |
184 | | - /// Useful extension methods which are added to base types |
185 | | - /// </summary> |
186 | | - public static class ExtensionMethods |
187 | | - { |
188 | 180 | /// <summary> |
189 | | - /// Returns the last portion of a path separated by slashes |
| 181 | + /// Creates a new <see cref="HashSet{T}"/> based off a <see cref="IEnumerable{T}"/> |
190 | 182 | /// </summary> |
191 | | - public static string Basename(this string text, string extension = null) |
192 | | - { |
193 | | - if (extension != null) |
194 | | - { |
195 | | - if (extension.Equals("*.*")) |
196 | | - { |
197 | | - // Return the name excluding any extension |
198 | | - Match match = Regex.Match(text, @"([^\\/]+)\.[^\.]+$"); |
199 | | - if (match.Success) |
200 | | - { |
201 | | - return match.Groups[1].Value; |
202 | | - } |
203 | | - } |
204 | | - else |
205 | | - { |
206 | | - // Return the name excluding the given extension |
207 | | - if (extension[0] == '*') |
208 | | - { |
209 | | - extension = extension.Substring(1); |
210 | | - } |
211 | | - |
212 | | - return Regex.Match(text, @"([^\\/]+)\" + extension + "+$").Groups[1].Value; |
213 | | - } |
214 | | - } |
215 | | - // No extension was given or the path has no extension, return the full file name |
216 | | - return Regex.Match(text, @"[^\\/]+$").Groups[0].Value; |
217 | | - } |
218 | | - |
219 | | - /// <summary> |
220 | | - /// Checks if an array contains a specific item |
221 | | - /// </summary> |
222 | | - public static bool Contains<T>(this T[] array, T value) |
223 | | - { |
224 | | - foreach (T item in array) |
225 | | - { |
226 | | - if (item.Equals(value)) |
227 | | - { |
228 | | - return true; |
229 | | - } |
230 | | - } |
231 | | - |
232 | | - return false; |
233 | | - } |
234 | | - |
235 | | - /// <summary> |
236 | | - /// Returns the directory portion of a path separated by slashes |
237 | | - /// </summary> |
238 | | - public static string Dirname(this string text) => Regex.Match(text, "(.+)[\\/][^\\/]+$").Groups[1].Value; |
239 | | - |
240 | | - /// <summary> |
241 | | - /// Converts PascalCase and camelCase to multiple words |
242 | | - /// </summary> |
243 | | - public static string Humanize(this string name) => Regex.Replace(name, @"(\B[A-Z])", " $1"); |
244 | | - |
245 | | - /// <summary> |
246 | | - /// Checks if a string is a valid 64-bit Steam ID |
247 | | - /// </summary> |
248 | | - public static bool IsSteamId(this string id) |
249 | | - { |
250 | | - return ulong.TryParse(id, out ulong targetId) && targetId > 76561197960265728ul; |
251 | | - } |
252 | | - |
253 | | - /// <summary> |
254 | | - /// Checks if a ulong is a valid 64-bit Steam ID |
255 | | - /// </summary> |
256 | | - public static bool IsSteamId(this ulong id) => id > 76561197960265728ul; |
257 | | - |
258 | | - /// <summary> |
259 | | - /// Converts a string to plain text |
260 | | - /// </summary> |
261 | | - /// <param name="text"></param> |
| 183 | + /// <param name="collection">The collection to copy items from</param> |
| 184 | + /// <typeparam name="T"></typeparam> |
262 | 185 | /// <returns></returns> |
263 | | - public static string Plaintext(this string text) => Formatter.ToPlaintext(text); |
| 186 | + public static HashSet<T> ToHashSet<T>(this IEnumerable<T> collection) => new HashSet<T>(collection); |
264 | 187 |
|
265 | | - /// <summary> |
266 | | - /// Converts a string into a quote safe string |
267 | | - /// </summary> |
268 | | - /// <param name="text"></param> |
269 | | - /// <returns></returns> |
270 | | - public static string QuoteSafe(this string text) => "\"" + text.Replace("\"", "\\\"").TrimEnd('\\') + "\""; |
271 | | - |
272 | | - /// <summary> |
273 | | - /// Converts a string into a quote safe string |
274 | | - /// </summary> |
275 | | - /// <param name="text"></param> |
276 | | - /// <returns></returns> |
277 | | - public static string Quote(this string text) => QuoteSafe(text); |
278 | | - |
279 | | - /// <summary> |
280 | | - /// Returns a random value from an array |
281 | | - /// </summary> |
282 | | - public static T Sample<T>(this T[] array) => array[Core.Random.Range(0, array.Length)]; |
283 | | - |
284 | | - /// <summary> |
285 | | - /// Converts a string into a sanitized string for string.Format |
286 | | - /// </summary> |
287 | | - /// <param name="text"></param> |
288 | | - /// <returns></returns> |
289 | | - public static string Sanitize(this string text) => text.Replace("{", "{{").Replace("}", "}}"); |
290 | | - |
291 | | - /// <summary> |
292 | | - /// Converts a string to Sentence case |
293 | | - /// </summary> |
294 | | - public static string SentenceCase(this string text) |
295 | | - { |
296 | | - Regex regex = new Regex(@"(^[a-z])|\.\s+(.)", RegexOptions.ExplicitCapture); |
297 | | - return regex.Replace(text.ToLower(), s => s.Value.ToUpper()); |
298 | | - } |
299 | | - |
300 | | - /// <summary> |
301 | | - /// Converts a string to Title Case |
302 | | - /// </summary> |
303 | | - public static string TitleCase(this string text) |
304 | | - { |
305 | | - return CultureInfo.InstalledUICulture.TextInfo.ToTitleCase(text.Contains('_') ? text.Replace('_', ' ') : text); |
306 | | - } |
307 | | - |
308 | | - /// <summary> |
309 | | - /// Converts a string to Title Case |
310 | | - /// </summary> |
311 | | - public static string Titleize(this string text) => TitleCase(text); |
| 188 | + #region Pooling |
312 | 189 |
|
313 | 190 | /// <summary> |
314 | | - /// Turns an array of strings into a sentence |
| 191 | + /// Gets a <see cref="IArrayPoolProvider{T}"/> from the factory |
315 | 192 | /// </summary> |
316 | | - /// <param name="items"></param> |
| 193 | + /// <param name="factory"></param> |
| 194 | + /// <typeparam name="T"></typeparam> |
317 | 195 | /// <returns></returns> |
318 | | - public static string ToSentence<T>(this IEnumerable<T> items) |
319 | | - { |
320 | | - IEnumerator<T> enumerator = items.GetEnumerator(); |
321 | | - if (!enumerator.MoveNext()) |
322 | | - { |
323 | | - return string.Empty; |
324 | | - } |
325 | | - |
326 | | - T firstItem = enumerator.Current; |
327 | | - if (!enumerator.MoveNext()) |
328 | | - { |
329 | | - return firstItem?.ToString(); |
330 | | - } |
| 196 | + public static IArrayPoolProvider<T> GetArrayProvider<T>(this IPoolFactory factory) => factory.GetProvider<T[]>() as IArrayPoolProvider<T>; |
331 | 197 |
|
332 | | - StringBuilder builder = new StringBuilder(firstItem?.ToString()); |
333 | | - bool moreItems = true; |
334 | | - while (moreItems) |
335 | | - { |
336 | | - T item = enumerator.Current; |
337 | | - moreItems = enumerator.MoveNext(); |
338 | | - builder.Append(moreItems ? ", " : " and "); |
339 | | - builder.Append(item); |
340 | | - } |
341 | | - return builder.ToString(); |
342 | | - } |
| 198 | + #endregion |
343 | 199 |
|
344 | | - /// <summary> |
345 | | - /// Shortens a string to the length specified |
346 | | - /// </summary> |
347 | | - /// <param name="text"></param> |
348 | | - /// <param name="max"></param> |
349 | | - /// <returns></returns> |
350 | | - public static string Truncate(this string text, int max) => text.Length <= max ? text : text.Substring(0, max) + " ..."; |
351 | 200 | } |
352 | 201 | } |
0 commit comments