]> icculus.org git repositories - mikachu/openbox.git/blob - otk/ustring.cc
more conversion to ustring. added more members
[mikachu/openbox.git] / otk / ustring.cc
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2
3 #ifdef    HAVE_CONFIG_H
4 #  include "../config.h"
5 #endif // HAVE_CONFIG_H
6
7 #include "ustring.hh"
8
9 extern "C" {
10 #include <assert.h>
11 }
12
13 namespace otk {
14
15 ustring::ustring()
16 {
17 }
18
19 ustring::~ustring()
20 {
21 }
22
23 ustring::ustring(const ustring& other)
24   : _string(other._string), _utf8(other._utf8)
25 {
26 }
27
28 ustring& ustring::operator=(const ustring& other)
29 {
30   _string = other._string;
31   _utf8 = other._utf8;
32   return *this;
33 }
34
35 ustring::ustring(const std::string& src)
36   : _string(src), _utf8(true)
37 {
38 }
39
40 ustring::ustring(const char* src)
41   : _string(src), _utf8(true)
42 {
43 }
44
45 ustring& ustring::operator+=(const ustring& src)
46 {
47   assert(_utf8 == src._utf8);
48   _string += src._string;
49   return *this;
50 }
51
52 ustring& ustring::operator+=(const char* src)
53 {
54   _string += src;
55   return *this;
56 }
57
58 ustring& ustring::operator+=(char c)
59 {
60   _string += c;
61   return *this;
62 }
63
64 static ustring::size_type find_utf8_offset(const char *str, const char *pos)
65 {
66   ustring::size_type offset = 0;
67
68   while (str < pos) {
69     str += g_utf8_skip[*str];
70     offset += g_utf8_skip[*str];
71   }
72
73   return offset;
74 }
75
76 ustring::size_type ustring::size() const
77 {
78   if (_utf8) {
79     const char *const pdata = _string.data();
80     return find_utf8_offset(pdata, pdata + _string.size());
81   } else
82     return _string.size();
83 }
84
85 ustring::size_type ustring::bytes() const
86 {
87   return _string.size();
88 }
89
90 ustring::size_type ustring::capacity() const
91 {
92   return _string.capacity();
93 }
94
95 ustring::size_type ustring::max_size() const
96 {
97   return _string.max_size();
98 }
99
100
101 const char* ustring::data() const
102 {
103   return _string.data();
104 }
105
106 const char* ustring::c_str() const
107 {
108   return _string.c_str();
109 }
110
111 bool ustring::utf8() const
112 {
113   return _utf8;
114 }
115
116 void ustring::setUtf8(bool utf8)
117 {
118   _utf8 = utf8;
119 }
120
121 }