1 /*=============================================================================
2 Copyright (c) 2001-2011 Joel de Guzman
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6 ==============================================================================*/
7 #if !defined(BOOST_SPIRIT_UC_TYPES_NOVEMBER_23_2008_0840PM)
8 #define BOOST_SPIRIT_UC_TYPES_NOVEMBER_23_2008_0840PM
14 #include <boost/cstdint.hpp>
15 #include <boost/regex/pending/unicode_iterator.hpp>
16 #include <boost/type_traits/make_unsigned.hpp>
20 namespace boost { namespace spirit
22 typedef ::boost::uint32_t ucs4_char;
23 typedef char utf8_char;
24 typedef std::basic_string<ucs4_char> ucs4_string;
25 typedef std::basic_string<utf8_char> utf8_string;
27 template <typename Char>
28 inline utf8_string to_utf8(Char value)
30 // always store as UTF8
32 typedef std::back_insert_iterator<utf8_string> insert_iter;
33 insert_iter out_iter(result);
34 utf8_output_iterator<insert_iter> utf8_iter(out_iter);
35 typedef typename make_unsigned<Char>::type UChar;
36 *utf8_iter = (UChar)value;
40 template <typename Char>
41 inline utf8_string to_utf8(Char const* str)
43 // always store as UTF8
45 typedef std::back_insert_iterator<utf8_string> insert_iter;
46 insert_iter out_iter(result);
47 utf8_output_iterator<insert_iter> utf8_iter(out_iter);
48 typedef typename make_unsigned<Char>::type UChar;
50 *utf8_iter++ = (UChar)*str++;
54 template <typename Char, typename Traits, typename Allocator>
56 to_utf8(std::basic_string<Char, Traits, Allocator> const& str)
58 // always store as UTF8
60 typedef std::back_insert_iterator<utf8_string> insert_iter;
61 insert_iter out_iter(result);
62 utf8_output_iterator<insert_iter> utf8_iter(out_iter);
63 typedef typename make_unsigned<Char>::type UChar;
64 for (Char const* ptr = str.data(),
65 * end = ptr + str.size(); ptr < end; ++ptr)
67 *utf8_iter++ = (UChar)*ptr;
72 // Assume wchar_t content is UTF-16 on MSVC, or mingw/wineg++ with -fshort-wchar
73 #if defined(_MSC_VER) || defined(__SIZEOF_WCHAR_T__) && __SIZEOF_WCHAR_T__ == 2
74 inline utf8_string to_utf8(wchar_t value)
77 typedef std::back_insert_iterator<utf8_string> insert_iter;
78 insert_iter out_iter(result);
79 utf8_output_iterator<insert_iter> utf8_iter(out_iter);
81 u16_to_u32_iterator<wchar_t const*, ucs4_char> ucs4_iter(&value);
82 *utf8_iter++ = *ucs4_iter;
87 inline utf8_string to_utf8(wchar_t const* str)
90 typedef std::back_insert_iterator<utf8_string> insert_iter;
91 insert_iter out_iter(result);
92 utf8_output_iterator<insert_iter> utf8_iter(out_iter);
94 u16_to_u32_iterator<wchar_t const*, ucs4_char> ucs4_iter(str);
95 for (ucs4_char c; (c = *ucs4_iter) != ucs4_char(); ++ucs4_iter) {
102 template <typename Traits, typename Allocator>
104 to_utf8(std::basic_string<wchar_t, Traits, Allocator> const& str)
106 return to_utf8(str.c_str());