From: Vsevolod Stakhov Date: Mon, 23 Feb 2015 13:45:44 +0000 (+0000) Subject: Add lowercase utility for utf8 strings. X-Git-Tag: 0.9.0~639 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=77570ec7f9d6865c29c55b1535c00fb7c1d5b4b3;p=thirdparty%2Frspamd.git Add lowercase utility for utf8 strings. --- diff --git a/src/libutil/util.c b/src/libutil/util.c index dbc34733e3..5a42e4f8eb 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -663,6 +663,37 @@ rspamd_str_lc (gchar *str, guint size) } } +/* + * The purpose of this function is fast and in place conversion of a unicode + * string to lower case, so some locale peculiarities are simply ignored + * If the target string is longer than initial one, then we just trim it + */ +void +rspamd_str_lc_utf8 (gchar *str, guint size) +{ + const gchar *s = str, *p; + gchar *d = str; + guint remain = size; + gint r; + gunichar uc; + + while (remain > 0) { + uc = g_utf8_get_char_validated (s, remain); + uc = g_unichar_tolower (uc); + p = g_utf8_next_char (s); + + if (p - s == 0) { + return; + } + + r = g_unichar_to_utf8 (uc, d); + g_assert (r > 0); + remain -= r; + s = p; + d += r; + } +} + #ifndef HAVE_SETPROCTITLE static gchar *title_buffer = 0; diff --git a/src/libutil/util.h b/src/libutil/util.h index fd584938f9..9e90b1089e 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -109,6 +109,7 @@ void rspamd_pass_signal (GHashTable *, gint ); * Convert string to lowercase */ void rspamd_str_lc (gchar *str, guint size); +void rspamd_str_lc_utf8 (gchar *str, guint size); #ifndef HAVE_SETPROCTITLE /*