<#@ template language="C#" debug="True" #> <#@ output extension=".gen.cs" encoding="utf-8" #> <#@ assembly name="System.Core" #> //------------------------------------------------------------------------------ // // 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 <# { var SIZES = new [] {32,64,128,512,4096}; #> 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 <# foreach (var OTHERBYTES in SIZES) { #> , IComparableBytes> , IEquatableBytes> <# } #> { // 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; } <# // // Generate easy conversion and comparison between this and other FixedString types // foreach (var OTHERBYTES in SIZES) { #> /// /// 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(FixedString<#=OTHERBYTES#>Bytes 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 FixedString<#=OTHERBYTES#>Bytes 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 FixedString<#=OTHERBYTES#>Bytes 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 FixedString<#=OTHERBYTES#>Bytes 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(FixedString<#=OTHERBYTES#>Bytes 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); <# foreach(var OTHERBYTES in SIZES) { #> if(other is FixedString<#=OTHERBYTES#>Bytes a<#=OTHERBYTES#>) return Equals(a<#=OTHERBYTES#>); <# } #> 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}\""); } } <#}#> }