Struct InternedString
Wraps around a string to allow for faster case-insensitive string comparisons while preserving original casing.
Namespace: UnityEngine.InputSystem.Utilities
Syntax
public struct InternedString : IEquatable<InternedString>, IComparable<InternedString>
Remarks
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.
Constructors
InternedString(String)
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).
Declaration
public InternedString(string text)
Parameters
Type | Name | Description |
---|---|---|
String | text | A string. Can be null. |
Remarks
The InternedString preserves the original casing. Meaning that ToString() will
return the string as it was supplied through text
. 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);
Properties
length
Length of the string in characters. Equivalent to string.Length
.
Declaration
public int length { get; }
Property Value
Type | Description |
---|---|
Int32 | Length of the string. |
Methods
CompareTo(InternedString)
Declaration
public int CompareTo(InternedString other)
Parameters
Type | Name | Description |
---|---|---|
InternedString | other |
Returns
Type | Description |
---|---|
Int32 |
Implements
Equals(Object)
Compare the InternedString to given object.
Declaration
public override bool Equals(object obj)
Parameters
Type | Name | Description |
---|---|---|
Object | obj | An object. If it is a |
Returns
Type | Description |
---|---|
Boolean | True if the InternedString is equal to |
Overrides
Equals(InternedString)
Compare two InternedStrings for equality. They are equal if, ignoring case and culture, their text is equal.
Declaration
public bool Equals(InternedString other)
Parameters
Type | Name | Description |
---|---|---|
InternedString | other | Another InternedString. |
Returns
Type | Description |
---|---|
Boolean | True if the two InternedStrings are equal. |
Implements
Remarks
This operation is cheap and does not involve an actual string comparison. Instead,
a simple Object.ReferenceEquals
comparison is performed.
GetHashCode()
Compute a hash code for the string. Equivalent to string.GetHashCode
.
Declaration
public override int GetHashCode()
Returns
Type | Description |
---|---|
Int32 | A hash code. |
Overrides
IsEmpty()
Whether the string is empty, i.e. has a length of zero. If so, the
InternedString corresponds to default(InternedString)
.
Declaration
public bool IsEmpty()
Returns
Type | Description |
---|---|
Boolean | True if the string is empty. |
ToLower()
Return a lower-case version of the string.
Declaration
public string ToLower()
Returns
Type | Description |
---|---|
String | A lower-case version of the string. |
Remarks
InternedStrings internally always store a lower-case version which means that this method does not incur a GC heap allocation cost.
ToString()
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
String |
Overrides
Operators
Equality(String, InternedString)
Declaration
public static bool operator ==(string a, InternedString b)
Parameters
Type | Name | Description |
---|---|---|
String | a | |
InternedString | b |
Returns
Type | Description |
---|---|
Boolean |
Equality(InternedString, String)
Declaration
public static bool operator ==(InternedString a, string b)
Parameters
Type | Name | Description |
---|---|---|
InternedString | a | |
String | b |
Returns
Type | Description |
---|---|
Boolean |
Equality(InternedString, InternedString)
Declaration
public static bool operator ==(InternedString a, InternedString b)
Parameters
Type | Name | Description |
---|---|---|
InternedString | a | |
InternedString | b |
Returns
Type | Description |
---|---|
Boolean |
GreaterThan(InternedString, InternedString)
Declaration
public static bool operator>(InternedString left, InternedString right)
Parameters
Type | Name | Description |
---|---|---|
InternedString | left | |
InternedString | right |
Returns
Type | Description |
---|---|
Boolean |
Implicit(InternedString to String)
Convert the given InternedString back to a string
. Equivalent to ToString().
Declaration
public static implicit operator string (InternedString str)
Parameters
Type | Name | Description |
---|---|---|
InternedString | str | An InternedString. |
Returns
Type | Description |
---|---|
String | A string. |
Inequality(String, InternedString)
Declaration
public static bool operator !=(string a, InternedString b)
Parameters
Type | Name | Description |
---|---|---|
String | a | |
InternedString | b |
Returns
Type | Description |
---|---|
Boolean |
Inequality(InternedString, String)
Declaration
public static bool operator !=(InternedString a, string b)
Parameters
Type | Name | Description |
---|---|---|
InternedString | a | |
String | b |
Returns
Type | Description |
---|---|
Boolean |
Inequality(InternedString, InternedString)
Declaration
public static bool operator !=(InternedString a, InternedString b)
Parameters
Type | Name | Description |
---|---|---|
InternedString | a | |
InternedString | b |
Returns
Type | Description |
---|---|
Boolean |
LessThan(InternedString, InternedString)
Declaration
public static bool operator <(InternedString left, InternedString right)
Parameters
Type | Name | Description |
---|---|---|
InternedString | left | |
InternedString | right |
Returns
Type | Description |
---|---|
Boolean |