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.

213 lines
7.1 KiB

  1. /* /////////////////////////////////////////////////////////////////////////
  2. * File: stlsoft/string/cstring_functions.hpp
  3. *
  4. * Purpose: String duplication functions.
  5. *
  6. * Created: 26th May 2005
  7. * Updated: 26th September 2010
  8. *
  9. * Home: http://stlsoft.org/
  10. *
  11. * Copyright (c) 2005-2010, 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/cstring_functions.hpp
  40. *
  41. * \brief [C++ only] C-string functions
  42. * (\ref group__library__string "String" Library).
  43. */
  44. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CSTRING_FUNCTIONS
  45. #define STLSOFT_INCL_STLSOFT_STRING_HPP_CSTRING_FUNCTIONS
  46. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  47. # define STLSOFT_VER_STLSOFT_STRING_HPP_CSTRING_FUNCTIONS_MAJOR 2
  48. # define STLSOFT_VER_STLSOFT_STRING_HPP_CSTRING_FUNCTIONS_MINOR 2
  49. # define STLSOFT_VER_STLSOFT_STRING_HPP_CSTRING_FUNCTIONS_REVISION 2
  50. # define STLSOFT_VER_STLSOFT_STRING_HPP_CSTRING_FUNCTIONS_EDIT 32
  51. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  52. /* /////////////////////////////////////////////////////////////////////////
  53. * Compatibility
  54. */
  55. /*
  56. [Incompatibilies-start]
  57. STLSOFT_COMPILER_IS_WATCOM:
  58. [Incompatibilies-end]
  59. */
  60. /* /////////////////////////////////////////////////////////////////////////
  61. * Includes
  62. */
  63. #ifndef STLSOFT_INCL_STLSOFT_H_STLSOFT
  64. # include <stlsoft/stlsoft.h>
  65. #endif /* !STLSOFT_INCL_STLSOFT_H_STLSOFT */
  66. #ifndef STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING
  67. # include <stlsoft/shims/access/string.hpp>
  68. #endif /* !STLSOFT_INCL_STLSOFT_SHIMS_ACCESS_HPP_STRING */
  69. #ifndef STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS
  70. # include <stlsoft/string/char_traits.hpp>
  71. #endif /* !STLSOFT_INCL_STLSOFT_STRING_HPP_CHAR_TRAITS */
  72. #ifndef STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_BASE
  73. # include <stlsoft/memory/allocator_base.hpp> // for STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT
  74. #endif /* !STLSOFT_INCL_STLSOFT_MEMORY_HPP_ALLOCATOR_BASE */
  75. #ifdef STLSOFT_UNITTEST
  76. # include <stlsoft/memory/malloc_allocator.hpp>
  77. # include <stlsoft/memory/new_allocator.hpp>
  78. # include <string.h>
  79. #endif /* STLSOFT_UNITTEST */
  80. /* /////////////////////////////////////////////////////////////////////////
  81. * Namespace
  82. */
  83. #ifndef _STLSOFT_NO_NAMESPACE
  84. namespace stlsoft
  85. {
  86. #endif /* _STLSOFT_NO_NAMESPACE */
  87. /* /////////////////////////////////////////////////////////////////////////
  88. * Classes
  89. */
  90. #ifndef STLSOFT_DOCUMENTATION_SKIP_SECTION
  91. /** \brief Allocates a copy of the string, using the given allocator
  92. *
  93. * \ingroup group__library__string
  94. *
  95. * \param str The string to copy
  96. * \param cch The number of elements in str to copy
  97. * \param ator The allocator to use to allocate the memory
  98. *
  99. * \note The caller is responsible for the allocated memory, and should free it
  100. * with ator (or a compatible allocator or memory function).
  101. *
  102. * \ingroup group__library__string
  103. */
  104. template< ss_typename_param_k C
  105. , ss_typename_param_k A
  106. >
  107. inline C* string_dup_impl(C const* str, ss_size_t cch, A& ator)
  108. {
  109. C* r = ator.allocate(1 + cch, NULL);
  110. #ifndef STLSOFT_CF_EXCEPTION_OPERATOR_NEW_THROWS_BAD_ALLOC
  111. if(NULL != r)
  112. #endif /* !STLSOFT_CF_EXCEPTION_OPERATOR_NEW_THROWS_BAD_ALLOC */
  113. {
  114. char_traits<C>::copy(r, str, cch);
  115. r[cch] = '\0';
  116. }
  117. return r;
  118. }
  119. #endif /* !STLSOFT_DOCUMENTATION_SKIP_SECTION */
  120. /** \brief Allocates a copy of the string, using the given allocator
  121. *
  122. * \ingroup group__library__string
  123. *
  124. * \param str The string to copy
  125. * \param cch The number of elements in str to copy
  126. * \param ator The allocator to use to allocate the memory
  127. *
  128. * \note The caller is responsible for the allocated memory, and should free it
  129. * with ator (or a compatible allocator or memory function).
  130. *
  131. * \ingroup group__library__string
  132. */
  133. template< ss_typename_param_k C
  134. , ss_typename_param_k A
  135. >
  136. inline C* string_dup(C const* str, ss_size_t cch, A& ator)
  137. {
  138. #ifdef STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT
  139. typedef C char_t;
  140. typedef ss_typename_type_k A::ss_template_qual_k rebind<C>::other ator_t;
  141. ator_t ator_;
  142. STLSOFT_SUPPRESS_UNUSED(ator);
  143. return string_dup_impl(str, cch, ator_);
  144. #else /* ? STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT */
  145. return string_dup_impl(str, cch, ator);
  146. #endif /* STLSOFT_LF_ALLOCATOR_REBIND_SUPPORT */
  147. }
  148. /** \brief Allocates a copy of the string, using the given allocator
  149. *
  150. * \ingroup group__library__string
  151. *
  152. * \param str The string to copy
  153. * \param ator The allocator to use to allocate the memory
  154. * \param psize Pointer to receive the size of the allocated string. May be NULL.
  155. *
  156. * \note The caller is responsible for the allocated memory, and should free it
  157. * with ator (or a compatible allocator or memory function).
  158. *
  159. * \ingroup group__library__string
  160. */
  161. template< ss_typename_param_k C
  162. , ss_typename_param_k A
  163. >
  164. inline C* string_dup(C const* str, A& ator, ss_size_t* psize = NULL)
  165. {
  166. ss_size_t dummy;
  167. if(NULL == psize)
  168. {
  169. psize = &dummy;
  170. }
  171. return string_dup(str, *psize = c_str_len(str), ator);
  172. }
  173. ////////////////////////////////////////////////////////////////////////////
  174. // Unit-testing
  175. #ifdef STLSOFT_UNITTEST
  176. # include "./unittest/cstring_functions_unittest_.h"
  177. #endif /* STLSOFT_UNITTEST */
  178. /* ////////////////////////////////////////////////////////////////////// */
  179. #ifndef _STLSOFT_NO_NAMESPACE
  180. } // namespace stlsoft
  181. #endif /* _STLSOFT_NO_NAMESPACE */
  182. /* ////////////////////////////////////////////////////////////////////// */
  183. #endif /* !STLSOFT_INCL_STLSOFT_HPP_CSTRING_FUNCTIONS */
  184. /* ///////////////////////////// end of file //////////////////////////// */