X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=otk%2Fustring.cc;h=8ffe07fe000ba7c5febf3639b1fd7cf3f7151633;hb=1e58c863bbaddd2f2dbebfde740ca842e8837a1c;hp=8f3cdfc452fae6bfdc2c4af2f716da9c769ff9d0;hpb=5a90d2b671f01f29043ab82f909440de0abfa362;p=chaz%2Fopenbox diff --git a/otk/ustring.cc b/otk/ustring.cc index 8f3cdfc4..8ffe07fe 100644 --- a/otk/ustring.cc +++ b/otk/ustring.cc @@ -1,31 +1,69 @@ // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*- -#ifdef HAVE_CONFIG_H -# include "../config.h" -#endif // HAVE_CONFIG_H +#include "config.h" #include "ustring.hh" -extern "C" { -#include -} +#include namespace otk { // helper functions -static ustring::size_type utf8_find_offset(const char *str, const char *pos) +// 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(*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(*++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[static_cast(*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(*str)]; + return str; +} + // First overload: stop on '\0' character. ustring::size_type utf8_byte_offset(const char* str, ustring::size_type offset) { @@ -39,7 +77,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[static_cast(*p)]; } return (p - str); @@ -60,7 +98,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[static_cast(*p)]; } return (p - str); @@ -69,7 +107,8 @@ ustring::size_type utf8_byte_offset(const char* str, ustring::size_type offset, // ustring methods -ustring::ustring() +ustring::ustring(bool utf8) + : _utf8(utf8) { } @@ -89,13 +128,13 @@ ustring& ustring::operator=(const ustring& other) return *this; } -ustring::ustring(const std::string& src) - : _string(src), _utf8(true) +ustring::ustring(const std::string& src, bool utf8) + : _string(src), _utf8(utf8) { } -ustring::ustring(const char* src) - : _string(src), _utf8(true) +ustring::ustring(const char* src, bool utf8) + : _string(src), _utf8(utf8) { } @@ -122,7 +161,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(); } @@ -142,6 +181,11 @@ ustring::size_type ustring::max_size() const return _string.max_size(); } +bool ustring::empty() const +{ + return _string.empty(); +} + void ustring::clear() { _string.erase(); @@ -176,6 +220,26 @@ 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)); +} + +bool ustring::operator==(const ustring &other) const +{ + return _string == other._string && _utf8 == other._utf8; +} + +bool ustring::operator==(const std::string &other) const +{ + return _string == other; +} + +bool ustring::operator==(const char *other) const +{ + return _string == other; +} + const char* ustring::data() const { return _string.data();