From: Peter Verthez Date: Sat, 1 Feb 2003 16:50:51 +0000 (+0000) Subject: New functions next_utf8_char and nth_utf8_char. X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=8f6712fc64a6d02770442f0c9c2142a8c4e38b9c;p=gedcom-parse.git New functions next_utf8_char and nth_utf8_char. --- diff --git a/utf8/ChangeLog b/utf8/ChangeLog index 3420dab..d23aad3 100644 --- a/utf8/ChangeLog +++ b/utf8/ChangeLog @@ -1,3 +1,7 @@ +2003-02-01 Peter Verthez + + * utf8tools.h, utf8.c: New functions next_utf8_char and nth_utf8_char. + 2002-12-30 Peter Verthez * release 0.1.0 diff --git a/utf8/utf8.c b/utf8/utf8.c index e3809b9..6c5f5be 100644 --- a/utf8/utf8.c +++ b/utf8/utf8.c @@ -21,6 +21,7 @@ /* $Name$ */ #include "utf8tools.h" +#include int is_utf8_string(const char* str) { @@ -78,11 +79,37 @@ int utf8_strlen(const char* str) if (!str) return 0; while (*str) { - if ((*str & 0x80) == 0 || (*str & 0xC0) == 0xC0) - num_char++; + if ((*str & 0xC0) != 0xC0) num_char++; str++; } return num_char; } +char* next_utf8_char(char* str) +{ + if (!str) return NULL; + + if (*str) { + str++; + while (*str && (*str & 0xC0) == 0x80) + str++; + } + return str; +} + +char* nth_utf8_char(char* str, int n) +{ + int num_char = 0; + if (!str) return NULL; + + if (*str) { + str++; + while (*str) { + if ((*str & 0xC0) != 0x80) num_char++; + if (num_char == n) break; + str++; + } + } + return str; +} diff --git a/utf8/utf8tools.h b/utf8/utf8tools.h index 0eca7da..d2e4e98 100644 --- a/utf8/utf8tools.h +++ b/utf8/utf8tools.h @@ -41,6 +41,12 @@ int utf8_strlen(const char* input); /* Returns 1 if string is valid UTF-8 string, 0 otherwise */ int is_utf8_string(const char* input); + /* Returns respectively a pointer to the next or the nth UTF-8 character. + The value n = 0 is the first character of the input, i.e. + next_utf8_char(input) is the same as nth_utf8_char(input, 1) */ +char* next_utf8_char(char* input); +char* nth_utf8_char(char* input, int n); + /* Functions for creating and freeing conversion buffers yourself */ conv_buffer_t create_conv_buffer(int size); void free_conv_buffer(conv_buffer_t buf);