[nautilus/ellipsization-truncation-a-united-nation: 2/4] 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: 2/4] eel: string: refactor middle truncation
- Date: Mon, 19 Feb 2018 20:03:56 +0000 (UTC)
commit ea0998e7ac3d448fbc51c359a53711b0ba759e4d
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 | 36 +++++++++++++-----------------------
1 file changed, 13 insertions(+), 23 deletions(-)
---
diff --git a/eel/eel-string.c b/eel/eel-string.c
index df1209919..0694649f5 100644
--- a/eel/eel-string.c
+++ b/eel/eel-string.c
@@ -106,26 +106,21 @@ 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;
+ 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;
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)
{
return g_strdup (string);
@@ -133,23 +128,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 *
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]