~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to drizzled/utf8/unchecked.h

  • Committer: Brian Aker
  • Date: 2010-10-10 02:00:34 UTC
  • mfrom: (1830.1.5 trunk-drizzle)
  • Revision ID: brian@tangent.org-20101010020034-d67x3d09fssxq1v6
Merge rollup of utf8 and table encapsulation.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// Copyright 2006 Nemanja Trifunovic
 
2
 
 
3
/*
 
4
Permission is hereby granted, free of charge, to any person or organization
 
5
obtaining a copy of the software and accompanying documentation covered by
 
6
this license (the "Software") to use, reproduce, display, distribute,
 
7
execute, and transmit the Software, and to prepare derivative works of the
 
8
Software, and to permit third-parties to whom the Software is furnished to
 
9
do so, all subject to the following:
 
10
 
 
11
The copyright notices in the Software and this entire statement, including
 
12
the above license grant, this restriction and the following disclaimer,
 
13
must be included in all copies of the Software, in whole or in part, and
 
14
all derivative works of the Software, unless such copies or derivative
 
15
works are solely in the form of machine-executable object code generated by
 
16
a source language processor.
 
17
 
 
18
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 
19
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 
20
FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
 
21
SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
 
22
FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
 
23
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 
24
DEALINGS IN THE SOFTWARE.
 
25
*/
 
26
 
 
27
 
 
28
#ifndef DRIZZLED_UTF8_UNCHECKED_H
 
29
#define DRIZZLED_UTF8_UNCHECKED_H
 
30
 
 
31
#include "drizzled/utf8/core.h"
 
32
 
 
33
namespace drizzled
 
34
{
 
35
namespace utf8
 
36
{
 
37
    namespace unchecked 
 
38
    {
 
39
        template <typename octet_iterator>
 
40
        octet_iterator append(uint32_t cp, octet_iterator result)
 
41
        {
 
42
            if (cp < 0x80)                        // one octet
 
43
                *(result++) = static_cast<uint8_t>(cp);  
 
44
            else if (cp < 0x800) {                // two octets
 
45
                *(result++) = static_cast<uint8_t>((cp >> 6)          | 0xc0);
 
46
                *(result++) = static_cast<uint8_t>((cp & 0x3f)        | 0x80);
 
47
            }
 
48
            else if (cp < 0x10000) {              // three octets
 
49
                *(result++) = static_cast<uint8_t>((cp >> 12)         | 0xe0);
 
50
                *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
 
51
                *(result++) = static_cast<uint8_t>((cp & 0x3f)        | 0x80);
 
52
            }
 
53
            else {                                // four octets
 
54
                *(result++) = static_cast<uint8_t>((cp >> 18)         | 0xf0);
 
55
                *(result++) = static_cast<uint8_t>(((cp >> 12) & 0x3f)| 0x80);
 
56
                *(result++) = static_cast<uint8_t>(((cp >> 6) & 0x3f) | 0x80);
 
57
                *(result++) = static_cast<uint8_t>((cp & 0x3f)        | 0x80);
 
58
            }
 
59
            return result;
 
60
        }
 
61
 
 
62
        template <typename octet_iterator>
 
63
        uint32_t next(octet_iterator& it)
 
64
        {
 
65
            uint32_t cp = internal::mask8(*it);
 
66
            typename std::iterator_traits<octet_iterator>::difference_type length = utf8::internal::sequence_length(it);
 
67
            switch (length) {
 
68
                case 1:
 
69
                    break;
 
70
                case 2:
 
71
                    it++;
 
72
                    cp = ((cp << 6) & 0x7ff) + ((*it) & 0x3f);
 
73
                    break;
 
74
                case 3:
 
75
                    ++it; 
 
76
                    cp = ((cp << 12) & 0xffff) + ((internal::mask8(*it) << 6) & 0xfff);
 
77
                    ++it;
 
78
                    cp += (*it) & 0x3f;
 
79
                    break;
 
80
                case 4:
 
81
                    ++it;
 
82
                    cp = ((cp << 18) & 0x1fffff) + ((internal::mask8(*it) << 12) & 0x3ffff);                
 
83
                    ++it;
 
84
                    cp += (internal::mask8(*it) << 6) & 0xfff;
 
85
                    ++it;
 
86
                    cp += (*it) & 0x3f; 
 
87
                    break;
 
88
            }
 
89
            ++it;
 
90
            return cp;        
 
91
        }
 
92
 
 
93
        template <typename octet_iterator>
 
94
        uint32_t peek_next(octet_iterator it)
 
95
        {
 
96
            return next(it);    
 
97
        }
 
98
 
 
99
        template <typename octet_iterator>
 
100
        uint32_t prior(octet_iterator& it)
 
101
        {
 
102
            while (internal::is_trail(*(--it))) ;
 
103
            octet_iterator temp = it;
 
104
            return next(temp);
 
105
        }
 
106
 
 
107
        // Deprecated in versions that include prior, but only for the sake of consistency (see utf8::previous)
 
108
        template <typename octet_iterator>
 
109
        inline uint32_t previous(octet_iterator& it)
 
110
        {
 
111
            return prior(it);
 
112
        }
 
113
 
 
114
        template <typename octet_iterator, typename distance_type>
 
115
        void advance (octet_iterator& it, distance_type n)
 
116
        {
 
117
            for (distance_type i = 0; i < n; ++i)
 
118
                next(it);
 
119
        }
 
120
 
 
121
        template <typename octet_iterator>
 
122
        typename std::iterator_traits<octet_iterator>::difference_type
 
123
        distance (octet_iterator first, octet_iterator last)
 
124
        {
 
125
            typename std::iterator_traits<octet_iterator>::difference_type dist;
 
126
            for (dist = 0; first < last; ++dist) 
 
127
                next(first);
 
128
            return dist;
 
129
        }
 
130
 
 
131
        template <typename u16bit_iterator, typename octet_iterator>
 
132
        octet_iterator utf16to8 (u16bit_iterator start, u16bit_iterator end, octet_iterator result)
 
133
        {       
 
134
            while (start != end) {
 
135
                uint32_t cp = internal::mask16(*start++);
 
136
            // Take care of surrogate pairs first
 
137
                if (internal::is_lead_surrogate(cp)) {
 
138
                    uint32_t trail_surrogate = internal::mask16(*start++);
 
139
                    cp = (cp << 10) + trail_surrogate + internal::SURROGATE_OFFSET;
 
140
                }
 
141
                result = append(cp, result);
 
142
            }
 
143
            return result;         
 
144
        }
 
145
 
 
146
        template <typename u16bit_iterator, typename octet_iterator>
 
147
        u16bit_iterator utf8to16 (octet_iterator start, octet_iterator end, u16bit_iterator result)
 
148
        {
 
149
            while (start < end) {
 
150
                uint32_t cp = next(start);
 
151
                if (cp > 0xffff) { //make a surrogate pair
 
152
                    *result++ = static_cast<uint16_t>((cp >> 10)   + internal::LEAD_OFFSET);
 
153
                    *result++ = static_cast<uint16_t>((cp & 0x3ff) + internal::TRAIL_SURROGATE_MIN);
 
154
                }
 
155
                else
 
156
                    *result++ = static_cast<uint16_t>(cp);
 
157
            }
 
158
            return result;
 
159
        }
 
160
 
 
161
        template <typename octet_iterator, typename u32bit_iterator>
 
162
        octet_iterator utf32to8 (u32bit_iterator start, u32bit_iterator end, octet_iterator result)
 
163
        {
 
164
            while (start != end)
 
165
                result = append(*(start++), result);
 
166
 
 
167
            return result;
 
168
        }
 
169
 
 
170
        template <typename octet_iterator, typename u32bit_iterator>
 
171
        u32bit_iterator utf8to32 (octet_iterator start, octet_iterator end, u32bit_iterator result)
 
172
        {
 
173
            while (start < end)
 
174
                (*result++) = next(start);
 
175
 
 
176
            return result;
 
177
        }
 
178
 
 
179
        // The iterator class
 
180
        template <typename octet_iterator>
 
181
          class iterator : public std::iterator <std::bidirectional_iterator_tag, uint32_t> { 
 
182
            octet_iterator it;
 
183
            public:
 
184
            iterator () {};
 
185
            explicit iterator (const octet_iterator& octet_it): it(octet_it) {}
 
186
            // the default "big three" are OK
 
187
            octet_iterator base () const { return it; }
 
188
            uint32_t operator * () const
 
189
            {
 
190
                octet_iterator temp = it;
 
191
                return next(temp);
 
192
            }
 
193
            bool operator == (const iterator& rhs) const 
 
194
            { 
 
195
                return (it == rhs.it);
 
196
            }
 
197
            bool operator != (const iterator& rhs) const
 
198
            {
 
199
                return !(operator == (rhs));
 
200
            }
 
201
            iterator& operator ++ () 
 
202
            {
 
203
                std::advance(it, internal::sequence_length(it));
 
204
                return *this;
 
205
            }
 
206
            iterator operator ++ (int)
 
207
            {
 
208
                iterator temp = *this;
 
209
                std::advance(it, internal::sequence_length(it));
 
210
                return temp;
 
211
            }  
 
212
            iterator& operator -- ()
 
213
            {
 
214
                prior(it);
 
215
                return *this;
 
216
            }
 
217
            iterator operator -- (int)
 
218
            {
 
219
                iterator temp = *this;
 
220
                prior(it);
 
221
                return temp;
 
222
            }
 
223
          }; // class iterator
 
224
 
 
225
    } // namespace utf8::unchecked
 
226
} // namespace utf8 
 
227
} // namespace drizzled
 
228
 
 
229
 
 
230
#endif /* DRIZZLED_UTF8_UNCHECKED_H */
 
231