1 | // Boost.Range library
|
---|
2 | //
|
---|
3 | // Copyright Thorsten Ottosen 2006. Use, modification and
|
---|
4 | // distribution is subject to the Boost Software License, Version
|
---|
5 | // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
|
---|
6 | // http://www.boost.org/LICENSE_1_0.txt)
|
---|
7 | //
|
---|
8 | // For more information, see http://www.boost.org/libs/range/
|
---|
9 | //
|
---|
10 |
|
---|
11 | #ifndef BOOST_RANGE_AS_LITERAL_HPP
|
---|
12 | #define BOOST_RANGE_AS_LITERAL_HPP
|
---|
13 |
|
---|
14 | #if defined(_MSC_VER)
|
---|
15 | # pragma once
|
---|
16 | #endif
|
---|
17 |
|
---|
18 | #ifdef BOOST_NO_FUNCTION_TEMPLATE_ORDERING
|
---|
19 | #include <boost/range/detail/as_literal.hpp>
|
---|
20 | #else
|
---|
21 |
|
---|
22 | #include <boost/range/iterator_range.hpp>
|
---|
23 | #include <boost/range/detail/str_types.hpp>
|
---|
24 |
|
---|
25 | #include <boost/detail/workaround.hpp>
|
---|
26 |
|
---|
27 | #include <cstring>
|
---|
28 | #if !defined(BOOST_NO_CHAR16_T) || !defined(BOOST_NO_CHAR32_T)
|
---|
29 | #include <string>
|
---|
30 | #endif
|
---|
31 | #ifndef BOOST_NO_CWCHAR
|
---|
32 | #include <cwchar>
|
---|
33 | #endif
|
---|
34 |
|
---|
35 | namespace boost
|
---|
36 | {
|
---|
37 | namespace range_detail
|
---|
38 | {
|
---|
39 | inline std::size_t length( const char* s )
|
---|
40 | {
|
---|
41 | return strlen( s );
|
---|
42 | }
|
---|
43 |
|
---|
44 | #ifndef BOOST_NO_CHAR16_T
|
---|
45 | inline std::size_t length( const char16_t* s )
|
---|
46 | {
|
---|
47 | return std::char_traits<char16_t>::length( s );
|
---|
48 | }
|
---|
49 | #endif
|
---|
50 |
|
---|
51 | #ifndef BOOST_NO_CHAR32_T
|
---|
52 | inline std::size_t length( const char32_t* s )
|
---|
53 | {
|
---|
54 | return std::char_traits<char32_t>::length( s );
|
---|
55 | }
|
---|
56 | #endif
|
---|
57 |
|
---|
58 | #ifndef BOOST_NO_CWCHAR
|
---|
59 | inline std::size_t length( const wchar_t* s )
|
---|
60 | {
|
---|
61 | return wcslen( s );
|
---|
62 | }
|
---|
63 | #endif
|
---|
64 |
|
---|
65 | //
|
---|
66 | // Remark: the compiler cannot choose between T* and T[sz]
|
---|
67 | // overloads, so we must put the T* internal to the
|
---|
68 | // unconstrained version.
|
---|
69 | //
|
---|
70 |
|
---|
71 | inline bool is_char_ptr( char* )
|
---|
72 | {
|
---|
73 | return true;
|
---|
74 | }
|
---|
75 |
|
---|
76 | inline bool is_char_ptr( const char* )
|
---|
77 | {
|
---|
78 | return true;
|
---|
79 | }
|
---|
80 |
|
---|
81 | #ifndef BOOST_NO_CHAR16_T
|
---|
82 | inline bool is_char_ptr( char16_t* )
|
---|
83 | {
|
---|
84 | return true;
|
---|
85 | }
|
---|
86 |
|
---|
87 | inline bool is_char_ptr( const char16_t* )
|
---|
88 | {
|
---|
89 | return true;
|
---|
90 | }
|
---|
91 | #endif
|
---|
92 |
|
---|
93 | #ifndef BOOST_NO_CHAR32_T
|
---|
94 | inline bool is_char_ptr( char32_t* )
|
---|
95 | {
|
---|
96 | return true;
|
---|
97 | }
|
---|
98 |
|
---|
99 | inline bool is_char_ptr( const char32_t* )
|
---|
100 | {
|
---|
101 | return true;
|
---|
102 | }
|
---|
103 | #endif
|
---|
104 |
|
---|
105 | #ifndef BOOST_NO_CWCHAR
|
---|
106 | inline bool is_char_ptr( wchar_t* )
|
---|
107 | {
|
---|
108 | return true;
|
---|
109 | }
|
---|
110 |
|
---|
111 | inline bool is_char_ptr( const wchar_t* )
|
---|
112 | {
|
---|
113 | return true;
|
---|
114 | }
|
---|
115 | #endif
|
---|
116 |
|
---|
117 | template< class T >
|
---|
118 | inline long is_char_ptr( const T& /* r */ )
|
---|
119 | {
|
---|
120 | return 0L;
|
---|
121 | }
|
---|
122 |
|
---|
123 | template< class T >
|
---|
124 | inline iterator_range<T*>
|
---|
125 | make_range( T* const r, bool )
|
---|
126 | {
|
---|
127 | return iterator_range<T*>( r, r + length(r) );
|
---|
128 | }
|
---|
129 |
|
---|
130 | template< class T >
|
---|
131 | inline iterator_range<BOOST_DEDUCED_TYPENAME range_iterator<T>::type>
|
---|
132 | make_range( T& r, long )
|
---|
133 | {
|
---|
134 | return boost::make_iterator_range( r );
|
---|
135 | }
|
---|
136 |
|
---|
137 | }
|
---|
138 |
|
---|
139 | template< class Range >
|
---|
140 | inline iterator_range<BOOST_DEDUCED_TYPENAME range_iterator<Range>::type>
|
---|
141 | as_literal( Range& r )
|
---|
142 | {
|
---|
143 | return range_detail::make_range( r, range_detail::is_char_ptr(r) );
|
---|
144 | }
|
---|
145 |
|
---|
146 | template< class Range >
|
---|
147 | inline iterator_range<BOOST_DEDUCED_TYPENAME range_iterator<const Range>::type>
|
---|
148 | as_literal( const Range& r )
|
---|
149 | {
|
---|
150 | return range_detail::make_range( r, range_detail::is_char_ptr(r) );
|
---|
151 | }
|
---|
152 |
|
---|
153 | template< class Char, std::size_t sz >
|
---|
154 | inline iterator_range<Char*> as_literal( Char (&arr)[sz] )
|
---|
155 | {
|
---|
156 | return range_detail::make_range( arr, range_detail::is_char_ptr(arr) );
|
---|
157 | }
|
---|
158 |
|
---|
159 | template< class Char, std::size_t sz >
|
---|
160 | inline iterator_range<const Char*> as_literal( const Char (&arr)[sz] )
|
---|
161 | {
|
---|
162 | return range_detail::make_range( arr, range_detail::is_char_ptr(arr) );
|
---|
163 | }
|
---|
164 | }
|
---|
165 |
|
---|
166 | #endif // BOOST_NO_FUNCTION_TEMPLATE_ORDERING
|
---|
167 |
|
---|
168 | #endif
|
---|