[tepl] utils: copy str_middle_truncate() from gedit
- From: Sébastien Wilmet <swilmet src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [tepl] utils: copy str_middle_truncate() from gedit
- Date: Sun, 6 Aug 2017 07:33:17 +0000 (UTC)
commit 7407ac37fafd2992183303acce998e08bd1ce1ff
Author: Sébastien Wilmet <swilmet gnome org>
Date: Sun Aug 6 09:19:32 2017 +0200
utils: copy str_middle_truncate() from gedit
tepl/tepl-utils.c | 76 +++++++++++++++++++++++++++++++++++++++++++++++++++++
tepl/tepl-utils.h | 5 +++
2 files changed, 81 insertions(+), 0 deletions(-)
---
diff --git a/tepl/tepl-utils.c b/tepl/tepl-utils.c
index eb4a020..be6ce77 100644
--- a/tepl/tepl-utils.c
+++ b/tepl/tepl-utils.c
@@ -33,6 +33,82 @@
* Utility functions.
*/
+static gchar *
+str_truncate (const gchar *string,
+ guint truncate_length,
+ gboolean middle)
+{
+ GString *truncated;
+ guint length;
+ guint n_chars;
+ guint num_left_chars;
+ guint right_offset;
+ guint delimiter_length;
+ const gchar *delimiter = "\342\200\246";
+
+ g_return_val_if_fail (string != NULL, NULL);
+
+ length = strlen (string);
+
+ g_return_val_if_fail (g_utf8_validate (string, length, NULL), NULL);
+
+ /* It doesnt make sense to truncate strings to less than
+ * the size of the delimiter plus 2 characters (one on each
+ * side)
+ */
+ delimiter_length = g_utf8_strlen (delimiter, -1);
+ if (truncate_length < (delimiter_length + 2))
+ {
+ return g_strdup (string);
+ }
+
+ n_chars = g_utf8_strlen (string, length);
+
+ /* Make sure the string is not already small enough. */
+ if (n_chars <= truncate_length)
+ {
+ return g_strdup (string);
+ }
+
+ /* Find the 'middle' where the truncation will occur. */
+ if (middle)
+ {
+ num_left_chars = (truncate_length - delimiter_length) / 2;
+ right_offset = n_chars - truncate_length + num_left_chars + delimiter_length;
+
+ truncated = g_string_new_len (string,
+ g_utf8_offset_to_pointer (string, num_left_chars) - string);
+ g_string_append (truncated, delimiter);
+ g_string_append (truncated, g_utf8_offset_to_pointer (string, right_offset));
+ }
+ else
+ {
+ num_left_chars = truncate_length - delimiter_length;
+ truncated = g_string_new_len (string,
+ g_utf8_offset_to_pointer (string, num_left_chars) - string);
+ g_string_append (truncated, delimiter);
+ }
+
+ return g_string_free (truncated, FALSE);
+}
+
+gchar *
+_tepl_utils_str_middle_truncate (const gchar *str,
+ guint truncate_length)
+{
+ return str_truncate (str, truncate_length, TRUE);
+}
+
+#if 0
+/* Not yet used in Tepl. */
+gchar *
+_tepl_utils_str_end_truncate (const gchar *str,
+ guint truncate_length)
+{
+ return str_truncate (str, truncate_length, FALSE);
+}
+#endif
+
/*
* _tepl_utils_replace_home_dir_with_tilde:
* @filename: the filename.
diff --git a/tepl/tepl-utils.h b/tepl/tepl-utils.h
index f88900d..c4a7684 100644
--- a/tepl/tepl-utils.h
+++ b/tepl/tepl-utils.h
@@ -28,6 +28,11 @@
G_BEGIN_DECLS
+/* String utilities */
+
+gchar * _tepl_utils_str_middle_truncate (const gchar *str,
+ guint truncate_length);
+
/* File utilities */
G_GNUC_INTERNAL
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]