regex_workaround.hpp 5.7 KB

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