trim.hpp 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Boost string_algo library trim.hpp header file ---------------------------//
  2. // Copyright Pavol Droba 2002-2003.
  3. //
  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)
  7. // See http://www.boost.org/ for updates, documentation, and revision history.
  8. #ifndef BOOST_STRING_TRIM_DETAIL_HPP
  9. #define BOOST_STRING_TRIM_DETAIL_HPP
  10. #include <boost/algorithm/string/config.hpp>
  11. #include <boost/detail/iterator.hpp>
  12. namespace boost {
  13. namespace algorithm {
  14. namespace detail {
  15. // trim iterator helper -----------------------------------------------//
  16. template< typename ForwardIteratorT, typename PredicateT >
  17. inline ForwardIteratorT trim_end_iter_select(
  18. ForwardIteratorT InBegin,
  19. ForwardIteratorT InEnd,
  20. PredicateT IsSpace,
  21. std::forward_iterator_tag )
  22. {
  23. ForwardIteratorT TrimIt=InBegin;
  24. for( ForwardIteratorT It=InBegin; It!=InEnd; ++It )
  25. {
  26. if ( !IsSpace(*It) )
  27. {
  28. TrimIt=It;
  29. ++TrimIt;
  30. }
  31. }
  32. return TrimIt;
  33. }
  34. template< typename ForwardIteratorT, typename PredicateT >
  35. inline ForwardIteratorT trim_end_iter_select(
  36. ForwardIteratorT InBegin,
  37. ForwardIteratorT InEnd,
  38. PredicateT IsSpace,
  39. std::bidirectional_iterator_tag )
  40. {
  41. for( ForwardIteratorT It=InEnd; It!=InBegin; )
  42. {
  43. if ( !IsSpace(*(--It)) )
  44. return ++It;
  45. }
  46. return InBegin;
  47. }
  48. // Search for first non matching character from the beginning of the sequence
  49. template< typename ForwardIteratorT, typename PredicateT >
  50. inline ForwardIteratorT trim_begin(
  51. ForwardIteratorT InBegin,
  52. ForwardIteratorT InEnd,
  53. PredicateT IsSpace )
  54. {
  55. ForwardIteratorT It=InBegin;
  56. for(; It!=InEnd; ++It )
  57. {
  58. if (!IsSpace(*It))
  59. return It;
  60. }
  61. return It;
  62. }
  63. // Search for first non matching character from the end of the sequence
  64. template< typename ForwardIteratorT, typename PredicateT >
  65. inline ForwardIteratorT trim_end(
  66. ForwardIteratorT InBegin,
  67. ForwardIteratorT InEnd,
  68. PredicateT IsSpace )
  69. {
  70. typedef BOOST_STRING_TYPENAME boost::detail::
  71. iterator_traits<ForwardIteratorT>::iterator_category category;
  72. return ::boost::algorithm::detail::trim_end_iter_select( InBegin, InEnd, IsSpace, category() );
  73. }
  74. } // namespace detail
  75. } // namespace algorithm
  76. } // namespace boost
  77. #endif // BOOST_STRING_TRIM_DETAIL_HPP