Utf8Utility Class

Provides useful methods to work with UTF-8 encoding.

public static class Utf8Utility

Methods

NameDescription
ComputeMaxCharsInBytes(ReadOnlySpan<char>, int)Computes the maximum number of characters from a given span that will fit in a given number of bytes when encoded using UTF-8.
ComputeMaxCharsInBytes(string, int)Computes the maximum number of characters from a given string that will fit in a given number of bytes when encoded using UTF-8.

ComputeMaxCharsInBytes overloads

ComputeMaxCharsInBytes(ReadOnlySpan<char>, int)

Computes the maximum number of characters from a given span that will fit in a given number of bytes when encoded using UTF-8.

public static int ComputeMaxCharsInBytes(ReadOnlySpan<char> chars, int maxBytes)
Parameters
chars ReadOnlySpan<char>
The characters to encode.
maxBytes int
The maximum number of bytes available for the encoded text.
Returns

int The maximum number of characters, starting from the beginning of chars, whose UTF-8 representation will fit in a maximum of maxBytes bytes.

Exceptions
ArgumentOutOfRangeException
maxBytes is a negative number.
Remarks

This method does not use any functionality from the System.Text namespace; its computations are based solely on the UTF-8 standard.

This method assumes that any unrepresentable character will be encoded as the Unicode replacement character (code point 0xFFFD). This is the default for both Encoding.UTF8 and Utf8Utility.Utf8NoBomEncoding.

This method does not take into account the UTF-8 byte order mark, which would add 3 additional bytes at the start of the encoded string. If you plan to write a byte order mark, you should subtract 3 from the number of available bytes when calling this method.

ComputeMaxCharsInBytes(string, int)

Computes the maximum number of characters from a given string that will fit in a given number of bytes when encoded using UTF-8.

public static int ComputeMaxCharsInBytes(string str, int maxBytes)
Parameters
str string
The string to encode.
maxBytes int
The maximum number of bytes available for the encoded text.
Returns

int The maximum number of characters, starting from the beginning of str, whose UTF-8 representation will fit in a maximum of maxBytes bytes.

Exceptions
ArgumentNullException
str is null.
ArgumentOutOfRangeException
maxBytes is a negative number.
Remarks

This method does not use any functionality from the System.Text namespace; its computations are based solely on the UTF-8 standard.

This method assumes that any unrepresentable character will be encoded as the Unicode replacement character (code point 0xFFFD). This is the default for both Encoding.UTF8 and Utf8Utility.Utf8NoBomEncoding.

This method does not take into account the UTF-8 byte order mark, which would add 3 additional bytes at the start of the encoded string. If you plan to write a byte order mark, you should subtract 3 from the number of available bytes when calling this method.

Fields

NameDescription
Utf8NoBomEncodingAn Encoding that can be used to encode UTF-8 text without a byte order mark.

Utf8NoBomEncoding

An Encoding that can be used to encode UTF-8 text without a byte order mark.

public static readonly Encoding Utf8NoBomEncoding