object: string: add front, back, and pop_back functions

This commit is contained in:
2025-02-10 21:15:48 +00:00
parent 4f9ea02126
commit 4c3cc27d08
3 changed files with 38 additions and 0 deletions

View File

@@ -51,6 +51,11 @@ BLUE_API void b_string_clear(b_string *str);
BLUE_API size_t b_string_get_size(const b_string *str, b_strlen_flags flags);
BLUE_API size_t b_string_get_capacity(const b_string *str);
BLUE_API char b_string_front(const b_string *str);
BLUE_API char b_string_back(const b_string *str);
BLUE_API void b_string_pop_back(b_string *str);
BLUE_API const char *b_string_ptr(const b_string *str);
BLUE_API char *b_strdup(const char *s);

View File

@@ -26,6 +26,7 @@ typedef enum b_fundamental_type_id {
B_OBJECT_TYPE_STRING,
B_OBJECT_TYPE_TREE,
B_OBJECT_TYPE_UUID,
B_OBJECT_TYPE_PATH,
B_OBJECT_TYPE_FILE,
B_OBJECT_TYPE_DIRECTORY,
} b_fundamental_type_id;

View File

@@ -324,6 +324,38 @@ size_t b_string_get_capacity(const struct b_string *str)
return str->s_max;
}
char b_string_front(const struct b_string *str)
{
if (str->s_len == 0) {
return 0;
}
const char *s = b_string_ptr(str);
return s[0];
}
char b_string_back(const struct b_string *str)
{
if (str->s_len == 0) {
return 0;
}
const char *s = b_string_ptr(str);
return s[str->s_len - 1];
}
void b_string_pop_back(struct b_string *str)
{
if (str->s_len == 0) {
return;
}
char *s = string_ptr(str);
s[str->s_len - 1] = '\0';
str->s_len--;
}
const char *b_string_ptr(const struct b_string *str)
{
if (string_is_inline(str)) {