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.

250 lines
7.9 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/string/functionals.hpp
  3. *
  4. * Purpose: String function classes
  5. *
  6. * Created: 22nd 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/functionals.hpp
  40. *
  41. * \brief [C++ only] Definition of the stlsoft::quoter and
  42. * stlsoft::string_begins_with_function function class templates
  43. * (\ref group__library__string "String" Library).
  44. */
  45. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_FUNCTIONALS
  46. #define STLSOFT_INCL_STLSOFT_STRING_HPP_FUNCTIONALS
  47. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  48. # define STLSOFT_VER_STLSOFT_STRING_HPP_FUNCTIONALS_MAJOR 2
  49. # define STLSOFT_VER_STLSOFT_STRING_HPP_FUNCTIONALS_MINOR 2
  50. # define STLSOFT_VER_STLSOFT_STRING_HPP_FUNCTIONALS_REVISION 4
  51. # define STLSOFT_VER_STLSOFT_STRING_HPP_FUNCTIONALS_EDIT 32
  52. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  53. /* /////////////////////////////////////////////////////////////////////////
  54. * Compatibility
  55. */
  56. /*
  57. [Incompatibilies-start]
  58. STLSOFT_COMPILER_IS_WATCOM:
  59. [Incompatibilies-end]
  60. */
  61. /* /////////////////////////////////////////////////////////////////////////
  62. * Includes
  63. */
  64. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  65. # include <stlsoft/stlsoft.h>
  66. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  67. #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
  68. # include <stlsoft/shims/access/string.hpp>
  69. #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
  70. #ifndef STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER
  71. # include <stlsoft/util/std/iterator_helper.hpp>
  72. #endif /* !STLSOFT_INCL_STLSOFT_UTIL_STD_HPP_ITERATOR_HELPER */
  73. #ifndef _STLSOFT_STRING_FUNCTIONALS_NO_STD
  74. # include <functional>
  75. #else /* ? _STLSOFT_STRING_FUNCTIONALS_NO_STD */
  76. # error Now need to write that std_binary_function stuff!!
  77. #endif /* _STLSOFT_STRING_FUNCTIONALS_NO_STD */
  78. #ifndef STLSOFT_INCL_H_STRING
  79. # define STLSOFT_INCL_H_STRING
  80. # include <string.h>
  81. #endif /* !STLSOFT_INCL_H_STRING */
  82. #ifndef STLSOFT_INCL_H_WCHAR
  83. # define STLSOFT_INCL_H_WCHAR
  84. # include <wchar.h>
  85. #endif /* !STLSOFT_INCL_H_WCHAR */
  86. /* /////////////////////////////////////////////////////////////////////////
  87. * Namespace
  88. */
  89. #ifndef _STLSOFT_NO_NAMESPACE
  90. namespace stlsoft
  91. {
  92. #endif /* _STLSOFT_NO_NAMESPACE */
  93. #ifdef __SYNSOFT_DBS_COMPILER_SUPPORTS_PRAGMA_MESSAGE
  94. # pragma message(_sscomp_fileline_message("TODO: Need a function that can do quoting (or anything else)"))
  95. #endif /* __SYNSOFT_DBS_COMPILER_SUPPORTS_PRAGMA_MESSAGE */
  96. /* /////////////////////////////////////////////////////////////////////////
  97. * Function classes
  98. */
  99. /** \brief Unary function class that returns a quoted form of its argument, if
  100. * the argument contains spaces
  101. *
  102. * \ingroup group__library__string
  103. */
  104. template <ss_typename_param_k S>
  105. struct quoter
  106. : public stlsoft_ns_qual_std(unary_function)<char const*, void>
  107. {
  108. public:
  109. S operator()(S const& s) const
  110. {
  111. if(NULL != ::strpbrk(c_str_ptr(s), " \t\""))
  112. {
  113. // Check whether it has quotes. If it does, we'll just assume it's ok
  114. if(NULL != ::strchr(c_str_ptr(s), '"'))
  115. {
  116. return s;
  117. }
  118. else
  119. {
  120. return '"' + s + '"';
  121. }
  122. }
  123. else
  124. {
  125. return s;
  126. }
  127. }
  128. };
  129. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  130. /** \brief
  131. *
  132. * \note This is a work-in-progress, and is subject to change in a later release
  133. *
  134. * \ingroup group__library__string
  135. */
  136. template <ss_typename_param_k C>
  137. struct string_begins_with_function
  138. : public stlsoft_ns_qual_std(unary_function)<C const*, ss_bool_t>
  139. {
  140. public:
  141. public:
  142. string_begins_with_function(C const* prefix)
  143. : m_prefix(prefix)
  144. , m_prefixLen(stlsoft::c_str_len(m_prefix))
  145. {}
  146. public:
  147. ss_bool_t operator ()(C const* line) const
  148. {
  149. return 0 == ::strncmp(line, m_prefix, m_prefixLen);
  150. }
  151. template <ss_typename_param_k S>
  152. ss_bool_t operator ()(S const& line) const
  153. {
  154. return operator ()(c_str_ptr(line));
  155. }
  156. private:
  157. C const* const m_prefix;
  158. const ss_size_t m_prefixLen;
  159. };
  160. /** \brief \note This is a work-in-progress, and is subject to change in a later release
  161. *
  162. * \ingroup group__library__string
  163. */
  164. template <ss_typename_param_k C>
  165. inline string_begins_with_function<C> string_begins_with(C const* prefix)
  166. {
  167. return string_begins_with_function<C>(prefix);
  168. }
  169. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  170. /* /////////////////////////////////////////////////////////////////////////
  171. * Predicate Classes
  172. */
  173. #if 0
  174. /** \brief Predicate used to test the equivalence of strings (of
  175. * heterogeneous types).
  176. *
  177. * \ingroup group__library__string
  178. *
  179. */
  180. template< ss_typename_param_k C
  181. >
  182. // [[synesis:class:function-class:binary-predicate: string_equal]]
  183. struct string_equal
  184. : public stlsoft_ns_qual_std(binary_function)<C const*, C const*, ss_bool_t>
  185. {
  186. public:
  187. template< ss_typename_param_k S0
  188. , ss_typename_param_k S1
  189. >
  190. ss_bool_t operator ()(S0 const& s0, S1 const& s1) const
  191. {
  192. return compare_(stlsoft_ns_qual(c_str_data)(s0)
  193. , stlsoft_ns_qual(c_str_len)(s0)
  194. , stlsoft_ns_qual(c_str_data)(s1)
  195. , stlsoft_ns_qual(c_str_len)(s1));
  196. }
  197. private:
  198. static ss_bool_t compare_(ss_char_a_t const* p0, ss_size_t l0, ss_char_a_t const* p1, ss_size_t l1)
  199. {
  200. return (l0 != l1) ? false : 0 == ::strncmp(p0, p1, l0);
  201. }
  202. static ss_bool_t compare_(ss_char_w_t const* p0, ss_size_t l0, ss_char_w_t const* p1, ss_size_t l1)
  203. {
  204. return (l0 != l1) ? false : 0 == ::wcsncmp(p0, p1, l0);
  205. }
  206. };
  207. #endif /* 0 */
  208. ////////////////////////////////////////////////////////////////////////////
  209. // Unit-testing
  210. #ifdef STLSOFT_UNITTEST
  211. # include "./unittest/functionals_unittest_.h"
  212. #endif /* STLSOFT_UNITTEST */
  213. /* ////////////////////////////////////////////////////////////////////// */
  214. #ifndef _STLSOFT_NO_NAMESPACE
  215. } // namespace stlsoft
  216. #endif /* _STLSOFT_NO_NAMESPACE */
  217. /* ////////////////////////////////////////////////////////////////////// */
  218. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_FUNCTIONALS */
  219. /* ///////////////////////////// end of file //////////////////////////// */