trim_all.hpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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_ALL_HPP
  9. #define BOOST_STRING_TRIM_ALL_HPP
  10. #include <boost/algorithm/string/config.hpp>
  11. #include <boost/algorithm/string/trim.hpp>
  12. #include <boost/algorithm/string/classification.hpp>
  13. #include <boost/algorithm/string/find_format.hpp>
  14. #include <boost/algorithm/string/formatter.hpp>
  15. #include <boost/algorithm/string/finder.hpp>
  16. #include <locale>
  17. /*! \file
  18. Defines trim_all algorithms.
  19. Just like \c trim, \c trim_all removes all trailing and leading spaces from a
  20. sequence (string). In addition, spaces in the middle of the sequence are truncated
  21. to just one character. Space is recognized using given locales.
  22. \c trim_fill acts as trim_all, but the spaces in the middle are replaces with
  23. a user-define sequence of character.
  24. Parametric (\c _if) variants use a predicate (functor) to select which characters
  25. are to be trimmed..
  26. Functions take a selection predicate as a parameter, which is used to determine
  27. whether a character is a space. Common predicates are provided in classification.hpp header.
  28. */
  29. namespace boost {
  30. namespace algorithm {
  31. // multi line trim ----------------------------------------------- //
  32. //! Trim All - parametric
  33. /*!
  34. Remove all leading and trailing spaces from the input and
  35. compress all other spaces to a single character.
  36. The result is a trimmed copy of the input
  37. \param Input An input sequence
  38. \param IsSpace A unary predicate identifying spaces
  39. \return A trimmed copy of the input
  40. */
  41. template<typename SequenceT, typename PredicateT>
  42. inline SequenceT trim_all_copy_if(const SequenceT& Input, PredicateT IsSpace)
  43. {
  44. return
  45. ::boost::find_format_all_copy(
  46. ::boost::trim_copy_if(Input, IsSpace),
  47. ::boost::token_finder(IsSpace, ::boost::token_compress_on),
  48. ::boost::dissect_formatter(::boost::head_finder(1)));
  49. }
  50. //! Trim All
  51. /*!
  52. Remove all leading and trailing spaces from the input and
  53. compress all other spaces to a single character.
  54. The input sequence is modified in-place.
  55. \param Input An input sequence
  56. \param IsSpace A unary predicate identifying spaces
  57. */
  58. template<typename SequenceT, typename PredicateT>
  59. inline void trim_all_if(SequenceT& Input, PredicateT IsSpace)
  60. {
  61. ::boost::trim_if(Input, IsSpace);
  62. ::boost::find_format_all(
  63. Input,
  64. ::boost::token_finder(IsSpace, ::boost::token_compress_on),
  65. ::boost::dissect_formatter(::boost::head_finder(1)));
  66. }
  67. //! Trim All
  68. /*!
  69. Remove all leading and trailing spaces from the input and
  70. compress all other spaces to a single character.
  71. The result is a trimmed copy of the input
  72. \param Input An input sequence
  73. \param Loc A locale used for 'space' classification
  74. \return A trimmed copy of the input
  75. */
  76. template<typename SequenceT>
  77. inline SequenceT trim_all_copy(const SequenceT& Input, const std::locale& Loc =std::locale())
  78. {
  79. return trim_all_copy_if(Input, ::boost::is_space(Loc));
  80. }
  81. //! Trim All
  82. /*!
  83. Remove all leading and trailing spaces from the input and
  84. compress all other spaces to a single character.
  85. The input sequence is modified in-place.
  86. \param Input An input sequence
  87. \param Loc A locale used for 'space' classification
  88. \return A trimmed copy of the input
  89. */
  90. template<typename SequenceT>
  91. inline void trim_all(SequenceT& Input, const std::locale& Loc =std::locale())
  92. {
  93. trim_all_if(Input, ::boost::is_space(Loc));
  94. }
  95. //! Trim Fill - parametric
  96. /*!
  97. Remove all leading and trailing spaces from the input and
  98. replace all every block of consecutive spaces with a fill string
  99. defined by user.
  100. The result is a trimmed copy of the input
  101. \param Input An input sequence
  102. \param Fill A string used to fill the inner spaces
  103. \param IsSpace A unary predicate identifying spaces
  104. \return A trimmed copy of the input
  105. */
  106. template<typename SequenceT, typename RangeT, typename PredicateT>
  107. inline SequenceT trim_fill_copy_if(const SequenceT& Input, const RangeT& Fill, PredicateT IsSpace)
  108. {
  109. return
  110. ::boost::find_format_all_copy(
  111. ::boost::trim_copy_if(Input, IsSpace),
  112. ::boost::token_finder(IsSpace, ::boost::token_compress_on),
  113. ::boost::const_formatter(::boost::as_literal(Fill)));
  114. }
  115. //! Trim Fill
  116. /*!
  117. Remove all leading and trailing spaces from the input and
  118. replace all every block of consecutive spaces with a fill string
  119. defined by user.
  120. The input sequence is modified in-place.
  121. \param Input An input sequence
  122. \param Fill A string used to fill the inner spaces
  123. \param IsSpace A unary predicate identifying spaces
  124. */
  125. template<typename SequenceT, typename RangeT, typename PredicateT>
  126. inline void trim_fill_if(SequenceT& Input, const RangeT& Fill, PredicateT IsSpace)
  127. {
  128. ::boost::trim_if(Input, IsSpace);
  129. ::boost::find_format_all(
  130. Input,
  131. ::boost::token_finder(IsSpace, ::boost::token_compress_on),
  132. ::boost::const_formatter(::boost::as_literal(Fill)));
  133. }
  134. //! Trim Fill
  135. /*!
  136. Remove all leading and trailing spaces from the input and
  137. replace all every block of consecutive spaces with a fill string
  138. defined by user.
  139. The result is a trimmed copy of the input
  140. \param Input An input sequence
  141. \param Fill A string used to fill the inner spaces
  142. \param Loc A locale used for 'space' classification
  143. \return A trimmed copy of the input
  144. */
  145. template<typename SequenceT, typename RangeT>
  146. inline SequenceT trim_fill_copy(const SequenceT& Input, const RangeT& Fill, const std::locale& Loc =std::locale())
  147. {
  148. return trim_fill_copy_if(Input, Fill, ::boost::is_space(Loc));
  149. }
  150. //! Trim Fill
  151. /*!
  152. Remove all leading and trailing spaces from the input and
  153. replace all every block of consecutive spaces with a fill string
  154. defined by user.
  155. The input sequence is modified in-place.
  156. \param Input An input sequence
  157. \param Fill A string used to fill the inner spaces
  158. \param Loc A locale used for 'space' classification
  159. \return A trimmed copy of the input
  160. */
  161. template<typename SequenceT, typename RangeT>
  162. inline void trim_fill(SequenceT& Input, const RangeT& Fill, const std::locale& Loc =std::locale())
  163. {
  164. trim_fill_if(Input, Fill, ::boost::is_space(Loc));
  165. }
  166. } // namespace algorithm
  167. // pull names to the boost namespace
  168. using algorithm::trim_all;
  169. using algorithm::trim_all_if;
  170. using algorithm::trim_all_copy;
  171. using algorithm::trim_all_copy_if;
  172. using algorithm::trim_fill;
  173. using algorithm::trim_fill_if;
  174. using algorithm::trim_fill_copy;
  175. using algorithm::trim_fill_copy_if;
  176. } // namespace boost
  177. #endif // BOOST_STRING_TRIM_ALL_HPP