object: b_strlen and b_string_get_size can now ignore term's formatting codes
This commit is contained in:
@@ -15,6 +15,7 @@ typedef struct b_string b_string;
|
||||
typedef enum b_strlen_flags {
|
||||
B_STRLEN_NORMAL = 0,
|
||||
B_STRLEN_IGNORE_ESC = 0x01u,
|
||||
B_STRLEN_IGNORE_MOD = 0x02u,
|
||||
} b_strlen_flags;
|
||||
|
||||
typedef struct b_strv_builder {
|
||||
|
||||
@@ -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++;
|
||||
}
|
||||
|
||||
if (!esc) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (s[i] == '[' && (flags & B_STRLEN_IGNORE_MOD)) {
|
||||
i++;
|
||||
if (s[i] == '[') {
|
||||
out++;
|
||||
continue;
|
||||
}
|
||||
|
||||
if (isalpha(s[i])) {
|
||||
esc = false;
|
||||
while (s[i] != ']' && s[i]) {
|
||||
i++;
|
||||
}
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
out++;
|
||||
}
|
||||
|
||||
return out;
|
||||
|
||||
Reference in New Issue
Block a user