1 // ----------------------------------------------------------------------------
2 // Copyright (C) 2002-2006 Marcin Kalicinski
4 // Distributed under the Boost Software License, Version 1.0.
5 // (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // For more information, see www.boost.org
9 // ----------------------------------------------------------------------------
10 #ifndef BOOST_PROPERTY_TREE_DETAIL_PTREE_UTILS_HPP_INCLUDED
11 #define BOOST_PROPERTY_TREE_DETAIL_PTREE_UTILS_HPP_INCLUDED
13 #include <boost/limits.hpp>
14 #include <boost/type_traits/integral_constant.hpp>
15 #include <boost/mpl/has_xxx.hpp>
16 #include <boost/mpl/and.hpp>
21 namespace boost { namespace property_tree { namespace detail
27 typedef typename T::value_type Ch;
29 inline bool operator()(Ch c1, Ch c2) const
31 return std::toupper(c1, m_locale) < std::toupper(c2, m_locale);
33 inline bool operator()(const T &t1, const T &t2) const
35 return std::lexicographical_compare(t1.begin(), t1.end(),
36 t2.begin(), t2.end(), *this);
40 template <typename Ch>
41 struct is_character : public boost::false_type {};
43 struct is_character<char> : public boost::true_type {};
45 struct is_character<wchar_t> : public boost::true_type {};
48 BOOST_MPL_HAS_XXX_TRAIT_DEF(internal_type)
49 BOOST_MPL_HAS_XXX_TRAIT_DEF(external_type)
51 struct is_translator : public boost::mpl::and_<
52 has_internal_type<T>, has_external_type<T> > {};
56 // Naively convert narrow string to another character type
57 template<typename Str>
58 Str widen(const char *text)
63 result += typename Str::value_type(*text);
69 // Naively convert string to narrow character type
70 template<typename Str, typename char_type>
71 Str narrow(const char_type *text)
76 if (*text < 0 || *text > (std::numeric_limits<char>::max)())
79 result += typename Str::value_type(*text);
85 // Remove trailing and leading spaces
87 Str trim(const Str &s, const std::locale &loc = std::locale())
89 typename Str::const_iterator first = s.begin();
90 typename Str::const_iterator end = s.end();
91 while (first != end && std::isspace(*first, loc))
95 typename Str::const_iterator last = end;
96 do --last; while (std::isspace(*last, loc));
97 if (first != s.begin() || last + 1 != end)
98 return Str(first, last + 1);