common: implement ident and selector manipulation

This commit is contained in:
2024-12-13 12:25:40 +00:00
parent 24d443b818
commit 0a8d913fdd
5 changed files with 201 additions and 7 deletions

69
common/ident.c Normal file
View File

@@ -0,0 +1,69 @@
#include <ivy/ident.h>
#include <blue/core/queue.h>
#include <blue/object/string.h>
#include <stdlib.h>
#include <string.h>
struct ivy_ident *ivy_ident_create(void)
{
struct ivy_ident *id = malloc(sizeof *id);
if (!id) {
return NULL;
}
memset(id, 0x0, sizeof *id);
return id;
}
void ivy_ident_destroy(struct ivy_ident *ident)
{
b_queue_iterator it = {0};
b_queue_iterator_begin(&ident->id_parts, &it);
while (b_queue_iterator_is_valid(&it)) {
struct ivy_ident_part *part = b_unbox(struct ivy_ident_part, it.entry, p_entry);
b_queue_iterator_erase(&it);
free(part->p_str);
free(part);
}
free(ident);
}
void ivy_ident_add_part(struct ivy_ident *ident, const char *s)
{
struct ivy_ident_part *part = malloc(sizeof *part);
if (!part) {
return;
}
memset(part, 0x0, sizeof *part);
part->p_str = b_strdup(s);
b_queue_push_back(&ident->id_parts, &part->p_entry);
}
size_t ivy_ident_to_string(const struct ivy_ident *ident, char *out, size_t max)
{
b_strv_builder strv;
b_strv_builder_begin(&strv, out, max);
int i = 0;
b_queue_iterator it = {0};
b_queue_foreach (&it, &ident->id_parts) {
if (i > 0) {
b_strv_builder_add(&strv, ".");
}
struct ivy_ident_part *part = b_unbox(struct ivy_ident_part, it.entry, p_entry);
b_strv_builder_add(&strv, part->p_str);
i++;
}
/* TODO return string length. */
return 0;
}