aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/libs/locale/src/icu/uconv.hpp
blob: f9eb2d1bdc450e34188f27c33cd62bdfb3e56646 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
//
//  Copyright (c) 2009-2011 Artyom Beilis (Tonkikh)
//
//  Distributed under the Boost Software License, Version 1.0. (See
//  accompanying file LICENSE_1_0.txt or copy at
//  http://www.boost.org/LICENSE_1_0.txt)
//
#ifndef BOOST_SRC_LOCALE_ICU_UCONV_HPP
#define BOOST_SRC_LOCALE_ICU_UCONV_HPP
#include <unicode/unistr.h>
#include <unicode/ucnv.h>
#include <unicode/ustring.h>
#include <unicode/utf.h>
#include <unicode/utf16.h>

#include <boost/locale/encoding.hpp>

#include <string>
#include <memory>
#include "icu_util.hpp"

namespace boost {
namespace locale {
namespace impl_icu {

    typedef enum {
        cvt_skip,
        cvt_stop
    } cpcvt_type;

       
    template<typename CharType,int char_size = sizeof(CharType) >
    class icu_std_converter {
    public:
        typedef CharType char_type;
        typedef std::basic_string<char_type> string_type;

        icu_std_converter(std::string charset,cpcvt_type cv=cvt_skip);         
        icu::UnicodeString icu(char_type const *begin,char_type const *end) const;
        string_type std(icu::UnicodeString const &str) const;
        size_t cut(icu::UnicodeString const &str,char_type const *begin,char_type const *end,size_t n,size_t from_u=0,size_t from_c=0) const;
    };

    template<typename CharType>
    class icu_std_converter<CharType,1> {
    public:
        typedef CharType char_type;
        typedef std::basic_string<char_type> string_type;

        
        icu::UnicodeString icu_checked(char_type const *vb,char_type const *ve) const
        {
            return icu(vb,ve); // Already done
        }
        icu::UnicodeString icu(char_type const *vb,char_type const *ve) const
        {
            char const *begin=reinterpret_cast<char const *>(vb);
            char const *end=reinterpret_cast<char const *>(ve);
            uconv cvt(charset_,cvt_type_);
            UErrorCode err=U_ZERO_ERROR;
            icu::UnicodeString tmp(begin,end-begin,cvt.cvt(),err);
            check_and_throw_icu_error(err);
            return tmp;
        }
        
        string_type std(icu::UnicodeString const &str) const
        {
            uconv cvt(charset_,cvt_type_);
            return cvt.go(str.getBuffer(),str.length(),max_len_);
        }

        icu_std_converter(std::string charset,cpcvt_type cvt_type = cvt_skip) : 
            charset_(charset),
            cvt_type_(cvt_type)
        {
            uconv cvt(charset_,cvt_type);
            max_len_=cvt.max_char_size();
        }

        size_t cut(icu::UnicodeString const &str,char_type const *begin,char_type const *end,
                        size_t n,size_t from_u=0,size_t from_char=0) const
        {
            size_t code_points = str.countChar32(from_u,n);
            uconv cvt(charset_,cvt_type_);
            return cvt.cut(code_points,begin+from_char,end);
        }

        struct uconv  {
            uconv(uconv const &other);
            void operator=(uconv const &other);
        public:
            uconv(std::string const &charset,cpcvt_type cvt_type=cvt_skip) 
            {
                UErrorCode err=U_ZERO_ERROR;
                cvt_ = ucnv_open(charset.c_str(),&err);
                if(!cvt_ || U_FAILURE(err)) {
                    if(cvt_)
                        ucnv_close(cvt_);
                    throw conv::invalid_charset_error(charset);
                }
                
                try {
                    if(cvt_type==cvt_skip) {
                        ucnv_setFromUCallBack(cvt_,UCNV_FROM_U_CALLBACK_SKIP,0,0,0,&err);
                        check_and_throw_icu_error(err);
                
                        err=U_ZERO_ERROR;
                        ucnv_setToUCallBack(cvt_,UCNV_TO_U_CALLBACK_SKIP,0,0,0,&err);
                        check_and_throw_icu_error(err);
                    }
                    else {
                        ucnv_setFromUCallBack(cvt_,UCNV_FROM_U_CALLBACK_STOP,0,0,0,&err);
                        check_and_throw_icu_error(err);
                
                        err=U_ZERO_ERROR;
                        ucnv_setToUCallBack(cvt_,UCNV_TO_U_CALLBACK_STOP,0,0,0,&err);
                        check_and_throw_icu_error(err);
                    }
                }
                catch(...) { ucnv_close(cvt_) ; throw; }
            }

            int max_char_size()
            {
                return ucnv_getMaxCharSize(cvt_);
            }

            string_type go(UChar const *buf,int length,int max_size)
            {
                string_type res;
                res.resize(UCNV_GET_MAX_BYTES_FOR_STRING(length,max_size));
                char *ptr=reinterpret_cast<char *>(&res[0]);
                UErrorCode err=U_ZERO_ERROR;
                int n = ucnv_fromUChars(cvt_,ptr,res.size(),buf,length,&err);
                check_and_throw_icu_error(err);
                res.resize(n);
                return res;
            }

            size_t cut(size_t n,char_type const *begin,char_type const *end)
            {
                char_type const *saved = begin;
                while(n > 0 && begin < end) {
                    UErrorCode err=U_ZERO_ERROR;
                    ucnv_getNextUChar(cvt_,&begin,end,&err);
                    if(U_FAILURE(err))
                        return 0;
                    n--;
                }
                return begin - saved;
            }

            UConverter *cvt() { return cvt_; }

            ~uconv()
            {
                ucnv_close(cvt_);
            }
                
        private:
            UConverter *cvt_;
        };

    private:
        int max_len_;
        std::string charset_;
        cpcvt_type cvt_type_;
    };
   
    template<typename CharType>
    class icu_std_converter<CharType,2> {
    public:
        typedef CharType char_type;
        typedef std::basic_string<char_type> string_type;

        
        icu::UnicodeString icu_checked(char_type const *begin,char_type const *end) const
        {
            icu::UnicodeString tmp(end-begin,0,0); // make inital capacity
            while(begin!=end) {
                UChar cl = *begin++;
                if(U16_IS_SINGLE(cl))
                    tmp.append(static_cast<UChar32>(cl));
                else if(U16_IS_LEAD(cl)) {
                    if(begin==end) {
                        throw_if_needed();
                    }
                    else {
                        UChar ct=*begin++;
                        if(!U16_IS_TRAIL(ct))
                            throw_if_needed();
                        else {
                            UChar32 c=U16_GET_SUPPLEMENTARY(cl,ct);
                            tmp.append(c);
                        }
                    }
                }
                else
                    throw_if_needed();
            }
            return tmp;
        }     
        void throw_if_needed() const
        {
            if(mode_ == cvt_stop)
                throw conv::conversion_error();
        }
        icu::UnicodeString icu(char_type const *vb,char_type const *ve) const
        {
            UChar const *begin=reinterpret_cast<UChar const *>(vb);
            UChar const *end=reinterpret_cast<UChar const *>(ve);
            icu::UnicodeString tmp(begin,end-begin);
            return tmp;

        }

        string_type std(icu::UnicodeString const &str) const
        {
            char_type const *ptr=reinterpret_cast<char_type const *>(str.getBuffer());
            return string_type(ptr,str.length());
        }
        size_t cut(icu::UnicodeString const &/*str*/,char_type const * /*begin*/,char_type const * /*end*/,size_t n,
                        size_t /*from_u*/=0,size_t /*from_c*/=0) const
        {
            return n;
        }
        
        icu_std_converter(std::string /*charset*/,cpcvt_type mode=cvt_skip) :
            mode_(mode)
        {
        }
    private:
        cpcvt_type mode_;

    };
    
    template<typename CharType>
    class icu_std_converter<CharType,4> {
    public:

        typedef CharType char_type;
        typedef std::basic_string<char_type> string_type;

        icu::UnicodeString icu_checked(char_type const *begin,char_type const *end) const
        {
            icu::UnicodeString tmp(end-begin,0,0); // make inital capacity
            while(begin!=end) {
                UChar32 c = static_cast<UChar32>(*begin++);
                if(U_IS_UNICODE_CHAR(c))
                        tmp.append(c);
                else
                    throw_if_needed();
            }
            return tmp;
        }     
        void throw_if_needed() const
        {
            if(mode_ == cvt_stop)
                throw conv::conversion_error();
        }
 
        icu::UnicodeString icu(char_type const *begin,char_type const *end) const
        {
            icu::UnicodeString tmp(end-begin,0,0); // make inital capacity
            while(begin!=end) {
                UChar32 c=static_cast<UChar32>(*begin++);
                tmp.append(c);
            }
            return tmp;

        }

        string_type std(icu::UnicodeString const &str) const
        {
            string_type tmp;
            tmp.resize(str.length());
            UChar32 *ptr=reinterpret_cast<UChar32 *>(&tmp[0]);

            #ifdef __SUNPRO_CC
            int len=0;
            #else
            ::int32_t len=0;
            #endif

            UErrorCode code=U_ZERO_ERROR;
            u_strToUTF32(ptr,tmp.size(),&len,str.getBuffer(),str.length(),&code);

            check_and_throw_icu_error(code);

            tmp.resize(len);

            return tmp;
        }
        
        size_t cut(icu::UnicodeString const &str,char_type const * /*begin*/,char_type const * /*end*/,size_t n,
                size_t from_u=0,size_t /*from_c*/=0) const
        {
            return str.countChar32(from_u,n);
        }

        icu_std_converter(std::string /*charset*/,cpcvt_type mode=cvt_skip) :
            mode_(mode)
        {
        }
    private:
        cpcvt_type mode_;

    };
} /// impl_icu
} //  locale
} // boost

#endif


// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4