split.hpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  51. Range1T&& Input,
  52. #else
  53. Range1T& Input,
  54. #endif
  55. const Range2T& Search)
  56. {
  57. return ::boost::algorithm::iter_find(
  58. Result,
  59. Input,
  60. ::boost::algorithm::first_finder(Search) );
  61. }
  62. //! Find all algorithm ( case insensitive )
  63. /*!
  64. This algorithm finds all occurrences of the search string
  65. in the input.
  66. Each part is copied and added as a new element to the
  67. output container. Thus the result container must be able to hold copies
  68. of the matches (in a compatible structure like std::string) or
  69. a reference to it (e.g. using the iterator range class).
  70. Examples of such a container are \c std::vector<std::string>
  71. or \c std::list<boost::iterator_range<std::string::iterator>>
  72. Searching is case insensitive.
  73. \param Result A container that can hold copies of references to the substrings
  74. \param Input A container which will be searched.
  75. \param Search A substring to be searched for.
  76. \param Loc A locale used for case insensitive comparison
  77. \return A reference the result
  78. \note Prior content of the result will be overwritten.
  79. \note This function provides the strong exception-safety guarantee
  80. */
  81. template< typename SequenceSequenceT, typename Range1T, typename Range2T >
  82. inline SequenceSequenceT& ifind_all(
  83. SequenceSequenceT& Result,
  84. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  85. Range1T&& Input,
  86. #else
  87. Range1T& Input,
  88. #endif
  89. const Range2T& Search,
  90. const std::locale& Loc=std::locale() )
  91. {
  92. return ::boost::algorithm::iter_find(
  93. Result,
  94. Input,
  95. ::boost::algorithm::first_finder(Search, is_iequal(Loc) ) );
  96. }
  97. // tokenize -------------------------------------------------------------//
  98. //! Split algorithm
  99. /*!
  100. Tokenize expression. This function is equivalent to C strtok. Input
  101. sequence is split into tokens, separated by separators. Separators
  102. are given by means of the predicate.
  103. Each part is copied and added as a new element to the
  104. output container.
  105. Thus the result container must be able to hold copies
  106. of the matches (in a compatible structure like std::string) or
  107. a reference to it (e.g. using the iterator range class).
  108. Examples of such a container are \c std::vector<std::string>
  109. or \c std::list<boost::iterator_range<std::string::iterator>>
  110. \param Result A container that can hold copies of references to the substrings
  111. \param Input A container which will be searched.
  112. \param Pred A predicate to identify separators. This predicate is
  113. supposed to return true if a given element is a separator.
  114. \param eCompress If eCompress argument is set to token_compress_on, adjacent
  115. separators are merged together. Otherwise, every two separators
  116. delimit a token.
  117. \return A reference the result
  118. \note Prior content of the result will be overwritten.
  119. \note This function provides the strong exception-safety guarantee
  120. */
  121. template< typename SequenceSequenceT, typename RangeT, typename PredicateT >
  122. inline SequenceSequenceT& split(
  123. SequenceSequenceT& Result,
  124. #if !defined(BOOST_NO_CXX11_RVALUE_REFERENCES)
  125. RangeT&& Input,
  126. #else
  127. RangeT& Input,
  128. #endif
  129. PredicateT Pred,
  130. token_compress_mode_type eCompress=token_compress_off )
  131. {
  132. return ::boost::algorithm::iter_split(
  133. Result,
  134. Input,
  135. ::boost::algorithm::token_finder( Pred, eCompress ) );
  136. }
  137. } // namespace algorithm
  138. // pull names to the boost namespace
  139. using algorithm::find_all;
  140. using algorithm::ifind_all;
  141. using algorithm::split;
  142. } // namespace boost
  143. #endif // BOOST_STRING_SPLIT_HPP