// -*- 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 <assert.h>
-}
+#include <cassert>
namespace otk {
// 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)
ustring::size_type offset = 0;
while (str < pos) {
- str += utf8_skip[*str];
+ str += utf8_skip[static_cast<unsigned char>(*str)];
offset++;
}
const char *utf8_offset_to_ptr(const char *str, ustring::size_type offset)
{
while (offset--)
- str += utf8_skip[*str];
+ str += utf8_skip[static_cast<unsigned char>(*str)];
return str;
}
if(*p == '\0')
return ustring::npos;
- p += utf8_skip[*p];
+ p += utf8_skip[static_cast<unsigned char>(*p)];
}
return (p - str);
if(p >= pend)
return ustring::npos;
- p += utf8_skip[*p];
+ p += utf8_skip[static_cast<unsigned char>(*p)];
}
return (p - str);
// ustring methods
-ustring::ustring()
+ustring::ustring(bool utf8)
+ : _utf8(utf8)
{
}
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)
{
}
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();