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



commit 1acb17affb8f1d40dc7a1867f4d8f54bafd35bf9
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 | 23 +++++++++++++++++++++++
 eel/eel-string.h |  7 ++++---
 2 files changed, 27 insertions(+), 3 deletions(-)
---
diff --git a/eel/eel-string.c b/eel/eel-string.c
index 0694649f5..0cb211594 100644
--- a/eel/eel-string.c
+++ b/eel/eel-string.c
@@ -142,6 +142,29 @@ 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);
+    g_return_val_if_fail (truncate_length <= G_MAXLONG, 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, truncate_length - G_N_ELEMENTS (ellipsis));
+
+    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..cc84bd667 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]