diff options
author | Neil Herald <neil.herald@gmail.com> | 2016-04-03 13:28:11 +0100 |
---|---|---|
committer | Neil Herald <neil.herald@gmail.com> | 2016-07-10 08:30:48 +0100 |
commit | ca0e00b6b3c48a61f0ad091e6ae16d4f4e3e39fb (patch) | |
tree | 6ec71979a99d00c17a3e2bee804a5cdf95c42486 /eel | |
parent | 54b6341c49917b1eae5f1342062ab60cf2f650f7 (diff) | |
download | nautilus-ca0e00b6b3c48a61f0ad091e6ae16d4f4e3e39fb.tar.gz |
files-view: change "New Folder /w Selection" to offer a name
New Folder with Selection currently doesn't offer a folder name. It
would be better if it suggested a folder name based on the files that
are selected.
With this change, it now looks for a common filename prefix of the
selected files, and pre-populates the folder name entry with that. If no
common prefix is found that is greater than 3 characters long, the
folder name entry will be left blank.
https://bugzilla.gnome.org/show_bug.cgi?id=747907
Diffstat (limited to 'eel')
-rw-r--r-- | eel/eel-string.c | 126 | ||||
-rw-r--r-- | eel/eel-string.h | 18 |
2 files changed, 144 insertions, 0 deletions
diff --git a/eel/eel-string.c b/eel/eel-string.c index 28c7e5024..e0f849c7f 100644 --- a/eel/eel-string.c +++ b/eel/eel-string.c @@ -208,6 +208,132 @@ 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 + * @str_b: second string + * @min_required_len: the minimum number of characters required in the prefix + * + * Returns: the size of the common prefix of two strings, in characters. + * If there's no common prefix, or the common prefix is smaller than + * min_required_len, this will return -1 + */ +static int +get_common_prefix_length (char *str_a, + char *str_b, + int min_required_len) +{ + int a_len; + int b_len; + int intersection_len; + int matching_chars; + char *a; + char *b; + + a_len = g_utf8_strlen (str_a, -1); + b_len = g_utf8_strlen (str_b, -1); + + intersection_len = MIN (a_len, b_len); + if (intersection_len < min_required_len) + return -1; + + matching_chars = 0; + a = str_a; + b = str_b; + while (matching_chars < intersection_len) { + if (g_utf8_get_char (a) != g_utf8_get_char (b)) + break; + + ++matching_chars; + + a = g_utf8_next_char (a); + b = g_utf8_next_char (b); + } + + if (matching_chars < min_required_len) + return -1; + + return matching_chars; +} + +char * +eel_str_get_common_prefix (GList *strs, int min_required_len) +{ + GList *l; + char *common_part; + char *name; + char *truncated; + int matching_chars; + + if (strs == NULL) + return NULL; + + common_part = NULL; + for (l = strs; l != NULL; l = l->next) { + + name = l->data; + if (name == NULL) { + g_free (common_part); + return NULL; + } + + if (l->prev == NULL) { + common_part = g_strdup (name); + continue; + } + + matching_chars = get_common_prefix_length (common_part, name, min_required_len); + + if (matching_chars == -1) { + g_free (common_part); + return NULL; + } + + truncated = g_utf8_substring (common_part, 0, matching_chars); + g_free (common_part); + common_part = truncated; + } + + matching_chars = g_utf8_strlen (common_part, -1); + if (matching_chars < min_required_len) { + g_free (common_part); + return NULL; + } + + return common_part; +} + /**************** Custom printf ***********/ typedef struct { diff --git a/eel/eel-string.h b/eel/eel-string.h index 202eec2a3..98cca7e9f 100644 --- a/eel/eel-string.h +++ b/eel/eel-string.h @@ -57,6 +57,24 @@ 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: + * @str: set of strings + * @min_required_len: the minimum number of characters required in the prefix + * + * Returns: the common prefix for a set of strings, or NULL if there isn't a + * common prefix of length min_required_len + */ +char * eel_str_get_common_prefix (GList *strs, int min_required_len); typedef char * eel_ref_str; |