[geany/geany] 724e78: Changes for review comments

Thomas Martitz git-noreply at xxxxx
Mon Dec 3 21:33:59 UTC 2018


Branch:      refs/heads/master
Author:      Thomas Martitz <kugel at rockbox.org>
Committer:   Thomas Martitz <kugel at rockbox.org>
Date:        Mon, 03 Dec 2018 21:33:59 UTC
Commit:      724e7886dec8da92cc3e7b50c861499a6b6932eb
             https://github.com/geany/geany/commit/724e7886dec8da92cc3e7b50c861499a6b6932eb

Log Message:
-----------
Changes for review comments

- Notably the utils_strv_{find_common_prefix,find_lcs,shorten_file_list}
  now take -1 for num to mean to compute the array length.

- utils_strv_find_common_prefix implementation simplified.

- if num == 0 is passed to the above functions the passed strv is not
  dereferenced (so could be NULL).


Modified Paths:
--------------
    src/symbols.c
    src/utils.c
    src/utils.h

Modified: src/symbols.c
4 lines changed, 2 insertions(+), 2 deletions(-)
===================================================================
@@ -1933,10 +1933,10 @@ static void show_goto_popup(GeanyDocument *doc, GPtrArray *tags, gboolean have_b
 	GdkEventButton *button_event = NULL;
 	TMTag *tmtag;
 	guint i;
-	gchar **short_names, **file_names, **p;
+	gchar **short_names, **file_names;
 	menu = gtk_menu_new();
 
-	/* If popup would show multiple files presend a smart file list that allows
+	/* If popup would show multiple files present a smart file list that allows
 	 * to easily distinguish the files while avoiding the file paths in their entirety */
 	file_names = g_new(gchar *, tags->len);
 	foreach_ptr_array(tmtag, i, tags)


Modified: src/utils.c
47 lines changed, 22 insertions(+), 25 deletions(-)
===================================================================
@@ -2049,51 +2049,45 @@ gchar **utils_strv_join(gchar **first, gchar **second)
  * The size of the list may be given explicitely, but defaults to @c g_strv_length(strv).
  *
  * @param strv The list of strings to process.
- * @param num The number of strings contained in @a strv. Can be 0 if it's terminated by @c NULL.
+ * @param num The number of strings contained in @a strv. Can be -1 if it's terminated by @c NULL.
  *
  * @return The common prefix that is part of all strings (maybe empty), or NULL if an empty list
  *         was passed in.
  */
-static gchar *utils_strv_find_common_prefix(gchar **strv, gsize num)
+static gchar *utils_strv_find_common_prefix(gchar **strv, gssize num)
 {
-	gchar *prefix;
-
 	if (!NZV(strv))
 		return NULL;
 
-	if (num == 0)
+	if (num == -1)
 		num = g_strv_length(strv);
 
-	prefix = g_strdup(strv[0]);
-
-	for (gint i = 0; prefix[i]; i++)
+	for (gsize i = 0; strv[0][i]; i++)
 	{
 		for (gsize j = 1; j < num; j++)
 		{
-			gchar *s = strv[j];
-			if (s[i] != prefix[i])
+			if (strv[j][i] != strv[0][i])
 			{
-				/* terminate prefix on first mismatch and return */
-				prefix[i] = '\0';
-				break;
+				/* return prefix on first mismatch */
+				return g_strndup(strv[0], i);
 			}
 		}
-		if (prefix[i] == '\0')
-			break;
 	}
-	return prefix;
+
+	return g_strdup(strv[0]);
 }
 
+
 /* * Returns the longest common substring in a list of strings.
  *
  * The size of the list may be given explicitely, but defaults to @c g_strv_length(strv).
  *
  * @param strv The list of strings to process.
- * @param num The number of strings contained in @a strv. Can be 0 if it's terminated by @c NULL.
+ * @param num The number of strings contained in @a strv. Can be -1 if it's terminated by @c NULL.
  *
  * @return The common prefix that is part of all strings.
  */
-static gchar *utils_strv_find_lcs(gchar **strv, gsize num)
+static gchar *utils_strv_find_lcs(gchar **strv, gssize num)
 {
 	gchar *first, *_sub, *sub;
 	gsize n_chars;
@@ -2102,15 +2096,15 @@ static gchar *utils_strv_find_lcs(gchar **strv, gsize num)
 	char *lcs;
 	gsize found;
 
-	if (strv == NULL)
+	if (num == -1)
+		num = g_strv_length(strv);
+
+	if (num < 1)
 		return NULL;
 
 	first = strv[0];
 	len = strlen(first);
 
-	if (num == 0)
-		num = g_strv_length(strv);
-
 	/* sub is the working area where substrings from first are copied to */
 	sub = g_malloc(len+1);
 	lcs = g_strdup("");
@@ -2154,24 +2148,27 @@ static gchar *utils_strv_find_lcs(gchar **strv, gsize num)
  * replaces the longest common substring with an ellipsis ("...").
  *
  * @param file_names @array{length=num} The list of strings to process.
- * @param num The number of strings contained in @a file_names. Can be 0 if it's terminated by @c NULL.
+ * @param num The number of strings contained in @a file_names. Can be -1 if it's terminated by @c NULL.
  * @return @transfer{full} A newly-allocated array of transformed paths strings, terminated by
             @c NULL. Use @c g_strfreev() to free it.
  *
  * @since 1.34 (API 239)
  */
 GEANY_API_SYMBOL
-gchar **utils_strv_shorten_file_list(gchar **file_names, gsize num)
+gchar **utils_strv_shorten_file_list(gchar **file_names, gssize num)
 {
 	gsize i;
 	gchar *prefix, *substring, *lcs;
 	gchar *start, *end;
 	gchar **names;
 	gsize prefix_len, lcs_len;
 
+	/* We don't dereference file_names if num == 0. */
+	g_return_val_if_fail(num == 0 || file_names != NULL, NULL);
+
 	/* The return value shall have exactly the same size as the input. If the input is a
 	 * GStrv (last element is NULL), the output will follow suit. */
-	if (!num)
+	if (num == -1)
 		num = g_strv_length(file_names);
 	/* Always include a terminating NULL, enables easy freeing with g_strfreev() */
 	names = g_new0(gchar *, num + 1);


Modified: src/utils.h
2 lines changed, 1 insertions(+), 1 deletions(-)
===================================================================
@@ -301,7 +301,7 @@ gchar **utils_strv_new(const gchar *first, ...) G_GNUC_NULL_TERMINATED;
 
 gchar **utils_strv_join(gchar **first, gchar **second) G_GNUC_WARN_UNUSED_RESULT;
 
-gchar **utils_strv_shorten_file_list(gchar **file_names, size_t num);
+gchar **utils_strv_shorten_file_list(gchar **file_names, gssize num);
 
 GSList *utils_get_config_files(const gchar *subdir);
 



--------------
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