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, ISpanFormattable, IFormattable, IUtf8SpanFormattable
- Inheritance
-
Utf8String
- Implements
-
IEquatable<byte[]>
- Inherited Members
Constructors
Utf8String(byte[])
Creates a new UTF-8 string from the provided raw data.
public Utf8String(byte[] data)
Parameters
databyte[]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
databyte[]The raw UTF-8 data.
indexintThe starting index to read from.
countintThe 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
dataReadOnlySpan<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
valueReadOnlySpan<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
valuestringThe string value to encode as UTF-8.
Fields
Empty
Represents the empty UTF-8 string.
public static readonly Utf8String Empty
Field Value
Properties
ByteCount
Gets the number of bytes used by the string.
public int ByteCount { get; }
Property Value
this[int]
Gets a single character in the string.
public char this[int index] { get; }
Parameters
indexintThe character index.
Property Value
Length
Gets the number of characters in the string.
public int Length { get; }
Property Value
Value
Gets the string value represented by the UTF-8 bytes.
public string Value { get; }
Property Value
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
startintThe 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
Returns
- ReadOnlySpan<byte>
The read-only span representation of the string.
CompareTo(Utf8String?)
public int CompareTo(Utf8String? other)
Parameters
otherUtf8String
Returns
Concat(Utf8String?)
Produces a new string that is the concatenation of the current string and the provided string.
public Utf8String Concat(Utf8String? other)
Parameters
otherUtf8StringThe 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
otherbyte[]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
otherstringThe 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
needlestringThe string to search.
Returns
- bool
trueif the string is present,falseotherwise.
CreateUnsafe(byte[])
Creates a Utf8String given an underlying byte array.
public static Utf8String CreateUnsafe(byte[] data)
Parameters
databyte[]The underlying array to use in the Utf8String.
Returns
- Utf8String
The created Utf8String.
Remarks
This method should only be used if performance is critical. Modifying the given array will change the internal state of the string.
Equals(Utf8String?)
Determines whether two strings are considered equal.
public bool Equals(Utf8String? other)
Parameters
otherUtf8StringThe other string.
Returns
- bool
trueif the strings are considered equal,falseotherwise.
Remarks
This operation performs a byte-level comparison of the two strings.
Equals(byte[]?)
public bool Equals(byte[]? other)
Parameters
otherbyte[]
Returns
Remarks
This operation performs a byte-level comparison of the two strings.
Equals(object?)
public override bool Equals(object? obj)
Parameters
objobject
Returns
Remarks
This operation performs a byte-level comparison of the two strings.
Equals(string?)
public bool Equals(string? other)
Parameters
otherstring
Returns
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
bufferbyte[]The output buffer to receive the bytes in.
indexintThe index into the output buffer to start writing at.
lengthintThe 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()
public IEnumerator<char> GetEnumerator()
Returns
GetHashCode()
public override int GetHashCode()
Returns
IndexOf(char)
Gets the zero-based index of the first occurrence of the provided character in the string.
public int IndexOf(char needle)
Parameters
needlecharThe 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
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
needlestringThe 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
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
needlestringThe string to search.
startIndexintThe index to start searching at.
comparisonStringComparisonThe 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
needlestringThe string to search.
comparisonStringComparisonThe 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
valueUtf8StringThe string to verify.
Returns
- bool
trueif the string isnullor empty,falseotherwise.
LastIndexOf(char)
Gets the zero-based index of the last occurrence of the provided character in the string.
public int LastIndexOf(char needle)
Parameters
needlecharThe 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
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
needlestringThe 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
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
needlestringThe string to search.
startIndexintThe index to start searching at.
comparisonStringComparisonThe 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
needlestringThe string to search.
comparisonStringComparisonThe comparison algorithm to use.
Returns
- int
The index, or -1 if the string is not present.
ToString()
public override string ToString()
Returns
ToString(string?, IFormatProvider?)
public string ToString(string? format, IFormatProvider? formatProvider)
Parameters
formatstringformatProviderIFormatProvider
Returns
TryFormat(Span<byte>, out int, ReadOnlySpan<char>, IFormatProvider?)
public bool TryFormat(Span<byte> utf8Destination, out int bytesWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
Parameters
utf8DestinationSpan<byte>bytesWrittenintformatReadOnlySpan<char>providerIFormatProvider
Returns
TryFormat(Span<char>, out int, ReadOnlySpan<char>, IFormatProvider?)
public bool TryFormat(Span<char> destination, out int charsWritten, ReadOnlySpan<char> format, IFormatProvider? provider)
Parameters
destinationSpan<char>charsWrittenintformatReadOnlySpan<char>providerIFormatProvider
Returns
Operators
operator +(Utf8String?, Utf8String?)
Concatenates two UTF-8 encoded strings together.
public static Utf8String operator +(Utf8String? a, Utf8String? b)
Parameters
aUtf8StringThe first string.
bUtf8StringThe 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
aUtf8StringThe string.
bbyte[]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
aUtf8StringThe first string.
bstringThe 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
aUtf8StringThe first string.
bUtf8StringThe second string.
Returns
- bool
trueif the strings are considered equal,falseotherwise.
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
aUtf8StringThe UTF-8 string.
bbyte[]The byte array.
Returns
- bool
trueif the byte arrays are considered equal,falseotherwise.
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
aUtf8StringThe first string.
bstringThe second string.
Returns
- bool
trueif the strings are considered equal,falseotherwise.
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
valueUtf8StringThe 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
valueUtf8StringThe 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
valueUtf8StringThe 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
databyte[]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
dataReadOnlySpan<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
dataReadOnlySpan<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
valuestringThe 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
aUtf8StringThe first string.
bUtf8StringThe second string.
Returns
- bool
trueif the strings are not considered equal,falseotherwise.
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
aUtf8StringThe UTF-8 string.
bbyte[]The byte array.
Returns
- bool
trueif the byte arrays are not considered equal,falseotherwise.
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
aUtf8StringThe first string.
bstringThe second string.
Returns
- bool
trueif the strings are not considered equal,falseotherwise.
Remarks
This operation performs a string-level comparison.