Utf8Utility Class
Provides useful methods to work with UTF-8 encoding.
public static class Utf8UtilityMethods
| Name | Description |
|---|---|
| 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
charsReadOnlySpan<char>- The characters to encode.
maxBytesint- 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
ArgumentOutOfRangeExceptionmaxBytesis 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.
Parameters
strstring- The string to encode.
maxBytesint- 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
ArgumentNullExceptionstrisnull.ArgumentOutOfRangeExceptionmaxBytesis 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
| Name | Description |
|---|---|
| Utf8NoBomEncoding | An Encoding that can be used to encode UTF-8 text without a byte order mark. |