Rune.DecodeFromUtf8(ReadOnlySpan<Byte>, Rune, Int32) Method

Definition

在提供的 UTF-8 源缓冲区开始处解码 RuneDecodes the Rune at the beginning of the provided UTF-8 source buffer.

public:
 static System::Buffers::OperationStatus DecodeFromUtf8(ReadOnlySpan<System::Byte> source, [Runtime::InteropServices::Out] System::Text::Rune % result, [Runtime::InteropServices::Out] int % bytesConsumed);
public static System.Buffers.OperationStatus DecodeFromUtf8 (ReadOnlySpan<byte> source, out System.Text.Rune result, out int bytesConsumed);
static member DecodeFromUtf8 : ReadOnlySpan<byte> * Rune * int -> System.Buffers.OperationStatus
Public Shared Function DecodeFromUtf8 (source As ReadOnlySpan(Of Byte), ByRef result As Rune, ByRef bytesConsumed As Integer) As OperationStatus

Parameters

source
ReadOnlySpan<Byte>

只读 UTF-8 编码字节范围。A read-only UTF-8 encoded byte span.

result
Rune

此方法返回时,已解码的 rune。When this method returns, the decoded rune.

bytesConsumed
Int32

此方法返回时,为创建 rune 而读取的字节数。When this method returns, the number of bytes read to create the rune.

Returns

OperationStatus

如果源缓冲区以有效的 UTF-8 编码标量值开头,则 DoneDone if the source buffer begins with a valid UTF-8 encoded scalar value. 然后 result 包含解码的 RunebytesConsumed 包含用于在输入缓冲区中解码 RuneByte 值的数目。result then contains the decoded Rune, and bytesConsumed contains the number of Byte values used in the input buffer to encode the Rune.

如果源缓冲区为空,或仅包含独立的 UTF-8 高代理项字符,则 NeedMoreDataNeedMoreData if the source buffer is empty or contains only a standalone UTF-8 high surrogate character. 然后 result 包含 ReplacementCharbytesConsumed 包含输入缓冲区的长度。result then contains ReplacementChar, and bytesConsumed contains the length of the input buffer.

如果源缓冲区以格式错误的 UTF-8 编码标量值开头,则 InvalidDataInvalidData if the source buffer begins with an ill-formed UTF-8 encoded scalar value. 然后 result 包含 ReplacementCharbytesConsumed 包含用于在输入缓冲区解码格式错误的序列的 Byte 值的数目。result then contains ReplacementChar, and bytesConsumed contains the number of Byte values used in the input buffer to encode the ill-formed sequence. .

Remarks

一般约定是在循环中调用此方法,通过 bytesConsumed 循环的每次迭代上的元素来切分 source 缓冲区。The general convention is to call this method in a loop, slicing the source buffer by bytesConsumed elements on each iteration of the loop. 在循环的每次迭代中,如果成功进行解码,则 result 包含实标量值; 如果数据无法成功解码,则包含 Rune.ReplacementCharOn each iteration of the loop, result contains the real scalar value if successfully decoded, or it contains Rune.ReplacementChar if the data could not be successfully decoded. 在循环访问循环时,此模式提供了方便的自动 U + FFFD 替换无效序列。This pattern provides convenient automatic U+FFFD substitution of invalid sequences while iterating through the loop.

Applies to