/src/Perpetuum/StringExtensions.cs

https://github.com/PerpetuumOnline/PerpetuumServer · C# · 107 lines · 72 code · 17 blank · 18 comment · 7 complexity · 6f34da6d3a92af644ef0f717b82f6cce MD5 · raw file

  1. using System;
  2. using System.Diagnostics;
  3. using System.Linq;
  4. using System.Text.RegularExpressions;
  5. namespace Perpetuum
  6. {
  7. public static class StringExtensions
  8. {
  9. [UsedImplicitly]
  10. public static bool IsAttributeName(this string attributeName)
  11. {
  12. if (!string.IsNullOrEmpty(attributeName))
  13. {
  14. return Regex.IsMatch(attributeName, "^attribute[a-zA-Z]$");
  15. }
  16. return false;
  17. }
  18. /// <summary>
  19. /// Filters the character's nick
  20. /// </summary>
  21. public static bool IsNickAllowedForPlayers(this string nick)
  22. {
  23. nick = nick.ToLowerInvariant();
  24. return !nick.StartsWith("gm ") && !nick.StartsWith("dev ");
  25. }
  26. [CanBeNull]
  27. public static string Clamp(this string str,int length)
  28. {
  29. if (string.IsNullOrEmpty(str))
  30. return null;
  31. return str.Length >= length ? str.Substring(0, length) : str;
  32. }
  33. public static string[] GetLines(this string str)
  34. {
  35. if ( string.IsNullOrEmpty(str))
  36. return new string[0];
  37. var delimiters = new[] { '\r', '\n' };
  38. return str.Split(delimiters, StringSplitOptions.RemoveEmptyEntries).ToArray();
  39. }
  40. /// <summary>
  41. /// this method allows: a-z A-Z 0-9 _
  42. /// </summary>
  43. /// <param name="source"></param>
  44. /// <returns>true if the evil character is NOT found
  45. /// </returns>
  46. /// <remarks></remarks>
  47. public static bool AllowAscii(this string source)
  48. {
  49. //returns false if an illegal char is found
  50. return Regex.IsMatch(source, "^[a-zA-Z0-9_ ]*$");
  51. }
  52. /// <summary>
  53. /// Extended character set filter
  54. /// </summary>
  55. public static bool AllowExtras(this string source)
  56. {
  57. //returns false if an illegal char is found
  58. return Regex.IsMatch(source, "^[a-zA-Z0-9_\\[\\]\\|\\!\\#\\+\\-\\.\\$\\%\\^\\(\\)\\>\\<\\:\\{\\}\\&\\' ]*$");
  59. }
  60. public static bool IsEmail(this string email)
  61. {
  62. return (Regex.Match(email, "([\\w\\-]+\\.)*[\\w\\-]+@([\\w\\-]+\\.)+([\\w\\-]{2,3})")).Success;
  63. }
  64. public static bool IsIPv4(this string ip)
  65. {
  66. if (string.IsNullOrWhiteSpace(ip))
  67. return false;
  68. var splitValues = ip.Split('.');
  69. if (splitValues.Length != 4)
  70. return false;
  71. return splitValues.All(r => byte.TryParse(r, out byte tempForParsing));
  72. }
  73. /// <summary>
  74. /// removes the // comment // from the string
  75. /// </summary>
  76. public static string RemoveComment(this string text)
  77. {
  78. return string.IsNullOrEmpty(text) ? null : Regex.Replace(text, @"\/\/.*\/\/|\/\/.*", "");
  79. }
  80. public static string RemoveSpecialCharacters(this string input)
  81. {
  82. return Regex.Replace(input,"(?:[^a-z0-9 ]|(?<=['\"])s)",string.Empty, RegexOptions.IgnoreCase | RegexOptions.CultureInvariant | RegexOptions.Compiled);
  83. }
  84. public static T ToEnum<T>(this string input)
  85. {
  86. Debug.Assert(typeof (T).IsEnum,"t.IsEnum");
  87. return (T) Enum.Parse(typeof (T), input, true);
  88. }
  89. }
  90. }