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/foreach.hpp>
16 #include <boost/regex/pending/unicode_iterator.hpp>
17 #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 BOOST_FOREACH(Char ch, str)
66 *utf8_iter++ = (UChar)ch;