From 23f1275a247148ebe76ad180346b9f846a0d3989 Mon Sep 17 00:00:00 2001 From: "Dirk-Jan C. Binnema" Date: Tue, 2 Nov 2010 00:05:27 +0200 Subject: [PATCH] * add mu_msg_str_normalize (to remove accents from strings, optionally lowercase) --- src/mu-msg-str.c | 25 +++++++++++++++++++++++++ src/mu-msg-str.h | 17 +++++++++++++++++ 2 files changed, 42 insertions(+) diff --git a/src/mu-msg-str.c b/src/mu-msg-str.c index c23ef2a1..b9e05073 100644 --- a/src/mu-msg-str.c +++ b/src/mu-msg-str.c @@ -23,6 +23,31 @@ #include "mu-msg-str.h" #include "mu-msg-flags.h" +char* +mu_msg_str_normalize (const char *str, gboolean downcase) +{ + gchar *s; + + if (!str) + return NULL; + + s = g_utf8_normalize (str, -1, G_NORMALIZE_ALL); + if (!s) { + g_warning ("%s: not valid utf8 '%s'", __FUNCTION__, str); + return NULL; + } + + if (downcase) { + gchar *tmp; + tmp = g_utf8_strdown (s, -1); + g_free (s); + s = tmp; + } + + return s; +} + + const char* mu_msg_str_date_s (time_t t) diff --git a/src/mu-msg-str.h b/src/mu-msg-str.h index 289a3108..ddd793bd 100644 --- a/src/mu-msg-str.h +++ b/src/mu-msg-str.h @@ -26,6 +26,8 @@ #include "mu-msg.h" #include "mu-msg-flags.h" +G_BEGIN_DECLS + /** * get a display string for a given time_t; * use the preferred date/time for the current locale @@ -104,4 +106,19 @@ const char* mu_msg_str_prio (MuMsgPrio prio) G_GNUC_CONST; char* mu_msg_str_summarize (const char* str, size_t max_lines) G_GNUC_WARN_UNUSED_RESULT; + + +/** + * normalize a string (ie., collapse accented characters etc.), and + * optionally, downcase it + * + * @param str a valid utf8 string or NULL + * @param downcase if TRUE, convert the string to lowercase + * + * @return the normalize string, or NULL in case of error or str was NULL + */ +char* mu_msg_str_normalize (const char *str, gboolean downcase); + +G_END_DECLS + #endif /*__MU_MSG_STR_H__*/