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 {
|
typedef enum b_strlen_flags {
|
||||||
B_STRLEN_NORMAL = 0,
|
B_STRLEN_NORMAL = 0,
|
||||||
B_STRLEN_IGNORE_ESC = 0x01u,
|
B_STRLEN_IGNORE_ESC = 0x01u,
|
||||||
|
B_STRLEN_IGNORE_MOD = 0x02u,
|
||||||
} b_strlen_flags;
|
} b_strlen_flags;
|
||||||
|
|
||||||
typedef struct b_strv_builder {
|
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)
|
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);
|
return strlen(s);
|
||||||
}
|
}
|
||||||
|
|
||||||
size_t out = 0;
|
size_t out = 0;
|
||||||
bool esc = false;
|
|
||||||
for (size_t i = 0; s[i]; i++) {
|
for (size_t i = 0; s[i]; i++) {
|
||||||
if (s[i] == '\033') {
|
if (s[i] == '\033' && (flags & B_STRLEN_IGNORE_ESC)) {
|
||||||
esc = true;
|
while (!isalpha(s[i]) && s[i]) {
|
||||||
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!esc) {
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (s[i] == '[' && (flags & B_STRLEN_IGNORE_MOD)) {
|
||||||
|
i++;
|
||||||
|
if (s[i] == '[') {
|
||||||
out++;
|
out++;
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (isalpha(s[i])) {
|
while (s[i] != ']' && s[i]) {
|
||||||
esc = false;
|
i++;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
out++;
|
||||||
}
|
}
|
||||||
|
|
||||||
return out;
|
return out;
|
||||||
|
|||||||
Reference in New Issue
Block a user