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

Code before:
    // - S_FALSE       - the resulting string is converted from the previously cached partials only
    // - 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  - an unexpected error occurred
    template<class outT>
    [[nodiscard]] HRESULT u8u16(const std::string_view& in, outT& out, u8state& state) noexcept
    {
        try
        {
            /* clang-format off */
            enum Utf8BitMasks : char
            {
                IsAscii   = gsl::narrow_cast<char>(0b0'0000000), // Any byte representing an ASCII character has the MSB set to 0
                MaskAscii = gsl::narrow_cast<char>(0b1'0000000), // Bit mask to be used in a bitwise AND operation to find out whether or not a byte match the IsAscii pattern
                IsCont    = gsl::narrow_cast<char>(0b10'000000), // Continuation bytes of any UTF-8 non-ASCII character have the MSB set to 1 and the adjacent bit set to 0
                MaskCont  = gsl::narrow_cast<char>(0b11'000000), // Bit mask to be used in a bitwise AND operation to find out whether or not a byte match the IsCont pattern
                IsLead2   = gsl::narrow_cast<char>(0b110'00000), // A lead byte that indicates a UTF-8 non-ASCII character consisting of two bytes has the two highest bits set to 1 and the adjacent bit set to 0
                MaskLead2 = gsl::narrow_cast<char>(0b111'00000), // Bit mask to be used in a bitwise AND operation to find out whether or not a lead byte match the IsLead2 pattern
                IsLead3   = gsl::narrow_cast<char>(0b1110'0000), // A lead byte that indicates a UTF-8 non-ASCII character consisting of three bytes has the three highest bits set to 1 and the adjacent bit set to 0
                MaskLead3 = gsl::narrow_cast<char>(0b1111'0000), // Bit mask to be used in a bitwise AND operation to find out whether or not a lead byte match the IsLead3 pattern
                IsLead4   = gsl::narrow_cast<char>(0b11110'000), // A lead byte that indicates a UTF-8 non-ASCII character consisting of four bytes has the four highest bits set to 1 and the adjacent bit set to 0
                MaskLead4 = gsl::narrow_cast<char>(0b11111'000), // Bit mask to be used in a bitwise AND operation to find out whether or not a lead byte match the IsLead4 pattern
            };
            /* clang-format on */

            constexpr const std::array<char, 4> cmpMasks{
                '\0', // unused
                MaskCont,
                MaskLead2,
                MaskLead3,
            };

            constexpr const std::array<char, 4> cmpOperands{
                '\0', // unused
                IsAscii, // intentionally conflicts with MaskCont
                IsLead2,
                IsLead3,
            };

            int capa16{};
            // The worst ratio of UTF-8 code units to UTF-16 code units is 1 to 1 if UTF-8 consists of ASCII only.
            RETURN_HR_IF(E_ABORT, !base::CheckAdd(in.length(), state.partials[3]).AssignIfValid(&capa16));

            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])

Review comment (suggestion):
`constexpr` implies `const`. But you should make them `static` otherwise a copy is made every time the function is called.

Code after:
            // The worst ratio of UTF-8 code units to UTF-16 code units is 1 to 1 if UTF-8 consists of ASCII only.
            RETURN_HR_IF(E_ABORT, !base::CheckAdd(in.length(), state.have).AssignIfValid(&capa16));

            out.resize(gsl::narrow_cast<size_t>(capa16));
            auto len8{ gsl::narrow_cast<int>(in.length()) };
            int len16{};
            auto cursor8{ in.data() };
            if (state.have)
            {
                const auto copyable{ std::min<int>(state.want, len8) };
                std::move(cursor8, cursor8 + copyable, &state.partials[state.have]);
                state.have += gsl::narrow_cast<uint8_t>(copyable);
                state.want -= gsl::narrow_cast<uint8_t>(copyable);
                if (state.want) // we still didn't get enough data to complete the code point, however this is not an error
                {
                    out.clear();
                    return S_OK;
                }

                len16 = MultiByteToWideChar(CP_UTF8, 0UL, &state.partials[0], gsl::narrow_cast<int>(state.have), out.data(), capa16);
                RETURN_HR_IF(E_UNEXPECTED, !len16);

                capa16 -= len16;
                len8 -= copyable;
                cursor8 += copyable;
                // state.want is already zero at this point
                state.have = 0;
            }

            if (len8)
            {
                auto backIter{ cursor8 + len8 - 1 };
                int sequenceLen{ 1 };

                // skip UTF8 continuation bytes
                while (backIter != cursor8 && (*backIter & 0b11'000000) == 0b10'000000)
                {
                    --backIter;
                    ++sequenceLen;
                }

                // credits go to Christopher Wellons for this algorithm to determine the length of a UTF-8 code point
                // 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);