]> icculus.org git repositories - mikachu/openbox.git/blob - otk/ustring.hh
more shit
[mikachu/openbox.git] / otk / ustring.hh
1 // -*- mode: C++; indent-tabs-mode: nil; c-basic-offset: 2; -*-
2 #ifndef   __ustring_hh
3 #define   __ustring_hh
4
5 /*! @file ustring.hh
6   @brief Provides a simple UTF-8 encoded string
7 */
8
9 extern "C" {
10 #ifdef HAVE_STDINT_H
11 #  include <stdint.h>
12 #else
13 #  ifdef HAVE_SYS_TYPES_H
14 #    include <sys/types.h>
15 #  endif
16 #endif
17 }
18
19 #include <string>
20
21 namespace otk {
22
23 //! The number of bytes to skip to find the next character in the string
24 const char g_utf8_skip[256] = {
25   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
26   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
27   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
28   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
29   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
30   1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,
31   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,
32   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
33 };
34
35 #ifdef HAVE_STDINT_H
36 typedef uint32_t unichar;
37 #else
38 typedef u_int32_t unichar;
39 #endif
40
41 //! The iterator type for ustring
42 /*!
43   Note this is not a random access iterator but a bidirectional one, since all
44   index operations need to iterate over the UTF-8 data.  Use std::advance() to
45   move to a certain position.
46   <p>
47   A writeable iterator isn't provided because:  The number of bytes of the old
48   UTF-8 character and the new one to write could be different. Therefore, any
49   write operation would invalidate all other iterators pointing into the same
50   string.
51 */
52 template <class T>
53 class ustring_Iterator
54 {
55 public:
56   typedef std::bidirectional_iterator_tag   iterator_category;
57   typedef unichar                           value_type;
58   typedef std::string::difference_type      difference_type;
59   typedef value_type                        reference;
60   typedef void                              pointer;
61
62   inline ustring_Iterator() {}
63   inline ustring_Iterator(const ustring_Iterator<std::string::iterator>&
64                           other) : _pos(other.base()) {}
65
66   inline value_type operator*() const {
67     // get a unicode character from the iterator's position
68
69     // get an iterator to the internal string
70     std::string::const_iterator pos = _pos;
71     
72     unichar result = static_cast<unsigned char>(*pos);
73
74     // if its not a 7-bit ascii character
75     if((result & 0x80) != 0) {
76       // len is the number of bytes this character takes up in the string
77       unsigned char len = g_utf8_skip[result];
78       result &= 0x7F >> len;
79       
80       while(--len != 0) {
81         result <<= 6;
82         result |= static_cast<unsigned char>(*++pos) & 0x3F;
83       }
84     }
85     
86     return result;
87   }
88
89   inline ustring_Iterator<T> &     operator++() {
90     pos_ += g_utf8_skip[static_cast<unsigned char>(*pos_)];
91     return *this;
92   }
93   inline ustring_Iterator<T> &     operator--() {
94     do { --_pos; } while((*_pos & '\xC0') == '\x80');
95     return *this;
96   }
97
98   explicit inline ustring_Iterator(T pos) : _pos(pos) {}
99   inline T base() const { return _pos; }
100
101 private:
102   T _pos;
103 };
104
105 //! This class provides a simple wrapper to a std::string that is encoded as
106 //! UTF-8.
107 /*!
108   This class does <b>not</b> handle extended 8-bit ASCII charsets like
109   ISO-8859-1.
110   <p>
111   More info on Unicode and UTF-8 can be found here:
112   http://www.cl.cam.ac.uk/~mgk25/unicode.html
113   <p>
114   This does not subclass std::string, because std::string was intended to be a
115   final class.  For instance, it does not have a virtual destructor.
116 */
117 class ustring {
118   std::string _string;
119
120 public:
121   typedef std::string::size_type                        size_type;
122   typedef std::string::difference_type                  difference_type;
123
124   typedef unichar                                       value_type;
125   typedef unichar &                                     reference;
126   typedef const unichar &                               const_reference;
127
128   typedef ustring_Iterator<std::string::iterator>       iterator;
129   typedef ustring_Iterator<std::string::const_iterator> const_iterator;
130
131   static const size_type npos = std::string::npos;
132
133   ustring();
134   ~ustring();
135
136   // make new strings
137   
138   ustring(const ustring& other);
139   ustring& operator=(const ustring& other);
140   ustring(const std::string& src);
141   ustring::ustring(const char* src);
142
143   
144 };
145
146 }
147
148 #endif // __ustring_hh