Chris@16
|
1 // Boost string_algo library trim.hpp header file ---------------------------//
|
Chris@16
|
2
|
Chris@16
|
3 // Copyright Pavol Droba 2002-2003.
|
Chris@16
|
4 //
|
Chris@16
|
5 // Distributed under the Boost Software License, Version 1.0.
|
Chris@16
|
6 // (See accompanying file LICENSE_1_0.txt or copy at
|
Chris@16
|
7 // http://www.boost.org/LICENSE_1_0.txt)
|
Chris@16
|
8
|
Chris@16
|
9 // See http://www.boost.org/ for updates, documentation, and revision history.
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_STRING_TRIM_HPP
|
Chris@16
|
12 #define BOOST_STRING_TRIM_HPP
|
Chris@16
|
13
|
Chris@16
|
14 #include <boost/algorithm/string/config.hpp>
|
Chris@16
|
15
|
Chris@16
|
16 #include <boost/range/begin.hpp>
|
Chris@16
|
17 #include <boost/range/end.hpp>
|
Chris@16
|
18 #include <boost/range/const_iterator.hpp>
|
Chris@16
|
19 #include <boost/range/as_literal.hpp>
|
Chris@16
|
20 #include <boost/range/iterator_range_core.hpp>
|
Chris@16
|
21
|
Chris@16
|
22 #include <boost/algorithm/string/detail/trim.hpp>
|
Chris@16
|
23 #include <boost/algorithm/string/classification.hpp>
|
Chris@16
|
24 #include <locale>
|
Chris@16
|
25
|
Chris@16
|
26 /*! \file
|
Chris@16
|
27 Defines trim algorithms.
|
Chris@16
|
28 Trim algorithms are used to remove trailing and leading spaces from a
|
Chris@16
|
29 sequence (string). Space is recognized using given locales.
|
Chris@16
|
30
|
Chris@16
|
31 Parametric (\c _if) variants use a predicate (functor) to select which characters
|
Chris@16
|
32 are to be trimmed..
|
Chris@16
|
33 Functions take a selection predicate as a parameter, which is used to determine
|
Chris@16
|
34 whether a character is a space. Common predicates are provided in classification.hpp header.
|
Chris@16
|
35
|
Chris@16
|
36 */
|
Chris@16
|
37
|
Chris@16
|
38 namespace boost {
|
Chris@16
|
39 namespace algorithm {
|
Chris@16
|
40
|
Chris@16
|
41 // left trim -----------------------------------------------//
|
Chris@16
|
42
|
Chris@16
|
43
|
Chris@16
|
44 //! Left trim - parametric
|
Chris@16
|
45 /*!
|
Chris@16
|
46 Remove all leading spaces from the input.
|
Chris@16
|
47 The supplied predicate is used to determine which characters are considered spaces.
|
Chris@16
|
48 The result is a trimmed copy of the input. It is returned as a sequence
|
Chris@16
|
49 or copied to the output iterator
|
Chris@16
|
50
|
Chris@16
|
51 \param Output An output iterator to which the result will be copied
|
Chris@16
|
52 \param Input An input range
|
Chris@16
|
53 \param IsSpace A unary predicate identifying spaces
|
Chris@16
|
54 \return
|
Chris@16
|
55 An output iterator pointing just after the last inserted character or
|
Chris@16
|
56 a copy of the input
|
Chris@16
|
57
|
Chris@16
|
58 \note The second variant of this function provides the strong exception-safety guarantee
|
Chris@16
|
59 */
|
Chris@16
|
60 template<typename OutputIteratorT, typename RangeT, typename PredicateT>
|
Chris@16
|
61 inline OutputIteratorT trim_left_copy_if(
|
Chris@16
|
62 OutputIteratorT Output,
|
Chris@16
|
63 const RangeT& Input,
|
Chris@16
|
64 PredicateT IsSpace)
|
Chris@16
|
65 {
|
Chris@16
|
66 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_range(::boost::as_literal(Input));
|
Chris@16
|
67
|
Chris@16
|
68 std::copy(
|
Chris@16
|
69 ::boost::algorithm::detail::trim_begin(
|
Chris@16
|
70 ::boost::begin(lit_range),
|
Chris@16
|
71 ::boost::end(lit_range),
|
Chris@16
|
72 IsSpace ),
|
Chris@16
|
73 ::boost::end(lit_range),
|
Chris@16
|
74 Output);
|
Chris@16
|
75
|
Chris@16
|
76 return Output;
|
Chris@16
|
77 }
|
Chris@16
|
78
|
Chris@16
|
79 //! Left trim - parametric
|
Chris@16
|
80 /*!
|
Chris@16
|
81 \overload
|
Chris@16
|
82 */
|
Chris@16
|
83 template<typename SequenceT, typename PredicateT>
|
Chris@16
|
84 inline SequenceT trim_left_copy_if(const SequenceT& Input, PredicateT IsSpace)
|
Chris@16
|
85 {
|
Chris@16
|
86 return SequenceT(
|
Chris@16
|
87 ::boost::algorithm::detail::trim_begin(
|
Chris@16
|
88 ::boost::begin(Input),
|
Chris@16
|
89 ::boost::end(Input),
|
Chris@16
|
90 IsSpace ),
|
Chris@16
|
91 ::boost::end(Input));
|
Chris@16
|
92 }
|
Chris@16
|
93
|
Chris@16
|
94 //! Left trim - parametric
|
Chris@16
|
95 /*!
|
Chris@16
|
96 Remove all leading spaces from the input.
|
Chris@16
|
97 The result is a trimmed copy of the input.
|
Chris@16
|
98
|
Chris@16
|
99 \param Input An input sequence
|
Chris@16
|
100 \param Loc a locale used for 'space' classification
|
Chris@16
|
101 \return A trimmed copy of the input
|
Chris@16
|
102
|
Chris@16
|
103 \note This function provides the strong exception-safety guarantee
|
Chris@16
|
104 */
|
Chris@16
|
105 template<typename SequenceT>
|
Chris@16
|
106 inline SequenceT trim_left_copy(const SequenceT& Input, const std::locale& Loc=std::locale())
|
Chris@16
|
107 {
|
Chris@16
|
108 return
|
Chris@16
|
109 ::boost::algorithm::trim_left_copy_if(
|
Chris@16
|
110 Input,
|
Chris@16
|
111 is_space(Loc));
|
Chris@16
|
112 }
|
Chris@16
|
113
|
Chris@16
|
114 //! Left trim
|
Chris@16
|
115 /*!
|
Chris@16
|
116 Remove all leading spaces from the input. The supplied predicate is
|
Chris@16
|
117 used to determine which characters are considered spaces.
|
Chris@16
|
118 The input sequence is modified in-place.
|
Chris@16
|
119
|
Chris@16
|
120 \param Input An input sequence
|
Chris@16
|
121 \param IsSpace A unary predicate identifying spaces
|
Chris@16
|
122 */
|
Chris@16
|
123 template<typename SequenceT, typename PredicateT>
|
Chris@16
|
124 inline void trim_left_if(SequenceT& Input, PredicateT IsSpace)
|
Chris@16
|
125 {
|
Chris@16
|
126 Input.erase(
|
Chris@16
|
127 ::boost::begin(Input),
|
Chris@16
|
128 ::boost::algorithm::detail::trim_begin(
|
Chris@16
|
129 ::boost::begin(Input),
|
Chris@16
|
130 ::boost::end(Input),
|
Chris@16
|
131 IsSpace));
|
Chris@16
|
132 }
|
Chris@16
|
133
|
Chris@16
|
134 //! Left trim
|
Chris@16
|
135 /*!
|
Chris@16
|
136 Remove all leading spaces from the input.
|
Chris@16
|
137 The Input sequence is modified in-place.
|
Chris@16
|
138
|
Chris@16
|
139 \param Input An input sequence
|
Chris@16
|
140 \param Loc A locale used for 'space' classification
|
Chris@16
|
141 */
|
Chris@16
|
142 template<typename SequenceT>
|
Chris@16
|
143 inline void trim_left(SequenceT& Input, const std::locale& Loc=std::locale())
|
Chris@16
|
144 {
|
Chris@16
|
145 ::boost::algorithm::trim_left_if(
|
Chris@16
|
146 Input,
|
Chris@16
|
147 is_space(Loc));
|
Chris@16
|
148 }
|
Chris@16
|
149
|
Chris@16
|
150 // right trim -----------------------------------------------//
|
Chris@16
|
151
|
Chris@16
|
152 //! Right trim - parametric
|
Chris@16
|
153 /*!
|
Chris@16
|
154 Remove all trailing spaces from the input.
|
Chris@16
|
155 The supplied predicate is used to determine which characters are considered spaces.
|
Chris@16
|
156 The result is a trimmed copy of the input. It is returned as a sequence
|
Chris@16
|
157 or copied to the output iterator
|
Chris@16
|
158
|
Chris@16
|
159 \param Output An output iterator to which the result will be copied
|
Chris@16
|
160 \param Input An input range
|
Chris@16
|
161 \param IsSpace A unary predicate identifying spaces
|
Chris@16
|
162 \return
|
Chris@16
|
163 An output iterator pointing just after the last inserted character or
|
Chris@16
|
164 a copy of the input
|
Chris@16
|
165
|
Chris@16
|
166 \note The second variant of this function provides the strong exception-safety guarantee
|
Chris@16
|
167 */
|
Chris@16
|
168 template<typename OutputIteratorT, typename RangeT, typename PredicateT>
|
Chris@16
|
169 inline OutputIteratorT trim_right_copy_if(
|
Chris@16
|
170 OutputIteratorT Output,
|
Chris@16
|
171 const RangeT& Input,
|
Chris@16
|
172 PredicateT IsSpace )
|
Chris@16
|
173 {
|
Chris@16
|
174 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_range(::boost::as_literal(Input));
|
Chris@16
|
175
|
Chris@16
|
176 std::copy(
|
Chris@16
|
177 ::boost::begin(lit_range),
|
Chris@16
|
178 ::boost::algorithm::detail::trim_end(
|
Chris@16
|
179 ::boost::begin(lit_range),
|
Chris@16
|
180 ::boost::end(lit_range),
|
Chris@16
|
181 IsSpace ),
|
Chris@16
|
182 Output );
|
Chris@16
|
183
|
Chris@16
|
184 return Output;
|
Chris@16
|
185 }
|
Chris@16
|
186
|
Chris@16
|
187 //! Right trim - parametric
|
Chris@16
|
188 /*!
|
Chris@16
|
189 \overload
|
Chris@16
|
190 */
|
Chris@16
|
191 template<typename SequenceT, typename PredicateT>
|
Chris@16
|
192 inline SequenceT trim_right_copy_if(const SequenceT& Input, PredicateT IsSpace)
|
Chris@16
|
193 {
|
Chris@16
|
194 return SequenceT(
|
Chris@16
|
195 ::boost::begin(Input),
|
Chris@16
|
196 ::boost::algorithm::detail::trim_end(
|
Chris@16
|
197 ::boost::begin(Input),
|
Chris@16
|
198 ::boost::end(Input),
|
Chris@16
|
199 IsSpace)
|
Chris@16
|
200 );
|
Chris@16
|
201 }
|
Chris@16
|
202
|
Chris@16
|
203 //! Right trim
|
Chris@16
|
204 /*!
|
Chris@16
|
205 Remove all trailing spaces from the input.
|
Chris@16
|
206 The result is a trimmed copy of the input
|
Chris@16
|
207
|
Chris@16
|
208 \param Input An input sequence
|
Chris@16
|
209 \param Loc A locale used for 'space' classification
|
Chris@16
|
210 \return A trimmed copy of the input
|
Chris@16
|
211
|
Chris@16
|
212 \note This function provides the strong exception-safety guarantee
|
Chris@16
|
213 */
|
Chris@16
|
214 template<typename SequenceT>
|
Chris@16
|
215 inline SequenceT trim_right_copy(const SequenceT& Input, const std::locale& Loc=std::locale())
|
Chris@16
|
216 {
|
Chris@16
|
217 return
|
Chris@16
|
218 ::boost::algorithm::trim_right_copy_if(
|
Chris@16
|
219 Input,
|
Chris@16
|
220 is_space(Loc));
|
Chris@16
|
221 }
|
Chris@16
|
222
|
Chris@16
|
223
|
Chris@16
|
224 //! Right trim - parametric
|
Chris@16
|
225 /*!
|
Chris@16
|
226 Remove all trailing spaces from the input.
|
Chris@16
|
227 The supplied predicate is used to determine which characters are considered spaces.
|
Chris@16
|
228 The input sequence is modified in-place.
|
Chris@16
|
229
|
Chris@16
|
230 \param Input An input sequence
|
Chris@16
|
231 \param IsSpace A unary predicate identifying spaces
|
Chris@16
|
232 */
|
Chris@16
|
233 template<typename SequenceT, typename PredicateT>
|
Chris@16
|
234 inline void trim_right_if(SequenceT& Input, PredicateT IsSpace)
|
Chris@16
|
235 {
|
Chris@16
|
236 Input.erase(
|
Chris@16
|
237 ::boost::algorithm::detail::trim_end(
|
Chris@16
|
238 ::boost::begin(Input),
|
Chris@16
|
239 ::boost::end(Input),
|
Chris@16
|
240 IsSpace ),
|
Chris@16
|
241 ::boost::end(Input)
|
Chris@16
|
242 );
|
Chris@16
|
243 }
|
Chris@16
|
244
|
Chris@16
|
245
|
Chris@16
|
246 //! Right trim
|
Chris@16
|
247 /*!
|
Chris@16
|
248 Remove all trailing spaces from the input.
|
Chris@16
|
249 The input sequence is modified in-place.
|
Chris@16
|
250
|
Chris@16
|
251 \param Input An input sequence
|
Chris@16
|
252 \param Loc A locale used for 'space' classification
|
Chris@16
|
253 */
|
Chris@16
|
254 template<typename SequenceT>
|
Chris@16
|
255 inline void trim_right(SequenceT& Input, const std::locale& Loc=std::locale())
|
Chris@16
|
256 {
|
Chris@16
|
257 ::boost::algorithm::trim_right_if(
|
Chris@16
|
258 Input,
|
Chris@16
|
259 is_space(Loc) );
|
Chris@16
|
260 }
|
Chris@16
|
261
|
Chris@16
|
262 // both side trim -----------------------------------------------//
|
Chris@16
|
263
|
Chris@16
|
264 //! Trim - parametric
|
Chris@16
|
265 /*!
|
Chris@16
|
266 Remove all trailing and leading spaces from the input.
|
Chris@16
|
267 The supplied predicate is used to determine which characters are considered spaces.
|
Chris@16
|
268 The result is a trimmed copy of the input. It is returned as a sequence
|
Chris@16
|
269 or copied to the output iterator
|
Chris@16
|
270
|
Chris@16
|
271 \param Output An output iterator to which the result will be copied
|
Chris@16
|
272 \param Input An input range
|
Chris@16
|
273 \param IsSpace A unary predicate identifying spaces
|
Chris@16
|
274 \return
|
Chris@16
|
275 An output iterator pointing just after the last inserted character or
|
Chris@16
|
276 a copy of the input
|
Chris@16
|
277
|
Chris@16
|
278 \note The second variant of this function provides the strong exception-safety guarantee
|
Chris@16
|
279 */
|
Chris@16
|
280 template<typename OutputIteratorT, typename RangeT, typename PredicateT>
|
Chris@16
|
281 inline OutputIteratorT trim_copy_if(
|
Chris@16
|
282 OutputIteratorT Output,
|
Chris@16
|
283 const RangeT& Input,
|
Chris@16
|
284 PredicateT IsSpace)
|
Chris@16
|
285 {
|
Chris@16
|
286 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_range(::boost::as_literal(Input));
|
Chris@16
|
287
|
Chris@16
|
288 BOOST_STRING_TYPENAME
|
Chris@16
|
289 range_const_iterator<RangeT>::type TrimEnd=
|
Chris@16
|
290 ::boost::algorithm::detail::trim_end(
|
Chris@16
|
291 ::boost::begin(lit_range),
|
Chris@16
|
292 ::boost::end(lit_range),
|
Chris@16
|
293 IsSpace);
|
Chris@16
|
294
|
Chris@16
|
295 std::copy(
|
Chris@16
|
296 detail::trim_begin(
|
Chris@16
|
297 ::boost::begin(lit_range), TrimEnd, IsSpace),
|
Chris@16
|
298 TrimEnd,
|
Chris@16
|
299 Output
|
Chris@16
|
300 );
|
Chris@16
|
301
|
Chris@16
|
302 return Output;
|
Chris@16
|
303 }
|
Chris@16
|
304
|
Chris@16
|
305 //! Trim - parametric
|
Chris@16
|
306 /*!
|
Chris@16
|
307 \overload
|
Chris@16
|
308 */
|
Chris@16
|
309 template<typename SequenceT, typename PredicateT>
|
Chris@16
|
310 inline SequenceT trim_copy_if(const SequenceT& Input, PredicateT IsSpace)
|
Chris@16
|
311 {
|
Chris@16
|
312 BOOST_STRING_TYPENAME
|
Chris@16
|
313 range_const_iterator<SequenceT>::type TrimEnd=
|
Chris@16
|
314 ::boost::algorithm::detail::trim_end(
|
Chris@16
|
315 ::boost::begin(Input),
|
Chris@16
|
316 ::boost::end(Input),
|
Chris@16
|
317 IsSpace);
|
Chris@16
|
318
|
Chris@16
|
319 return SequenceT(
|
Chris@16
|
320 detail::trim_begin(
|
Chris@16
|
321 ::boost::begin(Input),
|
Chris@16
|
322 TrimEnd,
|
Chris@16
|
323 IsSpace),
|
Chris@16
|
324 TrimEnd
|
Chris@16
|
325 );
|
Chris@16
|
326 }
|
Chris@16
|
327
|
Chris@16
|
328 //! Trim
|
Chris@16
|
329 /*!
|
Chris@16
|
330 Remove all leading and trailing spaces from the input.
|
Chris@16
|
331 The result is a trimmed copy of the input
|
Chris@16
|
332
|
Chris@16
|
333 \param Input An input sequence
|
Chris@16
|
334 \param Loc A locale used for 'space' classification
|
Chris@16
|
335 \return A trimmed copy of the input
|
Chris@16
|
336
|
Chris@16
|
337 \note This function provides the strong exception-safety guarantee
|
Chris@16
|
338 */
|
Chris@16
|
339 template<typename SequenceT>
|
Chris@16
|
340 inline SequenceT trim_copy( const SequenceT& Input, const std::locale& Loc=std::locale() )
|
Chris@16
|
341 {
|
Chris@16
|
342 return
|
Chris@16
|
343 ::boost::algorithm::trim_copy_if(
|
Chris@16
|
344 Input,
|
Chris@16
|
345 is_space(Loc) );
|
Chris@16
|
346 }
|
Chris@16
|
347
|
Chris@16
|
348 //! Trim
|
Chris@16
|
349 /*!
|
Chris@16
|
350 Remove all leading and trailing spaces from the input.
|
Chris@16
|
351 The supplied predicate is used to determine which characters are considered spaces.
|
Chris@16
|
352 The input sequence is modified in-place.
|
Chris@16
|
353
|
Chris@16
|
354 \param Input An input sequence
|
Chris@16
|
355 \param IsSpace A unary predicate identifying spaces
|
Chris@16
|
356 */
|
Chris@16
|
357 template<typename SequenceT, typename PredicateT>
|
Chris@16
|
358 inline void trim_if(SequenceT& Input, PredicateT IsSpace)
|
Chris@16
|
359 {
|
Chris@16
|
360 ::boost::algorithm::trim_right_if( Input, IsSpace );
|
Chris@16
|
361 ::boost::algorithm::trim_left_if( Input, IsSpace );
|
Chris@16
|
362 }
|
Chris@16
|
363
|
Chris@16
|
364 //! Trim
|
Chris@16
|
365 /*!
|
Chris@16
|
366 Remove all leading and trailing spaces from the input.
|
Chris@16
|
367 The input sequence is modified in-place.
|
Chris@16
|
368
|
Chris@16
|
369 \param Input An input sequence
|
Chris@16
|
370 \param Loc A locale used for 'space' classification
|
Chris@16
|
371 */
|
Chris@16
|
372 template<typename SequenceT>
|
Chris@16
|
373 inline void trim(SequenceT& Input, const std::locale& Loc=std::locale())
|
Chris@16
|
374 {
|
Chris@16
|
375 ::boost::algorithm::trim_if(
|
Chris@16
|
376 Input,
|
Chris@16
|
377 is_space( Loc ) );
|
Chris@16
|
378 }
|
Chris@16
|
379
|
Chris@16
|
380 } // namespace algorithm
|
Chris@16
|
381
|
Chris@16
|
382 // pull names to the boost namespace
|
Chris@16
|
383 using algorithm::trim_left;
|
Chris@16
|
384 using algorithm::trim_left_if;
|
Chris@16
|
385 using algorithm::trim_left_copy;
|
Chris@16
|
386 using algorithm::trim_left_copy_if;
|
Chris@16
|
387 using algorithm::trim_right;
|
Chris@16
|
388 using algorithm::trim_right_if;
|
Chris@16
|
389 using algorithm::trim_right_copy;
|
Chris@16
|
390 using algorithm::trim_right_copy_if;
|
Chris@16
|
391 using algorithm::trim;
|
Chris@16
|
392 using algorithm::trim_if;
|
Chris@16
|
393 using algorithm::trim_copy;
|
Chris@16
|
394 using algorithm::trim_copy_if;
|
Chris@16
|
395
|
Chris@16
|
396 } // namespace boost
|
Chris@16
|
397
|
Chris@16
|
398 #endif // BOOST_STRING_TRIM_HPP
|