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 pop_back_impl;
247 struct pop_back_impl<mpl::string_tag>
249 template<typename Sequence>
252 #define M0(z,n,data) \
253 template<BOOST_PP_ENUM_PARAMS_Z(z, n, int C)> \
254 struct apply<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)> > \
256 BOOST_MPL_ASSERT_MSG((C0 != 0), POP_BACK_FAILED_MPL_STRING_IS_EMPTY, (mpl::string<>)); \
259 BOOST_PP_ENUM_PARAMS_Z(z, BOOST_PP_DEC(n), C) \
260 BOOST_PP_COMMA_IF(BOOST_PP_DEC(n)) \
261 BOOST_MPL_MULTICHAR_POP_BACK(BOOST_PP_CAT(C,BOOST_PP_DEC(n))) \
266 BOOST_PP_REPEAT_FROM_TO(1, BOOST_PP_INC(BOOST_MPL_STRING_MAX_PARAMS), M0, ~)
270 template<typename Tag>
271 struct push_front_impl;
274 struct push_front_impl<mpl::string_tag>
276 template<typename Sequence, typename Value, bool B = (4==BOOST_MPL_MULTICHAR_LENGTH(Sequence::front_))>
279 BOOST_MPL_ASSERT_MSG(
280 (BOOST_MPL_LIMIT_STRING_SIZE != mpl::size<Sequence>::type::value)
281 , PUSH_FRONT_FAILED_MPL_STRING_IS_FULL
284 // If the above assertion didn't fire, then the string is sparse.
285 // Repack the string and retry the push_front.
287 typename mpl::push_front<
288 typename mpl::reverse_copy<
290 , mpl::front_inserter<string<> >
297 #if !BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
298 template<typename Value>
299 struct apply<mpl::string<>, Value, false>
301 typedef mpl::string<(char)Value::value> type;
305 #define M0(z,n,data) \
306 template<BOOST_PP_ENUM_PARAMS_Z(z, n, int C), typename Value> \
307 struct apply<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)>, Value, true> \
312 BOOST_PP_ENUM_TRAILING_PARAMS_Z(z, n, C) \
317 BOOST_PP_REPEAT_FROM_TO(1, BOOST_MPL_STRING_MAX_PARAMS, M0, ~)
320 template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C), typename Value>
321 struct apply<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>, Value, false>
325 BOOST_MPL_MULTICHAR_PUSH_FRONT(C0, Value::value)
326 , BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)
330 #if BOOST_WORKAROUND(__SUNPRO_CC, BOOST_TESTED_AT(0x590))
333 mpl::empty<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)> >
334 , mpl::string<(char)Value::value>
344 template<typename Tag>
345 struct pop_front_impl;
348 struct pop_front_impl<mpl::string_tag>
350 template<typename Sequence, bool B = (1==BOOST_MPL_MULTICHAR_LENGTH(Sequence::front_))>
353 #define M0(z,n,data) \
354 template<BOOST_PP_ENUM_PARAMS_Z(z, n, int C)> \
355 struct apply<mpl::string<BOOST_PP_ENUM_PARAMS_Z(z, n, C)>, true> \
357 BOOST_MPL_ASSERT_MSG((C0 != 0), POP_FRONT_FAILED_MPL_STRING_IS_EMPTY, (mpl::string<>)); \
359 mpl::string<BOOST_PP_ENUM_SHIFTED_PARAMS_Z(z, n, C)> \
363 BOOST_PP_REPEAT_FROM_TO(1, BOOST_MPL_STRING_MAX_PARAMS, M0, ~)
366 template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C)>
367 struct apply<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>, false>
371 BOOST_MPL_MULTICHAR_POP_FRONT(C0)
372 , BOOST_PP_ENUM_SHIFTED_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)
378 template<typename Tag>
379 struct insert_range_impl;
382 struct insert_range_impl<mpl::string_tag>
384 template<typename Sequence, typename Pos, typename Range>
389 mpl::string_iterator<Sequence, 0, 0>
394 , mpl::iterator_range<
396 , typename mpl::end<Sequence>::type
400 , mpl::back_inserter<mpl::string<> >
405 template<typename Tag>
409 struct insert_impl<mpl::string_tag>
411 template<typename Sequence, typename Pos, typename Value>
413 : mpl::insert_range<Sequence, Pos, mpl::string<(char)Value::value> >
417 template<typename Tag>
421 struct erase_impl<mpl::string_tag>
423 template<typename Sequence, typename First, typename Last>
428 mpl::string_iterator<Sequence, 0, 0>
431 , mpl::iterator_range<
432 typename mpl::if_na<Last, typename mpl::next<First>::type>::type
433 , typename mpl::end<Sequence>::type
436 , mpl::back_inserter<mpl::string<> >
441 template<typename Tag>
445 struct clear_impl<mpl::string_tag>
450 typedef mpl::string<> type;
454 #define M0(z, n, data) \
455 template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C), int J> \
456 struct string_iterator<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>, n, J> \
458 enum { eomc_ = (BOOST_MPL_MULTICHAR_LENGTH(BOOST_PP_CAT(C, n)) == J + 1) }; \
459 typedef mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)> string; \
460 typedef std::bidirectional_iterator_tag category; \
462 mpl::string_iterator<string, n + eomc_, eomc_ ? 0 : J + 1> \
465 mpl::string_iterator<string, n, J - 1> \
467 typedef mpl::char_<BOOST_MPL_MULTICHAR_AT(BOOST_PP_CAT(C, n), J)> type; \
469 template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C)> \
470 struct string_iterator<mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)>, n, 0> \
472 enum { eomc_ = (BOOST_MPL_MULTICHAR_LENGTH(BOOST_PP_CAT(C, n)) == 1) }; \
473 typedef mpl::string<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, C)> string; \
474 typedef std::bidirectional_iterator_tag category; \
476 mpl::string_iterator<string, n + eomc_, !eomc_> \
479 mpl::string_iterator< \
482 , BOOST_MPL_MULTICHAR_LENGTH(BOOST_PP_CAT(C, BOOST_PP_DEC(n))) - 1 \
485 typedef mpl::char_<BOOST_MPL_MULTICHAR_AT(BOOST_PP_CAT(C, n), 0)> type; \
488 BOOST_PP_REPEAT(BOOST_MPL_STRING_MAX_PARAMS, M0, ~)
491 template<BOOST_PP_ENUM_PARAMS(BOOST_MPL_STRING_MAX_PARAMS, int C)>
498 , back_ = BOOST_PP_CAT(C, BOOST_PP_DEC(BOOST_MPL_STRING_MAX_PARAMS))
501 typedef char value_type;
503 typedef string_tag tag;
508 template<typename It, typename End>
513 template<typename End>
514 struct next_unless<End, End>
519 template<typename It, typename End>
524 template<typename End>
525 struct deref_unless<End, End>
527 typedef mpl::char_<'\0'> type;
531 template<typename Sequence>
534 typedef typename mpl::end<Sequence>::type iend;
535 typedef typename mpl::begin<Sequence>::type i0;
536 #define M0(z, n, data) \
538 typename mpl::aux_::next_unless<BOOST_PP_CAT(i, n), iend>::type \
539 BOOST_PP_CAT(i, BOOST_PP_INC(n));
540 BOOST_PP_REPEAT(BOOST_MPL_LIMIT_STRING_SIZE, M0, ~)
544 static typename Sequence::value_type const value[BOOST_MPL_LIMIT_STRING_SIZE+1];
547 template<typename Sequence>
548 typename Sequence::value_type const c_str<Sequence>::value[BOOST_MPL_LIMIT_STRING_SIZE+1] =
550 #define M0(z, n, data) \
551 mpl::aux_::deref_unless<BOOST_PP_CAT(i, n), iend>::type::value,
552 BOOST_PP_REPEAT(BOOST_MPL_LIMIT_STRING_SIZE, M0, ~)
557 }} // namespace boost
559 #endif // BOOST_MPL_STRING_HPP_INCLUDED