diff --git a/object/include/blue/object/string.h b/object/include/blue/object/string.h index 8100fff..1e5aa62 100644 --- a/object/include/blue/object/string.h +++ b/object/include/blue/object/string.h @@ -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 { diff --git a/object/string.c b/object/string.c index f85a1cb..7a4a75c 100644 --- a/object/string.c +++ b/object/string.c @@ -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;