Repository: microsoft/terminal
File: src/inc/til/u8u16convert.h (C/C++)

Code before:

            out.clear();
            RETURN_HR_IF(S_OK, !capa16); // nothing to convert, however this is not an error

            out.resize(gsl::narrow_cast<size_t>(capa16));
            auto len8{ gsl::narrow_cast<int>(in.length()) };
            int len16{};
            auto cursor8{ in.data() };
            if (!!state.partials[3])
            {
                if (!len8) // only the partial code point will be converted and published
                {
                    len16 = MultiByteToWideChar(CP_UTF8, 0UL, state.partials.data(), gsl::narrow_cast<int>(state.partials[3]), out.data(), capa16);
                    RETURN_HR_IF(E_UNEXPECTED, !len16);

                    state.reset();
                    out.resize(gsl::narrow_cast<size_t>(len16));
                    return S_FALSE;
                }

                // determine the number of bytes of the code point indicated by the lead byte in the cache
                const auto codePointLen{
                    2 + // the code point consists of at least 2 bytes because if we cached data it will never be ASCII
                    gsl::narrow_cast<int>((state.partials[0] & IsLead3) == IsLead3) + // add one if 3 high-order bits of the lead byte are set
                    gsl::narrow_cast<int>((state.partials[0] & IsLead4) == IsLead4) // add another one if 4 high-order bits of the lead byte are set
                };

                const auto fetch8{ codePointLen - gsl::narrow_cast<int>(state.partials[3]) }; // number of bytes we need from the begin of the chunk to complete the code point
                if (fetch8 > len8) // we still didn't get enough data to complete the code point, however this is not an error
                {
                    std::move(cursor8, cursor8 + len8, state.partials.data() + state.partials[3]);
                    state.partials[3] += gsl::narrow_cast<char>(len8);
                    out.clear();
                    return S_OK;
                }

                std::move(cursor8, cursor8 + fetch8, state.partials.data() + state.partials[3]);
                len16 = MultiByteToWideChar(CP_UTF8, 0UL, state.partials.data(), gsl::narrow_cast<int>(codePointLen), out.data(), capa16);
                RETURN_HR_IF(E_UNEXPECTED, !len16);

                state.reset();
                capa16 -= len16;
                len8 -= fetch8;
                cursor8 += fetch8;
            }

            if (!!len8)
            {
                auto backIter{ cursor8 + len8 };
                // If the last byte in the string was a byte belonging to a UTF-8 multi-byte character
                if (!!(backIter[-1] & MaskAscii))

Review comment (suggestion):
If you store the number of remaining/missing characters in `state.partials[3]` instead, you don't need to calculate this.

Code after:
                // it is released into the Public Domain. https://github.com/skeeto/branchless-utf8
                static constexpr uint8_t lengths[]{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 2, 2, 2, 2, 3, 3, 4, 0 };
                const auto codePointLen{ lengths[gsl::narrow_cast<uint8_t>(*backIter) >> 3] };

                if (codePointLen > sequenceLen)
                {
                    std::move(backIter, backIter + sequenceLen, &state.partials[0]);
                    len8 -= sequenceLen;
                    state.have = gsl::narrow_cast<uint8_t>(sequenceLen);
                    state.want = gsl::narrow_cast<uint8_t>(codePointLen - sequenceLen);
                }
            }

            if (len8)
            {
                const auto convLen{ MultiByteToWideChar(CP_UTF8, 0UL, cursor8, len8, out.data() + len16, capa16) };
                RETURN_HR_IF(E_UNEXPECTED, !convLen);

                len16 += convLen;
            }

            out.resize(gsl::narrow_cast<size_t>(len16));
            return S_OK;
        }
        CATCH_RETURN();
    }
#pragma warning(pop)

    // Routine Description:
    // - Takes a UTF-16 string and performs the conversion to UTF-8. NOTE: The function relies on getting complete UTF-16 characters at the string boundaries.
    // Arguments:
    // - in - UTF-16 string to be converted
    // - out - reference to the resulting UTF-8 string
    // Return Value:
    // - S_OK          - the conversion succeeded
    // - E_OUTOFMEMORY - the function failed to allocate memory for the resulting string
    // - E_ABORT       - the resulting string length would exceed the upper boundary of an int and thus, the conversion was aborted before the conversion has been completed
    // - E_UNEXPECTED  - the underlying conversion function failed
    // - HRESULT value converted from a caught exception
    template<class outT>
    [[nodiscard]] HRESULT u16u8(const std::wstring_view& in, outT& out) noexcept
    {
        try
        {
            out.clear();
            RETURN_HR_IF(S_OK, in.empty());

            int lengthIn{};
            int lengthRequired{};
            // Code Point U+0000..U+FFFF: 1 UTF-16 code unit --> 1..3 UTF-8 code units.
            // Code Points >U+FFFF: 2 UTF-16 code units --> 4 UTF-8 code units.