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
{
///
/// Wraps around a string to allow for faster case-insensitive string comparisons while
/// preserving original casing.
///
///
/// Unlike string, InternedStrings can be compared with a quick Object.ReferenceEquals
/// comparison and without actually comparing string contents.
///
/// Also, unlike string, the representation of an empty and a null 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.
///
public struct InternedString : IEquatable, IComparable
{
private readonly string m_StringOriginalCase;
private readonly string m_StringLowerCase;
///
/// Length of the string in characters. Equivalent to string.Length.
///
/// Length of the string.
public int length => m_StringLowerCase?.Length ?? 0;
///
/// Initialize the InternedString with the given string. Except if the string is null
/// or empty, this requires an internal lookup (this is the reason the conversion from string
/// to InternedString is not implicit).
///
/// A string. Can be null.
///
/// The InternedString preserves the original casing. Meaning that will
/// return the string as it was supplied through . However, comparison
/// between two InternedStrings is still always just a reference comparisons regardless of case
/// and culture.
///
///
///
/// 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);
///
///
///
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));
}
}
///
/// Whether the string is empty, i.e. has a of zero. If so, the
/// InternedString corresponds to default(InternedString).
///
/// True if the string is empty.
public bool IsEmpty()
{
return m_StringLowerCase == null;
}
///
/// Return a lower-case version of the string.
///
/// A lower-case version of the string.
///
/// InternedStrings internally always store a lower-case version which means that this
/// method does not incur a GC heap allocation cost.
///
public string ToLower()
{
return m_StringLowerCase;
}
///
/// Compare the InternedString to given object.
///
/// An object. If it is a string, performs a string comparison. If
/// it is an InternedString, performs an InternedString-comparison. Otherwise returns false.
/// True if the InternedString is equal to .
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;
}
///
/// Compare two InternedStrings for equality. They are equal if, ignoring case and culture,
/// their text is equal.
///
/// Another InternedString.
/// True if the two InternedStrings are equal.
///
/// This operation is cheap and does not involve an actual string comparison. Instead,
/// a simple Object.ReferenceEquals comparison is performed.
///
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);
}
///
/// Compute a hash code for the string. Equivalent to string.GetHashCode.
///
/// A hash code.
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;
}
///
/// Convert the given InternedString back to a string. Equivalent to .
///
/// An InternedString.
/// A string.
public static implicit operator string(InternedString str)
{
return str.ToString();
}
}
}