TMP_TextParsingUtilities.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. using System.Collections;
  2. using System.Collections.Generic;
  3. using UnityEngine;
  4. namespace TMPro
  5. {
  6. public class TMP_TextParsingUtilities
  7. {
  8. private static readonly TMP_TextParsingUtilities s_Instance = new TMP_TextParsingUtilities();
  9. /// <summary>
  10. /// Default constructor
  11. /// </summary>
  12. static TMP_TextParsingUtilities() { }
  13. /// <summary>
  14. /// Get a singleton instance of the TextModuleUtilities.
  15. /// </summary>
  16. public static TMP_TextParsingUtilities instance
  17. {
  18. get { return s_Instance; }
  19. }
  20. /// <summary>
  21. /// Function returning the hashcode value of a given string.
  22. /// </summary>
  23. public static uint GetHashCode(string s)
  24. {
  25. uint hashCode = 0;
  26. for (int i = 0; i < s.Length; i++)
  27. hashCode = ((hashCode << 5) + hashCode) ^ ToUpperASCIIFast(s[i]);
  28. return hashCode;
  29. }
  30. public static int GetHashCodeCaseSensitive(string s)
  31. {
  32. int hashCode = 0;
  33. for (int i = 0; i < s.Length; i++)
  34. hashCode = ((hashCode << 5) + hashCode) ^ s[i];
  35. return hashCode;
  36. }
  37. /// <summary>
  38. /// Table used to convert character to lowercase.
  39. /// </summary>
  40. const string k_LookupStringL = "-------------------------------- !-#$%&-()*+,-./0123456789:;<=>?@abcdefghijklmnopqrstuvwxyz[-]^_`abcdefghijklmnopqrstuvwxyz{|}~-";
  41. /// <summary>
  42. /// Table used to convert character to uppercase.
  43. /// </summary>
  44. const string k_LookupStringU = "-------------------------------- !-#$%&-()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[-]^_`ABCDEFGHIJKLMNOPQRSTUVWXYZ{|}~-";
  45. /// <summary>
  46. /// Get lowercase version of this ASCII character.
  47. /// </summary>
  48. //[MethodImpl(MethodImplOptions.AggressiveInlining)]
  49. public static char ToLowerASCIIFast(char c)
  50. {
  51. if (c > k_LookupStringL.Length - 1)
  52. return c;
  53. return k_LookupStringL[c];
  54. }
  55. /// <summary>
  56. /// Get uppercase version of this ASCII character.
  57. /// </summary>
  58. //[MethodImpl(MethodImplOptions.AggressiveInlining)]
  59. public static char ToUpperASCIIFast(char c)
  60. {
  61. if (c > k_LookupStringU.Length - 1)
  62. return c;
  63. return k_LookupStringU[c];
  64. }
  65. /// <summary>
  66. /// Get uppercase version of this ASCII character.
  67. /// </summary>
  68. //[MethodImpl(MethodImplOptions.AggressiveInlining)]
  69. public static uint ToUpperASCIIFast(uint c)
  70. {
  71. if (c > k_LookupStringU.Length - 1)
  72. return c;
  73. return k_LookupStringU[(int)c];
  74. }
  75. /// <summary>
  76. /// Get lowercase version of this ASCII character.
  77. /// </summary>
  78. //[MethodImpl(MethodImplOptions.AggressiveInlining)]
  79. public static uint ToLowerASCIIFast(uint c)
  80. {
  81. if (c > k_LookupStringL.Length - 1)
  82. return c;
  83. return k_LookupStringL[(int)c];
  84. }
  85. /// <summary>
  86. /// Check if Unicode is High Surrogate
  87. /// </summary>
  88. /// <param name="c"></param>
  89. /// <returns></returns>
  90. //[MethodImpl(MethodImplOptions.AggressiveInlining)]
  91. public static bool IsHighSurrogate(uint c)
  92. {
  93. return c > 0xD800 && c < 0xDBFF;
  94. }
  95. /// <summary>
  96. /// Check if Unicode is Low Surrogate
  97. /// </summary>
  98. /// <param name="c"></param>
  99. /// <returns></returns>
  100. //[MethodImpl(MethodImplOptions.AggressiveInlining)]
  101. public static bool IsLowSurrogate(uint c)
  102. {
  103. return c > 0xDC00 && c < 0xDFFF;
  104. }
  105. }
  106. }