static unsigned Utf32ToUtf8Size()

in src/LttngHelpers.c [247:298]


static unsigned Utf32ToUtf8Size(
    char32_t const *pch32,
    unsigned cch32)
    lttng_ust_notrace;
static unsigned Utf32ToUtf8Size(
    char32_t const *pch32,
    unsigned cch32)
{
    unsigned ich8 = 0;
    unsigned ich32 = 0;

    // Since we never get cch32 > 65535, we can safely skip testing for overflow of ich8.
    assert(cch32 <= (0xFFFFFFFF / 7));

    while (ich32 != cch32)
    {
        // Note that this algorithm accepts non-Unicode values (above 0x10FFFF).
        // That's probably the right decision for logging - we want to preserve
        // them so they can be noticed and fixed.
        unsigned val32 = pch32[ich32];
        ich32 += 1;
        if (caa_likely(val32 < 0x80))
        {
            ich8 += 1;
        }
        else if (caa_likely(val32 < 0x800))
        {
            ich8 += 2;
        }
        else if (caa_likely(val32 < 0x10000))
        {
            ich8 += 3;
        }
        else if (caa_likely(val32 < 0x200000))
        {
            ich8 += 4;
        }
        else if (caa_likely(val32 < 0x4000000))
        {
            ich8 += 5;
        }
        else if (caa_likely(val32 < 0x80000000))
        {
            ich8 += 6;
        }
        else
        {
            ich8 += 7;
        }
    }
    return ich8;
}