1 | // Boost string_algo library trim.hpp header file ---------------------------// |
---|---|

2 | |

3 | // Copyright Pavol Droba 2002-2003. |

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_TRIM_ALL_HPP |

12 | #define BOOST_STRING_TRIM_ALL_HPP |

13 | |

14 | #include <boost/algorithm/string/config.hpp> |

15 | |

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> |

21 | #include <locale> |

22 | |

23 | /*! \file |

24 | Defines trim_all algorithms. |

25 | |

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. |

29 | |

30 | \c trim_fill acts as trim_all, but the spaces in the middle are replaces with |

31 | a user-define sequence of character. |

32 | |

33 | Parametric (\c _if) variants use a predicate (functor) to select which characters |

34 | are to be trimmed.. |

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. |

37 | |

38 | */ |

39 | |

40 | namespace boost { |

41 | namespace algorithm { |

42 | |

43 | // multi line trim ----------------------------------------------- // |

44 | |

45 | //! Trim All - parametric |

46 | /*! |

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 |

50 | |

51 | \param Input An input sequence |

52 | \param IsSpace A unary predicate identifying spaces |

53 | \return A trimmed copy of the input |

54 | */ |

55 | template<typename SequenceT, typename PredicateT> |

56 | inline SequenceT trim_all_copy_if(const SequenceT& Input, PredicateT IsSpace) |

57 | { |

58 | return |

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))); |

63 | } |

64 | |

65 | |

66 | //! Trim All |

67 | /*! |

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. |

71 | |

72 | \param Input An input sequence |

73 | \param IsSpace A unary predicate identifying spaces |

74 | */ |

75 | template<typename SequenceT, typename PredicateT> |

76 | inline void trim_all_if(SequenceT& Input, PredicateT IsSpace) |

77 | { |

78 | ::boost::trim_if(Input, IsSpace); |

79 | ::boost::find_format_all( |

80 | Input, |

81 | ::boost::token_finder(IsSpace, ::boost::token_compress_on), |

82 | ::boost::dissect_formatter(::boost::head_finder(1))); |

83 | } |

84 | |

85 | |

86 | //! Trim All |

87 | /*! |

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 |

91 | |

92 | \param Input An input sequence |

93 | \param Loc A locale used for 'space' classification |

94 | \return A trimmed copy of the input |

95 | */ |

96 | template<typename SequenceT> |

97 | inline SequenceT trim_all_copy(const SequenceT& Input, const std::locale& Loc =std::locale()) |

98 | { |

99 | return trim_all_copy_if(Input, ::boost::is_space(Loc)); |

100 | } |

101 | |

102 | |

103 | //! Trim All |

104 | /*! |

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. |

108 | |

109 | \param Input An input sequence |

110 | \param Loc A locale used for 'space' classification |

111 | \return A trimmed copy of the input |

112 | */ |

113 | template<typename SequenceT> |

114 | inline void trim_all(SequenceT& Input, const std::locale& Loc =std::locale()) |

115 | { |

116 | trim_all_if(Input, ::boost::is_space(Loc)); |

117 | } |

118 | |

119 | |

120 | //! Trim Fill - parametric |

121 | /*! |

122 | Remove all leading and trailing spaces from the input and |

123 | replace all every block of consecutive spaces with a fill string |

124 | defined by user. |

125 | The result is a trimmed copy of the input |

126 | |

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 |

131 | */ |

132 | template<typename SequenceT, typename RangeT, typename PredicateT> |

133 | inline SequenceT trim_fill_copy_if(const SequenceT& Input, const RangeT& Fill, PredicateT IsSpace) |

134 | { |

135 | return |

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))); |

140 | } |

141 | |

142 | |

143 | //! Trim Fill |

144 | /*! |

145 | Remove all leading and trailing spaces from the input and |

146 | replace all every block of consecutive spaces with a fill string |

147 | defined by user. |

148 | The input sequence is modified in-place. |

149 | |

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 |

153 | */ |

154 | template<typename SequenceT, typename RangeT, typename PredicateT> |

155 | inline void trim_fill_if(SequenceT& Input, const RangeT& Fill, PredicateT IsSpace) |

156 | { |

157 | ::boost::trim_if(Input, IsSpace); |

158 | ::boost::find_format_all( |

159 | Input, |

160 | ::boost::token_finder(IsSpace, ::boost::token_compress_on), |

161 | ::boost::const_formatter(::boost::as_literal(Fill))); |

162 | } |

163 | |

164 | |

165 | //! Trim Fill |

166 | /*! |

167 | Remove all leading and trailing spaces from the input and |

168 | replace all every block of consecutive spaces with a fill string |

169 | defined by user. |

170 | The result is a trimmed copy of the input |

171 | |

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 |

176 | */ |

177 | template<typename SequenceT, typename RangeT> |

178 | inline SequenceT trim_fill_copy(const SequenceT& Input, const RangeT& Fill, const std::locale& Loc =std::locale()) |

179 | { |

180 | return trim_fill_copy_if(Input, Fill, ::boost::is_space(Loc)); |

181 | } |

182 | |

183 | |

184 | //! Trim Fill |

185 | /*! |

186 | Remove all leading and trailing spaces from the input and |

187 | replace all every block of consecutive spaces with a fill string |

188 | defined by user. |

189 | The input sequence is modified in-place. |

190 | |

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 |

195 | */ |

196 | template<typename SequenceT, typename RangeT> |

197 | inline void trim_fill(SequenceT& Input, const RangeT& Fill, const std::locale& Loc =std::locale()) |

198 | { |

199 | trim_fill_if(Input, Fill, ::boost::is_space(Loc)); |

200 | } |

201 | |

202 | |

203 | } // namespace algorithm |

204 | |

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; |

214 | |

215 | } // namespace boost |

216 | |

217 | #endif // BOOST_STRING_TRIM_ALL_HPP |

218 |