summaryrefslogtreecommitdiff
path: root/otk/ustring.cc
diff options
context:
space:
mode:
authorDana Jansens <danakj@orodu.net>2003-01-13 07:12:08 +0000
committerDana Jansens <danakj@orodu.net>2003-01-13 07:12:08 +0000
commit55f138186097931f3bf81618e36d2ad53e0c07fd (patch)
tree6eb2cd1018b8647a3c75cd97439f5e5bfb902b29 /otk/ustring.cc
parentef02a0c8ae65f169157c90064a335303e964a4c1 (diff)
ustring seems to be working! yay!
Diffstat (limited to 'otk/ustring.cc')
-rw-r--r--otk/ustring.cc47
1 files changed, 41 insertions, 6 deletions
diff --git a/otk/ustring.cc b/otk/ustring.cc
index c15effb0..b721089e 100644
--- a/otk/ustring.cc
+++ b/otk/ustring.cc
@@ -14,18 +14,48 @@ namespace otk {
// helper functions
-static ustring::size_type utf8_find_offset(const char *str, const char *pos)
+// 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 += g_utf8_skip[*str];
- offset += g_utf8_skip[*str];
+ str += utf8_skip[*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[*str];
+ return str;
+}
+
// First overload: stop on '\0' character.
ustring::size_type utf8_byte_offset(const char* str, ustring::size_type offset)
{
@@ -39,7 +69,7 @@ ustring::size_type utf8_byte_offset(const char* str, ustring::size_type offset)
if(*p == '\0')
return ustring::npos;
- p += g_utf8_skip[*p];
+ p += utf8_skip[*p];
}
return (p - str);
@@ -60,7 +90,7 @@ ustring::size_type utf8_byte_offset(const char* str, ustring::size_type offset,
if(p >= pend)
return ustring::npos;
- p += g_utf8_skip[*p];
+ p += utf8_skip[*p];
}
return (p - str);
@@ -122,7 +152,7 @@ ustring::size_type ustring::size() const
{
if (_utf8) {
const char *const pdata = _string.data();
- return utf8_find_offset(pdata, pdata + _string.size());
+ return utf8_ptr_to_offset(pdata, pdata + _string.size());
} else
return _string.size();
}
@@ -181,6 +211,11 @@ void ustring::resize(ustring::size_type n, char c)
_string.resize(n, c);
}
+ustring::value_type ustring::operator[](ustring::size_type i) const
+{
+ return utf8_get_char(utf8_offset_to_ptr(_string.data(), i));
+}
+
const char* ustring::data() const
{
return _string.data();