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.

396 lines
11 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/string/container_slice_functions.hpp
  3. *
  4. * Purpose: String container slice functions.
  5. *
  6. * Created: 25th April 2005
  7. * Updated: 12th October 2010
  8. *
  9. * Thanks: To Pablo Aguilar for inspiration for these functions.
  10. *
  11. * Home: http://stlsoft.org/
  12. *
  13. * Copyright (c) 2005-2010, Matthew Wilson and Synesis Software
  14. * All rights reserved.
  15. *
  16. * Redistribution and use in source and binary forms, with or without
  17. * modification, are permitted provided that the following conditions are met:
  18. *
  19. * - Redistributions of source code must retain the above copyright notice, this
  20. * list of conditions and the following disclaimer.
  21. * - Redistributions in binary form must reproduce the above copyright notice,
  22. * this list of conditions and the following disclaimer in the documentation
  23. * and/or other materials provided with the distribution.
  24. * - Neither the name(s) of Matthew Wilson and Synesis Software nor the names of
  25. * any contributors may be used to endorse or promote products derived from
  26. * this software without specific prior written permission.
  27. *
  28. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  29. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  30. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  31. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  32. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  33. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  34. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  35. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  36. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  37. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  38. * POSSIBILITY OF SUCH DAMAGE.
  39. *
  40. * ////////////////////////////////////////////////////////////////////// */
  41. /** \file stlsoft/string/container_slice_functions.hpp
  42. *
  43. * \brief [C++ only] String container slice functions
  44. * (\ref group__library__string "String" Library).
  45. */
  46. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CONTAINER_SLICE_FUNCTIONS
  47. #define STLSOFT_INCL_STLSOFT_STRING_HPP_CONTAINER_SLICE_FUNCTIONS
  48. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  49. # define STLSOFT_VER_INCL_STLSOFT_STRING_HPP_CONTAINER_SLICE_FUNCTIONS_MAJOR 2
  50. # define STLSOFT_VER_INCL_STLSOFT_STRING_HPP_CONTAINER_SLICE_FUNCTIONS_MINOR 1
  51. # define STLSOFT_VER_INCL_STLSOFT_STRING_HPP_CONTAINER_SLICE_FUNCTIONS_REVISION 3
  52. # define STLSOFT_VER_INCL_STLSOFT_STRING_HPP_CONTAINER_SLICE_FUNCTIONS_EDIT 23
  53. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  54. /* /////////////////////////////////////////////////////////////////////////
  55. * Compatibility
  56. */
  57. /*
  58. [Incompatibilies-start]
  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. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_STRING_TRAITS
  69. # include <stlsoft/string/string_traits.hpp>
  70. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_TRAITS */
  71. #if !defined(STLSOFT_STRING_CONTAINER_SLICE_FNS_STRING_TEMPLATE)
  72. # if defined(STLSOFT_STRING_CONTAINERS_SLICE_FNS_USE_std_basic_string) || \
  73. defined(STLSOFT_STRING_CONTAINERS_SLICE_FNS_USE_STD_BASIC_STRING)
  74. # include <string>
  75. # else /* ? std::basic_string */
  76. # ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_SIMPLE_STRING
  77. # include <stlsoft/string/simple_string.hpp>
  78. # endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_SIMPLE_STRING */
  79. # endif /* std::basic_string */
  80. #endif /* !STLSOFT_STRING_CONTAINER_SLICE_FNS_STRING_TEMPLATE */
  81. #ifndef STLSOFT_INCL_STLSOFT_UTIL_HPP_MINMAX
  82. # include <stlsoft/util/minmax.hpp>
  83. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_HPP_MINMAX */
  84. #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
  85. # include <stlsoft/shims/access/string.hpp>
  86. #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
  87. #ifdef STLSOFT_UNITTEST
  88. # include <string>
  89. # ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_SIMPLE_STRING
  90. # include <stlsoft/string/simple_string.hpp>
  91. # endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_SIMPLE_STRING */
  92. #if 0
  93. # if defined(WIN32) && \
  94. ( defined(STLSOFT_COMPILER_IS_INTEL) || \
  95. defined(STLSOFT_COMPILER_IS_MSVC))
  96. # include <afx.h>
  97. # ifndef MFCSTL_INCL_MFCSTL_SHIMS_ACCESS_HPP_STRING
  98. # include <mfcstl/shims/access/string.hpp>
  99. # endif /* !MFCSTL_INCL_MFCSTL_SHIMS_ACCESS_HPP_STRING */
  100. # ifndef MFCSTL_INCL_MFCSTL_HPP_STRING_TRAITS
  101. # include <mfcstl/string_traits.hpp>
  102. # endif /* !MFCSTL_INCL_MFCSTL_HPP_STRING_TRAITS */
  103. # endif /* WIN32 && Intel or MSVC */
  104. #endif /* 0 */
  105. #endif /* STLSOFT_UNITTEST */
  106. /* /////////////////////////////////////////////////////////////////////////
  107. * Namespace
  108. */
  109. #ifndef _STLSOFT_NO_NAMESPACE
  110. namespace stlsoft
  111. {
  112. #endif /* _STLSOFT_NO_NAMESPACE */
  113. /* /////////////////////////////////////////////////////////////////////////
  114. * Helper functions
  115. */
  116. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  117. template <ss_typename_param_k S>
  118. inline ss_size_t left_len(S const& s, ss_size_t n)
  119. {
  120. const ss_size_t len = stlsoft_ns_qual(c_str_len)(s);
  121. if(n > len)
  122. {
  123. n = len;
  124. }
  125. return n;
  126. }
  127. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  128. /* /////////////////////////////////////////////////////////////////////////
  129. * String class functions
  130. */
  131. /** \brief
  132. *
  133. * \ingroup group__library__string
  134. */
  135. template <ss_typename_param_k S>
  136. inline S left(S const& s, ss_size_t n)
  137. {
  138. typedef string_traits<S> traits_t;
  139. return traits_t::construct(s, 0, left_len(s, n));
  140. }
  141. /** \brief
  142. *
  143. * \ingroup group__library__string
  144. */
  145. template <ss_typename_param_k S>
  146. inline S right(S const& s, ss_size_t n)
  147. {
  148. typedef string_traits<S> traits_t;
  149. const ss_size_t len = stlsoft_ns_qual(c_str_len)(s);
  150. if(n > len)
  151. {
  152. n = len;
  153. }
  154. return traits_t::construct(s, len - n, n);
  155. }
  156. /** \brief
  157. *
  158. * \ingroup group__library__string
  159. */
  160. template <ss_typename_param_k S>
  161. inline S mid(S const& s, ss_size_t pos, ss_size_t n)
  162. {
  163. typedef string_traits<S> traits_t;
  164. const ss_size_t len = stlsoft_ns_qual(c_str_len)(s);
  165. if(pos > len)
  166. {
  167. pos = len;
  168. }
  169. if(n > len - pos)
  170. {
  171. n = len - pos;
  172. }
  173. return traits_t::construct(s, pos, n);
  174. }
  175. /* /////////////////////////////////////////////////////////////////////////
  176. * C-style string slice functions
  177. */
  178. #if defined(STLSOFT_STRING_CONTAINER_SLICE_FNS_STRING_TEMPLATE)
  179. # define _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE STLSOFT_STRING_CONTAINER_SLICE_FNS_STRING_TEMPLATE
  180. #elif defined(STLSOFT_STRING_CONTAINERS_SLICE_FNS_USE_std_basic_string) || \
  181. defined(STLSOFT_STRING_CONTAINERS_SLICE_FNS_USE_STD_BASIC_STRING)
  182. # define _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE stlsoft_ns_qual_std(basic_string)
  183. #else /* ? STLSOFT_STRING_CONTAINER_SLICE_FNS_STRING_TEMPLATE */
  184. # define _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE stlsoft_ns_qual(basic_simple_string)
  185. #endif /* !STLSOFT_STRING_CONTAINER_SLICE_FNS_STRING_TEMPLATE */
  186. /** \brief
  187. *
  188. * \ingroup group__library__string
  189. */
  190. inline _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_a_t> left(ss_char_a_t const* s, ss_size_t n)
  191. {
  192. typedef _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_a_t> string_t;
  193. return string_t(s, left_len(s, n));
  194. }
  195. /** \brief
  196. *
  197. * \ingroup group__library__string
  198. */
  199. inline _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_a_t> left(ss_char_a_t *s, ss_size_t n)
  200. {
  201. return left(const_cast<ss_char_a_t const*>(s), n);
  202. }
  203. /** \brief
  204. *
  205. * \ingroup group__library__string
  206. */
  207. inline _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_w_t> left(ss_char_w_t const* s, ss_size_t n)
  208. {
  209. typedef _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_w_t> string_t;
  210. return string_t(s, left_len(s, n));
  211. }
  212. /** \brief
  213. *
  214. * \ingroup group__library__string
  215. */
  216. inline _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_w_t> left(ss_char_w_t *s, ss_size_t n)
  217. {
  218. return left(const_cast<ss_char_w_t const*>(s), n);
  219. }
  220. /** \brief
  221. *
  222. * \ingroup group__library__string
  223. */
  224. inline _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_a_t> right(ss_char_a_t const* s, ss_size_t n)
  225. {
  226. typedef _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_a_t> string_t;
  227. const ss_size_t len = stlsoft_ns_qual(c_str_len)(s);
  228. if(n > len)
  229. {
  230. n = len;
  231. }
  232. return string_t(s + (len - n), n);
  233. }
  234. /** \brief
  235. *
  236. * \ingroup group__library__string
  237. */
  238. inline _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_a_t> right(ss_char_a_t *s, ss_size_t n)
  239. {
  240. return right(const_cast<ss_char_a_t const*>(s), n);
  241. }
  242. /** \brief
  243. *
  244. * \ingroup group__library__string
  245. */
  246. inline _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_w_t> right(ss_char_w_t const* s, ss_size_t n)
  247. {
  248. typedef _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_w_t> string_t;
  249. const ss_size_t len = stlsoft_ns_qual(c_str_len)(s);
  250. if(n > len)
  251. {
  252. n = len;
  253. }
  254. return string_t(s + (len - n), n);
  255. }
  256. /** \brief
  257. *
  258. * \ingroup group__library__string
  259. */
  260. inline _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_w_t> right(ss_char_w_t *s, ss_size_t n)
  261. {
  262. return right(const_cast<ss_char_w_t const*>(s), n);
  263. }
  264. /** \brief
  265. *
  266. * \ingroup group__library__string
  267. */
  268. inline _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_a_t> mid(ss_char_a_t const* s, ss_size_t pos, ss_size_t n)
  269. {
  270. typedef _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_a_t> string_t;
  271. const ss_size_t len = stlsoft_ns_qual(c_str_len)(s);
  272. if(pos > len)
  273. {
  274. pos = len;
  275. }
  276. if(n > len - pos)
  277. {
  278. n = len - pos;
  279. }
  280. return string_t(s + pos, n);
  281. }
  282. /** \brief
  283. *
  284. * \ingroup group__library__string
  285. */
  286. inline _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_a_t> mid(ss_char_a_t *s, ss_size_t pos, ss_size_t n)
  287. {
  288. return mid(const_cast<ss_char_a_t const*>(s), pos, n);
  289. }
  290. /** \brief
  291. *
  292. * \ingroup group__library__string
  293. */
  294. inline _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_w_t> mid(ss_char_w_t const* s, ss_size_t pos, ss_size_t n)
  295. {
  296. typedef _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_w_t> string_t;
  297. const ss_size_t len = stlsoft_ns_qual(c_str_len)(s);
  298. if(pos > len)
  299. {
  300. pos = len;
  301. }
  302. if(n > len - pos)
  303. {
  304. n = len - pos;
  305. }
  306. return string_t(s + pos, n);
  307. }
  308. /** \brief
  309. *
  310. * \ingroup group__library__string
  311. */
  312. inline _STLSOFT_STRING_CONTAINER_SLICE_FUNCTIONS_RETURN_TEMPLATE<ss_char_w_t> mid(ss_char_w_t *s, ss_size_t pos, ss_size_t n)
  313. {
  314. return mid(const_cast<ss_char_w_t const*>(s), pos, n);
  315. }
  316. ////////////////////////////////////////////////////////////////////////////
  317. // Unit-testing
  318. #ifdef STLSOFT_UNITTEST
  319. # include "./unittest/container_slice_functions_unittest_.h"
  320. #endif /* STLSOFT_UNITTEST */
  321. /* ////////////////////////////////////////////////////////////////////// */
  322. #ifndef _STLSOFT_NO_NAMESPACE
  323. } // namespace stlsoft
  324. #endif /* _STLSOFT_NO_NAMESPACE */
  325. /* ////////////////////////////////////////////////////////////////////// */
  326. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_CONTAINER_SLICE_FUNCTIONS */
  327. /* ///////////////////////////// end of file //////////////////////////// */