+// helper functions
+
+// The number of bytes to skip to find the next character in the string
+static const char utf8_skip[256] = {
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
+ 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
+ 3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,3,4,4,4,4,4,4,4,4,5,5,5,5,6,6,1,1
+};
+
+// takes a pointer into a utf8 string and returns a unicode character for the
+// first character at the pointer
+unichar utf8_get_char (const char *p)
+{
+ unichar result = static_cast<unsigned char>(*p);
+
+ // if its not a 7-bit ascii character
+ if((result & 0x80) != 0) {
+ // len is the number of bytes this character takes up in the string
+ unsigned char len = utf8_skip[result];
+ result &= 0x7F >> len;
+
+ while(--len != 0) {
+ result <<= 6;
+ result |= static_cast<unsigned char>(*++p) & 0x3F;
+ }
+ }
+
+ return result;
+}
+
+// takes a pointer into a string and finds its offset
+static ustring::size_type utf8_ptr_to_offset(const char *str, const char *pos)
+{
+ ustring::size_type offset = 0;
+
+ while (str < pos) {
+ str += utf8_skip[static_cast<unsigned char>(*str)];
+ offset++;
+ }
+
+ return offset;
+}
+
+// takes an offset into a string and returns a pointer to it
+const char *utf8_offset_to_ptr(const char *str, ustring::size_type offset)
+{
+ while (offset--)
+ str += utf8_skip[static_cast<unsigned char>(*str)];
+ return str;
+}
+
+// First overload: stop on '\0' character.
+ustring::size_type utf8_byte_offset(const char* str, ustring::size_type offset)
+{
+ if(offset == ustring::npos)
+ return ustring::npos;
+
+ const char* p = str;
+
+ for(; offset != 0; --offset)
+ {
+ if(*p == '\0')
+ return ustring::npos;
+
+ p += utf8_skip[static_cast<unsigned char>(*p)];
+ }
+
+ return (p - str);
+}
+
+// Second overload: stop when reaching maxlen.
+ustring::size_type utf8_byte_offset(const char* str, ustring::size_type offset,
+ ustring::size_type maxlen)
+{
+ if(offset == ustring::npos)
+ return ustring::npos;
+
+ const char *const pend = str + maxlen;
+ const char* p = str;
+
+ for(; offset != 0; --offset)
+ {
+ if(p >= pend)
+ return ustring::npos;
+
+ p += utf8_skip[static_cast<unsigned char>(*p)];
+ }
+
+ return (p - str);
+}
+
+
+// ustring methods
+
+ustring::ustring(bool utf8)
+ : _utf8(utf8)