From 9d1008c523a56e27c0f568de1a63a649d01a006e Mon Sep 17 00:00:00 2001 From: Pavol Babincak Date: Mon, 1 May 2006 22:58:51 +0200 Subject: [PATCH] Added utf8_prevchar for moving throught UTF-8 string to left. --- src/intl/charsets.c | 18 ++++++++++++++++++ src/intl/charsets.h | 1 + 2 files changed, 19 insertions(+) diff --git a/src/intl/charsets.c b/src/intl/charsets.c index 52c9dfd04..35c6ce8d3 100644 --- a/src/intl/charsets.c +++ b/src/intl/charsets.c @@ -252,6 +252,24 @@ strlen_utf8(unsigned char **str) return x; } +#define utf8_issingle(p) (((p) & 0x80) == 0) +#define utf8_islead(p) (utf8_issingle(p) || ((p) & 0xc0) == 0xc0) + +/* Start from @current and move back to @pos char. This pointer return. The + * most left pointer is @start. */ +inline unsigned char * +utf8_prevchar(unsigned char *current, int pos, unsigned char *start) +{ + if (current == NULL || start == NULL || pos < 0) + return NULL; + while (pos > 0 && current != start) { + current--; + if (utf8_islead(*current)) + pos--; + } + return current; +} + /* Count number of standard terminal cells needed for displaying UTF-8 * character. */ int diff --git a/src/intl/charsets.h b/src/intl/charsets.h index 2b5380b3f..04e6a0f68 100644 --- a/src/intl/charsets.h +++ b/src/intl/charsets.h @@ -55,6 +55,7 @@ int is_cp_special(int); void free_conv_table(void); #ifdef CONFIG_UTF_8 inline unsigned char *encode_utf_8(unicode_val_T); +inline unsigned char *utf8_prevchar(unsigned char *, int, unsigned char *); inline int utf8charlen(const unsigned char *); int utf8_char2cells(unsigned char *, unsigned char *); int utf8_ptr2cells(unsigned char *, unsigned char *);