Update of /cvsroot/boost/boost/boost/xpressive/detail/utility
In directory sc8-pr-cvs3.sourceforge.net:/tmp/cvs-serv18713/detail/utility
Modified Files:
algorithm.hpp traits_utils.hpp
Log Message:
more work in support of non-char data
Index: algorithm.hpp
===================================================================
RCS file: /cvsroot/boost/boost/boost/xpressive/detail/utility/algorithm.hpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- algorithm.hpp 26 Nov 2005 17:12:25 -0000 1.2
+++ algorithm.hpp 30 May 2007 07:58:23 -0000 1.3
@@ -13,8 +13,14 @@
# pragma once
#endif
+#include <string>
#include <climits>
#include <algorithm>
+#include <boost/range/end.hpp>
+#include <boost/range/begin.hpp>
+#include <boost/range/size.hpp>
+#include <boost/range/value_type.hpp>
+#include <boost/type_traits/remove_const.hpp>
#include <boost/iterator/iterator_traits.hpp>
namespace boost { namespace xpressive { namespace detail
@@ -97,6 +103,67 @@
return detail::advance_to_impl(iter, diff, end, typename
iterator_category<Iter>::type());
}
+///////////////////////////////////////////////////////////////////////////////
+// range_data
+//
+template<typename T>
+struct range_data
+ : range_value<T>
+{};
+
+template<typename T>
+struct range_data<T *>
+ : remove_const<T>
+{};
+
+template<typename T> std::ptrdiff_t is_null_terminated(T const &) { return 0; }
+inline std::ptrdiff_t is_null_terminated(char const *) { return 1; }
+#ifndef BOOST_XPRESSIVE_NO_WREGEX
+inline std::ptrdiff_t is_null_terminated(wchar_t const *) { return 1; }
+#endif
+
+///////////////////////////////////////////////////////////////////////////////
+// data_begin/data_end
+//
+template<typename Cont>
+typename range_data<Cont>::type const *data_begin(Cont const &cont)
+{
+ return &*boost::begin(cont);
+}
+
+template<typename Cont>
+typename range_data<Cont>::type const *data_end(Cont const &cont)
+{
+ return &*boost::begin(cont) + boost::size(cont) - is_null_terminated(cont);
+}
+
+template<typename Char, typename Traits, typename Alloc>
+Char const *data_begin(std::basic_string<Char, Traits, Alloc> const &str)
+{
+ return str.data();
+}
+
+template<typename Char, typename Traits, typename Alloc>
+Char const *data_end(std::basic_string<Char, Traits, Alloc> const &str)
+{
+ return str.data() + str.size();
+}
+
+template<typename Char>
+Char const *data_begin(Char const *const &sz)
+{
+ return sz;
+}
+
+template<typename Char>
+Char const *data_end(Char const *const &sz)
+{
+ Char const *tmp = sz;
+ for(; *tmp; ++tmp)
+ ;
+ return tmp;
+}
+
}}}
#endif
Index: traits_utils.hpp
===================================================================
RCS file: /cvsroot/boost/boost/boost/xpressive/detail/utility/traits_utils.hpp,v
retrieving revision 1.4
retrieving revision 1.5
diff -u -d -r1.4 -r1.5
--- traits_utils.hpp 26 Nov 2005 17:12:25 -0000 1.4
+++ traits_utils.hpp 30 May 2007 07:58:23 -0000 1.5
@@ -20,6 +20,8 @@
#include <boost/mpl/assert.hpp>
#include <boost/utility/enable_if.hpp>
#include <boost/type_traits/is_same.hpp>
+#include <boost/iterator/transform_iterator.hpp>
+#include <boost/xpressive/detail/utility/algorithm.hpp>
namespace boost { namespace xpressive { namespace detail
{
@@ -43,27 +45,80 @@
}
///////////////////////////////////////////////////////////////////////////////
- // string_cast
+ // widen_fun
//
- template<typename ToChar, typename FromChar, typename Traits>
- inline std::basic_string<ToChar> const &
- string_cast(std::basic_string<FromChar> const &from, Traits const &,
typename enable_if<is_same<ToChar, FromChar> >::type * = 0)
+ template<typename Traits>
+ struct widen_fun
{
- return from;
- }
+ typedef typename Traits::char_type result_type;
+ explicit widen_fun(Traits const &traits)
+ : traits_(traits)
+ {}
- template<typename ToChar, typename FromChar, typename Traits>
- inline std::basic_string<ToChar> const
- string_cast(std::basic_string<FromChar> const &from, Traits const &traits,
typename disable_if<is_same<ToChar, FromChar> >::type * = 0)
+ result_type operator()(char ch) const
+ {
+ return this->traits_.widen(ch);
+ }
+
+ Traits const &traits_;
+ };
+
+
///////////////////////////////////////////////////////////////////////////////
+ // string_cast_
+ //
+ template<
+ typename To
+ , typename From
+ , typename ToChar = typename detail::range_data<To>::type
+ , typename FromChar = typename detail::range_data<From>::type
+ >
+ struct string_cast_
{
BOOST_MPL_ASSERT((is_same<FromChar, char>));
- std::basic_string<ToChar> to;
- to.reserve(from.size());
- for(typename std::basic_string<FromChar>::size_type i = 0; i <
from.size(); ++i)
+ typedef To const result_type;
+ template<typename Traits>
+ result_type operator()(From const &from, Traits const &traits) const
{
- to.push_back(traits.widen(from[i]));
+ widen_fun<Traits> widen(traits);
+ To to(
+ boost::make_transform_iterator(detail::data_begin(from), widen)
+ , boost::make_transform_iterator(detail::data_end(from), widen)
+ );
+ return to;
}
- return to;
+ };
+
+ template<typename To, typename From, typename Char>
+ struct string_cast_<To, From, Char, Char>
+ {
+ typedef To const result_type;
+ template<typename Traits>
+ result_type operator()(From const &from, Traits const &) const
+ {
+ To to(detail::data_begin(from), detail::data_end(from));
+ return to;
+ }
+ };
+
+ template<typename From, typename Char>
+ struct string_cast_<From, From, Char, Char>
+ {
+ typedef From const &result_type;
+ template<typename Traits>
+ result_type operator()(From const &from, Traits const &) const
+ {
+ return from;
+ }
+ };
+
+
///////////////////////////////////////////////////////////////////////////////
+ // string_cast
+ //
+ template<typename To, typename From, typename Traits>
+ typename string_cast_<To, From>::result_type
+ string_cast(From const &from, Traits const &traits)
+ {
+ return string_cast_<To, From>()(from, traits);
}
///////////////////////////////////////////////////////////////////////////////
-------------------------------------------------------------------------
This SF.net email is sponsored by DB2 Express
Download DB2 Express C - the FREE version of DB2 express and take
control of your XML. No limits. Just data. Click to get it now.
http://sourceforge.net/powerbar/db2/
_______________________________________________
Boost-cvs mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/boost-cvs