InternedString.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. using System;
  2. using System.Globalization;
  3. ////TODO: goal should be to end up with this being internal
  4. ////TODO: instead of using string.Intern, put them in a custom table and allow passing them around as indices
  5. //// (this will probably also be useful for jobs)
  6. //// when this is implemented, also allow interning directly from Substrings
  7. namespace UnityEngine.InputSystem.Utilities
  8. {
  9. /// <summary>
  10. /// Wraps around a string to allow for faster case-insensitive string comparisons while
  11. /// preserving original casing.
  12. /// </summary>
  13. /// <remarks>
  14. /// Unlike <c>string</c>, InternedStrings can be compared with a quick <c>Object.ReferenceEquals</c>
  15. /// comparison and without actually comparing string contents.
  16. ///
  17. /// Also, unlike <c>string</c>, the representation of an empty and a <c>null</c> string is identical.
  18. ///
  19. /// Note that all string comparisons using InternedStrings are both case-insensitive and culture-insensitive.
  20. ///
  21. /// There is a non-zero cost to creating an InternedString. The first time a new unique InternedString
  22. /// is encountered, there may also be a GC heap allocation.
  23. /// </remarks>
  24. public struct InternedString : IEquatable<InternedString>, IComparable<InternedString>
  25. {
  26. private readonly string m_StringOriginalCase;
  27. private readonly string m_StringLowerCase;
  28. /// <summary>
  29. /// Length of the string in characters. Equivalent to <c>string.Length</c>.
  30. /// </summary>
  31. /// <value>Length of the string.</value>
  32. public int length => m_StringLowerCase?.Length ?? 0;
  33. /// <summary>
  34. /// Initialize the InternedString with the given string. Except if the string is <c>null</c>
  35. /// or empty, this requires an internal lookup (this is the reason the conversion from <c>string</c>
  36. /// to InternedString is not implicit).
  37. /// </summary>
  38. /// <param name="text">A string. Can be null.</param>
  39. /// <remarks>
  40. /// The InternedString preserves the original casing. Meaning that <see cref="ToString()"/> will
  41. /// return the string as it was supplied through <paramref name="text"/>. However, comparison
  42. /// between two InternedStrings is still always just a reference comparisons regardless of case
  43. /// and culture.
  44. ///
  45. /// <example>
  46. /// <code>
  47. /// var lowerCase = new InternedString("text");
  48. /// var upperCase = new InternedString("TEXT");
  49. ///
  50. /// // This is still just a quick reference comparison:
  51. /// if (lowerCase == upperCase)
  52. /// Debug.Log("True");
  53. ///
  54. /// // But this prints the strings in their original casing.
  55. /// Debug.Log(lowerCase);
  56. /// Debug.Log(upperCase);
  57. /// </code>
  58. /// </example>
  59. /// </remarks>
  60. public InternedString(string text)
  61. {
  62. if (string.IsNullOrEmpty(text))
  63. {
  64. m_StringOriginalCase = null;
  65. m_StringLowerCase = null;
  66. }
  67. else
  68. {
  69. ////TODO: I think instead of string.Intern() this should use a custom weak-referenced intern table
  70. //// (this way we can also avoid the garbage from ToLower())
  71. m_StringOriginalCase = string.Intern(text);
  72. m_StringLowerCase = string.Intern(text.ToLower(CultureInfo.InvariantCulture));
  73. }
  74. }
  75. /// <summary>
  76. /// Whether the string is empty, i.e. has a <see cref="length"/> of zero. If so, the
  77. /// InternedString corresponds to <c>default(InternedString)</c>.
  78. /// </summary>
  79. /// <returns>True if the string is empty.</returns>
  80. public bool IsEmpty()
  81. {
  82. return m_StringLowerCase == null;
  83. }
  84. /// <summary>
  85. /// Return a lower-case version of the string.
  86. /// </summary>
  87. /// <returns>A lower-case version of the string.</returns>
  88. /// <remarks>
  89. /// InternedStrings internally always store a lower-case version which means that this
  90. /// method does not incur a GC heap allocation cost.
  91. /// </remarks>
  92. public string ToLower()
  93. {
  94. return m_StringLowerCase;
  95. }
  96. /// <summary>
  97. /// Compare the InternedString to given object.
  98. /// </summary>
  99. /// <param name="obj">An object. If it is a <c>string</c>, performs a string comparison. If
  100. /// it is an InternedString, performs an InternedString-comparison. Otherwise returns false.</param>
  101. /// <returns>True if the InternedString is equal to <paramref name="obj"/>.</returns>
  102. public override bool Equals(object obj)
  103. {
  104. if (obj is InternedString other)
  105. return Equals(other);
  106. if (obj is string str)
  107. {
  108. if (m_StringLowerCase == null)
  109. return string.IsNullOrEmpty(str);
  110. return string.Equals(m_StringLowerCase, str.ToLower(CultureInfo.InvariantCulture));
  111. }
  112. return false;
  113. }
  114. /// <summary>
  115. /// Compare two InternedStrings for equality. They are equal if, ignoring case and culture,
  116. /// their text is equal.
  117. /// </summary>
  118. /// <param name="other">Another InternedString.</param>
  119. /// <returns>True if the two InternedStrings are equal.</returns>
  120. /// <remarks>
  121. /// This operation is cheap and does not involve an actual string comparison. Instead,
  122. /// a simple <c>Object.ReferenceEquals</c> comparison is performed.
  123. /// </remarks>
  124. public bool Equals(InternedString other)
  125. {
  126. return ReferenceEquals(m_StringLowerCase, other.m_StringLowerCase);
  127. }
  128. public int CompareTo(InternedString other)
  129. {
  130. return string.Compare(m_StringLowerCase, other.m_StringLowerCase,
  131. StringComparison.InvariantCultureIgnoreCase);
  132. }
  133. /// <summary>
  134. /// Compute a hash code for the string. Equivalent to <c>string.GetHashCode</c>.
  135. /// </summary>
  136. /// <returns>A hash code.</returns>
  137. public override int GetHashCode()
  138. {
  139. if (m_StringLowerCase == null)
  140. return 0;
  141. return m_StringLowerCase.GetHashCode();
  142. }
  143. public override string ToString()
  144. {
  145. return m_StringOriginalCase ?? string.Empty;
  146. }
  147. public static bool operator==(InternedString a, InternedString b)
  148. {
  149. return a.Equals(b);
  150. }
  151. public static bool operator!=(InternedString a, InternedString b)
  152. {
  153. return !a.Equals(b);
  154. }
  155. public static bool operator==(InternedString a, string b)
  156. {
  157. return string.Compare(a.m_StringLowerCase, b.ToLower(CultureInfo.InvariantCulture),
  158. StringComparison.InvariantCultureIgnoreCase) == 0;
  159. }
  160. public static bool operator!=(InternedString a, string b)
  161. {
  162. return string.Compare(a.m_StringLowerCase, b.ToLower(CultureInfo.InvariantCulture),
  163. StringComparison.InvariantCultureIgnoreCase) != 0;
  164. }
  165. public static bool operator==(string a, InternedString b)
  166. {
  167. return string.Compare(a.ToLower(CultureInfo.InvariantCulture), b.m_StringLowerCase,
  168. StringComparison.InvariantCultureIgnoreCase) == 0;
  169. }
  170. public static bool operator!=(string a, InternedString b)
  171. {
  172. return string.Compare(a.ToLower(CultureInfo.InvariantCulture), b.m_StringLowerCase,
  173. StringComparison.InvariantCultureIgnoreCase) != 0;
  174. }
  175. public static bool operator<(InternedString left, InternedString right)
  176. {
  177. return string.Compare(left.m_StringLowerCase, right.m_StringLowerCase,
  178. StringComparison.InvariantCultureIgnoreCase) < 0;
  179. }
  180. public static bool operator>(InternedString left, InternedString right)
  181. {
  182. return string.Compare(left.m_StringLowerCase, right.m_StringLowerCase,
  183. StringComparison.InvariantCultureIgnoreCase) > 0;
  184. }
  185. /// <summary>
  186. /// Convert the given InternedString back to a <c>string</c>. Equivalent to <see cref="ToString()"/>.
  187. /// </summary>
  188. /// <param name="str">An InternedString.</param>
  189. /// <returns>A string.</returns>
  190. public static implicit operator string(InternedString str)
  191. {
  192. return str.ToString();
  193. }
  194. }
  195. }