Struct UnsafeText
An unmanaged, mutable, resizable UTF-8 string.
Namespace: Unity.Collections.LowLevel.Unsafe
Assembly: Unity.Collections.dll
Syntax
public struct UnsafeText : INativeDisposable, IUTF8Bytes, INativeList<byte>, IIndexable<byte>
Remarks
The string is always null-terminated, meaning a zero byte always immediately follows the last character.
Constructors
UnsafeText(int, AllocatorHandle)
Initializes and returns an instance of UnsafeText.
Declaration
public UnsafeText(int capacity, AllocatorManager.AllocatorHandle allocator)
Parameters
Type | Name | Description |
---|---|---|
int | capacity | The initial capacity, in bytes. |
AllocatorManager.AllocatorHandle | allocator | The allocator to use. |
Properties
Capacity
The current capacity in bytes of this string.
Declaration
public int Capacity { get; set; }
Property Value
Type | Description |
---|---|
int | The current capacity in bytes of the string. |
Remarks
The null-terminator byte is not included in the capacity, so the string's character buffer is Capacity + 1
in size.
IsCreated
Whether this string's character buffer has been allocated (and not yet deallocated).
Declaration
public bool IsCreated { get; }
Property Value
Type | Description |
---|---|
bool | Whether this string's character buffer has been allocated (and not yet deallocated). |
IsEmpty
Reports whether container is empty.
Declaration
public bool IsEmpty { get; }
Property Value
Type | Description |
---|---|
bool | True if the string is empty or the string has not been constructed. |
this[int]
The byte at an index.
Declaration
public byte this[int index] { get; set; }
Parameters
Type | Name | Description |
---|---|---|
int | index | A zero-based byte index. |
Property Value
Type | Description |
---|---|
byte | The byte at the index. |
Exceptions
Type | Condition |
---|---|
IndexOutOfRangeException | Thrown if the index is out of bounds. |
Length
The current length in bytes of this string.
Declaration
public int Length { get; set; }
Property Value
Type | Description |
---|---|
int | The current length in bytes of the UTF-8 encoded string. |
Remarks
The length does not include the null terminator byte.
Methods
Clear()
Sets the length to 0.
Declaration
public void Clear()
Dispose()
Releases all resources (memory).
Declaration
public void Dispose()
Dispose(JobHandle)
Creates and schedules a job that will dispose this string.
Declaration
public JobHandle Dispose(JobHandle inputDeps)
Parameters
Type | Name | Description |
---|---|---|
JobHandle | inputDeps | The handle of a job which the new job will depend upon. |
Returns
Type | Description |
---|---|
JobHandle | The handle of a new job that will dispose this string. The new job depends upon inputDeps. |
ElementAt(int)
Returns a reference to the byte (not character) at an index.
Declaration
public ref byte ElementAt(int index)
Parameters
Type | Name | Description |
---|---|---|
int | index | A byte index. |
Returns
Type | Description |
---|---|
byte | A reference to the byte at the index. |
Remarks
Deallocating or reallocating this string's character buffer makes the reference invalid.
Exceptions
Type | Condition |
---|---|
IndexOutOfRangeException | Thrown if the index is out of bounds. |
GetUnsafePtr()
Returns a pointer to this string's character buffer.
Declaration
public byte* GetUnsafePtr()
Returns
Type | Description |
---|---|
byte* | A pointer to this string's character buffer. |
Remarks
The pointer is made invalid by operations that reallocate the character buffer, such as setting Capacity
.
ToString()
Returns a managed string copy of this string.
Declaration
public override string ToString()
Returns
Type | Description |
---|---|
string | A managed string copy of this string. |
Overrides
TryResize(int, NativeArrayOptions)
Attempt to set the length in bytes of this string.
Declaration
public bool TryResize(int newLength, NativeArrayOptions clearOptions = NativeArrayOptions.ClearMemory)
Parameters
Type | Name | Description |
---|---|---|
int | newLength | The new length in bytes of the string. |
NativeArrayOptions | clearOptions | Whether any bytes added should be zeroed out. |
Returns
Type | Description |
---|---|
bool | Always true. |