refactred autocomplete_remove

This commit is contained in:
Dmitry Podgorny
2013-08-26 03:29:04 +03:00
parent 92763d2e68
commit 982f1174a8

View File

@@ -101,32 +101,23 @@ autocomplete_add(Autocomplete ac, const char *item)
gboolean gboolean
autocomplete_remove(Autocomplete ac, const char * const item) autocomplete_remove(Autocomplete ac, const char * const item)
{ {
// reset last found if it points to the item to be removed GSList *curr = g_slist_find_custom(ac->items, item, (GCompareFunc)strcmp);
if (ac->last_found != NULL)
if (g_strcmp0(ac->last_found->data, item) == 0)
ac->last_found = NULL;
if (!ac->items) { if (!curr) {
return FALSE; return FALSE;
} else { }
GSList *curr = ac->items;
while(curr) { // reset last found if it points to the item to be removed
if (g_strcmp0(curr->data, item) == 0) { if (ac->last_found == curr) {
void *current_item = curr->data; ac->last_found = NULL;
ac->items = g_slist_remove(ac->items, curr->data); }
free(current_item);
free(curr->data);
ac->items = g_slist_delete_link(ac->items, curr);
return TRUE; return TRUE;
} }
curr = g_slist_next(curr);
}
return FALSE;
}
}
GSList * GSList *
autocomplete_get_list(Autocomplete ac) autocomplete_get_list(Autocomplete ac)
{ {