regex_workaround.hpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  1. /*
  2. *
  3. * Copyright (c) 1998-2005
  4. * John Maddock
  5. *
  6. * Use, modification and distribution are subject to the
  7. * Boost Software License, Version 1.0. (See accompanying file
  8. * LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
  9. *
  10. */
  11. /*
  12. * LOCATION: see http://www.boost.org for most recent version.
  13. * FILE regex_workarounds.cpp
  14. * VERSION see <boost/version.hpp>
  15. * DESCRIPTION: Declares Misc workarounds.
  16. */
  17. #ifndef BOOST_REGEX_WORKAROUND_HPP
  18. #define BOOST_REGEX_WORKAROUND_HPP
  19. #include <boost/config.hpp>
  20. #include <new>
  21. #include <cstring>
  22. #include <cstdlib>
  23. #include <cstddef>
  24. #include <cassert>
  25. #include <cstdio>
  26. #include <climits>
  27. #include <string>
  28. #include <stdexcept>
  29. #include <iterator>
  30. #include <algorithm>
  31. #include <iosfwd>
  32. #include <vector>
  33. #include <set>
  34. #include <map>
  35. #include <boost/limits.hpp>
  36. #include <boost/assert.hpp>
  37. #include <boost/cstdint.hpp>
  38. #include <boost/throw_exception.hpp>
  39. #include <boost/scoped_ptr.hpp>
  40. #include <boost/scoped_array.hpp>
  41. #include <boost/shared_ptr.hpp>
  42. #include <boost/mpl/bool_fwd.hpp>
  43. #include <boost/regex/config.hpp>
  44. #ifndef BOOST_NO_STD_LOCALE
  45. # include <locale>
  46. #endif
  47. #if defined(BOOST_NO_STDC_NAMESPACE)
  48. namespace std{
  49. using ::sprintf; using ::strcpy; using ::strcat; using ::strlen;
  50. }
  51. #endif
  52. namespace boost{ namespace BOOST_REGEX_DETAIL_NS{
  53. #ifdef BOOST_NO_STD_DISTANCE
  54. template <class T>
  55. std::ptrdiff_t distance(const T& x, const T& y)
  56. { return y - x; }
  57. #else
  58. using std::distance;
  59. #endif
  60. }}
  61. #ifdef BOOST_REGEX_NO_BOOL
  62. # define BOOST_REGEX_MAKE_BOOL(x) static_cast<bool>((x) ? true : false)
  63. #else
  64. # define BOOST_REGEX_MAKE_BOOL(x) static_cast<bool>(x)
  65. #endif
  66. /*****************************************************************************
  67. *
  68. * Fix broken namespace support:
  69. *
  70. ****************************************************************************/
  71. #if defined(BOOST_NO_STDC_NAMESPACE) && defined(__cplusplus)
  72. namespace std{
  73. using ::ptrdiff_t;
  74. using ::size_t;
  75. using ::abs;
  76. using ::memset;
  77. using ::memcpy;
  78. }
  79. #endif
  80. /*****************************************************************************
  81. *
  82. * helper functions pointer_construct/pointer_destroy:
  83. *
  84. ****************************************************************************/
  85. #ifdef __cplusplus
  86. namespace boost{ namespace BOOST_REGEX_DETAIL_NS{
  87. #ifdef BOOST_MSVC
  88. #pragma warning (push)
  89. #pragma warning (disable : 4100)
  90. #endif
  91. template <class T>
  92. inline void pointer_destroy(T* p)
  93. { p->~T(); (void)p; }
  94. #ifdef BOOST_MSVC
  95. #pragma warning (pop)
  96. #endif
  97. template <class T>
  98. inline void pointer_construct(T* p, const T& t)
  99. { new (p) T(t); }
  100. }} // namespaces
  101. #endif
  102. /*****************************************************************************
  103. *
  104. * helper function copy:
  105. *
  106. ****************************************************************************/
  107. #ifdef __cplusplus
  108. namespace boost{ namespace BOOST_REGEX_DETAIL_NS{
  109. #if BOOST_WORKAROUND(BOOST_MSVC,>=1400) && BOOST_WORKAROUND(BOOST_MSVC, <1600) && defined(_CPPLIB_VER) && defined(BOOST_DINKUMWARE_STDLIB) && !(defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION))
  110. //
  111. // MSVC 8 will either emit warnings or else refuse to compile
  112. // code that makes perfectly legitimate use of std::copy, when
  113. // the OutputIterator type is a user-defined class (apparently all user
  114. // defined iterators are "unsafe"). This code works around that:
  115. //
  116. template<class InputIterator, class OutputIterator>
  117. inline OutputIterator copy(
  118. InputIterator first,
  119. InputIterator last,
  120. OutputIterator dest
  121. )
  122. {
  123. return stdext::unchecked_copy(first, last, dest);
  124. }
  125. template<class InputIterator1, class InputIterator2>
  126. inline bool equal(
  127. InputIterator1 first,
  128. InputIterator1 last,
  129. InputIterator2 with
  130. )
  131. {
  132. return stdext::unchecked_equal(first, last, with);
  133. }
  134. #elif BOOST_WORKAROUND(BOOST_MSVC, > 1500)
  135. //
  136. // MSVC 10 will either emit warnings or else refuse to compile
  137. // code that makes perfectly legitimate use of std::copy, when
  138. // the OutputIterator type is a user-defined class (apparently all user
  139. // defined iterators are "unsafe"). What's more Microsoft have removed their
  140. // non-standard "unchecked" versions, even though their still in the MS
  141. // documentation!! Work around this as best we can:
  142. //
  143. template<class InputIterator, class OutputIterator>
  144. inline OutputIterator copy(
  145. InputIterator first,
  146. InputIterator last,
  147. OutputIterator dest
  148. )
  149. {
  150. while(first != last)
  151. *dest++ = *first++;
  152. return dest;
  153. }
  154. template<class InputIterator1, class InputIterator2>
  155. inline bool equal(
  156. InputIterator1 first,
  157. InputIterator1 last,
  158. InputIterator2 with
  159. )
  160. {
  161. while(first != last)
  162. if(*first++ != *with++) return false;
  163. return true;
  164. }
  165. #else
  166. using std::copy;
  167. using std::equal;
  168. #endif
  169. #if BOOST_WORKAROUND(BOOST_MSVC,>=1400) && defined(__STDC_WANT_SECURE_LIB__) && __STDC_WANT_SECURE_LIB__
  170. // use safe versions of strcpy etc:
  171. using ::strcpy_s;
  172. using ::strcat_s;
  173. #else
  174. inline std::size_t strcpy_s(
  175. char *strDestination,
  176. std::size_t sizeInBytes,
  177. const char *strSource
  178. )
  179. {
  180. std::size_t lenSourceWithNull = std::strlen(strSource) + 1;
  181. if (lenSourceWithNull > sizeInBytes)
  182. return 1;
  183. std::memcpy(strDestination, strSource, lenSourceWithNull);
  184. return 0;
  185. }
  186. inline std::size_t strcat_s(
  187. char *strDestination,
  188. std::size_t sizeInBytes,
  189. const char *strSource
  190. )
  191. {
  192. std::size_t lenSourceWithNull = std::strlen(strSource) + 1;
  193. std::size_t lenDestination = std::strlen(strDestination);
  194. if (lenSourceWithNull + lenDestination > sizeInBytes)
  195. return 1;
  196. std::memcpy(strDestination + lenDestination, strSource, lenSourceWithNull);
  197. return 0;
  198. }
  199. #endif
  200. inline void overflow_error_if_not_zero(std::size_t i)
  201. {
  202. if(i)
  203. {
  204. std::overflow_error e("String buffer too small");
  205. boost::throw_exception(e);
  206. }
  207. }
  208. }} // namespaces
  209. #endif // __cplusplus
  210. #endif // include guard