split.hpp 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. // Boost string_algo library split.hpp header file ---------------------------//
  2. // Copyright Pavol Droba 2002-2006.
  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_SPLIT_HPP
  9. #define BOOST_STRING_SPLIT_HPP
  10. #include <boost/algorithm/string/config.hpp>
  11. #include <boost/algorithm/string/iter_find.hpp>
  12. #include <boost/algorithm/string/finder.hpp>
  13. #include <boost/algorithm/string/compare.hpp>
  14. /*! \file
  15. Defines basic split algorithms.
  16. Split algorithms can be used to divide a string
  17. into several parts according to given criteria.
  18. Each part is copied and added as a new element to the
  19. output container.
  20. Thus the result container must be able to hold copies
  21. of the matches (in a compatible structure like std::string) or
  22. a reference to it (e.g. using the iterator range class).
  23. Examples of such a container are \c std::vector<std::string>
  24. or \c std::list<boost::iterator_range<std::string::iterator>>
  25. */
  26. namespace boost {
  27. namespace algorithm {
  28. // find_all ------------------------------------------------------------//
  29. //! Find all algorithm
  30. /*!
  31. This algorithm finds all occurrences of the search string
  32. in the input.
  33. Each part is copied and added as a new element to the
  34. output container.
  35. Thus the result container must be able to hold copies
  36. of the matches (in a compatible structure like std::string) or
  37. a reference to it (e.g. using the iterator range class).
  38. Examples of such a container are \c std::vector<std::string>
  39. or \c std::list<boost::iterator_range<std::string::iterator>>
  40. \param Result A container that can hold copies of references to the substrings
  41. \param Input A container which will be searched.
  42. \param Search A substring to be searched for.
  43. \return A reference the result
  44. \note Prior content of the result will be overwritten.
  45. \note This function provides the strong exception-safety guarantee
  46. */
  47. template< typename SequenceSequenceT, typename Range1T, typename Range2T >
  48. inline SequenceSequenceT& find_all(
  49. SequenceSequenceT& Result,
  50. Range1T& Input,
  51. const Range2T& Search)
  52. {
  53. return ::boost::algorithm::iter_find(
  54. Result,
  55. Input,
  56. ::boost::algorithm::first_finder(Search) );
  57. }
  58. //! Find all algorithm ( case insensitive )
  59. /*!
  60. This algorithm finds all occurrences of the search string
  61. in the input.
  62. Each part is copied and added as a new element to the
  63. output container. Thus the result container must be able to hold copies
  64. of the matches (in a compatible structure like std::string) or
  65. a reference to it (e.g. using the iterator range class).
  66. Examples of such a container are \c std::vector<std::string>
  67. or \c std::list<boost::iterator_range<std::string::iterator>>
  68. Searching is case insensitive.
  69. \param Result A container that can hold copies of references to the substrings
  70. \param Input A container which will be searched.
  71. \param Search A substring to be searched for.
  72. \param Loc A locale used for case insensitive comparison
  73. \return A reference the result
  74. \note Prior content of the result will be overwritten.
  75. \note This function provides the strong exception-safety guarantee
  76. */
  77. template< typename SequenceSequenceT, typename Range1T, typename Range2T >
  78. inline SequenceSequenceT& ifind_all(
  79. SequenceSequenceT& Result,
  80. Range1T& Input,
  81. const Range2T& Search,
  82. const std::locale& Loc=std::locale() )
  83. {
  84. return ::boost::algorithm::iter_find(
  85. Result,
  86. Input,
  87. ::boost::algorithm::first_finder(Search, is_iequal(Loc) ) );
  88. }
  89. // tokenize -------------------------------------------------------------//
  90. //! Split algorithm
  91. /*!
  92. Tokenize expression. This function is equivalent to C strtok. Input
  93. sequence is split into tokens, separated by separators. Separators
  94. are given by means of the predicate.
  95. Each part is copied and added as a new element to the
  96. output container.
  97. Thus the result container must be able to hold copies
  98. of the matches (in a compatible structure like std::string) or
  99. a reference to it (e.g. using the iterator range class).
  100. Examples of such a container are \c std::vector<std::string>
  101. or \c std::list<boost::iterator_range<std::string::iterator>>
  102. \param Result A container that can hold copies of references to the substrings
  103. \param Input A container which will be searched.
  104. \param Pred A predicate to identify separators. This predicate is
  105. supposed to return true if a given element is a separator.
  106. \param eCompress If eCompress argument is set to token_compress_on, adjacent
  107. separators are merged together. Otherwise, every two separators
  108. delimit a token.
  109. \return A reference the result
  110. \note Prior content of the result will be overwritten.
  111. \note This function provides the strong exception-safety guarantee
  112. */
  113. template< typename SequenceSequenceT, typename RangeT, typename PredicateT >
  114. inline SequenceSequenceT& split(
  115. SequenceSequenceT& Result,
  116. RangeT& Input,
  117. PredicateT Pred,
  118. token_compress_mode_type eCompress=token_compress_off )
  119. {
  120. return ::boost::algorithm::iter_split(
  121. Result,
  122. Input,
  123. ::boost::algorithm::token_finder( Pred, eCompress ) );
  124. }
  125. } // namespace algorithm
  126. // pull names to the boost namespace
  127. using algorithm::find_all;
  128. using algorithm::ifind_all;
  129. using algorithm::split;
  130. } // namespace boost
  131. #endif // BOOST_STRING_SPLIT_HPP