From 0d4b2056307ebb9ff2b938c1518014fbdf2c8f49 Mon Sep 17 00:00:00 2001 From: Werner Koch Date: Thu, 25 Jan 2007 10:26:55 +0000 Subject: jnlib/ * stringhelp.c (utf8_charcount): New. agent/ * genkey.c (check_passphrase_constraints): Use UTF-8 aware strlen. --- jnlib/stringhelp.c | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'jnlib/stringhelp.c') diff --git a/jnlib/stringhelp.c b/jnlib/stringhelp.c index 1731dd946..b74f84a9c 100644 --- a/jnlib/stringhelp.c +++ b/jnlib/stringhelp.c @@ -547,6 +547,23 @@ sanitize_buffer (const void *p_arg, size_t n, int delim) } +/* Given a string containing an UTF-8 encoded text, return the number + of characters in this string. It differs from strlen in that it + only counts complete UTF-8 characters. Note, that this function + does not take combined characters into account. */ +size_t +utf8_charcount (const char *s) +{ + size_t n; + + for (n=0; *s; s++) + if ( (*s&0xc0) != 0x80 ) /* Exclude continuation bytes: 10xxxxxx */ + n++; + + return n; +} + + /**************************************************** ********** W32 specific functions **************** ****************************************************/ -- cgit