mirror of
https://github.com/profanity-im/profanity.git
synced 2025-02-02 15:08:15 -05:00
refactored autocomplete_add
Now this function responsible for memory allocation
This commit is contained in:
parent
32e18fb1a5
commit
92763d2e68
@ -83,42 +83,19 @@ autocomplete_length(Autocomplete ac)
|
||||
}
|
||||
|
||||
gboolean
|
||||
autocomplete_add(Autocomplete ac, void *item)
|
||||
autocomplete_add(Autocomplete ac, const char *item)
|
||||
{
|
||||
if (ac->items == NULL) {
|
||||
ac->items = g_slist_append(ac->items, item);
|
||||
return TRUE;
|
||||
} else {
|
||||
GSList *curr = ac->items;
|
||||
char *item_cpy;
|
||||
GSList *curr = g_slist_find_custom(ac->items, item, (GCompareFunc)strcmp);
|
||||
|
||||
while(curr) {
|
||||
|
||||
// insert
|
||||
if (g_strcmp0(curr->data, item) > 0) {
|
||||
ac->items = g_slist_insert_before(ac->items,
|
||||
curr, item);
|
||||
return TRUE;
|
||||
|
||||
// update
|
||||
} else if (g_strcmp0(curr->data, item) == 0) {
|
||||
// only update if data different
|
||||
if (strcmp(curr->data, item) != 0) {
|
||||
free(curr->data);
|
||||
curr->data = item;
|
||||
return TRUE;
|
||||
} else {
|
||||
return FALSE;
|
||||
}
|
||||
}
|
||||
|
||||
curr = g_slist_next(curr);
|
||||
}
|
||||
|
||||
// hit end, append
|
||||
ac->items = g_slist_append(ac->items, item);
|
||||
|
||||
return TRUE;
|
||||
// if item already exists
|
||||
if (curr != NULL) {
|
||||
return FALSE;
|
||||
}
|
||||
|
||||
item_cpy = strdup(item);
|
||||
ac->items = g_slist_insert_sorted(ac->items, item_cpy, (GCompareFunc)strcmp);
|
||||
return TRUE;
|
||||
}
|
||||
|
||||
gboolean
|
||||
|
@ -38,7 +38,7 @@ Autocomplete obj_autocomplete_new(PStrFunc str_func, PCopyFunc copy_func,
|
||||
void autocomplete_clear(Autocomplete ac);
|
||||
void autocomplete_reset(Autocomplete ac);
|
||||
void autocomplete_free(Autocomplete ac);
|
||||
gboolean autocomplete_add(Autocomplete ac, void *item);
|
||||
gboolean autocomplete_add(Autocomplete ac, const char *item);
|
||||
gboolean autocomplete_remove(Autocomplete ac, const char * const item);
|
||||
GSList * autocomplete_get_list(Autocomplete ac);
|
||||
gchar * autocomplete_complete(Autocomplete ac, gchar *search_str);
|
||||
|
Loading…
Reference in New Issue
Block a user