#@ template language="C#" debug="True" #>
<#@ output extension=".gen.cs" encoding="utf-8" #>
<#@ assembly name="System.Core" #>
//------------------------------------------------------------------------------
//
// This code was generated by a tool.
//
// TextTransform Packages/com.unity.collections/Unity.Collections/NativeText.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.Burst;
using Unity.Collections.LowLevel.Unsafe;
using Unity.Jobs;
using UnityEngine.Internal;
using Unity.Properties;
<#
{
var SIZES = new [] {32,64,128,512,4096};
#>
namespace Unity.Collections
{
///
/// An unmanaged, mutable, resizable UTF-8 string.
///
///
/// The string is always null-terminated, meaning a zero byte always immediately follows the last character.
///
[StructLayout(LayoutKind.Sequential)]
[NativeContainer]
[DebuggerDisplay("Length = {Length}")]
[GenerateTestsForBurstCompatibility]
public unsafe partial struct NativeText
: INativeList
, INativeDisposable
, IUTF8Bytes
, IComparable
, IEquatable
, IComparable
, IEquatable
<#
foreach (var OTHERBYTES in SIZES)
{
#>
, IComparableBytes>
, IEquatableBytes>
<#
}
#>
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
internal AtomicSafetyHandle m_Safety;
internal static readonly SharedStatic s_staticSafetyId = SharedStatic.GetOrCreate();
#endif
// NOTE! This Length is always > 0, because we have a null terminating byte.
// We hide this byte from NativeText users.
[NativeDisableUnsafePtrRestriction]
internal UnsafeText* m_Data;
///
/// Initializes and returns an instance of NativeText with the characters copied from another string.
///
/// A string to copy characters from.
/// The allocator to use.
[ExcludeFromBurstCompatTesting("Takes managed string")]
public NativeText(String source, Allocator allocator) : this(source, (AllocatorManager.AllocatorHandle)allocator)
{
}
///
/// Initializes and returns an instance of NativeText with the characters copied from another string.
///
/// A string to copy characters from.
/// The allocator to use.
[ExcludeFromBurstCompatTesting("Takes managed string")]
public NativeText(String source, AllocatorManager.AllocatorHandle allocator) : this(source.Length * 2, allocator)
{
Length = source.Length * 2;
unsafe
{
fixed (char* sourceptr = source)
{
var error = UTF8ArrayUnsafeUtility.Copy(GetUnsafePtr(), out var actualBytes, Capacity, sourceptr, source.Length);
if (error != CopyError.None)
{
CheckNull(m_Data);
m_Data->Dispose();
m_Data = UnsafeText.Alloc(allocator);
*m_Data = default(UnsafeText);
ThrowCopyError(error, source);
}
Length = actualBytes;
}
}
#if ENABLE_UNITY_COLLECTIONS_CHECKS
CollectionHelper.SetStaticSafetyId(ref m_Safety, ref s_staticSafetyId.Data, "Unity.Collections.NativeText");
AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
#endif
}
///
/// Initializes and returns an instance of NativeText.
///
/// The initial capacity in bytes.
/// The allocator to use.
public NativeText(int capacity, Allocator allocator) : this(capacity, (AllocatorManager.AllocatorHandle)allocator)
{
}
///
/// Initializes and returns an instance of NativeText.
///
/// The initial capacity in bytes.
/// The allocator to use.
public NativeText(int capacity, AllocatorManager.AllocatorHandle allocator)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
CollectionHelper.CheckAllocator(allocator);
m_Safety = CollectionHelper.CreateSafetyHandle(allocator);
CollectionHelper.SetStaticSafetyId(ref m_Safety, ref s_staticSafetyId.Data, "Unity.Collections.NativeText");
AtomicSafetyHandle.SetBumpSecondaryVersionOnScheduleWrite(m_Safety, true);
#endif
m_Data = UnsafeText.Alloc(allocator);
*m_Data = new UnsafeText(capacity, allocator);
}
///
/// Initializes and returns an instance of NativeText with an initial capacity of 512 bytes.
///
/// The allocator to use.
public NativeText(Allocator allocator) : this((AllocatorManager.AllocatorHandle)allocator)
{
}
///
/// Initializes and returns an instance of NativeText with an initial capacity of 512 bytes.
///
/// The allocator to use.
public NativeText(AllocatorManager.AllocatorHandle allocator) : this(512, allocator)
{
}
<#
foreach(var OTHERBYTES in SIZES)
{
#>
///
/// Initializes and returns an instance of NativeText with the characters copied from another string.
///
/// A string to copy characters from.
/// The allocator to use.
public NativeText(in FixedString<#=OTHERBYTES#>Bytes source, AllocatorManager.AllocatorHandle allocator)
: this(source.utf8LengthInBytes, allocator)
{
Length = source.utf8LengthInBytes;
unsafe {
byte* sbytes = (byte*) UnsafeUtilityExtensions.AddressOf(source.bytes);
byte* dbytes = (byte*) m_Data->GetUnsafePtr();
UnsafeUtility.MemCpy(dbytes, sbytes, source.utf8LengthInBytes);
}
}
///
/// Initializes and returns an instance of NativeText with the characters copied from another string.
///
/// A string to copy characters from.
/// The allocator to use.
public NativeText(in FixedString<#=OTHERBYTES#>Bytes source, Allocator allocator)
: this(source, (AllocatorManager.AllocatorHandle)allocator)
{
}
<#
}
#>
///
/// 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
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get
{
CheckNull(m_Data);
CheckRead();
return m_Data->Length;
}
set
{
CheckNull(m_Data);
CheckWriteAndBumpSecondaryVersion();
m_Data->Length = value;
}
}
///
/// The current capacity in bytes 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 in bytes of the string.
public int Capacity
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get
{
CheckNull(m_Data);
CheckRead();
return m_Data->Capacity;
}
set
{
CheckNull(m_Data);
CheckWriteAndBumpSecondaryVersion();
m_Data->Capacity = value;
}
}
///
/// 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)
{
CheckWrite();
// 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 or the string has not been constructed.
/// Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined and a write is attempted.
public readonly bool IsEmpty
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
if (!IsCreated)
{
return true;
}
CheckRead();
return m_Data->IsEmpty;
}
}
///
/// 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 readonly bool IsCreated
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => m_Data != null;
}
///
/// 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()
{
CheckNull(m_Data);
CheckRead();
return m_Data->GetUnsafePtr();
}
///
/// The byte at an index. Note that writing to a NativeText.Readonly is not supported; the setter of this property throws when safety checks are enabled.
///
/// A zero-based byte index.
/// The byte at the index.
/// Thrown if the index is out of bounds.
public byte this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get
{
CheckNull(m_Data);
CheckRead();
return m_Data->ElementAt(index);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
CheckNull(m_Data);
CheckWrite();
m_Data->ElementAt(index) = value;
}
}
///
/// Returns a 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.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ref byte ElementAt(int index)
{
CheckNull(m_Data);
CheckWrite();
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.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public void Add(in byte value)
{
CheckWrite();
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(NativeText other)
{
CheckRead();
return FixedStringMethods.CompareTo(ref this, *other.m_Data);
}
///
/// 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(NativeText other)
{
CheckRead();
return FixedStringMethods.Equals(ref this, *other.m_Data);
}
///
/// 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(NativeText.ReadOnly other)
{
CheckRead();
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(NativeText.ReadOnly other)
{
CheckRead();
return FixedStringMethods.Equals(ref this, *other.m_Data);
}
///
/// Releases all resources (memory and safety handles).
///
public void Dispose()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
if (!AtomicSafetyHandle.IsDefaultValue(m_Safety))
{
AtomicSafetyHandle.CheckExistsAndThrow(m_Safety);
}
#endif
if (!IsCreated)
{
return;
}
#if ENABLE_UNITY_COLLECTIONS_CHECKS
CollectionHelper.DisposeSafetyHandle(ref m_Safety);
#endif
UnsafeText.Free(m_Data);
m_Data = null;
}
///
/// Creates and schedules a job that releases all resources (memory and safety handles) of this NativeText.
///
/// The dependency for the new job.
/// The handle of the new job. The job depends upon `inputDeps` and releases all resources (memory and safety handles) of this NativeText.
public JobHandle Dispose(JobHandle inputDeps)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
if (!AtomicSafetyHandle.IsDefaultValue(m_Safety))
{
AtomicSafetyHandle.CheckExistsAndThrow(m_Safety);
}
#endif
if (!IsCreated)
{
return inputDeps;
}
#if ENABLE_UNITY_COLLECTIONS_CHECKS
var jobHandle = new NativeTextDisposeJob { Data = new NativeTextDispose { m_TextData = m_Data, m_Safety = m_Safety } }.Schedule(inputDeps);
AtomicSafetyHandle.Release(m_Safety);
#else
var jobHandle = new NativeTextDisposeJob { Data = new NativeTextDispose { m_TextData = m_Data } }.Schedule(inputDeps);
#endif
m_Data = null;
return jobHandle;
}
///
/// 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)]
[ExcludeFromBurstCompatTesting("Returns managed string")]
public string Value => ToString();
///
/// An enumerator over the characters (not bytes) of a NativeText.
///
///
/// 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
{
NativeText.ReadOnly target;
int offset;
Unicode.Rune current;
///
/// Initializes and returns an instance of NativeText.Enumerator.
///
/// A NativeText for which to create an enumerator.
public Enumerator(NativeText source)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.CheckGetSecondaryDataPointerAndThrow(source.m_Safety);
var ash = source.m_Safety;
AtomicSafetyHandle.UseSecondaryVersion(ref ash);
target = new ReadOnly(source.m_Data, ash);
#else
target = source.AsReadOnly();
#endif
offset = 0;
current = default;
}
///
/// Initializes and returns an instance of NativeText.Enumerator.
///
/// A NativeText.ReadOnly for which to create an enumerator.
public Enumerator(NativeText.ReadOnly source)
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.CheckGetSecondaryDataPointerAndThrow(source.m_Safety);
var ash = source.m_Safety;
AtomicSafetyHandle.UseSecondaryVersion(ref ash);
target = new ReadOnly(source.m_Data, ash);
#else
target = source;
#endif
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.
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get => Current;
}
///
/// The current character.
///
/// The current character.
public Unicode.Rune Current => current;
}
///
/// Returns an enumerator for iterating over the characters of the NativeText.
///
/// An enumerator for iterating over the characters of the NativeText.
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.
///
[ExcludeFromBurstCompatTesting("Takes managed string")]
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.
[ExcludeFromBurstCompatTesting("Takes managed string")]
public bool Equals(String other)
{
return ToString().Equals(other);
}
<#
//
// 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);
}
///
/// 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 NativeText 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 NativeText 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.
[ExcludeFromBurstCompatTesting("Returns managed string")]
public override String ToString()
{
if (m_Data == null)
return "";
CheckRead();
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, NativeText, 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.
[ExcludeFromBurstCompatTesting("Takes managed object")]
public override bool Equals(object other)
{
if(ReferenceEquals(null, other)) return false;
if(other is String aString) return Equals(aString);
if(other is NativeText aNativeText) return Equals(aNativeText);
if(other is ReadOnly aReadOnly) return Equals(aReadOnly);
<#
foreach(var OTHERBYTES in SIZES)
{
#>
if(other is FixedString<#=OTHERBYTES#>Bytes a<#=OTHERBYTES#>) return Equals(a<#=OTHERBYTES#>);
<#
}
#>
return false;
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void CheckNull(void* dataPtr)
{
if (dataPtr == null)
{
throw new InvalidOperationException("NativeText has yet to be created or has been destroyed!");
}
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly void CheckRead()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void CheckWrite()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.CheckWriteAndThrow(m_Safety);
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly void CheckWriteAndBumpSecondaryVersion()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
AtomicSafetyHandle.CheckWriteAndBumpSecondaryVersion(m_Safety);
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
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 NativeText of {Length} length.");
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
void ThrowCopyError(CopyError error, String source)
{
throw new ArgumentException($"NativeText: {error} while copying \"{source}\"");
}
///
/// A read-only alias for the value of a NativeText. Does not have its own allocated storage.
///
[NativeContainer]
[NativeContainerIsReadOnly]
public unsafe struct ReadOnly
: INativeList
, IUTF8Bytes
, IComparable
, IEquatable
, IComparable
, IEquatable
<#
foreach (var OTHERBYTES in SIZES)
{
#>
, IComparableBytes>
, IEquatableBytes>
<#
}
#>
{
[NativeDisableUnsafePtrRestriction]
internal UnsafeText* m_Data;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
internal AtomicSafetyHandle m_Safety;
internal static readonly SharedStatic s_staticSafetyId = SharedStatic.GetOrCreate();
internal ReadOnly(UnsafeText* text, AtomicSafetyHandle safety)
{
m_Data = text;
m_Safety = safety;
CollectionHelper.SetStaticSafetyId(ref m_Safety, ref s_staticSafetyId.Data, "Unity.Collections.NativeText.ReadOnly");
}
#else
internal ReadOnly(UnsafeText* text)
{
m_Data = text;
}
#endif
///
/// The current capacity in bytes 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 in bytes of the string.
/// Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined and a write is attempted.
public int Capacity
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get
{
CheckNull(m_Data);
CheckRead();
return m_Data->Capacity;
}
set
{
ErrorWrite();
}
}
///
/// Whether this string has no characters.
///
/// True if this string has no characters or if the string has not been constructed.
public bool IsEmpty
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get
{
if (m_Data == null)
{
return true;
}
CheckRead();
return m_Data->IsEmpty;
}
set
{
ErrorWrite();
}
}
///
/// 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.
/// Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined and a write is attempted.
public int Length
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get
{
CheckNull(m_Data);
CheckRead();
return m_Data->Length;
}
set
{
ErrorWrite();
}
}
///
/// The byte at an index.
///
/// A zero-based byte index.
/// The byte at the index.
/// Thrown if the index is out of bounds.
/// Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined and a write is attempted.
public byte this[int index]
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly get
{
CheckNull(m_Data);
CheckRead();
return m_Data->ElementAt(index);
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
set
{
ErrorWrite();
}
}
///
/// Sets the length to 0. For a NativeText.Readonly this function does nothing, unless safety checks are enabled (in which case it throws).
///
/// Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined.
public void Clear()
{
ErrorWrite();
}
///
/// Returns a reference to the byte (not character) at an index. Unsupported by NativeText.ReadOnly.
///
///
/// This function is a no-op when ENABLE_UNITY_COLLECTIONS_CHECKS is not defined, throws otherwise.
///
/// A byte index.
/// A reference to the byte at the index.
/// Thrown when called. This operation is not supported.
public ref byte ElementAt(int index)
{
throw new NotSupportedException("Trying to retrieve non-readonly ref to NativeText.ReadOnly data. This is not permitted.");
}
///
/// 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 byte* GetUnsafePtr()
{
CheckNull(m_Data);
CheckRead();
return m_Data->GetUnsafePtr();
}
///
/// Attempt to set the length in bytes of this string. For NativeText.ReadOnly this function is a no-op and always returns false.
///
/// The new length in bytes of the string.
/// Whether any bytes added should be zeroed out.
/// Always false.
/// Thrown if ENABLE_UNITY_COLLECTIONS_CHECKS is defined.
public bool TryResize(int newLength, NativeArrayOptions clearOptions = NativeArrayOptions.ClearMemory)
{
ErrorWrite();
return false;
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static void CheckNull(void* dataPtr)
{
if (dataPtr == null)
{
throw new InvalidOperationException("NativeText.ReadOnly has yet to be created or has been destroyed!");
}
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS")]
[MethodImpl(MethodImplOptions.AggressiveInlining)]
readonly void CheckRead()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
// Ensure we are allowed to read
AtomicSafetyHandle.CheckReadAndThrow(m_Safety);
#endif
}
[Conditional("ENABLE_UNITY_COLLECTIONS_CHECKS"), Conditional("UNITY_DOTS_DEBUG")]
void ErrorWrite()
{
throw new NotSupportedException("Trying to write to a NativeText.ReadOnly. Write operations are not permitted and are ignored.");
}
///
/// 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.
///
[ExcludeFromBurstCompatTesting("Takes managed string")]
public int CompareTo(String other)
{
CheckNull(m_Data);
CheckRead();
return m_Data->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.
[ExcludeFromBurstCompatTesting("Takes managed string")]
public bool Equals(String other)
{
CheckNull(m_Data);
CheckRead();
return m_Data->ToString().Equals(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(ReadOnly other)
{
CheckNull(m_Data);
CheckRead();
return FixedStringMethods.CompareTo(ref *m_Data, *other.m_Data);
}
///
/// 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(ReadOnly other)
{
CheckNull(m_Data);
CheckRead();
return FixedStringMethods.Equals(ref *m_Data, *other.m_Data);
}
///
/// 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(NativeText other)
{
CheckNull(m_Data);
CheckRead();
return FixedStringMethods.CompareTo(ref this, *other.m_Data);
}
///
/// 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(NativeText other)
{
CheckNull(m_Data);
CheckRead();
return FixedStringMethods.Equals(ref this, *other.m_Data);
}
<#
//
// 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);
}
///
/// 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 ReadOnly a, in FixedString<#=OTHERBYTES#>Bytes b)
{
CheckNull(a.m_Data);
a.CheckRead();
unsafe {
var aref = *a.m_Data;
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 ReadOnly 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.
[ExcludeFromBurstCompatTesting("Returns managed string")]
public override String ToString()
{
if (m_Data == null)
return "";
CheckRead();
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()
{
CheckRead();
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, NativeText, 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.
[ExcludeFromBurstCompatTesting("Takes managed object")]
public override bool Equals(object other)
{
if(ReferenceEquals(null, other)) return false;
if(other is String aString) return Equals(aString);
if(other is NativeText aNativeText) return Equals(aNativeText);
if(other is ReadOnly aReadOnly) return Equals(aReadOnly);
<#
foreach(var OTHERBYTES in SIZES)
{
#>
if(other is FixedString<#=OTHERBYTES#>Bytes a<#=OTHERBYTES#>) return Equals(a<#=OTHERBYTES#>);
<#
}
#>
return false;
}
///
/// 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)]
[ExcludeFromBurstCompatTesting("Returns managed string")]
public string Value => ToString();
///
/// Returns an enumerator for iterating over the characters of the NativeText.
///
/// An enumerator for iterating over the characters of the NativeText.
public Enumerator GetEnumerator()
{
return new Enumerator(this);
}
}
///
/// Returns a readonly version of this NativeText instance.
///
/// ReadOnly containers point to the same underlying data as the NativeText it is made from. Note while ReadOnly contains methods that would write to the string data these methods will perform no writes and/or throw a NotSupportedException.
/// ReadOnly instance for this.
public ReadOnly AsReadOnly()
{
#if ENABLE_UNITY_COLLECTIONS_CHECKS
var ash = m_Safety;
return new ReadOnly(m_Data, ash);
#else
return new ReadOnly(m_Data);
#endif
}
}
[NativeContainer]
[GenerateTestsForBurstCompatibility]
internal unsafe struct NativeTextDispose
{
[NativeDisableUnsafePtrRestriction]
public UnsafeText* m_TextData;
#if ENABLE_UNITY_COLLECTIONS_CHECKS
public AtomicSafetyHandle m_Safety;
#endif
public void Dispose()
{
UnsafeText.Free(m_TextData);
}
}
[BurstCompile]
internal unsafe struct NativeTextDisposeJob : IJob
{
public NativeTextDispose Data;
public void Execute()
{
Data.Dispose();
}
}
}
<#}#>