//------------------------------------------------------------------------------ // // This code was generated by a tool. // // TextTransform Samples/Packages/com.unity.collections/Unity.Collections/HeapString.tt // // Changes to this file may cause incorrect behavior and will be lost if // the code is regenerated. // //------------------------------------------------------------------------------ using System; using System.Collections; using System.Collections.Generic; using System.Diagnostics; using System.Runtime.InteropServices; using System.Runtime.CompilerServices; using Unity.Collections.LowLevel.Unsafe; using UnityEngine.Internal; #if UNITY_PROPERTIES_EXISTS using Unity.Properties; #endif namespace Unity.Collections { /// /// An unmanaged, allocated, mutable, resizable UTF-8 string. /// /// /// The string is always null-terminated, meaning a zero byte always immediately follows the last character. /// [BurstCompatible] [Obsolete("HeapString has been removed and replaced with NativeText (RemovedAfter 2021-07-21) (UnityUpgradable) -> NativeText", false)] public partial struct HeapString : INativeList , IDisposable , IUTF8Bytes , IComparable , IEquatable , IComparable , IEquatable , IComparable , IEquatable , IComparable , IEquatable , IComparable , IEquatable , IComparable , IEquatable , IComparable , IEquatable { // NOTE! This Length is always > 0, because we have a null terminating byte. // We hide this byte from HeapString users. private NativeList m_Data; /// /// The current length in bytes of this string. /// /// /// The length does not include the null terminator byte. /// /// The current length in bytes of the UTF-8 encoded string. public int Length { get { return m_Data.Length - 1; } set { m_Data.Resize(value + 1, NativeArrayOptions.UninitializedMemory); m_Data[value] = 0; } } /// /// The current capacity of this string. /// /// /// The null-terminator byte is not included in the Capacity, so the string's character buffer is `Capacity + 1` in size. /// /// The current capacity of the string. public int Capacity { get { return m_Data.Capacity - 1; } set { m_Data.Capacity = value + 1; } } /// /// Attempt to set the length in bytes of this string. /// /// The new length in bytes of the string. /// Whether any bytes added should be zeroed out. /// Always true. public bool TryResize(int newLength, NativeArrayOptions clearOptions = NativeArrayOptions.ClearMemory) { // this can't ever fail, because if we can't resize malloc will abort Length = newLength; return true; } /// /// Whether this string has no characters. /// /// True if this string has no characters. public bool IsEmpty => m_Data.Length == 1; /// /// Whether this string's character buffer has been allocated and not yet deallocated. /// /// Whether this string's character buffer has been allocated and not yet deallocated. public bool IsCreated => m_Data.IsCreated; /// /// Returns a pointer to this string's character buffer. /// /// /// The pointer is made invalid by operations that reallocate the character buffer, such as setting . /// /// A pointer to this string's character buffer. public unsafe byte* GetUnsafePtr() { return (byte*) m_Data.GetUnsafePtr(); } /// /// The byte at an index. /// /// A zero-based byte index. /// The byte at the index. /// Thrown if the index is out of bounds. public byte this[int index] { get { CheckIndexInRange(index); return m_Data[index]; } set { CheckIndexInRange(index); m_Data[index] = value; } } /// /// Returns the reference to the byte (not character) at an index. /// /// /// Deallocating or reallocating this string's character buffer makes the reference invalid. /// /// A byte index. /// A reference to the byte at the index. /// Thrown if the index is out of bounds. public ref byte ElementAt(int index) { CheckIndexInRange(index); return ref m_Data.ElementAt(index); } /// /// Sets the length to 0. /// public void Clear() { Length = 0; } /// /// Appends a byte. /// /// /// A zero byte will always follow the newly appended byte. /// /// No validation is performed: it is your responsibility for the bytes of the string to form valid UTF-8 when you're done appending bytes. /// /// A byte to append. public void Add(in byte value) { this[Length++] = value; } /// /// Returns the lexicographical sort order of this string relative to another. /// /// Another string to compare with. /// A number denoting the lexicographical sort order of this string relative to the other string: /// /// 0 denotes both strings have the same sort position.
/// -1 denotes that this string should be sorted to precede the other.
/// +1 denotes that this string should be sorted to follow the other.
///
public int CompareTo(HeapString other) { return FixedStringMethods.CompareTo(ref this, other); } /// /// Returns true if this string and another are equal. /// /// Two strings are equal if they have equal length and all their characters match. /// Another string to compare with. /// True if the two strings are equal. public bool Equals(HeapString other) { return FixedStringMethods.Equals(ref this, other); } /// /// Releases all resources (memory and safety handles). /// public void Dispose() { m_Data.Dispose(); } /// /// A copy of this string as a managed string. /// /// /// For internal use only. Use instead. /// /// A copy of this string as a managed string. [CreateProperty] [System.ComponentModel.EditorBrowsable(System.ComponentModel.EditorBrowsableState.Never)] [NotBurstCompatible] public string Value => ToString(); /// /// An enumerator over the characters (not bytes) of a HeapString. /// /// /// In an enumerator's initial state, its index is invalid. The first call advances the enumerator's index to the first character. /// public struct Enumerator : IEnumerator { HeapString target; int offset; Unicode.Rune current; /// /// Initializes and returns an instance of HeapString.Enumerator. /// /// A HeapString for which to create an enumerator. public Enumerator(HeapString source) { target = source; offset = 0; current = default; } /// /// Does nothing. /// public void Dispose() { } /// /// Advances the enumerator to the next character, returning true if is valid to read afterwards. /// /// True if is valid to read after the call. public bool MoveNext() { if (offset >= target.Length) return false; unsafe { Unicode.Utf8ToUcs(out current, target.GetUnsafePtr(), ref offset, target.Length); } return true; } /// /// Resets the enumerator to its initial state. /// public void Reset() { offset = 0; current = default; } object IEnumerator.Current => Current; /// /// The current character. /// /// The current character. public Unicode.Rune Current => current; } /// /// Returns an enumerator for iterating over the characters of the HeapString. /// /// An enumerator for iterating over the characters of the HeapString. public Enumerator GetEnumerator() { return new Enumerator(this); } /// /// Returns the lexicographical sort order of this string relative to another. /// /// Another string to compare with. /// A number denoting the lexicographical sort order of this string relative to the other string: /// /// 0 denotes both strings have the same sort position.
/// -1 denotes that this string should be sorted to precede the other.
/// +1 denotes that this string should be sorted to follow the other.
///
[NotBurstCompatible] public int CompareTo(String other) { return ToString().CompareTo(other); } /// /// Returns true if this string and another are equal. /// /// Two strings are equal if they have equal length and all their characters match. /// Another string to compare with. /// True if the two strings are equal. [NotBurstCompatible] public bool Equals(String other) { return ToString().Equals(other); } /// /// Initializes and returns an instance of HeapString with the characters copied from another string. /// /// A string to copy characters from. /// The allocator to use. [NotBurstCompatible] public HeapString(String source, Allocator allocator) { m_Data = new NativeList(source.Length * 2 + 1, allocator); Length = source.Length * 2; // maximum possible unsafe { fixed (char* sourceptr = source) { var error = UTF8ArrayUnsafeUtility.Copy(GetUnsafePtr(), out var actualBytes, Capacity, sourceptr, source.Length); if (error != CopyError.None) { m_Data.Dispose(); m_Data = default; ThrowCopyError(error, source); } this.Length = actualBytes; } } } /// /// Initializes and returns an instance of HeapString with a specified initial capacity. /// /// The initial capacity in bytes. /// The allocator to use. public HeapString(int capacity, Allocator allocator) { m_Data = new NativeList(capacity + 1, allocator); this.Length = 0; } /// /// Initializes and returns an instance of HeapString with an initial capacity of 128 bytes. /// /// The allocator to use. public HeapString(Allocator allocator) { m_Data = new NativeList(128 + 1, allocator); this.Length = 0; } /// /// Returns the lexicographical sort order of this string relative to another. /// /// Another string to compare with. /// A number denoting the lexicographical sort order of this string relative to the other string: /// /// 0 denotes both strings have the same sort position.
/// -1 denotes that this string should be sorted to precede the other.
/// +1 denotes that this string should be sorted to follow the other.
///
public int CompareTo(FixedString32Bytes other) { return FixedStringMethods.CompareTo(ref this, other); } /// /// Initializes and returns an instance of HeapString with the characters copied from another string. /// /// A string to copy characters from. /// The allocator to use. public HeapString(in FixedString32Bytes source, Allocator allocator) { m_Data = new NativeList(source.utf8LengthInBytes + 1, allocator); Length = source.utf8LengthInBytes; unsafe { byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes); byte* dbytes = (byte*) m_Data.GetUnsafePtr(); UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes); } } /// /// Returns true if two strings are equal. /// /// Two strings are equal if they have equal length and all their characters match. /// A string to compare. /// Another string to compare. /// True if the two strings are equal. public static bool operator ==(in HeapString a, in FixedString32Bytes b) { unsafe { var aref = UnsafeUtilityExtensions.AsRef(a); int alen = aref.Length; int blen = b.utf8LengthInBytes; byte* aptr = (byte*) aref.GetUnsafePtr(); byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes); return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen); } } /// /// Returns true if two strings are unequal. /// /// Two strings are equal if they have equal length and all their characters match. /// A string to compare. /// Another string to compare. /// True if the two strings are unequal. public static bool operator !=(in HeapString a, in FixedString32Bytes b) { return !(a == b); } /// /// Returns true if this string and another are equal. /// /// Two strings are equal if they have equal length and all their characters match. /// Another string to compare with. /// True if the two strings are equal. public bool Equals(FixedString32Bytes other) { return this == other; } /// /// Returns the lexicographical sort order of this string relative to another. /// /// Another string to compare with. /// A number denoting the lexicographical sort order of this string relative to the other string: /// /// 0 denotes both strings have the same sort position.
/// -1 denotes that this string should be sorted to precede the other.
/// +1 denotes that this string should be sorted to follow the other.
///
public int CompareTo(FixedString64Bytes other) { return FixedStringMethods.CompareTo(ref this, other); } /// /// Initializes and returns an instance of HeapString with the characters copied from another string. /// /// A string to copy characters from. /// The allocator to use. public HeapString(in FixedString64Bytes source, Allocator allocator) { m_Data = new NativeList(source.utf8LengthInBytes + 1, allocator); Length = source.utf8LengthInBytes; unsafe { byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes); byte* dbytes = (byte*) m_Data.GetUnsafePtr(); UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes); } } /// /// Returns true if two strings are equal. /// /// Two strings are equal if they have equal length and all their characters match. /// A string to compare. /// Another string to compare. /// True if the two strings are equal. public static bool operator ==(in HeapString a, in FixedString64Bytes b) { unsafe { var aref = UnsafeUtilityExtensions.AsRef(a); int alen = aref.Length; int blen = b.utf8LengthInBytes; byte* aptr = (byte*) aref.GetUnsafePtr(); byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes); return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen); } } /// /// Returns true if two strings are unequal. /// /// Two strings are equal if they have equal length and all their characters match. /// A string to compare. /// Another string to compare. /// True if the two strings are unequal. public static bool operator !=(in HeapString a, in FixedString64Bytes b) { return !(a == b); } /// /// Returns true if this string and another are equal. /// /// Two strings are equal if they have equal length and all their characters match. /// Another string to compare with. /// True if the two strings are equal. public bool Equals(FixedString64Bytes other) { return this == other; } /// /// Returns the lexicographical sort order of this string relative to another. /// /// Another string to compare with. /// A number denoting the lexicographical sort order of this string relative to the other string: /// /// 0 denotes both strings have the same sort position.
/// -1 denotes that this string should be sorted to precede the other.
/// +1 denotes that this string should be sorted to follow the other.
///
public int CompareTo(FixedString128Bytes other) { return FixedStringMethods.CompareTo(ref this, other); } /// /// Initializes and returns an instance of HeapString with the characters copied from another string. /// /// A string to copy characters from. /// The allocator to use. public HeapString(in FixedString128Bytes source, Allocator allocator) { m_Data = new NativeList(source.utf8LengthInBytes + 1, allocator); Length = source.utf8LengthInBytes; unsafe { byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes); byte* dbytes = (byte*) m_Data.GetUnsafePtr(); UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes); } } /// /// Returns true if two strings are equal. /// /// Two strings are equal if they have equal length and all their characters match. /// A string to compare. /// Another string to compare. /// True if the two strings are equal. public static bool operator ==(in HeapString a, in FixedString128Bytes b) { unsafe { var aref = UnsafeUtilityExtensions.AsRef(a); int alen = aref.Length; int blen = b.utf8LengthInBytes; byte* aptr = (byte*) aref.GetUnsafePtr(); byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes); return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen); } } /// /// Returns true if two strings are unequal. /// /// Two strings are equal if they have equal length and all their characters match. /// A string to compare. /// Another string to compare. /// True if the two strings are unequal. public static bool operator !=(in HeapString a, in FixedString128Bytes b) { return !(a == b); } /// /// Returns true if this string and another are equal. /// /// Two strings are equal if they have equal length and all their characters match. /// Another string to compare with. /// True if the two strings are equal. public bool Equals(FixedString128Bytes other) { return this == other; } /// /// Returns the lexicographical sort order of this string relative to another. /// /// Another string to compare with. /// A number denoting the lexicographical sort order of this string relative to the other string: /// /// 0 denotes both strings have the same sort position.
/// -1 denotes that this string should be sorted to precede the other.
/// +1 denotes that this string should be sorted to follow the other.
///
public int CompareTo(FixedString512Bytes other) { return FixedStringMethods.CompareTo(ref this, other); } /// /// Initializes and returns an instance of HeapString with the characters copied from another string. /// /// A string to copy characters from. /// The allocator to use. public HeapString(in FixedString512Bytes source, Allocator allocator) { m_Data = new NativeList(source.utf8LengthInBytes + 1, allocator); Length = source.utf8LengthInBytes; unsafe { byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes); byte* dbytes = (byte*) m_Data.GetUnsafePtr(); UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes); } } /// /// Returns true if two strings are equal. /// /// Two strings are equal if they have equal length and all their characters match. /// A string to compare. /// Another string to compare. /// True if the two strings are equal. public static bool operator ==(in HeapString a, in FixedString512Bytes b) { unsafe { var aref = UnsafeUtilityExtensions.AsRef(a); int alen = aref.Length; int blen = b.utf8LengthInBytes; byte* aptr = (byte*) aref.GetUnsafePtr(); byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes); return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen); } } /// /// Returns true if two strings are unequal. /// /// Two strings are equal if they have equal length and all their characters match. /// A string to compare. /// Another string to compare. /// True if the two strings are unequal. public static bool operator !=(in HeapString a, in FixedString512Bytes b) { return !(a == b); } /// /// Returns true if this string and another are equal. /// /// Two strings are equal if they have equal length and all their characters match. /// Another string to compare with. /// True if the two strings are equal. public bool Equals(FixedString512Bytes other) { return this == other; } /// /// Returns the lexicographical sort order of this string relative to another. /// /// Another string to compare with. /// A number denoting the lexicographical sort order of this string relative to the other string: /// /// 0 denotes both strings have the same sort position.
/// -1 denotes that this string should be sorted to precede the other.
/// +1 denotes that this string should be sorted to follow the other.
///
public int CompareTo(FixedString4096Bytes other) { return FixedStringMethods.CompareTo(ref this, other); } /// /// Initializes and returns an instance of HeapString with the characters copied from another string. /// /// A string to copy characters from. /// The allocator to use. public HeapString(in FixedString4096Bytes source, Allocator allocator) { m_Data = new NativeList(source.utf8LengthInBytes + 1, allocator); Length = source.utf8LengthInBytes; unsafe { byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes); byte* dbytes = (byte*) m_Data.GetUnsafePtr(); UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes); } } /// /// Returns true if two strings are equal. /// /// Two strings are equal if they have equal length and all their characters match. /// A string to compare. /// Another string to compare. /// True if the two strings are equal. public static bool operator ==(in HeapString a, in FixedString4096Bytes b) { unsafe { var aref = UnsafeUtilityExtensions.AsRef(a); int alen = aref.Length; int blen = b.utf8LengthInBytes; byte* aptr = (byte*) aref.GetUnsafePtr(); byte* bptr = (byte*) UnsafeUtilityExtensions.AddressOf(b.bytes); return UTF8ArrayUnsafeUtility.EqualsUTF8Bytes(aptr, alen, bptr, blen); } } /// /// Returns true if two strings are unequal. /// /// Two strings are equal if they have equal length and all their characters match. /// A string to compare. /// Another string to compare. /// True if the two strings are unequal. public static bool operator !=(in HeapString a, in FixedString4096Bytes b) { return !(a == b); } /// /// Returns true if this string and another are equal. /// /// Two strings are equal if they have equal length and all their characters match. /// Another string to compare with. /// True if the two strings are equal. public bool Equals(FixedString4096Bytes other) { return this == other; } /// /// Returns a managed string copy of this string. /// /// A managed string copy of this string.> [NotBurstCompatible] public override String ToString() { if (!m_Data.IsCreated) return ""; return this.ConvertToString(); } /// /// Returns a hash code of this string. /// /// The hash code is an integer that is always the same for two equal strings but (very likely) different for two unequal strings. /// A hash code of this string. public override int GetHashCode() { return this.ComputeHashCode(); } /// /// Returns true if this string and another object are equal. /// /// For the object to be equal, it must itself be a managed string, HeapString, or FixedString*N*Bytes. /// /// Two strings are equal if they have equal length and all their characters match. /// Another string to compare with. /// True if this string and the object are equal. [NotBurstCompatible] public override bool Equals(object other) { if(ReferenceEquals(null, other)) return false; if(other is String aString) return Equals(aString); if(other is HeapString aHeapString) return Equals(aHeapString); if(other is FixedString32Bytes a32) return Equals(a32); if(other is FixedString64Bytes a64) return Equals(a64); if(other is FixedString128Bytes a128) return Equals(a128); if(other is FixedString512Bytes a512) return Equals(a512); if(other is FixedString4096Bytes a4096) return Equals(a4096); return false; } [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")] void CheckIndexInRange(int index) { if (index < 0) throw new IndexOutOfRangeException($"Index {index} must be positive."); if (index >= Length) throw new IndexOutOfRangeException($"Index {index} is out of range in HeapString of {Length} length."); } [Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")] void ThrowCopyError(CopyError error, String source) { throw new ArgumentException($"HeapString: {error} while copying \"{source}\""); } } }