Chris@16
|
1 /*=============================================================================
|
Chris@16
|
2 Copyright (c) 2001-2011 Joel de Guzman
|
Chris@16
|
3
|
Chris@16
|
4 Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
5 file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
6 ==============================================================================*/
|
Chris@16
|
7 #if !defined(BOOST_SPIRIT_UC_TYPES_NOVEMBER_23_2008_0840PM)
|
Chris@16
|
8 #define BOOST_SPIRIT_UC_TYPES_NOVEMBER_23_2008_0840PM
|
Chris@16
|
9
|
Chris@16
|
10 #if defined(_MSC_VER)
|
Chris@16
|
11 #pragma once
|
Chris@16
|
12 #endif
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/cstdint.hpp>
|
Chris@16
|
15 #include <boost/foreach.hpp>
|
Chris@16
|
16 #include <boost/regex/pending/unicode_iterator.hpp>
|
Chris@16
|
17 #include <boost/type_traits/make_unsigned.hpp>
|
Chris@16
|
18 #include <string>
|
Chris@16
|
19
|
Chris@16
|
20 namespace boost { namespace spirit
|
Chris@16
|
21 {
|
Chris@16
|
22 typedef ::boost::uint32_t ucs4_char;
|
Chris@16
|
23 typedef char utf8_char;
|
Chris@16
|
24 typedef std::basic_string<ucs4_char> ucs4_string;
|
Chris@16
|
25 typedef std::basic_string<utf8_char> utf8_string;
|
Chris@16
|
26
|
Chris@16
|
27 template <typename Char>
|
Chris@16
|
28 inline utf8_string to_utf8(Char value)
|
Chris@16
|
29 {
|
Chris@16
|
30 // always store as UTF8
|
Chris@16
|
31 utf8_string result;
|
Chris@16
|
32 typedef std::back_insert_iterator<utf8_string> insert_iter;
|
Chris@16
|
33 insert_iter out_iter(result);
|
Chris@16
|
34 utf8_output_iterator<insert_iter> utf8_iter(out_iter);
|
Chris@16
|
35 typedef typename make_unsigned<Char>::type UChar;
|
Chris@16
|
36 *utf8_iter = (UChar)value;
|
Chris@16
|
37 return result;
|
Chris@16
|
38 }
|
Chris@16
|
39
|
Chris@16
|
40 template <typename Char>
|
Chris@16
|
41 inline utf8_string to_utf8(Char const* str)
|
Chris@16
|
42 {
|
Chris@16
|
43 // always store as UTF8
|
Chris@16
|
44 utf8_string result;
|
Chris@16
|
45 typedef std::back_insert_iterator<utf8_string> insert_iter;
|
Chris@16
|
46 insert_iter out_iter(result);
|
Chris@16
|
47 utf8_output_iterator<insert_iter> utf8_iter(out_iter);
|
Chris@16
|
48 typedef typename make_unsigned<Char>::type UChar;
|
Chris@16
|
49 while (*str)
|
Chris@16
|
50 *utf8_iter++ = (UChar)*str++;
|
Chris@16
|
51 return result;
|
Chris@16
|
52 }
|
Chris@16
|
53
|
Chris@16
|
54 template <typename Char, typename Traits, typename Allocator>
|
Chris@16
|
55 inline utf8_string
|
Chris@16
|
56 to_utf8(std::basic_string<Char, Traits, Allocator> const& str)
|
Chris@16
|
57 {
|
Chris@16
|
58 // always store as UTF8
|
Chris@16
|
59 utf8_string result;
|
Chris@16
|
60 typedef std::back_insert_iterator<utf8_string> insert_iter;
|
Chris@16
|
61 insert_iter out_iter(result);
|
Chris@16
|
62 utf8_output_iterator<insert_iter> utf8_iter(out_iter);
|
Chris@16
|
63 typedef typename make_unsigned<Char>::type UChar;
|
Chris@16
|
64 BOOST_FOREACH(Char ch, str)
|
Chris@16
|
65 {
|
Chris@16
|
66 *utf8_iter++ = (UChar)ch;
|
Chris@16
|
67 }
|
Chris@16
|
68 return result;
|
Chris@16
|
69 }
|
Chris@16
|
70 }}
|
Chris@16
|
71
|
Chris@16
|
72 #endif
|