csharp-sdk/src/ModelContextProtocol.Core/Protocol/DebuggerDisplayHelper.cs
Copilot 80b1ad21d7
Use ReadOnlyMemory<byte> for binary data to eliminate UTF-16 transcoding (#1070)
Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com>
Co-authored-by: stephentoub <2642209+stephentoub@users.noreply.github.com>
Co-authored-by: ericstj <8918108+ericstj@users.noreply.github.com>
Co-authored-by: Eric StJohn <ericstj@microsoft.com>
2026-02-19 14:08:05 -08:00

51 lines
1.3 KiB
C#

using System;
namespace ModelContextProtocol.Protocol;
/// <summary>
/// Internal helper methods for DebuggerDisplay implementations.
/// </summary>
internal static class DebuggerDisplayHelper
{
/// <summary>
/// Gets the decoded length of base64 data for debugger display.
/// </summary>
internal static string GetBase64LengthDisplay(string base64Data)
{
#if NET
if (System.Buffers.Text.Base64.IsValid(base64Data, out int decodedLength))
{
return $"{decodedLength} bytes";
}
#else
try
{
return $"{Convert.FromBase64String(base64Data).Length} bytes";
}
catch { }
#endif
return "invalid base64";
}
/// <summary>
/// Gets the decoded length of base64 data encoded as UTF-8 bytes for debugger display.
/// </summary>
internal static string GetBase64LengthDisplay(ReadOnlyMemory<byte> base64Data)
{
#if NET
if (System.Buffers.Text.Base64.IsValid(base64Data.Span, out int decodedLength))
{
return $"{decodedLength} bytes";
}
#else
try
{
return $"{Convert.FromBase64String(System.Text.Encoding.UTF8.GetString(base64Data.ToArray())).Length} bytes";
}
catch { }
#endif
return "invalid base64";
}
}