diff options
author | Neal H. Walfield <[email protected]> | 2015-11-23 21:13:56 +0000 |
---|---|---|
committer | Neal H. Walfield <[email protected]> | 2015-11-23 21:23:37 +0000 |
commit | 5b84b0d660c8329e184d98682665aaea7e1703d2 (patch) | |
tree | 7eb86d1423780c332b27daa9226e1144e451bcb3 /common/stringhelp.c | |
parent | dirmngr: Fix http lookups when libadns is used. (diff) | |
download | gnupg-5b84b0d660c8329e184d98682665aaea7e1703d2.tar.gz gnupg-5b84b0d660c8329e184d98682665aaea7e1703d2.zip |
common: Extend utf8_charcount to include the string's length.
* common/stringhelp.c (utf8_charcount): Take additional parameter,
len. Process at most LEN bytes.
--
Signed-off-by: Neal H. Walfield <[email protected]>
Diffstat (limited to 'common/stringhelp.c')
-rw-r--r-- | common/stringhelp.c | 24 |
1 files changed, 19 insertions, 5 deletions
diff --git a/common/stringhelp.c b/common/stringhelp.c index 38c383212..d0b55615d 100644 --- a/common/stringhelp.c +++ b/common/stringhelp.c @@ -746,16 +746,30 @@ 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. */ + only counts complete UTF-8 characters. SIZE is the maximum length + of the string in bytes. If SIZE is -1, then a NUL character is + taken to be the end of the string. Note, that this function does + not take combined characters into account. */ size_t -utf8_charcount (const char *s) +utf8_charcount (const char *s, int len) { size_t n; + if (len == 0) + return 0; + for (n=0; *s; s++) - if ( (*s&0xc0) != 0x80 ) /* Exclude continuation bytes: 10xxxxxx */ - n++; + { + if ( (*s&0xc0) != 0x80 ) /* Exclude continuation bytes: 10xxxxxx */ + n++; + + if (len != -1) + { + len --; + if (len == 0) + break; + } + } return n; } |