aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/restricted/boost/libs/locale/src/std/converter.cpp
blob: e4f925ade445d578310307a8e0437fb079b66c76 (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
//
//  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)
//
#define BOOST_LOCALE_SOURCE

#include <boost/config.hpp>
#ifdef BOOST_MSVC
#  pragma warning(disable : 4996)
#endif

#include <locale>
#include <stdexcept>
#include <boost/locale/generator.hpp>
#include <boost/locale/conversion.hpp>
#include <boost/locale/encoding.hpp>
#include <vector>



#include "all_generator.hpp"

namespace boost {
namespace locale {
namespace impl_std {


template<typename CharType>
class std_converter : public converter<CharType> 
{
public:
    typedef CharType char_type;
    typedef std::basic_string<char_type> string_type;
    typedef std::ctype<char_type> ctype_type;
    std_converter(std::locale const &base,size_t refs = 0) : 
        converter<CharType>(refs),
        base_(base)
    {
    }
    virtual string_type convert(converter_base::conversion_type how,char_type const *begin,char_type const *end,int /*flags*/ = 0) const 
    {
        switch(how) {
        case converter_base::upper_case:
        case converter_base::lower_case:
        case converter_base::case_folding:
            {
                ctype_type const &ct=std::use_facet<ctype_type>(base_);
                size_t len = end - begin;
                std::vector<char_type> res(len+1,0);
                char_type *lbegin = &res[0];
                std::copy(begin,end,lbegin);
                if(how == converter_base::upper_case)
                    ct.toupper(lbegin,lbegin+len);
                else
                    ct.tolower(lbegin,lbegin+len);
                return string_type(lbegin,len);
            }
        default:
            return string_type(begin,end-begin);
        }
    }
private:
    std::locale base_;
};

class utf8_converter : public converter<char> {
public:
    typedef std::ctype<char> ctype_type;
    typedef std::ctype<wchar_t> wctype_type;
    utf8_converter(std::locale const &base,size_t refs = 0) : 
        converter<char>(refs),
        base_(base)
    {
    }
    virtual std::string convert(converter_base::conversion_type how,char const *begin,char const *end,int /*flags*/ = 0) const 
    {
        switch(how) {
        case upper_case:
        case lower_case:
        case case_folding:
            {
                std::wstring tmp = conv::to_utf<wchar_t>(begin,end,"UTF-8");
                wctype_type const &ct=std::use_facet<wctype_type>(base_);
                size_t len = tmp.size();
                std::vector<wchar_t> res(len+1,0);
                wchar_t *lbegin = &res[0];
                std::copy(tmp.c_str(),tmp.c_str()+len,lbegin);
                if(how == upper_case)
                    ct.toupper(lbegin,lbegin+len);
                else
                    ct.tolower(lbegin,lbegin+len);
                return conv::from_utf<wchar_t>(lbegin,lbegin+len,"UTF-8");
            }
        default:
            return std::string(begin,end-begin);
        }
    }
private:
    std::locale base_;
};

std::locale create_convert( std::locale const &in,
                            std::string const &locale_name,
                            character_facet_type type,
                            utf8_support utf)
{
        switch(type) {
        case char_facet: 
            {
                if(utf == utf8_native_with_wide || utf == utf8_from_wide) {
                    std::locale base(std::locale::classic(),new std::ctype_byname<wchar_t>(locale_name.c_str()));
                    return std::locale(in,new utf8_converter(base));
                }
                std::locale base(std::locale::classic(),new std::ctype_byname<char>(locale_name.c_str()));
                return std::locale(in,new std_converter<char>(base));
            }
        case wchar_t_facet:
            {
                std::locale base(std::locale::classic(),new std::ctype_byname<wchar_t>(locale_name.c_str()));
                return std::locale(in,new std_converter<wchar_t>(base));
            }
        #ifdef BOOST_LOCALE_ENABLE_CHAR16_T
        case char16_t_facet:
            {
                std::locale base(std::locale::classic(),new std::ctype_byname<char16_t>(locale_name.c_str()));
                return std::locale(in,new std_converter<char16_t>(base));
            }
        #endif
        #ifdef BOOST_LOCALE_ENABLE_CHAR32_T
        case char32_t_facet:
            {
                std::locale base(std::locale::classic(),new std::ctype_byname<char32_t>(locale_name.c_str()));
                return std::locale(in,new std_converter<char32_t>(base));
            }
        #endif
        default:
            return in;
        }
}


} // namespace impl_std
} // locale 
} // boost
// vim: tabstop=4 expandtab shiftwidth=4 softtabstop=4