2 #ifndef BOOST_MPL_STRING_HPP_INCLUDED
3 #define BOOST_MPL_STRING_HPP_INCLUDED
5 // Copyright Eric Niebler 2009
7 // Distributed under the Boost Software License, Version 1.0.
8 // (See accompanying file LICENSE_1_0.txt or copy at
9 // http://www.boost.org/LICENSE_1_0.txt)
11 // See http://www.boost.org/libs/mpl for documentation.
13 // $Id: string.hpp 49239 2009-04-01 09:10:26Z eric_niebler $
14 // $Date: 2009-04-01 02:10:26 -0700 (Wed, 1 Apr 2009) $
18 // Dmitry Goncharov for porting this to the Sun compiler
20 #include <boost/config.hpp>
21 #include <boost/detail/workaround.hpp>
22 #include <boost/detail/endian.hpp>
23 #include <boost/mpl/limits/string.hpp>
24 #include <boost/mpl/if.hpp>
25 #include <boost/mpl/char.hpp>
26 #include <boost/mpl/copy.hpp>
27 #include <boost/mpl/size.hpp>
28 #include <boost/mpl/empty.hpp>
29 #include <boost/mpl/assert.hpp>
30 #include <boost/mpl/size_t.hpp>
31 #include <boost/mpl/begin_end.hpp>
32 #include <boost/mpl/joint_view.hpp>
33 #include <boost/mpl/insert_range.hpp>
34 #include <boost/mpl/back_inserter.hpp>
35 #include <boost/mpl/front_inserter.hpp>
36 #include <boost/mpl/iterator_range.hpp>
37 #include <boost/preprocessor/arithmetic/dec.hpp>
38 #include <boost/preprocessor/arithmetic/add.hpp>
39 #include <boost/preprocessor/arithmetic/div.hpp>
40 #include <boost/preprocessor/punctuation/comma_if.hpp>
41 #include <boost/preprocessor/repetition/repeat.hpp>
42 #include <boost/preprocessor/repetition/enum_params.hpp>
43 #include <boost/preprocessor/repetition/repeat_from_to.hpp>
44 #include <boost/preprocessor/repetition/enum_shifted_params.hpp>
45 #include <boost/preprocessor/repetition/enum_trailing_params.hpp>
46 #include <boost/preprocessor/repetition/enum_params_with_a_default.hpp>
48 #include <iterator> // for bidirectional_iterator_tag
51 namespace boost { namespace mpl
53 #define BOOST_MPL_STRING_MAX_PARAMS \
54 BOOST_PP_DIV(BOOST_PP_ADD(BOOST_MPL_LIMIT_STRING_SIZE, 3), 4)
56 // Low-level bit-twiddling is done by macros. Any implementation-defined behavior of
57 // multi-character literals should be localized to these macros.
59 #define BOOST_MPL_MULTICHAR_LENGTH(c) \
60 (std::size_t)((c<CHAR_MIN) ? 4 : ((c>0xffffff)+(c>0xffff)+(c>0xff)+1))
62 #if defined(BOOST_LITTLE_ENDIAN) && defined(__SUNPRO_CC)
64 #define BOOST_MPL_MULTICHAR_AT(c,i) \
65 (char)(0xff&((unsigned)(c)>>(8*(std::size_t)(i))))
67 #define BOOST_MPL_MULTICHAR_PUSH_BACK(c,i) \
68 ((((unsigned char)(i))<<(BOOST_MPL_MULTICHAR_LENGTH(c)*8))|(unsigned)(c))
70 #define BOOST_MPL_MULTICHAR_PUSH_FRONT(c,i) \
71 (((unsigned)(c)<<8)|(unsigned char)(i))
73 #define BOOST_MPL_MULTICHAR_POP_BACK(c) \
74 (((1<<((BOOST_MPL_MULTICHAR_LENGTH(c)-1)*8))-1)&(unsigned)(c))
76 #define BOOST_MPL_MULTICHAR_POP_FRONT(c) \
81 #define BOOST_MPL_MULTICHAR_AT(c,i) \
82 (char)(0xff&((unsigned)(c)>>(8*(BOOST_MPL_MULTICHAR_LENGTH(c)-(std::size_t)(i)-1))))
84 #define BOOST_MPL_MULTICHAR_PUSH_BACK(c,i) \
85 (((unsigned)(c)<<8)|(unsigned char)(i))
87 #define BOOST_MPL_MULTICHAR_PUSH_FRONT(c,i) \
88 ((((unsigned char)(i))<<(BOOST_MPL_MULTICHAR_LENGTH(c)*8))|(unsigned)(c))
90 #define BOOST_MPL_MULTICHAR_POP_BACK(c) \
93 #define BOOST_MPL_MULTICHAR_POP_FRONT(c) \
94 (((1<<((BOOST_MPL_MULTICHAR_LENGTH(c)-1)*8))-1)&(unsigned)(c))
99 struct string_iterator_tag;
101 template<BOOST_PP_ENUM_PARAMS_WITH_A_DEFAULT(BOOST_MPL_STRING_MAX_PARAMS, int C, 0)>
104 template<typename Sequence, int I, int J>
105 struct string_iterator;
107 template<typename Sequence>
110 template<typename Tag>
114 struct size_impl<mpl::string_tag>
116 template<typename Sequence>
119 #define M0(z, n, data) \
120 + BOOST_MPL_MULTICHAR_LENGTH(BOOST_PP_CAT(C,n))
122 #define M1(z, n, data) \
123 template<BOOST_PP_ENUM_PARAMS_Z(z, n, int C)> \
124 struct apply<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)> > \
125 : mpl::size_t<(0 BOOST_PP_REPEAT_ ## z(n, M0, ~))> \
128 BOOST_PP_REPEAT_FROM_TO(1, BOOST_PP_INC(BOOST_MPL_STRING_MAX_PARAMS), M1, ~)
134 struct size_impl<mpl::string_tag>::apply<mpl::string<> >
138 template<typename Tag>
142 struct begin_impl<mpl::string_tag>
144 template<typename Sequence>
147 typedef mpl::string_iterator<Sequence, 0, 0> type;
151 template<typename Tag>
155 struct end_impl<mpl::string_tag>
157 template<typename Sequence>
160 #define M0(z,n,data) \
161 template<BOOST_PP_ENUM_PARAMS_Z(z, n, int C)> \
162 struct apply<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)> > \
164 typedef mpl::string_iterator<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)>, n, 0> type; \
167 BOOST_PP_REPEAT_FROM_TO(1, BOOST_PP_INC(BOOST_MPL_STRING_MAX_PARAMS), M0, ~)
172 struct end_impl<mpl::string_tag>::apply<mpl::string<> >
174 typedef mpl::string_iterator<mpl::string<>, 0, 0> type;
177 template<typename Tag>
178 struct push_back_impl;
181 struct push_back_impl<mpl::string_tag>
183 template<typename Sequence, typename Value, bool B = (4==BOOST_MPL_MULTICHAR_LENGTH(Sequence::back_))>
186 BOOST_MPL_ASSERT_MSG(
187 (BOOST_MPL_LIMIT_STRING_SIZE != mpl::size<Sequence>::type::value)
188 , PUSH_BACK_FAILED_MPL_STRING_IS_FULL
191 // If the above assertion didn't fire, then the string is sparse.
192 // Repack the string and retry the push_back
194 typename mpl::push_back<
197 , mpl::back_inserter<mpl::string<> >
204 template<typename Value>
205 struct apply<mpl::string<>, Value, false>
207 typedef mpl::string<(char)Value::value> type;
210 #define M0(z,n,data) \
211 template<BOOST_PP_ENUM_PARAMS_Z(z, n, int C), typename Value> \
212 struct apply<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)>, Value, false> \
216 BOOST_PP_ENUM_PARAMS_Z(z, BOOST_PP_DEC(n), C) \
217 BOOST_PP_COMMA_IF(BOOST_PP_DEC(n)) \
218 ((unsigned)BOOST_PP_CAT(C,BOOST_PP_DEC(n))>0xffffff) \
219 ?BOOST_PP_CAT(C,BOOST_PP_DEC(n)) \
220 :BOOST_MPL_MULTICHAR_PUSH_BACK(BOOST_PP_CAT(C,BOOST_PP_DEC(n)), Value::value) \
221 , ((unsigned)BOOST_PP_CAT(C,BOOST_PP_DEC(n))>0xffffff) \
222 ?(char)Value::value \
228 BOOST_PP_REPEAT_FROM_TO(1, BOOST_MPL_STRING_MAX_PARAMS, M0, ~)
231 template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C), typename Value>
232 struct apply<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>, Value, false>
236 BOOST_PP_ENUM_PARAMS(BOOST_PP_DEC(BOOST_MPL_STRING_MAX_PARAMS), C)
237 , BOOST_MPL_MULTICHAR_PUSH_BACK(BOOST_PP_CAT(C,BOOST_PP_DEC(BOOST_MPL_STRING_MAX_PARAMS)), Value::value)
243 template<typename Tag>
244 struct has_push_back_impl;
247 struct has_push_back_impl<mpl::string_tag>
249 template<typename Sequence>
255 template<typename Tag>
256 struct pop_back_impl;
259 struct pop_back_impl<mpl::string_tag>
261 template<typename Sequence>
264 #define M0(z,n,data) \
265 template<BOOST_PP_ENUM_PARAMS_Z(z, n, int C)> \
266 struct apply<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)> > \
268 BOOST_MPL_ASSERT_MSG((C0 != 0), POP_BACK_FAILED_MPL_STRING_IS_EMPTY, (mpl::string<>)); \
271 BOOST_PP_ENUM_PARAMS_Z(z, BOOST_PP_DEC(n), C) \
272 BOOST_PP_COMMA_IF(BOOST_PP_DEC(n)) \
273 BOOST_MPL_MULTICHAR_POP_BACK(BOOST_PP_CAT(C,BOOST_PP_DEC(n))) \
278 BOOST_PP_REPEAT_FROM_TO(1, BOOST_PP_INC(BOOST_MPL_STRING_MAX_PARAMS), M0, ~)
282 template<typename Tag>
283 struct has_pop_back_impl;
286 struct has_pop_back_impl<mpl::string_tag>
288 template<typename Sequence>
294 template<typename Tag>
295 struct push_front_impl;
298 struct push_front_impl<mpl::string_tag>
300 template<typename Sequence, typename Value, bool B = (4==BOOST_MPL_MULTICHAR_LENGTH(Sequence::front_))>
303 BOOST_MPL_ASSERT_MSG(
304 (BOOST_MPL_LIMIT_STRING_SIZE != mpl::size<Sequence>::type::value)
305 , PUSH_FRONT_FAILED_MPL_STRING_IS_FULL
308 // If the above assertion didn't fire, then the string is sparse.
309 // Repack the string and retry the push_front.
311 typename mpl::push_front<
312 typename mpl::reverse_copy<
314 , mpl::front_inserter<string<> >
321 #if !BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
322 template<typename Value>
323 struct apply<mpl::string<>, Value, false>
325 typedef mpl::string<(char)Value::value> type;
329 #define M0(z,n,data) \
330 template<BOOST_PP_ENUM_PARAMS_Z(z, n, int C), typename Value> \
331 struct apply<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)>, Value, true> \
336 BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, C) \
341 BOOST_PP_REPEAT_FROM_TO(1, BOOST_MPL_STRING_MAX_PARAMS, M0, ~)
344 template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C), typename Value>
345 struct apply<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>, Value, false>
349 BOOST_MPL_MULTICHAR_PUSH_FRONT(C0, Value::value)
350 , BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)
354 #if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
357 mpl::empty<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)> >
358 , mpl::string<(char)Value::value>
368 template<typename Tag>
369 struct has_push_front_impl;
372 struct has_push_front_impl<mpl::string_tag>
374 template<typename Sequence>
380 template<typename Tag>
381 struct pop_front_impl;
384 struct pop_front_impl<mpl::string_tag>
386 template<typename Sequence, bool B = (1==BOOST_MPL_MULTICHAR_LENGTH(Sequence::front_))>
389 #define M0(z,n,data) \
390 template<BOOST_PP_ENUM_PARAMS_Z(z, n, int C)> \
391 struct apply<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)>, true> \
393 BOOST_MPL_ASSERT_MSG((C0 != 0), POP_FRONT_FAILED_MPL_STRING_IS_EMPTY, (mpl::string<>)); \
395 mpl::string<BOOST_PP_ENUM_SHIFTED_PARAMS_Z(z, n, C)> \
399 BOOST_PP_REPEAT_FROM_TO(1, BOOST_MPL_STRING_MAX_PARAMS, M0, ~)
402 template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C)>
403 struct apply<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>, false>
407 BOOST_MPL_MULTICHAR_POP_FRONT(C0)
408 , BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)
414 template<typename Tag>
415 struct has_pop_front_impl;
418 struct has_pop_front_impl<mpl::string_tag>
420 template<typename Sequence>
426 template<typename Tag>
427 struct insert_range_impl;
430 struct insert_range_impl<mpl::string_tag>
432 template<typename Sequence, typename Pos, typename Range>
437 mpl::string_iterator<Sequence, 0, 0>
442 , mpl::iterator_range<
444 , typename mpl::end<Sequence>::type
448 , mpl::back_inserter<mpl::string<> >
453 template<typename Tag>
457 struct insert_impl<mpl::string_tag>
459 template<typename Sequence, typename Pos, typename Value>
461 : mpl::insert_range<Sequence, Pos, mpl::string<(char)Value::value> >
465 template<typename Tag>
469 struct erase_impl<mpl::string_tag>
471 template<typename Sequence, typename First, typename Last>
476 mpl::string_iterator<Sequence, 0, 0>
479 , mpl::iterator_range<
480 typename mpl::if_na<Last, typename mpl::next<First>::type>::type
481 , typename mpl::end<Sequence>::type
484 , mpl::back_inserter<mpl::string<> >
489 template<typename Tag>
493 struct clear_impl<mpl::string_tag>
498 typedef mpl::string<> type;
502 #define M0(z, n, data) \
503 template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C), int J> \
504 struct string_iterator<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>, n, J> \
506 enum { eomc_ = (BOOST_MPL_MULTICHAR_LENGTH(BOOST_PP_CAT(C, n)) == J + 1) }; \
507 typedef mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)> string; \
508 typedef std::bidirectional_iterator_tag category; \
510 mpl::string_iterator<string, n + eomc_, eomc_ ? 0 : J + 1> \
513 mpl::string_iterator<string, n, J - 1> \
515 typedef mpl::char_<BOOST_MPL_MULTICHAR_AT(BOOST_PP_CAT(C, n), J)> type; \
517 template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C)> \
518 struct string_iterator<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>, n, 0> \
520 enum { eomc_ = (BOOST_MPL_MULTICHAR_LENGTH(BOOST_PP_CAT(C, n)) == 1) }; \
521 typedef mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)> string; \
522 typedef std::bidirectional_iterator_tag category; \
524 mpl::string_iterator<string, n + eomc_, !eomc_> \
527 mpl::string_iterator< \
530 , BOOST_MPL_MULTICHAR_LENGTH(BOOST_PP_CAT(C, BOOST_PP_DEC(n))) - 1 \
533 typedef mpl::char_<BOOST_MPL_MULTICHAR_AT(BOOST_PP_CAT(C, n), 0)> type; \
536 BOOST_PP_REPEAT(BOOST_MPL_STRING_MAX_PARAMS, M0, ~)
539 template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C)>
546 , back_ = BOOST_PP_CAT(C, BOOST_PP_DEC(BOOST_MPL_STRING_MAX_PARAMS))
549 typedef char value_type;
551 typedef string_tag tag;
556 template<typename It, typename End>
561 template<typename End>
562 struct next_unless<End, End>
567 template<typename It, typename End>
572 template<typename End>
573 struct deref_unless<End, End>
575 typedef mpl::char_<'\0'> type;
579 template<typename Sequence>
582 typedef typename mpl::end<Sequence>::type iend;
583 typedef typename mpl::begin<Sequence>::type i0;
584 #define M0(z, n, data) \
586 typename mpl::aux_::next_unless<BOOST_PP_CAT(i, n), iend>::type \
587 BOOST_PP_CAT(i, BOOST_PP_INC(n));
588 BOOST_PP_REPEAT(BOOST_MPL_LIMIT_STRING_SIZE, M0, ~)
592 static typename Sequence::value_type const value[BOOST_MPL_LIMIT_STRING_SIZE+1];
595 template<typename Sequence>
596 typename Sequence::value_type const c_str<Sequence>::value[BOOST_MPL_LIMIT_STRING_SIZE+1] =
598 #define M0(z, n, data) \
599 mpl::aux_::deref_unless<BOOST_PP_CAT(i, n), iend>::type::value,
600 BOOST_PP_REPEAT(BOOST_MPL_LIMIT_STRING_SIZE, M0, ~)
605 }} // namespace boost
607 #endif // BOOST_MPL_STRING_HPP_INCLUDED