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