From 51a5febad7794ae9b57e1d6094d1b8429d88cd47 Mon Sep 17 00:00:00 2001 From: Michael Drake Date: Wed, 15 May 2013 22:13:30 +0100 Subject: Cache conversion descriptor since this func. is called many times in series for the same conversion. (UTF-8 --> iso-8859-1) --- utils/utf8.c | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) (limited to 'utils/utf8.c') diff --git a/utils/utf8.c b/utils/utf8.c index 885ca94ee..4f79377fb 100644 --- a/utils/utf8.c +++ b/utils/utf8.c @@ -419,6 +419,33 @@ utf8_convert_ret utf8_to_html(const char *string, const char *encname, return UTF8_CONVERT_NOMEM; } + /* we cache the last used conversion descriptor, + * so check if we're trying to use it here */ + if (strncasecmp(last_cd.from, "UTF-8", sizeof(last_cd.from)) == 0 && + strncasecmp(last_cd.to, encname, + sizeof(last_cd.to)) == 0) { + cd = last_cd.cd; + } + else { + /* no match, so create a new cd */ + cd = iconv_open(encname, "UTF-8"); + if (cd == (iconv_t)-1) { + if (errno == EINVAL) + return UTF8_CONVERT_BADENC; + /* default to no memory */ + return UTF8_CONVERT_NOMEM; + } + + /* close the last cd - we don't care if this fails */ + if (last_cd.cd) + iconv_close(last_cd.cd); + + /* and copy the to/from/cd data into last_cd */ + strncpy(last_cd.from, "UTF-8", sizeof(last_cd.from)); + strncpy(last_cd.to, encname, sizeof(last_cd.to)); + last_cd.cd = cd; + } + /* Worst case is ASCII -> UCS4, with all characters escaped: * "&#xYYYYYY;", thus each input character may become a string * of 10 UCS4 characters, each 4 bytes in length */ -- cgit v1.2.3