[geany/geany] 2328f8: Disallow the possibility to use tm_tags_find() on unsorted array

Jiří Techet git-noreply at xxxxx
Thu Feb 11 14:35:49 UTC 2016


Branch:      refs/heads/master
Author:      Jiří Techet <techet at gmail.com>
Committer:   Jiří Techet <techet at gmail.com>
Date:        Tue, 14 Jul 2015 07:46:59 UTC
Commit:      2328f84e37328a8d2e8a5fa58dcf8a836885fa41
             https://github.com/geany/geany/commit/2328f84e37328a8d2e8a5fa58dcf8a836885fa41

Log Message:
-----------
Disallow the possibility to use tm_tags_find() on unsorted array

This function won't work correctly on unsorted array because the second
part of the function (after the tags search) expects the array is sorted
by name. The only user of this is tm_source_file_set_tag_arglist() in which
we can go through the tags manually by ourselves (it needs only a single
value so the original behavior of tm_tags_find() wasn't a problem).
Eliminate the tags_search() function as it isn't needed any more.

Just cleanup, not functional change.


Modified Paths:
--------------
    tagmanager/src/tm_source_file.c
    tagmanager/src/tm_tag.c
    tagmanager/src/tm_tag.h
    tagmanager/src/tm_workspace.c

Modified: tagmanager/src/tm_source_file.c
18 lines changed, 10 insertions(+), 8 deletions(-)
===================================================================
@@ -118,8 +118,7 @@ static int tm_source_file_tags(const tagEntryInfo *tag)
 /* Set the argument list of tag identified by its name */
 static void tm_source_file_set_tag_arglist(const char *tag_name, const char *arglist)
 {
-	guint count;
-	TMTag **tags, *tag;
+	guint i;
 
 	if (NULL == arglist ||
 		NULL == tag_name ||
@@ -128,13 +127,16 @@ static void tm_source_file_set_tag_arglist(const char *tag_name, const char *arg
 		return;
 	}
 
-	tags = tm_tags_find(current_source_file->tags_array, tag_name, FALSE, FALSE,
-			&count);
-	if (tags != NULL && count == 1)
+	/* going in reverse order because the tag was added recently */
+	for (i = current_source_file->tags_array->len; i > 0; i--)
 	{
-		tag = tags[0];
-		g_free(tag->arglist);
-		tag->arglist = g_strdup(arglist);
+		TMTag *tag = (TMTag *) current_source_file->tags_array->pdata[i - 1];
+		if (g_strcmp0(tag->name, tag_name) == 0)
+		{
+			g_free(tag->arglist);
+			tag->arglist = g_strdup(arglist);
+			break;
+		}
 	}
 }
 


Modified: tagmanager/src/tm_tag.c
37 lines changed, 6 insertions(+), 31 deletions(-)
===================================================================
@@ -862,7 +862,7 @@ void tm_tags_remove_file_tags(TMSourceFile *source_file, GPtrArray *tags_array)
 			TMTag **found;
 			TMTag *tag = source_file->tags_array->pdata[i];
 
-			found = tm_tags_find(tags_array, tag->name, FALSE, TRUE, &tag_count);
+			found = tm_tags_find(tags_array, tag->name, FALSE, &tag_count);
 
 			for (j = 0; j < tag_count; j++)
 			{
@@ -1083,42 +1083,16 @@ static gpointer binary_search(gpointer key, gpointer base, size_t nmemb,
 	return NULL;
 }
 
-static TMTag **tags_search(const GPtrArray *tags_array, TMTag *tag,
-		gboolean tags_array_sorted, TMSortOptions *sort_options)
-{
-	if (tags_array_sorted)
-	{	/* fast binary search on sorted tags array */
-		return (TMTag **) binary_search(&tag, tags_array->pdata, tags_array->len, 
-			tm_tag_compare, sort_options);
-	}
-	else
-	{	/* the slow way: linear search (to make it a bit faster, search reverse assuming
-		 * that the tag to search was added recently) */
-		guint i;
-		TMTag **t;
-		for (i = tags_array->len; i > 0; i--)
-		{
-			t = (TMTag **) &tags_array->pdata[i - 1];
-			if (0 == tm_tag_compare(&tag, t, sort_options))
-				return t;
-		}
-	}
-	return NULL;
-}
-
 /*
  Returns a pointer to the position of the first matching tag in a (sorted) tags array.
- The passed array of tags should be already sorted by name for optimal performance. If
- \c tags_array_sorted is set to FALSE, it may be unsorted but the lookup will be slower.
- @param tags_array Tag array (may be sorted on name)
+ The passed array of tags must be already sorted by name (searched with binary search).
+ @param tags_array Tag array (sorted on name)
  @param name Name of the tag to locate.
  @param partial If TRUE, matches the first part of the name instead of doing exact match.
- @param tags_array_sorted If TRUE, the passed \c tags_array is sorted by name so it can be
- searched with binary search. Otherwise it is searched linear which is obviously slower.
  @param tagCount Return location of the matched tags.
 */
 TMTag **tm_tags_find(const GPtrArray *tags_array, const char *name,
-		gboolean partial, gboolean tags_array_sorted, guint * tagCount)
+		gboolean partial, guint * tagCount)
 {
 	static TMTag *tag = NULL;
 	TMTag **result;
@@ -1135,7 +1109,8 @@ TMTag **tm_tags_find(const GPtrArray *tags_array, const char *name,
 	sort_options.sort_attrs = NULL;
 	sort_options.partial = partial;
 
-	result = tags_search(tags_array, tag, tags_array_sorted, &sort_options);
+	result = (TMTag **)binary_search(&tag, tags_array->pdata, tags_array->len,
+			tm_tag_compare, &sort_options);
 	/* There can be matches on both sides of result */
 	if (result)
 	{


Modified: tagmanager/src/tm_tag.h
2 lines changed, 1 insertions(+), 1 deletions(-)
===================================================================
@@ -179,7 +179,7 @@ gboolean tm_tags_prune(GPtrArray *tags_array);
 gboolean tm_tags_dedup(GPtrArray *tags_array, TMTagAttrType *sort_attributes, gboolean unref_duplicates);
 
 TMTag **tm_tags_find(const GPtrArray *tags_array, const char *name,
-		gboolean partial, gboolean tags_array_sorted, guint * tagCount);
+		gboolean partial, guint * tagCount);
 
 void tm_tags_array_free(GPtrArray *tags_array, gboolean free_all);
 


Modified: tagmanager/src/tm_workspace.c
2 lines changed, 1 insertions(+), 1 deletions(-)
===================================================================
@@ -721,7 +721,7 @@ static guint fill_find_tags_array(GPtrArray *dst, const GPtrArray *src,
 	if (!src || !dst || !name || !*name)
 		return 0;
 
-	matches = tm_tags_find(src, name, partial, TRUE, &tagCount);
+	matches = tm_tags_find(src, name, partial, &tagCount);
 	for (tagIter = 0; tagIter < tagCount; ++tagIter)
 	{
 		if ((type & (*matches)->type) &&



--------------
This E-Mail was brought to you by github_commit_mail.py (Source: https://github.com/geany/infrastructure).


More information about the Commits mailing list