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