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