[nautilus: 1/2] file-utilities: rework common prefix computation
- From: Carlos Soriano <csoriano src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [nautilus: 1/2] file-utilities: rework common prefix computation
- Date: Mon, 1 Jan 2018 23:12:09 +0000 (UTC)
commit 61ac48bdc0230eed8466b7d1a3164aae2a5e8594
Author: Ernestas Kulik <ernestask gnome org>
Date: Sat Dec 23 17:28:00 2017 +0200
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.
eel/eel-string.c | 36 ---------
eel/eel-string.h | 8 --
src/nautilus-file-utilities.c | 51 +++++++++++--
test/meson.build | 5 --
test/test-eel-string-rtrim-punctuation.c | 122 -------------------------------
5 files changed, 46 insertions(+), 176 deletions(-)
---
diff --git a/eel/eel-string.c b/eel/eel-string.c
index 85c0841a4..0a41a0bf9 100644
--- a/eel/eel-string.c
+++ b/eel/eel-string.c
@@ -224,42 +224,6 @@ eel_str_replace_substring (const char *string,
return result;
}
-char *
-eel_str_rtrim_punctuation (char *str)
-{
- int num_punctuation_chars;
- int str_len;
- int num_chars_left;
- char *current_char_pos;
- gunichar current_char;
-
- num_punctuation_chars = 0;
- str_len = g_utf8_strlen (str, -1);
- current_char_pos = g_utf8_offset_to_pointer (str, str_len);
-
- while (num_punctuation_chars <= str_len)
- {
- current_char_pos = g_utf8_prev_char (current_char_pos);
- current_char = g_utf8_get_char (current_char_pos);
-
- if (!g_unichar_ispunct (current_char) && !g_unichar_isspace (current_char))
- {
- break;
- }
-
- ++num_punctuation_chars;
- }
-
- if (num_punctuation_chars == 0)
- {
- return g_strdup (str);
- }
-
- num_chars_left = str_len - num_punctuation_chars;
-
- return g_utf8_substring (str, 0, num_chars_left);
-}
-
/**
* get_common_prefix_length:
* @str_a: first string
diff --git a/eel/eel-string.h b/eel/eel-string.h
index 98cca7e9f..5df4b6b2a 100644
--- a/eel/eel-string.h
+++ b/eel/eel-string.h
@@ -57,14 +57,6 @@ char * eel_str_strip_substring_and_after (const char *str,
char * eel_str_replace_substring (const char *str,
const char *substring,
const char *replacement);
-/**
- * eel_str_rtrim_punctuation:
- * @str: string
- *
- * Returns: a copy of str with trailing punctuation characters removed
- */
-char * eel_str_rtrim_punctuation (char *str);
-
/**
* eel_str_get_common_prefix:
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);
diff --git a/test/meson.build b/test/meson.build
index 0c7d830d5..f6ad5b007 100644
--- a/test/meson.build
+++ b/test/meson.build
@@ -16,10 +16,6 @@ test_file_utilities_get_common_filename_prefix = executable ('test-file-utilitie
'test-file-utilities-get-common-filename-prefix.c',
dependencies: libnautilus_dep)
-test_eel_string_rtrim_punctuation = executable ('test-eel-string-rtrim-punctuation',
- 'test-eel-string-rtrim-punctuation.c',
- dependencies: libnautilus_dep)
-
test_eel_string_get_common_prefix = executable ('test-eel-string-get-common-prefix',
'test-eel-string-get-common-prefix.c',
dependencies: libnautilus_dep)
@@ -27,5 +23,4 @@ test_eel_string_get_common_prefix = executable ('test-eel-string-get-common-pref
test ('test-nautilus-search-engine', test_nautilus_search_engine)
test ('test-nautilus-directory-async', test_nautilus_directory_async)
test ('test-file-utilities-get-common-filename-prefix', test_file_utilities_get_common_filename_prefix)
-test ('test-eel-string-rtrim-punctuation', test_eel_string_rtrim_punctuation)
test ('test-eel-string-get-common-prefix', test_eel_string_get_common_prefix)
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]