Table of Contents

Class Utf8String

Namespace
AsmResolver
Assembly
AsmResolver.dll

Represents an immutable UTF-8 encoded string. This class supports preserving invalid UTF-8 code sequences.

public sealed class Utf8String : IEquatable<Utf8String>, IEquatable<string>, IEquatable<byte[]>, IComparable<Utf8String>, IEnumerable<char>, IEnumerable
Inheritance
Utf8String
Implements
Inherited Members

Constructors

Utf8String(byte[])

Creates a new UTF-8 string from the provided raw data.

public Utf8String(byte[] data)

Parameters

data byte[]

The raw UTF-8 data.

Utf8String(byte[], int, int)

Creates a new UTF-8 string from the provided raw data.

public Utf8String(byte[] data, int index, int count)

Parameters

data byte[]

The raw UTF-8 data.

index int

The starting index to read from.

count int

The number of bytes to read..

Utf8String(ReadOnlySpan<byte>)

Creates a new UTF-8 string from the provided raw data.

public Utf8String(ReadOnlySpan<byte> data)

Parameters

data ReadOnlySpan<byte>

The raw UTF-8 data.

Utf8String(ReadOnlySpan<char>)

Creates a new UTF-8 string from the provided ReadOnlySpan<T>.

public Utf8String(ReadOnlySpan<char> value)

Parameters

value ReadOnlySpan<char>

The string value to encode as UTF-8.

Utf8String(string)

Creates a new UTF-8 string from the provided string.

public Utf8String(string value)

Parameters

value string

The string value to encode as UTF-8.

Fields

Empty

Represents the empty UTF-8 string.

public static readonly Utf8String Empty

Field Value

Utf8String

Properties

ByteCount

Gets the number of bytes used by the string.

public int ByteCount { get; }

Property Value

int

this[int]

Gets a single character in the string.

public char this[int index] { get; }

Parameters

index int

The character index.

Property Value

char

Length

Gets the number of characters in the string.

public int Length { get; }

Property Value

int

Value

Gets the string value represented by the UTF-8 bytes.

public string Value { get; }

Property Value

string

Methods

AsSpan()

Creates a new read-only span over the string.

public ReadOnlySpan<byte> AsSpan()

Returns

ReadOnlySpan<byte>

The read-only span representation of the string.

AsSpan(int)

Creates a new read-only span over the string.

public ReadOnlySpan<byte> AsSpan(int start)

Parameters

start int

The index at which to begin this slice.

Returns

ReadOnlySpan<byte>

The read-only span representation of the string.

AsSpan(int, int)

Creates a new read-only span over the string.

public ReadOnlySpan<byte> AsSpan(int start, int length)

Parameters

start int

The index at which to begin this slice.

length int

The desired length for the slice.

Returns

ReadOnlySpan<byte>

The read-only span representation of the string.

CompareTo(Utf8String?)

Compares the current instance with another object of the same type and returns an integer that indicates whether the current instance precedes, follows, or occurs in the same position in the sort order as the other object.

public int CompareTo(Utf8String? other)

Parameters

other Utf8String

An object to compare with this instance.

Returns

int

A value that indicates the relative order of the objects being compared. The return value has these meanings:

Value Meaning
Less than zero This instance precedes other in the sort order.
Zero This instance occurs in the same position in the sort order as other.
Greater than zero This instance follows other in the sort order.

Concat(Utf8String?)

Produces a new string that is the concatenation of the current string and the provided string.

public Utf8String Concat(Utf8String? other)

Parameters

other Utf8String

The other string to append..

Returns

Utf8String

The new string.

Concat(byte[]?)

Produces a new string that is the concatenation of the current string and the provided byte array.

public Utf8String Concat(byte[]? other)

Parameters

other byte[]

The byte array to append.

Returns

Utf8String

The new string.

Concat(string?)

Produces a new string that is the concatenation of the current string and the provided string.

public Utf8String Concat(string? other)

Parameters

other string

The other string to append..

Returns

Utf8String

The new string.

Contains(string)

Determines whether the string contains the provided string.

public bool Contains(string needle)

Parameters

needle string

The string to search.

Returns

bool

true if the string is present, false otherwise.

Equals(Utf8String?)

Determines whether two strings are considered equal.

public bool Equals(Utf8String? other)

Parameters

other Utf8String

The other string.

Returns

bool

true if the strings are considered equal, false otherwise.

Remarks

This operation performs a byte-level comparison of the two strings.

Equals(byte[]?)

Indicates whether the current object is equal to another object of the same type.

public bool Equals(byte[]? other)

Parameters

other byte[]

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

Remarks

This operation performs a byte-level comparison of the two strings.

Equals(object?)

Determines whether the specified object is equal to the current object.

public override bool Equals(object? obj)

Parameters

obj object

The object to compare with the current object.

Returns

bool

true if the specified object is equal to the current object; otherwise, false.

Remarks

This operation performs a byte-level comparison of the two strings.

Equals(string?)

Indicates whether the current object is equal to another object of the same type.

public bool Equals(string? other)

Parameters

other string

An object to compare with this object.

Returns

bool

true if the current object is equal to the other parameter; otherwise, false.

Remarks

This operation performs a byte-level comparison of the two strings.

GetBytes()

Gets the raw UTF-8 bytes of the string.

public byte[] GetBytes()

Returns

byte[]

The bytes.

GetBytes(byte[], int, int)

Obtains the raw UTF-8 bytes of the string, and writes it into the provided buffer.

public int GetBytes(byte[] buffer, int index, int length)

Parameters

buffer byte[]

The output buffer to receive the bytes in.

index int

The index into the output buffer to start writing at.

length int

The number of bytes to write.

Returns

int

The actual number of bytes that were written.

GetBytesUnsafe()

Gets the underlying byte array of this string.

public byte[] GetBytesUnsafe()

Returns

byte[]

The bytes.

Remarks

This method should only be used if performance is critical. Modifying the returning array will change the internal state of the string.

GetEnumerator()

Returns an enumerator that iterates through the collection.

public IEnumerator<char> GetEnumerator()

Returns

IEnumerator<char>

An enumerator that can be used to iterate through the collection.

GetHashCode()

Serves as the default hash function.

public override int GetHashCode()

Returns

int

A hash code for the current object.

IndexOf(char)

Gets the zero-based index of the first occurrence of the provided character in the string.

public int IndexOf(char needle)

Parameters

needle char

The character to search.

Returns

int

The index, or -1 if the character is not present.

IndexOf(char, int)

Gets the zero-based index of the first occurrence of the provided character in the string.

public int IndexOf(char needle, int startIndex)

Parameters

needle char

The character to search.

startIndex int

The index to start searching at.

Returns

int

The index, or -1 if the character is not present.

IndexOf(string)

Gets the zero-based index of the first occurrence of the provided string in the string.

public int IndexOf(string needle)

Parameters

needle string

The string to search.

Returns

int

The index, or -1 if the string is not present.

IndexOf(string, int)

Gets the zero-based index of the first occurrence of the provided string in the string.

public int IndexOf(string needle, int startIndex)

Parameters

needle string

The string to search.

startIndex int

The index to start searching at.

Returns

int

The index, or -1 if the string is not present.

IndexOf(string, int, StringComparison)

Gets the zero-based index of the first occurrence of the provided string in the string.

public int IndexOf(string needle, int startIndex, StringComparison comparison)

Parameters

needle string

The string to search.

startIndex int

The index to start searching at.

comparison StringComparison

The comparison algorithm to use.

Returns

int

The index, or -1 if the string is not present.

IndexOf(string, StringComparison)

Gets the zero-based index of the first occurrence of the provided string in the string.

public int IndexOf(string needle, StringComparison comparison)

Parameters

needle string

The string to search.

comparison StringComparison

The comparison algorithm to use.

Returns

int

The index, or -1 if the string is not present.

IsNullOrEmpty(Utf8String?)

Determines whether the provided string is null or the empty string.

public static bool IsNullOrEmpty(Utf8String? value)

Parameters

value Utf8String

The string to verify.

Returns

bool

true if the string is null or empty, false otherwise.

LastIndexOf(char)

Gets the zero-based index of the last occurrence of the provided character in the string.

public int LastIndexOf(char needle)

Parameters

needle char

The character to search.

Returns

int

The index, or -1 if the character is not present.

LastIndexOf(char, int)

Gets the zero-based index of the last occurrence of the provided character in the string.

public int LastIndexOf(char needle, int startIndex)

Parameters

needle char

The character to search.

startIndex int

The index to start searching at.

Returns

int

The index, or -1 if the character is not present.

LastIndexOf(string)

Gets the zero-based index of the last occurrence of the provided string in the string.

public int LastIndexOf(string needle)

Parameters

needle string

The string to search.

Returns

int

The index, or -1 if the string is not present.

LastIndexOf(string, int)

Gets the zero-based index of the first occurrence of the provided string in the string.

public int LastIndexOf(string needle, int startIndex)

Parameters

needle string

The string to search.

startIndex int

The index to start searching at.

Returns

int

The index, or -1 if the string is not present.

LastIndexOf(string, int, StringComparison)

Gets the zero-based index of the last occurrence of the provided string in the string.

public int LastIndexOf(string needle, int startIndex, StringComparison comparison)

Parameters

needle string

The string to search.

startIndex int

The index to start searching at.

comparison StringComparison

The comparison algorithm to use.

Returns

int

The index, or -1 if the string is not present.

LastIndexOf(string, StringComparison)

Gets the zero-based index of the last occurrence of the provided string in the string.

public int LastIndexOf(string needle, StringComparison comparison)

Parameters

needle string

The string to search.

comparison StringComparison

The comparison algorithm to use.

Returns

int

The index, or -1 if the string is not present.

ToString()

Returns a string that represents the current object.

public override string ToString()

Returns

string

A string that represents the current object.

Operators

operator +(Utf8String?, Utf8String?)

Concatenates two UTF-8 encoded strings together.

public static Utf8String operator +(Utf8String? a, Utf8String? b)

Parameters

a Utf8String

The first string.

b Utf8String

The second string.

Returns

Utf8String

The newly produced string.

operator +(Utf8String?, byte[]?)

Concatenates an UTF-8 encoded string together with a byte array.

public static Utf8String operator +(Utf8String? a, byte[]? b)

Parameters

a Utf8String

The string.

b byte[]

The byte array.

Returns

Utf8String

The newly produced string.

operator +(Utf8String?, string?)

Concatenates two UTF-8 encoded strings together.

public static Utf8String operator +(Utf8String? a, string? b)

Parameters

a Utf8String

The first string.

b string

The second string.

Returns

Utf8String

The newly produced string.

operator ==(Utf8String?, Utf8String?)

Determines whether two UTF-8 encoded strings are considered equal.

public static bool operator ==(Utf8String? a, Utf8String? b)

Parameters

a Utf8String

The first string.

b Utf8String

The second string.

Returns

bool

true if the strings are considered equal, false otherwise.

Remarks

This operation performs a byte-level comparison of the two strings.

operator ==(Utf8String?, byte[]?)

Determines whether the underlying bytes of an UTF-8 encoded string is equal to the provided byte array.

public static bool operator ==(Utf8String? a, byte[]? b)

Parameters

a Utf8String

The UTF-8 string.

b byte[]

The byte array.

Returns

bool

true if the byte arrays are considered equal, false otherwise.

Remarks

This operation performs a byte-level comparison.

operator ==(Utf8String?, string?)

Determines whether an UTF-8 encoded string is considered equal to the provided string.

public static bool operator ==(Utf8String? a, string? b)

Parameters

a Utf8String

The first string.

b string

The second string.

Returns

bool

true if the strings are considered equal, false otherwise.

Remarks

This operation performs a string-level comparison.

implicit operator ReadOnlySpan<byte>(Utf8String?)

Converts a Utf8String into a ReadOnlySpan<T>.

public static implicit operator ReadOnlySpan<byte>(Utf8String? value)

Parameters

value Utf8String

The UTF-8 string value to convert.

Returns

ReadOnlySpan<byte>

The span.

implicit operator ReadOnlySpan<char>(Utf8String?)

Converts a Utf8String into a ReadOnlySpan<T>.

public static implicit operator ReadOnlySpan<char>(Utf8String? value)

Parameters

value Utf8String

The UTF-8 string value to convert.

Returns

ReadOnlySpan<char>

The span.

implicit operator string?(Utf8String?)

Converts a Utf8String into a string.

public static implicit operator string?(Utf8String? value)

Parameters

value Utf8String

The UTF-8 string value to convert.

Returns

string

The string.

implicit operator Utf8String?(byte[]?)

Converts a raw sequence of bytes into an Utf8String.

public static implicit operator Utf8String?(byte[]? data)

Parameters

data byte[]

The raw data to convert.

Returns

Utf8String

The new UTF-8 encoded string.

implicit operator Utf8String(ReadOnlySpan<byte>)

Converts a raw sequence of bytes into an Utf8String.

public static implicit operator Utf8String(ReadOnlySpan<byte> data)

Parameters

data ReadOnlySpan<byte>

The raw data to convert.

Returns

Utf8String

The new UTF-8 encoded string.

implicit operator Utf8String(ReadOnlySpan<char>)

Converts a ReadOnlySpan<T> into an Utf8String.

public static implicit operator Utf8String(ReadOnlySpan<char> data)

Parameters

data ReadOnlySpan<char>

The string value to convert.

Returns

Utf8String

The new UTF-8 encoded string.

implicit operator Utf8String?(string?)

Converts a string into an Utf8String.

public static implicit operator Utf8String?(string? value)

Parameters

value string

The string value to convert.

Returns

Utf8String

The new UTF-8 encoded string.

operator !=(Utf8String?, Utf8String?)

Determines whether two UTF-8 encoded strings are not considered equal.

public static bool operator !=(Utf8String? a, Utf8String? b)

Parameters

a Utf8String

The first string.

b Utf8String

The second string.

Returns

bool

true if the strings are not considered equal, false otherwise.

Remarks

This operation performs a byte-level comparison of the two strings.

operator !=(Utf8String?, byte[]?)

Determines whether the underlying bytes of an UTF-8 encoded string is not equal to the provided byte array.

public static bool operator !=(Utf8String? a, byte[]? b)

Parameters

a Utf8String

The UTF-8 string.

b byte[]

The byte array.

Returns

bool

true if the byte arrays are not considered equal, false otherwise.

Remarks

This operation performs a byte-level comparison.

operator !=(Utf8String?, string?)

Determines whether an UTF-8 encoded string is not equal to the provided string.

public static bool operator !=(Utf8String? a, string? b)

Parameters

a Utf8String

The first string.

b string

The second string.

Returns

bool

true if the strings are not considered equal, false otherwise.

Remarks

This operation performs a string-level comparison.