[nautilus/ellipsization-truncation-a-united-nation: 1/3] 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: 1/3] eel: string: refactor middle truncation
- Date: Tue, 20 Feb 2018 07:29:40 +0000 (UTC)
commit 1bb85c7df5c25f0d062a2d991558d5a2d869e701
Author: Ernestas Kulik <ernestask gnome org>
Date: Mon Feb 19 20:32:09 2018 +0200
eel: string: refactor middle truncation
The code is a bit suboptimal in places, namely using strlen() when the
array length is available at compile time. Another thing is wasteful
allocation of the new string: the truncated string of the same size as
the source string. This can be fixed by adding a couple of different
allocations. :)
The comments were also removed, as they’re obvious.
eel/eel-string.c | 45 +++++++++++++++++----------------------------
eel/eel-string.h | 15 ++++++++++-----
2 files changed, 27 insertions(+), 33 deletions(-)
---
diff --git a/eel/eel-string.c b/eel/eel-string.c
index df1209919..30e09808e 100644
--- a/eel/eel-string.c
+++ b/eel/eel-string.c
@@ -106,26 +106,20 @@ eel_str_capitalize (const char *string)
**/
char *
eel_str_middle_truncate (const char *string,
- guint truncate_length)
+ size_t 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;
- }
+ const char ellipsis[] = "…";
+ const size_t min_truncate_length = G_N_ELEMENTS (ellipsis) + 2;
+ size_t length;
+ size_t num_left_chars;
+ size_t num_right_chars;
+ g_autofree char *left_substring = NULL;
+ g_autofree char *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);
- /* 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)
{
return g_strdup (string);
@@ -133,23 +127,18 @@ eel_str_middle_truncate (const char *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 - G_N_ELEMENTS (ellipsis)) / 2;
+ num_right_chars = truncate_length - num_left_chars - G_N_ELEMENTS (ellipsis);
- 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..206815360 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);
+char *eel_str_middle_truncate (const char *string,
+ size_t truncate_length);
/* Remove all characters after the passed-in substring. */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]