From 7fa93ebe9ed7be18a6dba6174020434759db691b Mon Sep 17 00:00:00 2001 From: Emmanuele Bassi Date: Thu, 11 Dec 2008 11:47:06 +0000 Subject: [PATCH] Replace offset_to_bytes() implementation We should re-use the offset_to_bytes() implementation from ClutterEntry as it guaranteed some behaviour and sanity checks that we want to keep inside ClutterText. --- clutter/clutter-text.c | 46 +++++++++++++++++++++++++++++++++++++++------- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/clutter/clutter-text.c b/clutter/clutter-text.c index 1fdb61c..1c61035 100644 --- a/clutter/clutter-text.c +++ b/clutter/clutter-text.c @@ -182,15 +182,47 @@ enum static guint text_signals[LAST_SIGNAL] = { 0, }; -#define offset_real(text, pos) \ - (pos == -1 ? g_utf8_strlen (text, -1) : pos) +#define offset_real(t,p) \ + ((p) == -1 ? g_utf8_strlen ((t), -1) : (p)) -#define offset_to_bytes(text,pos) \ - (pos == -1 ? strlen (text) \ - : ((gint) (g_utf8_offset_to_pointer (text, pos) - text))) +static gint +offset_to_bytes (const gchar *text, + gint pos) +{ + gchar *c = NULL; + gint i, j, len; + + if (pos < 0) + return strlen (text); + +#if 0 + if (pos < 1) + return pos; +#endif + + c = g_utf8_next_char (text); + j = 1; + len = strlen (text); + + for (i = 0; i < len; i++) + { + if (&text[i] == c) + { + if (j == pos) + break; + else + { + c = g_utf8_next_char (c); + j++; + } + } + } + + return i; +} -#define bytes_to_offset(text, pos) \ - (g_utf8_pointer_to_offset (text, text + pos)) +#define bytes_to_offset(t,p) \ + (g_utf8_pointer_to_offset ((t), (t) + (p))) typedef struct TextCommand { -- 2.7.4