示例#1
0
bool utf8_is_printable_newline(const char* str, size_t length, bool newline) {
    const char *p;

    assert(str);

    for (p = str; length;) {
        int encoded_len, val;

        encoded_len = utf8_encoded_valid_unichar(p);
        if (encoded_len < 0 ||
                (size_t) encoded_len > length)
            return false;

        val = utf8_encoded_to_unichar(p);
        if (val < 0 ||
                unichar_is_control(val) ||
                (!newline && val == '\n'))
            return false;

        length -= encoded_len;
        p += encoded_len;
    }

    return true;
}
示例#2
0
/* validate one encoded unicode char and return its length */
int utf8_encoded_valid_unichar(const char *str)
{
    int len;
    int unichar;
    int i;

    len = utf8_encoded_expected_len(str);
    if (len == 0)
        return -1;

    /* ascii is valid */
    if (len == 1)
        return 1;

    /* check if expected encoded chars are available */
    for (i = 0; i < len; i++)
        if ((str[i] & 0x80) != 0x80)
            return -1;

    unichar = utf8_encoded_to_unichar(str);

    /* check if encoded length matches encoded value */
    if (utf8_unichar_to_encoded_len(unichar) != len)
        return -1;

    /* check if value has valid range */
    if (!utf8_unichar_valid_range(unichar))
        return -1;

    return len;
}