[nautilus/ellipsization-truncation-a-united-nation: 2/3] eel: string: add _end_truncate()



commit 65e461dd8e3eb41bd1cede3327d5ae31b50dda89
Author: Ernestas Kulik <ernestask gnome org>
Date:   Mon Feb 19 21:31:49 2018 +0200

    eel: string: add _end_truncate()
    
    This is useful for truncating GIO error messages that can be potentially
    extremely long.

 eel/eel-string.c | 24 ++++++++++++++++++++++++
 eel/eel-string.h |  7 ++++---
 2 files changed, 28 insertions(+), 3 deletions(-)
---
diff --git a/eel/eel-string.c b/eel/eel-string.c
index 0694649f5..49d5e8143 100644
--- a/eel/eel-string.c
+++ b/eel/eel-string.c
@@ -142,6 +142,30 @@ eel_str_middle_truncate (const char *string,
     return g_strconcat (left_substring, ellipsis, right_substring, NULL);
 }
 
+char *
+eel_str_end_truncate (const char *string,
+                      size_t      truncate_length)
+{
+    const char ellipsis[] = "…";
+    const size_t min_truncate_length = G_N_ELEMENTS (ellipsis) + 1;
+    g_autofree char *substring = NULL;
+
+    g_return_val_if_fail (string != NULL, NULL);
+    g_return_val_if_fail (truncate_length > 0, NULL);
+
+    if (truncate_length < min_truncate_length ||
+        g_utf8_strlen (string, -1) <= min_truncate_length)
+    {
+        return g_strdup (string);
+    }
+
+    substring = g_utf8_substring (string,
+                                  0, MIN (truncate_length - G_N_ELEMENTS (ellipsis),
+                                          SSIZE_MAX));
+
+    return g_strconcat (substring, ellipsis, NULL);
+}
+
 char *
 eel_str_strip_substring_and_after (const char *string,
                                    const char *substring)
diff --git a/eel/eel-string.h b/eel/eel-string.h
index 5df4b6b2a..d55cc8a33 100644
--- a/eel/eel-string.h
+++ b/eel/eel-string.h
@@ -45,9 +45,10 @@ char *   eel_str_capitalize                (const char    *str);
  * The resulting string will be truncated in the middle with a "..."
  * delimiter.
  */
-char *   eel_str_middle_truncate           (const char    *str,
-                                           guint          truncate_length);
-
+char *   eel_str_middle_truncate           (const char    *string,
+                                           size_t          truncate_length);
+char *   eel_str_end_truncate              (const char    *string,
+                                            size_t         truncate_length);
 
 /* Remove all characters after the passed-in substring. */
 char *   eel_str_strip_substring_and_after (const char    *str,


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]