1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-22 19:45:54 -04:00

Autcomplete defaults to string data

p_obj_autocomplete_new() can be used to create an autocompleter
that stores arbitrary data structures passing memory management
functions to get a string, copy and free
This commit is contained in:
James Booth 2012-05-20 00:54:17 +01:00
parent b02b52d86a
commit 2080a61663
4 changed files with 28 additions and 21 deletions

View File

@ -34,7 +34,7 @@ static PAutocomplete ac;
void contact_list_init(void)
{
ac = p_autocomplete_new((PStrFunc)p_contact_name,
ac = p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy,
(GDestroyNotify)p_contact_free);
}

View File

@ -41,7 +41,12 @@ static gchar * _search_from(PAutocomplete ac, GSList *curr);
static const char *_str_func_default(const char *orig);
static const char *_copy_func_default(const char *orig);
PAutocomplete p_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func,
PAutocomplete p_autocomplete_new(void)
{
return p_obj_autocomplete_new(NULL, NULL, NULL);
}
PAutocomplete p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func,
GDestroyNotify free_func)
{
PAutocomplete new = malloc(sizeof(struct p_autocomplete_t));

View File

@ -29,7 +29,8 @@ typedef struct p_autocomplete_t *PAutocomplete;
typedef const char * (*PStrFunc)(const void *obj);
typedef void * (*PCopyFunc)(const void *obj);
PAutocomplete p_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func,
PAutocomplete p_autocomplete_new(void);
PAutocomplete p_obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func,
GDestroyNotify free_func);
void p_autocomplete_clear(PAutocomplete ac);
void p_autocomplete_reset(PAutocomplete ac);

View File

@ -9,33 +9,34 @@
static void clear_empty(void)
{
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
PAutocomplete ac = p_autocomplete_new();
p_autocomplete_clear(ac);
}
static void clear_empty_with_free_func(void)
{
PAutocomplete ac = p_autocomplete_new(NULL, NULL, (GDestroyNotify)p_contact_free);
PAutocomplete ac = p_obj_autocomplete_new(NULL, NULL,
(GDestroyNotify)p_contact_free);
p_autocomplete_clear(ac);
}
static void reset_after_create(void)
{
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
PAutocomplete ac = p_autocomplete_new();
p_autocomplete_reset(ac);
p_autocomplete_clear(ac);
}
static void find_after_create(void)
{
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
PAutocomplete ac = p_autocomplete_new();
p_autocomplete_complete(ac, "hello");
p_autocomplete_clear(ac);
}
static void get_after_create_returns_null(void)
{
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
PAutocomplete ac = p_autocomplete_new();
GSList *result = p_autocomplete_get_list(ac);
assert_is_null(result);
@ -45,7 +46,7 @@ static void get_after_create_returns_null(void)
static void get_after_create_with_copy_func_returns_null(void)
{
PAutocomplete ac = p_autocomplete_new(NULL, (PCopyFunc)p_contact_copy,
PAutocomplete ac = p_obj_autocomplete_new(NULL, (PCopyFunc)p_contact_copy,
(GDestroyNotify)p_contact_free);
GSList *result = p_autocomplete_get_list(ac);
@ -57,7 +58,7 @@ static void get_after_create_with_copy_func_returns_null(void)
static void add_one_and_complete(void)
{
char *item = strdup("Hello");
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
PAutocomplete ac = p_autocomplete_new();
p_autocomplete_add(ac, item);
char *result = p_autocomplete_complete(ac, "Hel");
@ -69,7 +70,7 @@ static void add_one_and_complete(void)
static void add_one_and_complete_with_funcs(void)
{
PContact contact = p_contact_new("James", "Online", "I'm here");
PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name, NULL,
PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, NULL,
(GDestroyNotify)p_contact_free);
p_autocomplete_add(ac, contact);
char *result = p_autocomplete_complete(ac, "Jam");
@ -83,7 +84,7 @@ static void add_two_and_complete_returns_first(void)
{
char *item1 = strdup("Hello");
char *item2 = strdup("Help");
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
PAutocomplete ac = p_autocomplete_new();
p_autocomplete_add(ac, item1);
p_autocomplete_add(ac, item2);
char *result = p_autocomplete_complete(ac, "Hel");
@ -97,7 +98,7 @@ static void add_two_and_complete_returns_first_with_funcs(void)
{
PContact contact1 = p_contact_new("James", "Online", "I'm here");
PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch");
PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name, NULL,
PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, NULL,
(GDestroyNotify)p_contact_free);
p_autocomplete_add(ac, contact1);
p_autocomplete_add(ac, contact2);
@ -112,7 +113,7 @@ static void add_two_and_complete_returns_second(void)
{
char *item1 = strdup("Hello");
char *item2 = strdup("Help");
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
PAutocomplete ac = p_autocomplete_new();
p_autocomplete_add(ac, item1);
p_autocomplete_add(ac, item2);
char *result1 = p_autocomplete_complete(ac, "Hel");
@ -127,7 +128,7 @@ static void add_two_and_complete_returns_second_with_funcs(void)
{
PContact contact1 = p_contact_new("James", "Online", "I'm here");
PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch");
PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name, NULL,
PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name, NULL,
(GDestroyNotify)p_contact_free);
p_autocomplete_add(ac, contact1);
p_autocomplete_add(ac, contact2);
@ -143,7 +144,7 @@ static void add_two_adds_two(void)
{
char *item1 = strdup("Hello");
char *item2 = strdup("Help");
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
PAutocomplete ac = p_autocomplete_new();
p_autocomplete_add(ac, item1);
p_autocomplete_add(ac, item2);
GSList *result = p_autocomplete_get_list(ac);
@ -157,7 +158,7 @@ static void add_two_adds_two_with_funcs(void)
{
PContact contact1 = p_contact_new("James", "Online", "I'm here");
PContact contact2 = p_contact_new("Jamie", "Away", "Out to lunch");
PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name,
PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free);
p_autocomplete_add(ac, contact1);
p_autocomplete_add(ac, contact2);
@ -172,7 +173,7 @@ static void add_two_same_adds_one(void)
{
char *item1 = strdup("Hello");
char *item2 = strdup("Hello");
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
PAutocomplete ac = p_autocomplete_new();
p_autocomplete_add(ac, item1);
p_autocomplete_add(ac, item2);
GSList *result = p_autocomplete_get_list(ac);
@ -186,7 +187,7 @@ static void add_two_same_adds_one_with_funcs(void)
{
PContact contact1 = p_contact_new("James", "Online", "I'm here");
PContact contact2 = p_contact_new("James", "Away", "Out to lunch");
PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name,
PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free);
p_autocomplete_add(ac, contact1);
p_autocomplete_add(ac, contact2);
@ -201,7 +202,7 @@ static void add_two_same_updates(void)
{
char *item1 = strdup("Hello");
char *item2 = strdup("Hello");
PAutocomplete ac = p_autocomplete_new(NULL, NULL, NULL);
PAutocomplete ac = p_autocomplete_new();
p_autocomplete_add(ac, item1);
p_autocomplete_add(ac, item2);
GSList *result = p_autocomplete_get_list(ac);
@ -219,7 +220,7 @@ static void add_two_same_updates_with_funcs(void)
{
PContact contact1 = p_contact_new("James", "Online", "I'm here");
PContact contact2 = p_contact_new("James", "Away", "Out to lunch");
PAutocomplete ac = p_autocomplete_new((PStrFunc)p_contact_name,
PAutocomplete ac = p_obj_autocomplete_new((PStrFunc)p_contact_name,
(PCopyFunc)p_contact_copy, (GDestroyNotify)p_contact_free);
p_autocomplete_add(ac, contact1);
p_autocomplete_add(ac, contact2);