summaryrefslogtreecommitdiff
path: root/otk/ustring.cc
diff options
context:
space:
mode:
authorDana Jansens <danakj@orodu.net>2003-01-13 04:46:50 +0000
committerDana Jansens <danakj@orodu.net>2003-01-13 04:46:50 +0000
commit5a90d2b671f01f29043ab82f909440de0abfa362 (patch)
tree9f784a0c35d5594409b92d28f08404cb4e55f386 /otk/ustring.cc
parent4947902d269213edee40f3f31f97721fa0dd3877 (diff)
otk using ustring for shit that draws. expended its inferface yet some more.
Diffstat (limited to 'otk/ustring.cc')
-rw-r--r--otk/ustring.cc104
1 files changed, 91 insertions, 13 deletions
diff --git a/otk/ustring.cc b/otk/ustring.cc
index a04c08cf..8f3cdfc4 100644
--- a/otk/ustring.cc
+++ b/otk/ustring.cc
@@ -12,6 +12,63 @@ extern "C" {
namespace otk {
+// helper functions
+
+static ustring::size_type utf8_find_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];
+ }
+
+ return offset;
+}
+
+// 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 += g_utf8_skip[*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 += g_utf8_skip[*p];
+ }
+
+ return (p - str);
+}
+
+
+// ustring methods
+
ustring::ustring()
{
}
@@ -61,23 +118,11 @@ ustring& ustring::operator+=(char c)
return *this;
}
-static ustring::size_type find_utf8_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];
- }
-
- return offset;
-}
-
ustring::size_type ustring::size() const
{
if (_utf8) {
const char *const pdata = _string.data();
- return find_utf8_offset(pdata, pdata + _string.size());
+ return utf8_find_offset(pdata, pdata + _string.size());
} else
return _string.size();
}
@@ -97,6 +142,39 @@ ustring::size_type ustring::max_size() const
return _string.max_size();
}
+void ustring::clear()
+{
+ _string.erase();
+}
+
+ustring& ustring::erase(ustring::size_type i, ustring::size_type n)
+{
+ if (_utf8) {
+ // find a proper offset
+ size_type utf_i = utf8_byte_offset(_string.c_str(), i);
+ if (utf_i != npos) {
+ // if the offset is not npos, find a proper length for 'n'
+ size_type utf_n = utf8_byte_offset(_string.data() + utf_i, n,
+ _string.size() - utf_i);
+ _string.erase(utf_i, utf_n);
+ }
+ } else
+ _string.erase(i, n);
+
+ return *this;
+}
+
+void ustring::resize(ustring::size_type n, char c)
+{
+ if (_utf8) {
+ const size_type size_now = size();
+ if(n < size_now)
+ erase(n, npos);
+ else if(n > size_now)
+ _string.append(n - size_now, c);
+ } else
+ _string.resize(n, c);
+}
const char* ustring::data() const
{