? g_utf8_offset_to_pointer-optimize.patch Index: glib/gutf8.c =================================================================== RCS file: /cvs/gnome/glib/glib/gutf8.c,v retrieving revision 1.43.2.2 diff -u -p -r1.43.2.2 gutf8.c --- glib/gutf8.c 30 Oct 2005 03:07:24 -0000 1.43.2.2 +++ glib/gutf8.c 3 Nov 2005 00:15:33 -0000 @@ -290,11 +290,51 @@ gchar * g_utf8_offset_to_pointer (const gchar *str, glong offset) { - const gchar *s = str; - while (offset--) - s = g_utf8_next_char (s); - - return (gchar *)s; + union + { + const guint32 *p32; + const gchar *p8; + } + pt; + + pt.p8 = str; + + while (offset >= 4) + { + guint32 seg = *pt.p32; + guint32 seg_hi = seg & 0x80808080; + + if (!seg_hi) + { + pt.p32++; + offset -= 4; + continue; + } + else if G_LIKELY (seg_hi == 0x80808080) + { + if ((seg & 0x80e080e0) == 0x80c080c0) + { + pt.p32++; + offset -= 2; + continue; + } + + if ((seg & 0xf08080f0) == 0xe08080e0) + { + pt.p8 += 6; + offset -= 2; + continue; + } + } + + pt.p8 += g_utf8_skip [(guchar) seg]; + offset--; + } + + for ( ; offset; offset--) + pt.p8 = g_utf8_next_char (pt.p8); + + return (gchar *) pt.p8; } /**