trim.hpp 14 KB

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