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_ALL_HPP
12 #define BOOST_STRING_TRIM_ALL_HPP
14 #include <boost/algorithm/string/config.hpp>
16 #include <boost/algorithm/string/trim.hpp>
17 #include <boost/algorithm/string/classification.hpp>
18 #include <boost/algorithm/string/find_format.hpp>
19 #include <boost/algorithm/string/formatter.hpp>
20 #include <boost/algorithm/string/finder.hpp>
24 Defines trim_all algorithms.
26 Just like \c trim, \c trim_all removes all trailing and leading spaces from a
27 sequence (string). In addition, spaces in the middle of the sequence are truncated
28 to just one character. Space is recognized using given locales.
30 \c trim_fill acts as trim_all, but the spaces in the middle are replaces with
31 a user-define sequence of character.
33 Parametric (\c _if) variants use a predicate (functor) to select which characters
35 Functions take a selection predicate as a parameter, which is used to determine
36 whether a character is a space. Common predicates are provided in classification.hpp header.
43 // multi line trim ----------------------------------------------- //
45 //! Trim All - parametric
47 Remove all leading and trailing spaces from the input and
48 compress all other spaces to a single character.
49 The result is a trimmed copy of the input
51 \param Input An input sequence
52 \param IsSpace A unary predicate identifying spaces
53 \return A trimmed copy of the input
55 template<typename SequenceT, typename PredicateT>
56 inline SequenceT trim_all_copy_if(const SequenceT& Input, PredicateT IsSpace)
59 ::boost::find_format_all_copy(
60 ::boost::trim_copy_if(Input, IsSpace),
61 ::boost::token_finder(IsSpace, ::boost::token_compress_on),
62 ::boost::dissect_formatter(::boost::head_finder(1)));
68 Remove all leading and trailing spaces from the input and
69 compress all other spaces to a single character.
70 The input sequence is modified in-place.
72 \param Input An input sequence
73 \param IsSpace A unary predicate identifying spaces
75 template<typename SequenceT, typename PredicateT>
76 inline void trim_all_if(SequenceT& Input, PredicateT IsSpace)
78 ::boost::trim_if(Input, IsSpace);
79 ::boost::find_format_all(
81 ::boost::token_finder(IsSpace, ::boost::token_compress_on),
82 ::boost::dissect_formatter(::boost::head_finder(1)));
88 Remove all leading and trailing spaces from the input and
89 compress all other spaces to a single character.
90 The result is a trimmed copy of the input
92 \param Input An input sequence
93 \param Loc A locale used for 'space' classification
94 \return A trimmed copy of the input
96 template<typename SequenceT>
97 inline SequenceT trim_all_copy(const SequenceT& Input, const std::locale& Loc =std::locale())
99 return trim_all_copy_if(Input, ::boost::is_space(Loc));
105 Remove all leading and trailing spaces from the input and
106 compress all other spaces to a single character.
107 The input sequence is modified in-place.
109 \param Input An input sequence
110 \param Loc A locale used for 'space' classification
111 \return A trimmed copy of the input
113 template<typename SequenceT>
114 inline void trim_all(SequenceT& Input, const std::locale& Loc =std::locale())
116 trim_all_if(Input, ::boost::is_space(Loc));
120 //! Trim Fill - parametric
122 Remove all leading and trailing spaces from the input and
123 replace all every block of consecutive spaces with a fill string
125 The result is a trimmed copy of the input
127 \param Input An input sequence
128 \param Fill A string used to fill the inner spaces
129 \param IsSpace A unary predicate identifying spaces
130 \return A trimmed copy of the input
132 template<typename SequenceT, typename RangeT, typename PredicateT>
133 inline SequenceT trim_fill_copy_if(const SequenceT& Input, const RangeT& Fill, PredicateT IsSpace)
136 ::boost::find_format_all_copy(
137 ::boost::trim_copy_if(Input, IsSpace),
138 ::boost::token_finder(IsSpace, ::boost::token_compress_on),
139 ::boost::const_formatter(::boost::as_literal(Fill)));
145 Remove all leading and trailing spaces from the input and
146 replace all every block of consecutive spaces with a fill string
148 The input sequence is modified in-place.
150 \param Input An input sequence
151 \param Fill A string used to fill the inner spaces
152 \param IsSpace A unary predicate identifying spaces
154 template<typename SequenceT, typename RangeT, typename PredicateT>
155 inline void trim_fill_if(SequenceT& Input, const RangeT& Fill, PredicateT IsSpace)
157 ::boost::trim_if(Input, IsSpace);
158 ::boost::find_format_all(
160 ::boost::token_finder(IsSpace, ::boost::token_compress_on),
161 ::boost::const_formatter(::boost::as_literal(Fill)));
167 Remove all leading and trailing spaces from the input and
168 replace all every block of consecutive spaces with a fill string
170 The result is a trimmed copy of the input
172 \param Input An input sequence
173 \param Fill A string used to fill the inner spaces
174 \param Loc A locale used for 'space' classification
175 \return A trimmed copy of the input
177 template<typename SequenceT, typename RangeT>
178 inline SequenceT trim_fill_copy(const SequenceT& Input, const RangeT& Fill, const std::locale& Loc =std::locale())
180 return trim_fill_copy_if(Input, Fill, ::boost::is_space(Loc));
186 Remove all leading and trailing spaces from the input and
187 replace all every block of consecutive spaces with a fill string
189 The input sequence is modified in-place.
191 \param Input An input sequence
192 \param Fill A string used to fill the inner spaces
193 \param Loc A locale used for 'space' classification
194 \return A trimmed copy of the input
196 template<typename SequenceT, typename RangeT>
197 inline void trim_fill(SequenceT& Input, const RangeT& Fill, const std::locale& Loc =std::locale())
199 trim_fill_if(Input, Fill, ::boost::is_space(Loc));
203 } // namespace algorithm
205 // pull names to the boost namespace
206 using algorithm::trim_all;
207 using algorithm::trim_all_if;
208 using algorithm::trim_all_copy;
209 using algorithm::trim_all_copy_if;
210 using algorithm::trim_fill;
211 using algorithm::trim_fill_if;
212 using algorithm::trim_fill_copy;
213 using algorithm::trim_fill_copy_if;
217 #endif // BOOST_STRING_TRIM_ALL_HPP