From 8f6712fc64a6d02770442f0c9c2142a8c4e38b9c Mon Sep 17 00:00:00 2001 From: Peter Verthez Date: Sat, 1 Feb 2003 16:50:51 +0000 Subject: [PATCH] New functions next_utf8_char and nth_utf8_char. --- utf8/ChangeLog | 4 ++++ utf8/utf8.c | 31 +++++++++++++++++++++++++++++-- utf8/utf8tools.h | 6 ++++++ 3 files changed, 39 insertions(+), 2 deletions(-) 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); -- 2.30.2