Welcome to mirror list, hosted at ThFree Co, Russian Federation.

convert.hpp « nowide « boost « src - github.com/prusa3d/PrusaSlicer.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 89b8871d09536cd5756e9376809cd10f299e3800 (plain)
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
//
//  Copyright (c) 2012 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_NOWIDE_CONVERT_H_INCLUDED
#define BOOST_NOWIDE_CONVERT_H_INCLUDED

#include <string>
#include <boost/locale/encoding_utf.hpp>

namespace boost {
namespace nowide {
    ///
    /// \brief Template function that converts a buffer of UTF sequences in range [source_begin,source_end)
    /// to the output \a buffer of size \a buffer_size.
    ///
    /// In case of success a NULL terminated string is returned (buffer), otherwise 0 is returned.
    ///
    /// If there is not enough room in the buffer or the source sequence contains invalid UTF,
    /// 0 is returned, and the contents of the buffer are undefined.
    ///
    template<typename CharOut,typename CharIn>
    CharOut *basic_convert(CharOut *buffer,size_t buffer_size,CharIn const *source_begin,CharIn const *source_end)
    {
        CharOut *rv = buffer;
        if(buffer_size == 0)
            return 0;
        buffer_size --;
        while(source_begin!=source_end) {
            using namespace boost::locale::utf;
            code_point c = utf_traits<CharIn>::template decode<CharIn const *>(source_begin,source_end);
            if(c==illegal || c==incomplete) {
                rv = 0;
                break;
            }
            size_t width = utf_traits<CharOut>::width(c);
            if(buffer_size < width) {
                rv=0;
                break;
            }
            buffer = utf_traits<CharOut>::template encode<CharOut *>(c,buffer);
            buffer_size -= width;
        }
        *buffer++ = 0;
        return rv;
    }

    /// \cond INTERNAL
    namespace details {
        //
        // wcslen defined only in C99... So we will not use it
        //
        template<typename Char>
        Char const *basic_strend(Char const *s)
        {
            while(*s)
                s++;
            return s;
        }
    }
    /// \endcond

    ///
    /// Convert NULL terminated UTF source string to NULL terminated \a output string of size at
    /// most output_size (including NULL)
    /// 
    /// In case of success output is returned, if the input sequence is illegal,
    /// or there is not enough room NULL is returned 
    ///
    inline char *narrow(char *output,size_t output_size,wchar_t const *source)
    {
        return basic_convert(output,output_size,source,details::basic_strend(source));
    }
    ///
    /// Convert UTF text in range [begin,end) to NULL terminated \a output string of size at
    /// most output_size (including NULL)
    /// 
    /// In case of success output is returned, if the input sequence is illegal,
    /// or there is not enough room NULL is returned 
    ///
    inline char *narrow(char *output,size_t output_size,wchar_t const *begin,wchar_t const *end)
    {
        return basic_convert(output,output_size,begin,end);
    }
    ///
    /// Convert NULL terminated UTF source string to NULL terminated \a output string of size at
    /// most output_size (including NULL)
    /// 
    /// In case of success output is returned, if the input sequence is illegal,
    /// or there is not enough room NULL is returned 
    ///
    inline wchar_t *widen(wchar_t *output,size_t output_size,char const *source)
    {
        return basic_convert(output,output_size,source,details::basic_strend(source));
    }
    ///
    /// Convert UTF text in range [begin,end) to NULL terminated \a output string of size at
    /// most output_size (including NULL)
    /// 
    /// In case of success output is returned, if the input sequence is illegal,
    /// or there is not enough room NULL is returned 
    ///
    inline wchar_t *widen(wchar_t *output,size_t output_size,char const *begin,char const *end)
    {
        return basic_convert(output,output_size,begin,end);
    }


    ///
    /// Convert between Wide - UTF-16/32 string and UTF-8 string.
    ///
    /// boost::locale::conv::conversion_error is thrown in a case of a error
    ///
    inline std::string narrow(wchar_t const *s)
    {
        return boost::locale::conv::utf_to_utf<char>(s);
    }
    ///
    /// Convert between UTF-8 and UTF-16 string, implemented only on Windows platform
    ///
    /// boost::locale::conv::conversion_error is thrown in a case of a error
    ///
    inline std::wstring widen(char const *s)
    {
        return boost::locale::conv::utf_to_utf<wchar_t>(s);
    }
    ///
    /// Convert between Wide - UTF-16/32 string and UTF-8 string
    ///
    /// boost::locale::conv::conversion_error is thrown in a case of a error
    ///
    inline std::string narrow(std::wstring const &s) 
    {
        return boost::locale::conv::utf_to_utf<char>(s);
    }
    ///
    /// Convert between UTF-8 and UTF-16 string, implemented only on Windows platform
    ///
    /// boost::locale::conv::conversion_error is thrown in a case of a error
    ///
    inline std::wstring widen(std::string const &s) 
    {
        return boost::locale::conv::utf_to_utf<wchar_t>(s);
    }

} // nowide
} // namespace boost

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