From 7b0bd51d3373026d68d958538cb8012d343d2821 Mon Sep 17 00:00:00 2001 From: Vsevolod Stakhov Date: Mon, 23 Feb 2015 13:45:44 +0000 Subject: [PATCH] Add lowercase utility for utf8 strings. --- src/libutil/util.c | 31 +++++++++++++++++++++++++++++++ src/libutil/util.h | 1 + 2 files changed, 32 insertions(+) diff --git a/src/libutil/util.c b/src/libutil/util.c index 932246542..f0d9e0eb6 100644 --- a/src/libutil/util.c +++ b/src/libutil/util.c @@ -657,6 +657,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 bd2d069a1..153f1c16b 100644 --- a/src/libutil/util.h +++ b/src/libutil/util.h @@ -112,6 +112,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 /* -- 2.39.5