parent
3c14ba1cae
commit
d8879cf1d9
@ -0,0 +1,59 @@
|
||||
#include "webfused/util/string_list.h"
|
||||
#include <stdlib.h>
|
||||
#include <string.h>
|
||||
|
||||
#define WFD_STRING_LIST_INITIAL_CAPACITY 8
|
||||
|
||||
void
|
||||
wfd_string_list_init(
|
||||
struct wfd_string_list * list)
|
||||
{
|
||||
list->size = 0;
|
||||
list->capacity = WFD_STRING_LIST_INITIAL_CAPACITY;
|
||||
list->items = malloc(sizeof(char *) * list->capacity);
|
||||
}
|
||||
|
||||
void
|
||||
wfd_string_list_init_copy(
|
||||
struct wfd_string_list * list,
|
||||
struct wfd_string_list const * other)
|
||||
{
|
||||
if (0 < other->size)
|
||||
{
|
||||
list->size = other->size;
|
||||
list->capacity = other->capacity;
|
||||
list->items = malloc(sizeof(char *) * list->capacity);
|
||||
|
||||
memcpy(list->items, other->items, (sizeof(char*) * list->size));
|
||||
}
|
||||
else
|
||||
{
|
||||
wfd_string_list_init(list);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
wfd_string_list_cleanup(
|
||||
struct wfd_string_list * list)
|
||||
{
|
||||
for (size_t i = 0; i < list->size; i++)
|
||||
{
|
||||
free(list->items[i]);
|
||||
}
|
||||
free(list->items);
|
||||
}
|
||||
|
||||
void
|
||||
wfd_string_list_add(
|
||||
struct wfd_string_list * list,
|
||||
char const * item)
|
||||
{
|
||||
if (list->size >= list->capacity)
|
||||
{
|
||||
list->capacity *= 2;
|
||||
list->items = realloc(list->items, sizeof(char *) * list->capacity);
|
||||
}
|
||||
|
||||
list->items[list->size] = strdup(item);
|
||||
list->size++;
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
#ifndef WFD_UTIL_STRING_LIST_H
|
||||
#define WFD_UTIL_STRING_LIST_H
|
||||
|
||||
#ifndef __cplusplus
|
||||
#include <stddef.h>
|
||||
#else
|
||||
#include <cstddef>
|
||||
#endif
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C"
|
||||
{
|
||||
#endif
|
||||
|
||||
struct wfd_string_list
|
||||
{
|
||||
size_t size;
|
||||
size_t capacity;
|
||||
char * * items;
|
||||
};
|
||||
|
||||
extern void
|
||||
wfd_string_list_init(
|
||||
struct wfd_string_list * list);
|
||||
|
||||
extern void
|
||||
wfd_string_list_init_copy(
|
||||
struct wfd_string_list * list,
|
||||
struct wfd_string_list const * other);
|
||||
|
||||
extern void
|
||||
wfd_string_list_cleanup(
|
||||
struct wfd_string_list * list);
|
||||
|
||||
extern void
|
||||
wfd_string_list_add(
|
||||
struct wfd_string_list * list,
|
||||
char const * item);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif
|
@ -0,0 +1,44 @@
|
||||
#include "webfused/util/string_list.h"
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
TEST(string_list, init_cleanup)
|
||||
{
|
||||
wfd_string_list list;
|
||||
wfd_string_list_init(&list);
|
||||
|
||||
ASSERT_EQ(0, list.size);
|
||||
|
||||
wfd_string_list_cleanup(&list);
|
||||
}
|
||||
|
||||
TEST(string_list, add)
|
||||
{
|
||||
wfd_string_list list;
|
||||
wfd_string_list_init(&list);
|
||||
wfd_string_list_add(&list, "value");
|
||||
|
||||
ASSERT_EQ(1, list.size);
|
||||
ASSERT_STREQ("value", list.items[0]);
|
||||
|
||||
wfd_string_list_cleanup(&list);
|
||||
}
|
||||
|
||||
TEST(string_list, add_many)
|
||||
{
|
||||
wfd_string_list list;
|
||||
wfd_string_list_init(&list);
|
||||
|
||||
constexpr size_t count = 256;
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
wfd_string_list_add(&list, "value");
|
||||
}
|
||||
|
||||
ASSERT_EQ(count, list.size);
|
||||
for (size_t i = 0; i < count; i++)
|
||||
{
|
||||
ASSERT_STREQ("value", list.items[i]);
|
||||
}
|
||||
|
||||
wfd_string_list_cleanup(&list);
|
||||
}
|
Loading…
Reference in new issue