const unsigned char *s = (const unsigned char*)_s;
const unsigned char *end;
size_t len = 0;
+ size_t st, ln;
if (!utf8_enable.integer)
return COM_StringLengthNoColors(_s, size_s, valid);
++len; // the character
break;
}
- break;
+ ++s;
+ continue;
default:
- ++len;
break;
}
- // start of a wide character
- if (*s & 0xC0)
+ // ascii char, skip u8_analyze
+ if (*s < 0x80)
+ {
+ ++len;
+ ++s;
+ continue;
+ }
+
+ // invalid, skip u8_analyze
+ if (*s < 0xC2)
{
- for (++s; *s >= 0x80 && *s <= 0xC0; ++s);
+ ++s;
continue;
}
- // part of a wide character, we ignore that one
- if (*s <= 0xBF)
- --len;
- ++s;
+
+ if (!u8_analyze((const char*)s, &st, &ln, NULL, U8_ANALYZE_INFINITY))
+ {
+ // we CAN end up here, if an invalid char is between this one and the end of the string
+ if(valid)
+ *valid = TRUE;
+ return len;
+ }
+
+ if(end && s + st + ln > end)
+ {
+ // string length exceeded by new character
+ if(valid)
+ *valid = TRUE;
+ return len;
+ }
+
+ // valid character, skip after it
+ s += st + ln;
+ ++len;
}
// never get here
}