diff options
author | Ernestas Kulik <ernestask@gnome.org> | 2017-12-23 17:28:00 +0200 |
---|---|---|
committer | Ernestas Kulik <ernestask@gnome.org> | 2017-12-23 17:38:17 +0200 |
commit | 61ac48bdc0230eed8466b7d1a3164aae2a5e8594 (patch) | |
tree | bb96c868ab81ff65012b179585ebe660abea7fb7 /src/nautilus-file-utilities.c | |
parent | 351e3f7d4b25a5e8db6e5fee80acea5254fa32d1 (diff) | |
download | nautilus-61ac48bdc0230eed8466b7d1a3164aae2a5e8594.tar.gz |
file-utilities: rework common prefix computation
Currently, the process for getting the common prefix of a list of file
names is a tad too greedy:
1. Find the common prefix of all the strings.
2. Strip the extension from the prefix.
3. Strip trailing punctuation.
Step 2 may strip dots if there’s trailing whitespace and step 3 may
strip useful punctuation (e.g. parentheses). This commit reworks the
process as such:
1. Strip the extension from all the file names.
2. Find the common prefix of all the strings.
3. Trim trailing whitespace.
Fixes #174.
Diffstat (limited to 'src/nautilus-file-utilities.c')
-rw-r--r-- | src/nautilus-file-utilities.c | 51 |
1 files changed, 46 insertions, 5 deletions
diff --git a/src/nautilus-file-utilities.c b/src/nautilus-file-utilities.c index 65b50fa14..26683729e 100644 --- a/src/nautilus-file-utilities.c +++ b/src/nautilus-file-utilities.c @@ -1331,26 +1331,67 @@ nautilus_get_common_filename_prefix (GList *file_list, return result; } +static char * +trim_whitespace (const gchar *string) +{ + glong space_count; + glong length; + gchar *offset; + + space_count = 0; + length = g_utf8_strlen (string, -1); + offset = g_utf8_offset_to_pointer (string, length); + + while (space_count <= length) + { + gunichar character; + + offset = g_utf8_prev_char (offset); + character = g_utf8_get_char (offset); + + if (!g_unichar_isspace (character)) + { + break; + } + + space_count++; + } + + if (space_count == 0) + { + return g_strdup (string); + } + + return g_utf8_substring (string, 0, length - space_count); +} + char * nautilus_get_common_filename_prefix_from_filenames (GList *filenames, int min_required_len) { + GList *stripped_filenames = NULL; char *common_prefix; char *truncated; int common_prefix_len; - common_prefix = eel_str_get_common_prefix (filenames, min_required_len); + for (GList *i = filenames; i != NULL; i = i->next) + { + gchar *stripped_filename; + + stripped_filename = eel_filename_strip_extension (i->data); + stripped_filenames = g_list_prepend (stripped_filenames, stripped_filename); + } + + common_prefix = eel_str_get_common_prefix (stripped_filenames, min_required_len); if (common_prefix == NULL) { return NULL; } - truncated = eel_filename_strip_extension (common_prefix); - g_free (common_prefix); - common_prefix = truncated; + g_list_free_full (stripped_filenames, g_free); - truncated = eel_str_rtrim_punctuation (common_prefix); + truncated = trim_whitespace (common_prefix); g_free (common_prefix); common_prefix_len = g_utf8_strlen (truncated, -1); |