You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

553 lines
18 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/string/trim_functions.hpp
  3. *
  4. * Purpose: String utility functions for trimming and removing string contents.
  5. *
  6. * Created: 25th April 2005
  7. * Updated: 10th August 2009
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2005-2009, Matthew Wilson and Synesis Software
  12. * All rights reserved.
  13. *
  14. * Redistribution and use in source and binary forms, with or without
  15. * modification, are permitted provided that the following conditions are met:
  16. *
  17. * - Redistributions of source code must retain the above copyright notice, this
  18. * list of conditions and the following disclaimer.
  19. * - Redistributions in binary form must reproduce the above copyright notice,
  20. * this list of conditions and the following disclaimer in the documentation
  21. * and/or other materials provided with the distribution.
  22. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  23. * any contributors may be used to endorse or promote products derived from
  24. * this software without specific prior written permission.
  25. *
  26. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  27. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  28. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  29. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  30. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  31. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  32. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  33. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  34. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  35. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. * ////////////////////////////////////////////////////////////////////// */
  39. /** \file stlsoft/string/trim_functions.hpp
  40. *
  41. * \brief [C++ only] String utility functions for trimming and removing
  42. * string contents
  43. * (\ref group__library__string "String" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_TRIM_FUNCTIONS
  46. #define STLSOFT_INCL_STLSOFT_STRING_HPP_TRIM_FUNCTIONS
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_INCL_STLSOFT_STRING_HPP_TRIM_FUNCTIONS_MAJOR 2
  49. # define STLSOFT_VER_INCL_STLSOFT_STRING_HPP_TRIM_FUNCTIONS_MINOR 1
  50. # define STLSOFT_VER_INCL_STLSOFT_STRING_HPP_TRIM_FUNCTIONS_REVISION 8
  51. # define STLSOFT_VER_INCL_STLSOFT_STRING_HPP_TRIM_FUNCTIONS_EDIT 37
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Compatibility
  55. */
  56. /*
  57. [Incompatibilies-start]
  58. STLSOFT_COMPILER_IS_MSVC: _MSC_VER<1200
  59. STLSOFT_COMPILER_IS_WATCOM:
  60. [Incompatibilies-end]
  61. */
  62. /* /////////////////////////////////////////////////////////////////////////
  63. * Includes
  64. */
  65. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  66. # include <stlsoft/stlsoft.h>
  67. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  68. #if defined(STLSOFT_COMPILER_IS_MSVC) && \
  69. _MSC_VER < 1200
  70. # error stlsoft/string/trim_functions.hpp is not compatible with Visual C++ 5.0 or earlier
  71. #endif /* compiler */
  72. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_TRAITS
  73. # include <stlsoft/string/string_traits.hpp>
  74. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_TRAITS */
  75. #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
  76. # include <stlsoft/shims/access/string.hpp>
  77. #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
  78. #ifndef STLSOFT_INCL_ALGORITHM
  79. # define STLSOFT_INCL_ALGORITHM
  80. # include <algorithm>
  81. #endif /* !STLSOFT_INCL_ALGORITHM */
  82. #ifndef STLSOFT_INCL_FUNCTIONAL
  83. # define STLSOFT_INCL_FUNCTIONAL
  84. # include <functional>
  85. #endif /* !STLSOFT_INCL_FUNCTIONAL */
  86. #ifndef STLSOFT_INCL_H_STRING
  87. # define STLSOFT_INCL_H_STRING
  88. # include <string.h>
  89. #endif /* !STLSOFT_INCL_H_STRING */
  90. #ifndef STLSOFT_INCL_H_WCHAR
  91. # define STLSOFT_INCL_H_WCHAR
  92. # include <wchar.h>
  93. #endif /* !STLSOFT_INCL_H_WCHAR */
  94. #ifdef STLSOFT_UNITTEST
  95. # include <string>
  96. # ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_SIMPLE_STRING
  97. # include <stlsoft/string/simple_string.hpp>
  98. # endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_SIMPLE_STRING */
  99. # ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_VIEW
  100. # include <stlsoft/string/string_view.hpp>
  101. # endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_VIEW */
  102. #endif /* STLSOFT_UNITTEST */
  103. /* /////////////////////////////////////////////////////////////////////////
  104. * Namespace
  105. */
  106. #ifndef _STLSOFT_NO_NAMESPACE
  107. namespace stlsoft
  108. {
  109. #endif /* _STLSOFT_NO_NAMESPACE */
  110. /* ////////////////////////////////////////////////////////////////////// */
  111. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  112. struct trim_functions_impl
  113. {
  114. template <ss_typename_param_k I>
  115. static bool is_in_range(I from, I to, I it)
  116. {
  117. for(; from != to; ++from)
  118. {
  119. if(from == it)
  120. {
  121. return true;
  122. }
  123. }
  124. return false;
  125. }
  126. };
  127. inline char const* strchr_select(char const* s, char ch)
  128. {
  129. return ::strchr(s, ch);
  130. }
  131. inline wchar_t const* strchr_select(wchar_t const* s, wchar_t ch)
  132. {
  133. return ::wcschr(s, ch);
  134. }
  135. template <ss_typename_param_k C>
  136. inline C const* default_trim_chars(C const* , ss_size_t& n)
  137. {
  138. static const C s_trimChars[] =
  139. {
  140. ' '
  141. , '\n'
  142. , '\r'
  143. , '\t'
  144. , '\v'
  145. , '\0'
  146. };
  147. n = STLSOFT_NUM_ELEMENTS(s_trimChars) - 1;
  148. return &s_trimChars[0];
  149. }
  150. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  151. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  152. #if defined(STLSOFT_COMPILER_IS_MSVC) && \
  153. _MSC_VER < 1310
  154. template< ss_typename_param_k S
  155. , ss_typename_param_k C
  156. >
  157. inline S& trim_left_impl(S& str, C const* trimChars)
  158. #else /* ? compiler */
  159. template<ss_typename_param_k S>
  160. inline S& trim_left_impl(S& str, ss_typename_type_k string_traits<S>::char_type const* trimChars)
  161. #endif /* compiler */
  162. {
  163. // 1. 'use' the std namespace here, otherwise get totally clogged in stlsoft_ns_qual_std(XX)
  164. #ifdef STLSOFT_CF_std_NAMESPACE
  165. using namespace std;
  166. #endif /* STLSOFT_CF_std_NAMESPACE */
  167. // 2. typedef the string traits
  168. typedef string_traits<S> string_traits_t;
  169. // 3. typedef the char_t
  170. typedef ss_typename_type_k string_traits_t::char_type char_t;
  171. // 4. typedef the iterator type(s)
  172. typedef ss_typename_type_k string_traits_t::const_iterator iterator_t;
  173. // 5. Since ::stlsoft::strchr is an overloaded function, we disambiguate by casting to the type required
  174. char_t const* (*pfn)(char_t const*, char_t) = ::stlsoft::strchr_select;
  175. // Get an iterator to the first element that
  176. const iterator_t it_b = str.begin();
  177. const iterator_t it_e = str.end();
  178. const iterator_t it_l = find_if(it_b, it_e, not1(bind1st(ptr_fun(pfn), trimChars)));
  179. iterator_t const& it_r = it_e;
  180. return string_traits_t::assign_inplace(str, it_l, it_r);
  181. }
  182. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  183. /** \brief Trims all the leading whitespace characters, if any, from a string
  184. *
  185. * \ingroup group__library__string
  186. */
  187. template<ss_typename_param_k S>
  188. inline S& trim_left(S& str)
  189. {
  190. // 1. typedef the string traits
  191. typedef string_traits<S> string_traits_t;
  192. # if defined(STLSOFT_COMPILER_IS_BORLAND) && \
  193. __BORLANDC__ < 0x0564
  194. // This is needed here to tell the Borland compiler that it's a type!
  195. string_traits_t::char_type *p = NULL;
  196. # endif /* compiler */
  197. // 2. typedef the char_t
  198. typedef ss_typename_type_k string_traits_t::char_type char_t;
  199. ss_size_t n;
  200. char_t const* trimChars = default_trim_chars(static_cast<char_t const*>(0), n);
  201. STLSOFT_SUPPRESS_UNUSED(n);
  202. return trim_left_impl(str, trimChars);
  203. }
  204. /** \brief
  205. *
  206. * \ingroup group__library__string
  207. */
  208. template< ss_typename_param_k S0
  209. , ss_typename_param_k S1
  210. >
  211. inline S0& trim_left(S0& str, S1 const& trimChars)
  212. {
  213. return trim_left_impl(str, stlsoft_ns_qual(c_str_ptr)(trimChars));
  214. }
  215. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  216. # if defined(__BORLANDC__)
  217. # pragma warn -8091 // Otherwise BC++ complains that rbegin()/rend() returns passed to find_if() are output iterators
  218. # pragma warn -8092 // Otherwise BC++ complains that rbegin()/rend() returns passed to find_if() are not iterators
  219. # endif /* compiler */
  220. #if defined(STLSOFT_COMPILER_IS_MSVC) && \
  221. _MSC_VER < 1310
  222. template< ss_typename_param_k S
  223. , ss_typename_param_k C
  224. >
  225. inline S& trim_right_impl(S& str, C const* trimChars)
  226. #else /* ? compiler */
  227. template<ss_typename_param_k S>
  228. inline S& trim_right_impl(S& str, ss_typename_type_k string_traits<S>::char_type const* trimChars)
  229. #endif /* compiler */
  230. {
  231. S const& cstr = str;
  232. // 1. 'use' the std namespace here, otherwise get totally clogged in stlsoft_ns_qual_std(XX)
  233. #ifdef STLSOFT_CF_std_NAMESPACE
  234. using namespace std;
  235. #endif /* STLSOFT_CF_std_NAMESPACE */
  236. // 2. typedef the string traits
  237. typedef string_traits<S> string_traits_t;
  238. // 3. typedef the char_t
  239. typedef ss_typename_type_k string_traits_t::char_type char_t;
  240. // 4. typedef the iterator type(s)
  241. typedef ss_typename_type_k string_traits_t::const_iterator iterator_t;
  242. typedef ss_typename_type_k string_traits_t::const_reverse_iterator reverse_iterator_t;
  243. // 5. Since ::stlsoft::strchr is an overloaded function, we disambiguate by casting to the type required
  244. char_t const* (*pfn)(char_t const*, char_t) = ::stlsoft::strchr_select;
  245. // Get a (reverse) iterator to the first element that is not a space === find_last_not_of
  246. const iterator_t it_b = cstr.begin();
  247. // const iterator_t it_e = cstr.end();
  248. const iterator_t it_l = it_b;
  249. const reverse_iterator_t rit = find_if(cstr.rbegin(), cstr.rend(), not1(bind1st(ptr_fun(pfn), trimChars)));
  250. const iterator_t it_r = rit.base();
  251. return string_traits_t::assign_inplace(str, it_l, it_r);
  252. }
  253. # if defined(__BORLANDC__)
  254. # pragma warn .8092
  255. # pragma warn .8091
  256. # endif /* compiler */
  257. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  258. /** \brief Trims all the trailing whitespace characters, if any, from a string
  259. *
  260. * \ingroup group__library__string
  261. */
  262. template<ss_typename_param_k S>
  263. inline S& trim_right(S& str)
  264. {
  265. // 1. typedef the string traits
  266. typedef string_traits<S> string_traits_t;
  267. # if defined(STLSOFT_COMPILER_IS_BORLAND) && \
  268. __BORLANDC__ < 0x0564
  269. // This is needed here to tell the Borland compiler that it's a type!
  270. string_traits_t::char_type *p = NULL;
  271. # endif /* compiler */
  272. // 2. typedef the char_t
  273. typedef ss_typename_type_k string_traits_t::char_type char_t;
  274. ss_size_t n;
  275. char_t const* trimChars = default_trim_chars(static_cast<char_t const*>(0), n);
  276. STLSOFT_SUPPRESS_UNUSED(n);
  277. return trim_right_impl(str, trimChars);
  278. }
  279. /** \brief
  280. *
  281. * \ingroup group__library__string
  282. */
  283. template< ss_typename_param_k S0
  284. , ss_typename_param_k S1
  285. >
  286. inline S0& trim_right(S0& str, S1 const& trimChars)
  287. {
  288. return trim_right_impl(str, stlsoft_ns_qual(c_str_ptr)(trimChars));
  289. }
  290. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  291. # if defined(__BORLANDC__)
  292. # pragma warn -8091 // Otherwise BC++ complains that rbegin()/rend() returns passed to find_if() are output iterators
  293. # pragma warn -8092 // Otherwise BC++ complains that rbegin()/rend() returns passed to find_if() are not iterators
  294. # endif /* compiler */
  295. #if defined(STLSOFT_COMPILER_IS_MSVC) && \
  296. _MSC_VER < 1310
  297. template< ss_typename_param_k S
  298. , ss_typename_param_k C
  299. >
  300. inline S& trim_all_impl(S& str, C const* trimChars)
  301. #else /* ? compiler */
  302. template<ss_typename_param_k S>
  303. inline S& trim_all_impl(S& str, ss_typename_type_k string_traits<S>::char_type const* trimChars)
  304. #endif /* compiler */
  305. {
  306. S const& cstr = str;
  307. // 1. 'use' the std namespace here, otherwise get totally clogged in stlsoft_ns_qual_std(XX)
  308. #ifdef STLSOFT_CF_std_NAMESPACE
  309. using namespace std;
  310. #endif /* STLSOFT_CF_std_NAMESPACE */
  311. // 2. typedef the string traits
  312. typedef string_traits<S> string_traits_t;
  313. // 3. typedef the char_t
  314. typedef ss_typename_type_k string_traits_t::char_type char_t;
  315. // 4. typedef the iterator type(s)
  316. typedef ss_typename_type_k string_traits_t::const_iterator iterator_t;
  317. typedef ss_typename_type_k string_traits_t::const_reverse_iterator reverse_iterator_t;
  318. // 5. Since ::stlsoft::strchr is an overloaded function, we disambiguate by casting to the type required
  319. char_t const* (*pfn)(char_t const*, char_t) = ::stlsoft::strchr_select;
  320. // Get an iterator to the first element that
  321. const iterator_t it_b = cstr.begin();
  322. const iterator_t it_e = cstr.end();
  323. const iterator_t it_l = find_if(it_b, it_e, not1(bind1st(ptr_fun(pfn), trimChars)));
  324. STLSOFT_MESSAGE_ASSERT("iterator not in range", (it_e == it_l || trim_functions_impl::is_in_range(it_b, it_e, it_l)));
  325. iterator_t it_r;
  326. if(it_l == it_e)
  327. {
  328. it_r = it_e;
  329. }
  330. else
  331. {
  332. const reverse_iterator_t itr_b = cstr.rbegin();
  333. const reverse_iterator_t itr_e = cstr.rend();
  334. const reverse_iterator_t rit = find_if(itr_b, itr_e, not1(bind1st(ptr_fun(pfn), trimChars)));
  335. STLSOFT_MESSAGE_ASSERT("iterator not in range", (itr_e == rit || trim_functions_impl::is_in_range(itr_b, itr_e, rit)));
  336. it_r = rit.base();
  337. STLSOFT_MESSAGE_ASSERT("iterator not in range", (it_e == it_r || trim_functions_impl::is_in_range(it_b, it_e, it_r)));
  338. }
  339. STLSOFT_MESSAGE_ASSERT("right-hand iterator not in range [left-hand, end)", (it_e == it_r || trim_functions_impl::is_in_range(it_l, it_e, it_r)));
  340. return string_traits_t::assign_inplace(str, it_l, it_r);
  341. }
  342. # if defined(__BORLANDC__)
  343. # pragma warn .8092
  344. # pragma warn .8091
  345. # endif /* compiler */
  346. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  347. /** \brief Trims all the leading and trailing whitespace characters, if any, from a string
  348. *
  349. * \ingroup group__library__string
  350. */
  351. template<ss_typename_param_k S>
  352. inline S& trim_all(S& str)
  353. {
  354. // 1. typedef the string traits
  355. typedef string_traits<S> string_traits_t;
  356. # if defined(STLSOFT_COMPILER_IS_BORLAND) && \
  357. __BORLANDC__ < 0x0564
  358. // This is needed here to tell the Borland compiler that it's a type!
  359. string_traits_t::char_type *p = NULL;
  360. # endif /* compiler */
  361. // 2. typedef the char_t
  362. typedef ss_typename_type_k string_traits_t::char_type char_t;
  363. ss_size_t n;
  364. char_t const* trimChars = default_trim_chars(static_cast<char_t const*>(0), n);
  365. STLSOFT_SUPPRESS_UNUSED(n);
  366. return trim_all_impl(str, trimChars);
  367. }
  368. /** \brief
  369. *
  370. * \ingroup group__library__string
  371. */
  372. template< ss_typename_param_k S0
  373. , ss_typename_param_k S1
  374. >
  375. inline S0& trim_all(S0& str, S1 const& trimChars)
  376. {
  377. return trim_all_impl(str, stlsoft_ns_qual(c_str_ptr)(trimChars));
  378. }
  379. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  380. #if defined(STLSOFT_COMPILER_IS_MSVC) && \
  381. _MSC_VER < 1310
  382. template< ss_typename_param_k S
  383. , ss_typename_param_k C
  384. >
  385. inline S& remove_all_impl(S& str, C const* removeChars)
  386. #else /* ? compiler */
  387. template<ss_typename_param_k S>
  388. inline S& remove_all_impl(S& str, ss_typename_type_k S::value_type const* removeChars)
  389. #endif /* compiler */
  390. {
  391. // 1. 'use' the std namespace here, otherwise get totally clogged in stlsoft_ns_qual_std(XX)
  392. #ifdef STLSOFT_CF_std_NAMESPACE
  393. using namespace std;
  394. #endif /* STLSOFT_CF_std_NAMESPACE */
  395. // 2. typedef the string traits
  396. typedef string_traits<S> string_traits_t;
  397. // 3. typedef the char_t
  398. typedef ss_typename_type_k string_traits_t::char_type char_t;
  399. // 4. typedef the iterator type(s)
  400. typedef ss_typename_type_k string_traits_t::iterator iterator_t;
  401. // 5. Since ::stlsoft::strchr is an overloaded function, we disambiguate by casting to the type required
  402. char_t const* (*pfn)(char_t const*, char_t) = ::stlsoft::strchr_select;
  403. // Get an iterator to the first element that
  404. iterator_t it_b = str.begin();
  405. iterator_t it_e = str.end();
  406. const iterator_t it_l = it_b;
  407. const iterator_t it_r = remove_if(it_b, it_e, bind1st(ptr_fun(pfn), removeChars));
  408. return string_traits_t::assign_inplace(str, it_l, it_r);
  409. }
  410. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  411. /** \brief
  412. *
  413. * \ingroup group__library__string
  414. */
  415. template<ss_typename_param_k S>
  416. inline S& remove_all(S& str)
  417. {
  418. // 1. typedef the string traits
  419. typedef string_traits<S> string_traits_t;
  420. # if defined(STLSOFT_COMPILER_IS_BORLAND) && \
  421. __BORLANDC__ < 0x0564
  422. // This is needed here to tell the Borland compiler that it's a type!
  423. string_traits_t::char_type *p = NULL;
  424. # endif /* compiler */
  425. // 2. typedef the char_t
  426. typedef ss_typename_type_k string_traits_t::char_type char_t;
  427. ss_size_t n;
  428. char_t const* removeChars = default_trim_chars(static_cast<char_t const*>(0), n);
  429. STLSOFT_SUPPRESS_UNUSED(n);
  430. return remove_all_impl(str, removeChars);
  431. }
  432. /** \brief
  433. *
  434. * \ingroup group__library__string
  435. */
  436. template< ss_typename_param_k S0
  437. , ss_typename_param_k S1
  438. >
  439. inline S0& remove_all(S0& str, S1 const& removeChars)
  440. {
  441. return remove_all_impl(str, stlsoft_ns_qual(c_str_ptr)(removeChars));
  442. }
  443. ////////////////////////////////////////////////////////////////////////////
  444. // Unit-testing
  445. #ifdef STLSOFT_UNITTEST
  446. # include "./unittest/trim_functions_unittest_.h"
  447. #endif /* STLSOFT_UNITTEST */
  448. /* ////////////////////////////////////////////////////////////////////// */
  449. #ifndef _STLSOFT_NO_NAMESPACE
  450. } // namespace stlsoft
  451. #endif /* _STLSOFT_NO_NAMESPACE */
  452. /* ////////////////////////////////////////////////////////////////////// */
  453. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_TRIM_FUNCTIONS */
  454. /* ///////////////////////////// end of file //////////////////////////// */