1 // Boost string_algo library trim.hpp header file ---------------------------//
3 // Copyright Pavol Droba 2002-2003. Use, modification and
4 // distribution is subject to the Boost Software License, Version
5 // 1.0. (See accompanying file LICENSE_1_0.txt or copy at
6 // http://www.boost.org/LICENSE_1_0.txt)
8 // See http://www.boost.org for updates, documentation, and revision history.
10 #ifndef BOOST_STRING_TRIM_HPP
11 #define BOOST_STRING_TRIM_HPP
13 #include <boost/algorithm/string/config.hpp>
15 #include <boost/range/begin.hpp>
16 #include <boost/range/end.hpp>
17 #include <boost/range/const_iterator.hpp>
19 #include <boost/algorithm/string/detail/trim.hpp>
20 #include <boost/algorithm/string/classification.hpp>
24 Defines trim algorithms.
25 Trim algorithms are used to remove trailing and leading spaces from a
26 sequence (string). Space is recognized using given locales.
28 Parametric (\c _if) variants use a predicate (functor) to select which characters
30 Functions take a selection predicate as a parameter, which is used to determine
31 whether a character is a space. Common predicates are provided in classification.hpp header.
38 // left trim -----------------------------------------------//
41 //! Left trim - parametric
43 Remove all leading spaces from the input.
44 The supplied predicate is used to determine which characters are considered spaces.
45 The result is a trimmed copy of the input. It is returned as a sequence
46 or copied to the output iterator
48 \param Output An output iterator to which the result will be copied
49 \param Input An input range
50 \param IsSpace An unary predicate identifying spaces
52 An output iterator pointing just after the last inserted character or
55 \note The second variant of this function provides the strong exception-safety guarantee
57 template<typename OutputIteratorT, typename RangeT, typename PredicateT>
58 inline OutputIteratorT trim_left_copy_if(
59 OutputIteratorT Output,
64 ::boost::algorithm::detail::trim_begin(
74 //! Left trim - parametric
78 template<typename SequenceT, typename PredicateT>
79 inline SequenceT trim_left_copy_if(const SequenceT& Input, PredicateT IsSpace)
82 ::boost::algorithm::detail::trim_begin(
89 //! Left trim - parametric
91 Remove all leading spaces from the input.
92 The result is a trimmed copy of the input.
94 \param Input An input sequence
95 \param Loc a locale used for 'space' classification
96 \return A trimmed copy of the input
98 \note This function provides the strong exception-safety guarantee
100 template<typename SequenceT>
101 inline SequenceT trim_left_copy(const SequenceT& Input, const std::locale& Loc=std::locale())
111 Remove all leading spaces from the input. The supplied predicate is
112 used to determine which characters are considered spaces.
113 The input sequence is modified in-place.
115 \param Input An input sequence
116 \param IsSpace An unary predicate identifying spaces
118 template<typename SequenceT, typename PredicateT>
119 inline void trim_left_if(SequenceT& Input, PredicateT IsSpace)
123 ::boost::algorithm::detail::trim_begin(
131 Remove all leading spaces from the input.
132 The Input sequence is modified in-place.
134 \param Input An input sequence
135 \param Loc A locale used for 'space' classification
137 template<typename SequenceT>
138 inline void trim_left(SequenceT& Input, const std::locale& Loc=std::locale())
145 // right trim -----------------------------------------------//
147 //! Right trim - parametric
149 Remove all trailing spaces from the input.
150 The supplied predicate is used to determine which characters are considered spaces.
151 The result is a trimmed copy of the input. It is returned as a sequence
152 or copied to the output iterator
154 \param Output An output iterator to which the result will be copied
155 \param Input An input range
156 \param IsSpace An unary predicate identifying spaces
158 An output iterator pointing just after the last inserted character or
161 \note The second variant of this function provides the strong exception-safety guarantee
163 template<typename OutputIteratorT, typename RangeT, typename PredicateT>
164 inline OutputIteratorT trim_right_copy_if(
165 OutputIteratorT Output,
171 ::boost::algorithm::detail::trim_end(
180 //! Right trim - parametric
184 template<typename SequenceT, typename PredicateT>
185 inline SequenceT trim_right_copy_if(const SequenceT& Input, PredicateT IsSpace)
189 ::boost::algorithm::detail::trim_end(
198 Remove all trailing spaces from the input.
199 The result is a trimmed copy of the input
201 \param Input An input sequence
202 \param Loc A locale used for 'space' classification
203 \return A trimmed copy of the input
205 \note This function provides the strong exception-safety guarantee
207 template<typename SequenceT>
208 inline SequenceT trim_right_copy(const SequenceT& Input, const std::locale& Loc=std::locale())
217 //! Right trim - parametric
219 Remove all trailing spaces from the input.
220 The supplied predicate is used to determine which characters are considered spaces.
221 The input sequence is modified in-place.
223 \param Input An input sequence
224 \param IsSpace An unary predicate identifying spaces
226 template<typename SequenceT, typename PredicateT>
227 inline void trim_right_if(SequenceT& Input, PredicateT IsSpace)
230 ::boost::algorithm::detail::trim_end(
241 Remove all trailing spaces from the input.
242 The input sequence is modified in-place.
244 \param Input An input sequence
245 \param Loc A locale used for 'space' classification
247 template<typename SequenceT>
248 inline void trim_right(SequenceT& Input, const std::locale& Loc=std::locale())
255 // both side trim -----------------------------------------------//
257 //! Trim - parametric
259 Remove all trailing and leading spaces from the input.
260 The supplied predicate is used to determine which characters are considered spaces.
261 The result is a trimmed copy of the input. It is returned as a sequence
262 or copied to the output iterator
264 \param Output An output iterator to which the result will be copied
265 \param Input An input range
266 \param IsSpace An unary predicate identifying spaces
268 An output iterator pointing just after the last inserted character or
271 \note The second variant of this function provides the strong exception-safety guarantee
273 template<typename OutputIteratorT, typename RangeT, typename PredicateT>
274 inline OutputIteratorT trim_copy_if(
275 OutputIteratorT Output,
279 BOOST_STRING_TYPENAME
280 range_const_iterator<RangeT>::type TrimEnd=
281 ::boost::algorithm::detail::trim_end(
288 begin(Input), TrimEnd, IsSpace),
296 //! Trim - parametric
300 template<typename SequenceT, typename PredicateT>
301 inline SequenceT trim_copy_if(const SequenceT& Input, PredicateT IsSpace)
303 BOOST_STRING_TYPENAME
304 range_const_iterator<SequenceT>::type TrimEnd=
305 ::boost::algorithm::detail::trim_end(
321 Remove all leading and trailing spaces from the input.
322 The result is a trimmed copy of the input
324 \param Input An input sequence
325 \param Loc A locale used for 'space' classification
326 \return A trimmed copy of the input
328 \note This function provides the strong exception-safety guarantee
330 template<typename SequenceT>
331 inline SequenceT trim_copy( const SequenceT& Input, const std::locale& Loc=std::locale() )
341 Remove all leading and trailing spaces from the input.
342 The supplied predicate is used to determine which characters are considered spaces.
343 The input sequence is modified in-place.
345 \param Input An input sequence
346 \param IsSpace An unary predicate identifying spaces
348 template<typename SequenceT, typename PredicateT>
349 inline void trim_if(SequenceT& Input, PredicateT IsSpace)
351 trim_right_if( Input, IsSpace );
352 trim_left_if( Input, IsSpace );
357 Remove all leading and trailing spaces from the input.
358 The input sequence is modified in-place.
360 \param Input An input sequence
361 \param Loc A locale used for 'space' classification
363 template<typename SequenceT>
364 inline void trim(SequenceT& Input, const std::locale& Loc=std::locale())
371 } // namespace algorithm
373 // pull names to the boost namespace
374 using algorithm::trim_left;
375 using algorithm::trim_left_if;
376 using algorithm::trim_left_copy;
377 using algorithm::trim_left_copy_if;
378 using algorithm::trim_right;
379 using algorithm::trim_right_if;
380 using algorithm::trim_right_copy;
381 using algorithm::trim_right_copy_if;
382 using algorithm::trim;
383 using algorithm::trim_if;
384 using algorithm::trim_copy;
385 using algorithm::trim_copy_if;
389 #endif // BOOST_STRING_TRIM_HPP