From 129bd2f44410c602da5fc8374083dec390d99e3e Mon Sep 17 00:00:00 2001 From: Pavol Babincak Date: Fri, 7 Apr 2006 22:06:17 +0200 Subject: [PATCH] Added function utf8_ptr2chars for counting number of characters in string. --- src/intl/charsets.c | 24 ++++++++++++++++++++++++ src/intl/charsets.h | 1 + 2 files changed, 25 insertions(+) diff --git a/src/intl/charsets.c b/src/intl/charsets.c index 085b2d4f1..52c9dfd04 100644 --- a/src/intl/charsets.c +++ b/src/intl/charsets.c @@ -299,6 +299,30 @@ utf8_ptr2cells(unsigned char *string, unsigned char *end) return cells; } +/* Count number of characters in string. */ +int +utf8_ptr2chars(unsigned char *string, unsigned char *end) +{ + int charlen, chars = 0; + + if (end == NULL) + end = strchr(string, '\0'); + + if(!string || !end) + return -1; + + do { + charlen = utf8charlen(string); + if (string + charlen > end) + break; + + chars++; + string += charlen; + } while (1); + + return chars; +} + /* * Count number of bytes from begining of the string needed for displaying * specified number of cells. diff --git a/src/intl/charsets.h b/src/intl/charsets.h index 5a26d43a7..2b5380b3f 100644 --- a/src/intl/charsets.h +++ b/src/intl/charsets.h @@ -58,6 +58,7 @@ inline unsigned char *encode_utf_8(unicode_val_T); inline int utf8charlen(const unsigned char *); int utf8_char2cells(unsigned char *, unsigned char *); int utf8_ptr2cells(unsigned char *, unsigned char *); +int utf8_ptr2chars(unsigned char *, unsigned char *); int utf8_cells2bytes(unsigned char *, int, unsigned char *); inline int unicode_to_cell(unicode_val_T); inline int strlen_utf8(unsigned char **);