1 // Boost string_algo library trim.hpp header file ---------------------------//
3 // Copyright Pavol Droba 2002-2003.
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)
9 // See http://www.boost.org/ for updates, documentation, and revision history.
11 #ifndef BOOST_STRING_TRIM_HPP
12 #define BOOST_STRING_TRIM_HPP
14 #include <boost/algorithm/string/config.hpp>
16 #include <boost/range/begin.hpp>
17 #include <boost/range/end.hpp>
18 #include <boost/range/const_iterator.hpp>
19 #include <boost/range/as_literal.hpp>
20 #include <boost/range/iterator_range_core.hpp>
22 #include <boost/algorithm/string/detail/trim.hpp>
23 #include <boost/algorithm/string/classification.hpp>
27 Defines trim algorithms.
28 Trim algorithms are used to remove trailing and leading spaces from a
29 sequence (string). Space is recognized using given locales.
31 Parametric (\c _if) variants use a predicate (functor) to select which characters
33 Functions take a selection predicate as a parameter, which is used to determine
34 whether a character is a space. Common predicates are provided in classification.hpp header.
41 // left trim -----------------------------------------------//
44 //! Left trim - parametric
46 Remove all leading spaces from the input.
47 The supplied predicate is used to determine which characters are considered spaces.
48 The result is a trimmed copy of the input. It is returned as a sequence
49 or copied to the output iterator
51 \param Output An output iterator to which the result will be copied
52 \param Input An input range
53 \param IsSpace A unary predicate identifying spaces
55 An output iterator pointing just after the last inserted character or
58 \note The second variant of this function provides the strong exception-safety guarantee
60 template<typename OutputIteratorT, typename RangeT, typename PredicateT>
61 inline OutputIteratorT trim_left_copy_if(
62 OutputIteratorT Output,
66 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_range(::boost::as_literal(Input));
69 ::boost::algorithm::detail::trim_begin(
70 ::boost::begin(lit_range),
71 ::boost::end(lit_range),
73 ::boost::end(lit_range),
79 //! Left trim - parametric
83 template<typename SequenceT, typename PredicateT>
84 inline SequenceT trim_left_copy_if(const SequenceT& Input, PredicateT IsSpace)
87 ::boost::algorithm::detail::trim_begin(
88 ::boost::begin(Input),
94 //! Left trim - parametric
96 Remove all leading spaces from the input.
97 The result is a trimmed copy of the input.
99 \param Input An input sequence
100 \param Loc a locale used for 'space' classification
101 \return A trimmed copy of the input
103 \note This function provides the strong exception-safety guarantee
105 template<typename SequenceT>
106 inline SequenceT trim_left_copy(const SequenceT& Input, const std::locale& Loc=std::locale())
109 ::boost::algorithm::trim_left_copy_if(
116 Remove all leading spaces from the input. The supplied predicate is
117 used to determine which characters are considered spaces.
118 The input sequence is modified in-place.
120 \param Input An input sequence
121 \param IsSpace A unary predicate identifying spaces
123 template<typename SequenceT, typename PredicateT>
124 inline void trim_left_if(SequenceT& Input, PredicateT IsSpace)
127 ::boost::begin(Input),
128 ::boost::algorithm::detail::trim_begin(
129 ::boost::begin(Input),
136 Remove all leading spaces from the input.
137 The Input sequence is modified in-place.
139 \param Input An input sequence
140 \param Loc A locale used for 'space' classification
142 template<typename SequenceT>
143 inline void trim_left(SequenceT& Input, const std::locale& Loc=std::locale())
145 ::boost::algorithm::trim_left_if(
150 // right trim -----------------------------------------------//
152 //! Right trim - parametric
154 Remove all trailing spaces from the input.
155 The supplied predicate is used to determine which characters are considered spaces.
156 The result is a trimmed copy of the input. It is returned as a sequence
157 or copied to the output iterator
159 \param Output An output iterator to which the result will be copied
160 \param Input An input range
161 \param IsSpace A unary predicate identifying spaces
163 An output iterator pointing just after the last inserted character or
166 \note The second variant of this function provides the strong exception-safety guarantee
168 template<typename OutputIteratorT, typename RangeT, typename PredicateT>
169 inline OutputIteratorT trim_right_copy_if(
170 OutputIteratorT Output,
174 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_range(::boost::as_literal(Input));
177 ::boost::begin(lit_range),
178 ::boost::algorithm::detail::trim_end(
179 ::boost::begin(lit_range),
180 ::boost::end(lit_range),
187 //! Right trim - parametric
191 template<typename SequenceT, typename PredicateT>
192 inline SequenceT trim_right_copy_if(const SequenceT& Input, PredicateT IsSpace)
195 ::boost::begin(Input),
196 ::boost::algorithm::detail::trim_end(
197 ::boost::begin(Input),
205 Remove all trailing spaces from the input.
206 The result is a trimmed copy of the input
208 \param Input An input sequence
209 \param Loc A locale used for 'space' classification
210 \return A trimmed copy of the input
212 \note This function provides the strong exception-safety guarantee
214 template<typename SequenceT>
215 inline SequenceT trim_right_copy(const SequenceT& Input, const std::locale& Loc=std::locale())
218 ::boost::algorithm::trim_right_copy_if(
224 //! Right trim - parametric
226 Remove all trailing spaces from the input.
227 The supplied predicate is used to determine which characters are considered spaces.
228 The input sequence is modified in-place.
230 \param Input An input sequence
231 \param IsSpace A unary predicate identifying spaces
233 template<typename SequenceT, typename PredicateT>
234 inline void trim_right_if(SequenceT& Input, PredicateT IsSpace)
237 ::boost::algorithm::detail::trim_end(
238 ::boost::begin(Input),
248 Remove all trailing spaces from the input.
249 The input sequence is modified in-place.
251 \param Input An input sequence
252 \param Loc A locale used for 'space' classification
254 template<typename SequenceT>
255 inline void trim_right(SequenceT& Input, const std::locale& Loc=std::locale())
257 ::boost::algorithm::trim_right_if(
262 // both side trim -----------------------------------------------//
264 //! Trim - parametric
266 Remove all trailing and leading spaces from the input.
267 The supplied predicate is used to determine which characters are considered spaces.
268 The result is a trimmed copy of the input. It is returned as a sequence
269 or copied to the output iterator
271 \param Output An output iterator to which the result will be copied
272 \param Input An input range
273 \param IsSpace A unary predicate identifying spaces
275 An output iterator pointing just after the last inserted character or
278 \note The second variant of this function provides the strong exception-safety guarantee
280 template<typename OutputIteratorT, typename RangeT, typename PredicateT>
281 inline OutputIteratorT trim_copy_if(
282 OutputIteratorT Output,
286 iterator_range<BOOST_STRING_TYPENAME range_const_iterator<RangeT>::type> lit_range(::boost::as_literal(Input));
288 BOOST_STRING_TYPENAME
289 range_const_iterator<RangeT>::type TrimEnd=
290 ::boost::algorithm::detail::trim_end(
291 ::boost::begin(lit_range),
292 ::boost::end(lit_range),
297 ::boost::begin(lit_range), TrimEnd, IsSpace),
305 //! Trim - parametric
309 template<typename SequenceT, typename PredicateT>
310 inline SequenceT trim_copy_if(const SequenceT& Input, PredicateT IsSpace)
312 BOOST_STRING_TYPENAME
313 range_const_iterator<SequenceT>::type TrimEnd=
314 ::boost::algorithm::detail::trim_end(
315 ::boost::begin(Input),
321 ::boost::begin(Input),
330 Remove all leading and trailing spaces from the input.
331 The result is a trimmed copy of the input
333 \param Input An input sequence
334 \param Loc A locale used for 'space' classification
335 \return A trimmed copy of the input
337 \note This function provides the strong exception-safety guarantee
339 template<typename SequenceT>
340 inline SequenceT trim_copy( const SequenceT& Input, const std::locale& Loc=std::locale() )
343 ::boost::algorithm::trim_copy_if(
350 Remove all leading and trailing spaces from the input.
351 The supplied predicate is used to determine which characters are considered spaces.
352 The input sequence is modified in-place.
354 \param Input An input sequence
355 \param IsSpace A unary predicate identifying spaces
357 template<typename SequenceT, typename PredicateT>
358 inline void trim_if(SequenceT& Input, PredicateT IsSpace)
360 ::boost::algorithm::trim_right_if( Input, IsSpace );
361 ::boost::algorithm::trim_left_if( Input, IsSpace );
366 Remove all leading and trailing spaces from the input.
367 The input sequence is modified in-place.
369 \param Input An input sequence
370 \param Loc A locale used for 'space' classification
372 template<typename SequenceT>
373 inline void trim(SequenceT& Input, const std::locale& Loc=std::locale())
375 ::boost::algorithm::trim_if(
380 } // namespace algorithm
382 // pull names to the boost namespace
383 using algorithm::trim_left;
384 using algorithm::trim_left_if;
385 using algorithm::trim_left_copy;
386 using algorithm::trim_left_copy_if;
387 using algorithm::trim_right;
388 using algorithm::trim_right_if;
389 using algorithm::trim_right_copy;
390 using algorithm::trim_right_copy_if;
391 using algorithm::trim;
392 using algorithm::trim_if;
393 using algorithm::trim_copy;
394 using algorithm::trim_copy_if;
398 #endif // BOOST_STRING_TRIM_HPP