[nautilus/ellipsization-truncation-a-united-nation: 4/5] eel: string: refactor middle truncation
- From: Ernestas Kulik <ernestask src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [nautilus/ellipsization-truncation-a-united-nation: 4/5] eel: string: refactor middle truncation
- Date: Mon, 26 Feb 2018 15:42:19 +0000 (UTC)
commit cb0ef67f6fec43a1143621b9e2cdc614ec04b8fb
Author: Ernestas Kulik <ernestask gnome org>
Date: Mon Feb 19 20:32:09 2018 +0200
eel: string: refactor middle truncation
This commit tries to make the function more readable and fixes the allocation of
the new string: the truncated string is of the same size as the source string.
This can be fixed by adding a couple of different allocations. :)
Some comments were trimmed down or removed.
eel/eel-string.c | 56 ++++++++++++++++++++++++--------------------------------
eel/eel-string.h | 15 ++++++++++-----
2 files changed, 34 insertions(+), 37 deletions(-)
---
diff --git a/eel/eel-string.c b/eel/eel-string.c
index df1209919..30cc39bfc 100644
--- a/eel/eel-string.c
+++ b/eel/eel-string.c
@@ -104,52 +104,44 @@ eel_str_capitalize (const char *string)
* Returns: (transfer full): a newly allocated truncated string, or a copy of
* @string if it was not longer than @truncate_length.
**/
-char *
-eel_str_middle_truncate (const char *string,
- guint truncate_length)
+gchar *
+eel_str_middle_truncate (const gchar *string,
+ guint truncate_length)
{
- char *truncated;
- guint length;
- guint num_left_chars;
- guint num_right_chars;
-
- const char delimter[] = "…";
- const guint delimter_length = strlen (delimter);
- const guint min_truncate_length = delimter_length + 2;
-
- if (string == NULL)
- {
- return NULL;
- }
-
- /* It doesnt make sense to truncate strings to less than
- * the size of the delimiter plus 2 characters (one on each
- * side)
- */
- if (truncate_length < min_truncate_length)
+ const gchar ellipsis[] = "…";
+ gsize ellipsis_length;
+ gsize length;
+ gsize num_left_chars;
+ gsize num_right_chars;
+ g_autofree gchar *left_substring = NULL;
+ g_autofree gchar *right_substring = NULL;
+
+ g_return_val_if_fail (string != NULL, NULL);
+ g_return_val_if_fail (truncate_length > 0, NULL);
+ g_return_val_if_fail (truncate_length <= G_MAXLONG, NULL);
+
+ ellipsis_length = g_utf8_strlen (ellipsis, -1);
+
+ /* Our ellipsis string + one character on each side. */
+ if (truncate_length < ellipsis_length + 2)
{
return g_strdup (string);
}
length = g_utf8_strlen (string, -1);
- /* Make sure the string is not already small enough. */
if (length <= truncate_length)
{
return g_strdup (string);
}
- /* Find the 'middle' where the truncation will occur. */
- num_left_chars = (truncate_length - delimter_length) / 2;
- num_right_chars = truncate_length - num_left_chars - delimter_length;
-
- truncated = g_new (char, strlen (string) + 1);
+ num_left_chars = (truncate_length - ellipsis_length) / 2;
+ num_right_chars = truncate_length - num_left_chars - ellipsis_length;
- g_utf8_strncpy (truncated, string, num_left_chars);
- strcat (truncated, delimter);
- strcat (truncated, g_utf8_offset_to_pointer (string, length - num_right_chars));
+ left_substring = g_utf8_substring (string, 0, num_left_chars);
+ right_substring = g_utf8_substring (string, length - num_right_chars, length);
- return truncated;
+ return g_strconcat (left_substring, ellipsis, right_substring, NULL);
}
char *
diff --git a/eel/eel-string.h b/eel/eel-string.h
index 5df4b6b2a..5187a4f8a 100644
--- a/eel/eel-string.h
+++ b/eel/eel-string.h
@@ -41,12 +41,17 @@ char * eel_str_double_underscores (const char *str);
/* Capitalize a string */
char * eel_str_capitalize (const char *str);
-/* Middle truncate a string to a maximum of truncate_length characters.
- * The resulting string will be truncated in the middle with a "..."
- * delimiter.
+/**
+ * eel_str_middle_truncate:
+ * @string: the string to truncate
+ * @truncate_length: the length limit at which to truncate
+ *
+ * Replaces the middle of @string with an ellipsis so it fits into @truncate_length characters.
+ *
+ * Returns: @string, truncated at the middle to @truncate_length
*/
-char * eel_str_middle_truncate (const char *str,
- guint truncate_length);
+gchar *eel_str_middle_truncate (const gchar *string,
+ guint truncate_length);
/* Remove all characters after the passed-in substring. */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]