public static string TranscodeHelper(ReadOnlySpan <byte> utf8Unescaped) { try { #if BUILDING_INBOX_LIBRARY return(s_utf8Encoding.GetString(utf8Unescaped)); #else if (utf8Unescaped.IsEmpty) { return(string.Empty); } unsafe { fixed(byte *bytePtr = utf8Unescaped) { return(s_utf8Encoding.GetString(bytePtr, utf8Unescaped.Length)); } } #endif } catch (DecoderFallbackException ex) { // We want to be consistent with the exception being thrown // so the user only has to catch a single exception. // Since we already throw InvalidOperationException for mismatch token type, // and while unescaping, using that exception for failure to decode invalid UTF-8 bytes as well. // Therefore, wrapping the DecoderFallbackException around an InvalidOperationException. throw ThrowHelper.GetInvalidOperationException_ReadInvalidUTF8(ex); } }
public static void ValidateUtf8(ReadOnlySpan <byte> utf8Buffer) { try { #if NETCOREAPP s_utf8Encoding.GetCharCount(utf8Buffer); #else if (utf8Buffer.IsEmpty) { return; } unsafe { fixed(byte *srcPtr = utf8Buffer) { s_utf8Encoding.GetCharCount(srcPtr, utf8Buffer.Length); } } #endif } catch (DecoderFallbackException ex) { // We want to be consistent with the exception being thrown // so the user only has to catch a single exception. // Since we already throw InvalidOperationException for mismatch token type, // and while unescaping, using that exception for failure to decode invalid UTF-8 bytes as well. // Therefore, wrapping the DecoderFallbackException around an InvalidOperationException. throw ThrowHelper.GetInvalidOperationException_ReadInvalidUTF8(ex); } }
public static int TranscodeHelper(ReadOnlySpan <byte> utf8Unescaped, Span <char> destination) { try { #if BUILDING_INBOX_LIBRARY return(s_utf8Encoding.GetChars(utf8Unescaped, destination)); #else if (utf8Unescaped.IsEmpty) { return(0); } unsafe { fixed(byte *srcPtr = utf8Unescaped) fixed(char *destPtr = destination) { return(s_utf8Encoding.GetChars(srcPtr, utf8Unescaped.Length, destPtr, destination.Length)); } } #endif } catch (DecoderFallbackException dfe) { // We want to be consistent with the exception being thrown // so the user only has to catch a single exception. // Since we already throw InvalidOperationException for mismatch token type, // and while unescaping, using that exception for failure to decode invalid UTF-8 bytes as well. // Therefore, wrapping the DecoderFallbackException around an InvalidOperationException. throw ThrowHelper.GetInvalidOperationException_ReadInvalidUTF8(dfe); } catch (ArgumentException) { // Destination buffer was too small; clear it up since the encoder might have not. destination.Clear(); throw; } }