Repository: microsoft/terminal
File: src/cascadia/inc/cppwinrt_utils.h (C/C++)

Code before:
inline ::winrt::hstring XamlThicknessToOptimalString(const ::winrt::Windows::UI::Xaml::Thickness& t)
{
    if (t.Left == t.Right)
    {
        if (t.Top == t.Bottom)
        {
            if (t.Top == t.Left)
            {
                return ::winrt::hstring{ fmt::format(FMT_COMPILE(L"{}"), t.Left) };
            }
            return ::winrt::hstring{ fmt::format(FMT_COMPILE(L"{},{}"), t.Left, t.Top) };
        }
        // fall through
    }
    return ::winrt::hstring{ fmt::format(FMT_COMPILE(L"{},{},{},{}"), t.Left, t.Top, t.Right, t.Bottom) };
}

inline ::winrt::Windows::UI::Xaml::Thickness StringToXamlThickness(std::wstring_view padding)
try
{
    uintptr_t count{ 0 };
    double t[4]{ 0. }; // left, top, right, bottom
    wchar_t buf[17];
    for (const auto& token : til::split_iterator{ padding, L',' })
    {
        const auto l{ std::min(token.size(), std::extent_v<decltype(buf)> - 1) };
#pragma warning(suppress : 26459) // You called an STL function '...' with a raw pointer parameter at position '...' that may be unsafe ... (stl.1).
        std::copy_n(token.data(), l, &buf[0]); // the length of buf is controlled for above
        til::at(buf, l) = L'\0';
        til::at(t, count++) = std::wcstod(&buf[0], nullptr);
        if (count >= 4)
        {
            break;
        }
    }

#pragma warning(push)
#pragma warning(disable : 26446) // Prefer to use gsl::at() instead of unchecked subscript operator (bounds.4).
    switch (count)
    {
    case 1: // one input = all 4 values are the same
        t[1] = t[0]; // top = left
        __fallthrough;
    case 2: // two inputs = top/bottom and left/right are the same
        t[2] = t[0]; // right = left
        t[3] = t[1]; // bottom = top
        __fallthrough;
    case 4: // four inputs = fully specified
        break;
    default:
        return {};

Review comment (suggestion):
You can use `std::size(buf)` here. But I think we can also just use a `std::wstring`. The primary cost of a `std::wstring` if any is the heap allocation and we can just reuse the object instance.

Code after:
inline ::winrt::hstring XamlThicknessToOptimalString(const ::winrt::Windows::UI::Xaml::Thickness& t)
{
    if (t.Left == t.Right)
    {
        if (t.Top == t.Bottom)
        {
            if (t.Top == t.Left)
            {
                return ::winrt::hstring{ fmt::format(FMT_COMPILE(L"{}"), t.Left) };
            }
            return ::winrt::hstring{ fmt::format(FMT_COMPILE(L"{},{}"), t.Left, t.Top) };
        }
        // fall through
    }
    return ::winrt::hstring{ fmt::format(FMT_COMPILE(L"{},{},{},{}"), t.Left, t.Top, t.Right, t.Bottom) };
}

inline ::winrt::Windows::UI::Xaml::Thickness StringToXamlThickness(std::wstring_view padding)
try
{
    uintptr_t count{ 0 };
    double t[4]{ 0. }; // left, top, right, bottom
    std::wstring buf;
    auto& errnoRef = errno; // Nonzero cost, pay it once
    for (const auto& token : til::split_iterator{ padding, L',' })
    {
        buf.assign(token);
        // wcstod handles whitespace prefix (which is ignored) & stops the
        // scan when first char outside the range of radix is encountered.
        // We'll be permissive till the extent that stod function allows us to be by default
        // Ex. a value like 100.3#535w2 will be read as 100.3, but ;df25 will fail
        errnoRef = 0;
        wchar_t* end;
        const auto val{ std::wcstod(buf.c_str(), &end) };
        if (end != buf.c_str() && errnoRef != ERANGE)
        {
            til::at(t, count) = val;
        }

        if (++count >= 4)
        {
            break;
        }
    }

#pragma warning(push)
#pragma warning(disable : 26446) // Prefer to use gsl::at() instead of unchecked subscript operator (bounds.4).
    switch (count)
    {
    case 1: // one input = all 4 values are the same
        t[1] = t[0]; // top = left