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. // Search for first non matching character from the beginning of the sequence
  17. template< typename ForwardIteratorT, typename PredicateT >
  18. inline ForwardIteratorT trim_begin(
  19. ForwardIteratorT InBegin,
  20. ForwardIteratorT InEnd,
  21. PredicateT IsSpace )
  22. {
  23. ForwardIteratorT It=InBegin;
  24. for(; It!=InEnd; ++It )
  25. {
  26. if (!IsSpace(*It))
  27. return It;
  28. }
  29. return It;
  30. }
  31. // Search for first non matching character from the end of the sequence
  32. template< typename ForwardIteratorT, typename PredicateT >
  33. inline ForwardIteratorT trim_end(
  34. ForwardIteratorT InBegin,
  35. ForwardIteratorT InEnd,
  36. PredicateT IsSpace )
  37. {
  38. typedef BOOST_STRING_TYPENAME boost::detail::
  39. iterator_traits<ForwardIteratorT>::iterator_category category;
  40. return trim_end_iter_select( InBegin, InEnd, IsSpace, category() );
  41. }
  42. template< typename ForwardIteratorT, typename PredicateT >
  43. inline ForwardIteratorT trim_end_iter_select(
  44. ForwardIteratorT InBegin,
  45. ForwardIteratorT InEnd,
  46. PredicateT IsSpace,
  47. std::forward_iterator_tag )
  48. {
  49. ForwardIteratorT TrimIt=InBegin;
  50. for( ForwardIteratorT It=InBegin; It!=InEnd; ++It )
  51. {
  52. if ( !IsSpace(*It) )
  53. {
  54. TrimIt=It;
  55. ++TrimIt;
  56. }
  57. }
  58. return TrimIt;
  59. }
  60. template< typename ForwardIteratorT, typename PredicateT >
  61. inline ForwardIteratorT trim_end_iter_select(
  62. ForwardIteratorT InBegin,
  63. ForwardIteratorT InEnd,
  64. PredicateT IsSpace,
  65. std::bidirectional_iterator_tag )
  66. {
  67. for( ForwardIteratorT It=InEnd; It!=InBegin; )
  68. {
  69. if ( !IsSpace(*(--It)) )
  70. return ++It;
  71. }
  72. return InBegin;
  73. }
  74. } // namespace detail
  75. } // namespace algorithm
  76. } // namespace boost
  77. #endif // BOOST_STRING_TRIM_DETAIL_HPP