GList* g_completion_complete (GCompletion* cmp, gchar* prefix, gchar** new_prefix) { gint plen, len; gint done = 0; GList* list; g_return_val_if_fail (cmp != NULL, NULL); g_return_val_if_fail (prefix != NULL, NULL); len = strlen (prefix); if (cmp->prefix && cmp->cache) { plen = strlen (cmp->prefix); if (plen <= len && !strncmp (prefix, cmp->prefix, plen)) { /* use the cache */ list = cmp->cache; while (list) { if (strncmp (prefix, cmp->func ? cmp->func (list->data) : (gchar*) list->data, len)) { list = g_list_remove_link (cmp->cache, list); if (list != cmp->cache) cmp->cache = list; } else list = list->next; } done = 1; } } if (!done) { /* normal code */ g_list_free (cmp->cache); cmp->cache = NULL; list = cmp->items; while (*prefix && list) { if (!strncmp (prefix, cmp->func ? cmp->func (list->data) : (gchar*) list->data, len)) cmp->cache = g_list_prepend (cmp->cache, list->data); list = list->next; } } if (cmp->prefix) { g_free (cmp->prefix); cmp->prefix = NULL; } if (cmp->cache) cmp->prefix = g_strdup (prefix); completion_check_cache (cmp, new_prefix); return *prefix ? cmp->cache : cmp->items; }
/** * g_completion_complete: * @cmp: the #GCompletion. * @prefix: the prefix string, typically typed by the user, which is * compared with each of the items. * @new_prefix: if non-%NULL, returns the longest prefix which is * common to all items that matched @prefix, or %NULL if * no items matched @prefix. This string should be freed * when no longer needed. * @Returns: the list of items whose strings begin with @prefix. This * should not be changed. * * Attempts to complete the string @prefix using the #GCompletion * target items. * * Deprecated: 2.26: Rarely used API **/ GList* g_completion_complete (GCompletion* cmp, const gchar* prefix, gchar** new_prefix) { gsize plen, len; gboolean done = FALSE; GList* list; g_return_val_if_fail (cmp != NULL, NULL); g_return_val_if_fail (prefix != NULL, NULL); len = strlen (prefix); if (cmp->prefix && cmp->cache) { plen = strlen (cmp->prefix); if (plen <= len && ! cmp->strncmp_func (prefix, cmp->prefix, plen)) { /* use the cache */ list = cmp->cache; while (list) { GList *next = list->next; if (cmp->strncmp_func (prefix, cmp->func ? cmp->func (list->data) : (gchar*) list->data, len)) cmp->cache = g_list_delete_link (cmp->cache, list); list = next; } done = TRUE; } } if (!done) { /* normal code */ g_list_free (cmp->cache); cmp->cache = NULL; list = cmp->items; while (*prefix && list) { if (!cmp->strncmp_func (prefix, cmp->func ? cmp->func (list->data) : (gchar*) list->data, len)) cmp->cache = g_list_prepend (cmp->cache, list->data); list = list->next; } } if (cmp->prefix) { g_free (cmp->prefix); cmp->prefix = NULL; } if (cmp->cache) cmp->prefix = g_strdup (prefix); completion_check_cache (cmp, new_prefix); return *prefix ? cmp->cache : cmp->items; }