object: b_strlen and b_string_get_size can now ignore term's formatting codes

This commit is contained in:
2024-11-14 22:03:09 +00:00
parent 9ac204e84e
commit b2c9c7cab9
2 changed files with 21 additions and 9 deletions

View File

@@ -468,24 +468,35 @@ char *b_strdup(const char *s)
size_t b_strlen(const char *s, b_strlen_flags flags)
{
if (!(flags & B_STRLEN_IGNORE_ESC)) {
if (!(flags & (B_STRLEN_IGNORE_ESC | B_STRLEN_IGNORE_MOD))) {
return strlen(s);
}
size_t out = 0;
bool esc = false;
for (size_t i = 0; s[i]; i++) {
if (s[i] == '\033') {
esc = true;
if (s[i] == '\033' && (flags & B_STRLEN_IGNORE_ESC)) {
while (!isalpha(s[i]) && s[i]) {
i++;
}
continue;
}
if (!esc) {
out++;
if (s[i] == '[' && (flags & B_STRLEN_IGNORE_MOD)) {
i++;
if (s[i] == '[') {
out++;
continue;
}
while (s[i] != ']' && s[i]) {
i++;
}
continue;
}
if (isalpha(s[i])) {
esc = false;
}
out++;
}
return out;