File size: 8,694 Bytes
18a519f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
using System;
using System.Globalization;

////TODO: goal should be to end up with this being internal

////TODO: instead of using string.Intern, put them in a custom table and allow passing them around as indices
////      (this will probably also be useful for jobs)
////      when this is implemented, also allow interning directly from Substrings

namespace UnityEngine.InputSystem.Utilities
{
    /// <summary>
    /// Wraps around a string to allow for faster case-insensitive string comparisons while
    /// preserving original casing.
    /// </summary>
    /// <remarks>
    /// Unlike <c>string</c>, InternedStrings can be compared with a quick <c>Object.ReferenceEquals</c>
    /// comparison and without actually comparing string contents.
    ///
    /// Also, unlike <c>string</c>, the representation of an empty and a <c>null</c> string is identical.
    ///
    /// Note that all string comparisons using InternedStrings are both case-insensitive and culture-insensitive.
    ///
    /// There is a non-zero cost to creating an InternedString. The first time a new unique InternedString
    /// is encountered, there may also be a GC heap allocation.
    /// </remarks>
    public struct InternedString : IEquatable<InternedString>, IComparable<InternedString>
    {
        private readonly string m_StringOriginalCase;
        private readonly string m_StringLowerCase;

        /// <summary>
        /// Length of the string in characters. Equivalent to <c>string.Length</c>.
        /// </summary>
        /// <value>Length of the string.</value>
        public int length => m_StringLowerCase?.Length ?? 0;

        /// <summary>
        /// Initialize the InternedString with the given string. Except if the string is <c>null</c>
        /// or empty, this requires an internal lookup (this is the reason the conversion from <c>string</c>
        /// to InternedString is not implicit).
        /// </summary>
        /// <param name="text">A string. Can be null.</param>
        /// <remarks>
        /// The InternedString preserves the original casing. Meaning that <see cref="ToString()"/> will
        /// return the string as it was supplied through <paramref name="text"/>. However, comparison
        /// between two InternedStrings is still always just a reference comparisons regardless of case
        /// and culture.
        ///
        /// <example>
        /// <code>
        /// var lowerCase = new InternedString("text");
        /// var upperCase = new InternedString("TEXT");
        ///
        /// // This is still just a quick reference comparison:
        /// if (lowerCase == upperCase)
        ///     Debug.Log("True");
        ///
        /// // But this prints the strings in their original casing.
        /// Debug.Log(lowerCase);
        /// Debug.Log(upperCase);
        /// </code>
        /// </example>
        /// </remarks>
        public InternedString(string text)
        {
            if (string.IsNullOrEmpty(text))
            {
                m_StringOriginalCase = null;
                m_StringLowerCase = null;
            }
            else
            {
                ////TODO: I think instead of string.Intern() this should use a custom weak-referenced intern table
                ////      (this way we can also avoid the garbage from ToLower())
                m_StringOriginalCase = string.Intern(text);
                m_StringLowerCase = string.Intern(text.ToLower(CultureInfo.InvariantCulture));
            }
        }

        /// <summary>
        /// Whether the string is empty, i.e. has a <see cref="length"/> of zero. If so, the
        /// InternedString corresponds to <c>default(InternedString)</c>.
        /// </summary>
        /// <returns>True if the string is empty.</returns>
        public bool IsEmpty()
        {
            return m_StringLowerCase == null;
        }

        /// <summary>
        /// Return a lower-case version of the string.
        /// </summary>
        /// <returns>A lower-case version of the string.</returns>
        /// <remarks>
        /// InternedStrings internally always store a lower-case version which means that this
        /// method does not incur a GC heap allocation cost.
        /// </remarks>
        public string ToLower()
        {
            return m_StringLowerCase;
        }

        /// <summary>
        /// Compare the InternedString to given object.
        /// </summary>
        /// <param name="obj">An object. If it is a <c>string</c>, performs a string comparison. If
        /// it is an InternedString, performs an InternedString-comparison. Otherwise returns false.</param>
        /// <returns>True if the InternedString is equal to <paramref name="obj"/>.</returns>
        public override bool Equals(object obj)
        {
            if (obj is InternedString other)
                return Equals(other);

            if (obj is string str)
            {
                if (m_StringLowerCase == null)
                    return string.IsNullOrEmpty(str);
                return string.Equals(m_StringLowerCase, str.ToLower(CultureInfo.InvariantCulture));
            }

            return false;
        }

        /// <summary>
        /// Compare two InternedStrings for equality. They are equal if, ignoring case and culture,
        /// their text is equal.
        /// </summary>
        /// <param name="other">Another InternedString.</param>
        /// <returns>True if the two InternedStrings are equal.</returns>
        /// <remarks>
        /// This operation is cheap and does not involve an actual string comparison. Instead,
        /// a simple <c>Object.ReferenceEquals</c> comparison is performed.
        /// </remarks>
        public bool Equals(InternedString other)
        {
            return ReferenceEquals(m_StringLowerCase, other.m_StringLowerCase);
        }

        public int CompareTo(InternedString other)
        {
            return string.Compare(m_StringLowerCase, other.m_StringLowerCase,
                StringComparison.InvariantCultureIgnoreCase);
        }

        /// <summary>
        /// Compute a hash code for the string. Equivalent to <c>string.GetHashCode</c>.
        /// </summary>
        /// <returns>A hash code.</returns>
        public override int GetHashCode()
        {
            if (m_StringLowerCase == null)
                return 0;
            return m_StringLowerCase.GetHashCode();
        }

        public override string ToString()
        {
            return m_StringOriginalCase ?? string.Empty;
        }

        public static bool operator==(InternedString a, InternedString b)
        {
            return a.Equals(b);
        }

        public static bool operator!=(InternedString a, InternedString b)
        {
            return !a.Equals(b);
        }

        public static bool operator==(InternedString a, string b)
        {
            return string.Compare(a.m_StringLowerCase, b.ToLower(CultureInfo.InvariantCulture),
                StringComparison.InvariantCultureIgnoreCase) == 0;
        }

        public static bool operator!=(InternedString a, string b)
        {
            return string.Compare(a.m_StringLowerCase, b.ToLower(CultureInfo.InvariantCulture),
                StringComparison.InvariantCultureIgnoreCase) != 0;
        }

        public static bool operator==(string a, InternedString b)
        {
            return string.Compare(a.ToLower(CultureInfo.InvariantCulture), b.m_StringLowerCase,
                StringComparison.InvariantCultureIgnoreCase) == 0;
        }

        public static bool operator!=(string a, InternedString b)
        {
            return string.Compare(a.ToLower(CultureInfo.InvariantCulture), b.m_StringLowerCase,
                StringComparison.InvariantCultureIgnoreCase) != 0;
        }

        public static bool operator<(InternedString left, InternedString right)
        {
            return string.Compare(left.m_StringLowerCase, right.m_StringLowerCase,
                StringComparison.InvariantCultureIgnoreCase) < 0;
        }

        public static bool operator>(InternedString left, InternedString right)
        {
            return string.Compare(left.m_StringLowerCase, right.m_StringLowerCase,
                StringComparison.InvariantCultureIgnoreCase) > 0;
        }

        /// <summary>
        /// Convert the given InternedString back to a <c>string</c>. Equivalent to <see cref="ToString()"/>.
        /// </summary>
        /// <param name="str">An InternedString.</param>
        /// <returns>A string.</returns>
        public static implicit operator string(InternedString str)
        {
            return str.ToString();
        }
    }
}