Skip to content

Utf8String

Namespace Mc.Scripting · Kind struct (readonly, ref)

public readonly ref struct Utf8String

Borrowed UTF-8 string (McStr). Only valid during the call/dispatch that provided it: call Utf8String.ToString to keep a copy.

Event string fields (Message, Username, …) are this type: borrowed UTF-8 memory owned by the engine. Only ToString() and Contains() allocate.

Reading an event string

Events.On((ref PlayerChatEvent e) =>
{
if (e.Message.IsEmpty) return; // no allocation
if (e.Message.EqualsAscii("!help"u8)) { /* ... */ } // byte compare, no allocation
string copy = e.Message.ToString(); // allocates; safe to keep
});
Member Description
Utf8String(byte* data, long size) Wraps native memory (the engine does this; plugins receive these in events).
public Utf8String(byte* data, long size)

Wraps native memory (the engine does this; plugins receive these in events).

Parameters

Name Type Description
data byte*
size long
Member Description
ReadOnlySpan<byte> Bytes { get; } The UTF-8 bytes (borrowed).
bool IsEmpty { get; } Whether the string is empty.
int Length { get; } Length in bytes.
public ReadOnlySpan<byte> Bytes { get; }

The UTF-8 bytes (borrowed).

public bool IsEmpty { get; }

Whether the string is empty.

public int Length { get; }

Length in bytes.

Member Description
bool Contains(string text, StringComparison comparison = StringComparison.Ordinal) Whether the decoded text contains text (allocates).
bool EqualsAscii(ReadOnlySpan<byte> utf8) Byte-wise equality with an ASCII/UTF-8 literal (no allocation).
override string ToString() Copies the text into a managed string.
public bool Contains(string text, StringComparison comparison = StringComparison.Ordinal)

Whether the decoded text contains text (allocates).

Parameters

Name Type Description
text string
comparison StringComparison = StringComparison.Ordinal

Returns bool

public bool EqualsAscii(ReadOnlySpan<byte> utf8)

Byte-wise equality with an ASCII/UTF-8 literal (no allocation).

Parameters

Name Type Description
utf8 ReadOnlySpan<byte>

Returns bool

public override string ToString()

Copies the text into a managed string.

Returns string