From d6be6b9cff1957fd9c40d202cb719af4c51566ef Mon Sep 17 00:00:00 2001 From: Emanuele Giaquinta Date: Fri, 28 Nov 2008 23:01:45 +0000 Subject: [PATCH] Factor code to check if a string is ascii only into a function. git-svn-id: file:///var/www/svn.irssi.org/SVN/irssi/trunk@4926 dbcabf3a-b0e7-0310-adc4-f8d773084564 --- src/core/recode.c | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/core/recode.c b/src/core/recode.c index 5e68cea8..6edd5235 100644 --- a/src/core/recode.c +++ b/src/core/recode.c @@ -77,6 +77,16 @@ static char *find_conversion(const SERVER_REC *server, const char *target) return conv; } +static int str_is_ascii(const char *str) +{ + int i; + + for (i = 0; str[i] != '\0'; i++) + if (str[i] & 0x80) + return 0; + return 1; +} + char *recode_in(const SERVER_REC *server, const char *str, const char *target) { const char *from = NULL; @@ -84,7 +94,6 @@ char *recode_in(const SERVER_REC *server, const char *str, const char *target) char *recoded = NULL; gboolean str_is_utf8, recode, autodetect; int len; - int i; if (!str) return NULL; @@ -97,12 +106,8 @@ char *recode_in(const SERVER_REC *server, const char *str, const char *target) /* Only validate for UTF-8 if an 8-bit encoding. */ str_is_utf8 = 0; - for (i = 0; i < len; ++i) { - if (str[i] & 0x80) { - str_is_utf8 = g_utf8_validate(str, len, NULL); - break; - } - } + if (!str_is_ascii(str)) + str_is_utf8 = g_utf8_validate(str, len, NULL); autodetect = settings_get_bool("recode_autodetect_utf8"); if (autodetect && str_is_utf8)