regex_workaround.hpp 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  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. #else
  132. using std::copy;
  133. using std::equal;
  134. #endif
  135. #if BOOST_WORKAROUND(BOOST_MSVC,>=1400) && defined(__STDC_WANT_SECURE_LIB__) && __STDC_WANT_SECURE_LIB__
  136. // use safe versions of strcpy etc:
  137. using ::strcpy_s;
  138. using ::strcat_s;
  139. #else
  140. inline std::size_t strcpy_s(
  141. char *strDestination,
  142. std::size_t sizeInBytes,
  143. const char *strSource
  144. )
  145. {
  146. if(std::strlen(strSource)+1 > sizeInBytes)
  147. return 1;
  148. std::strcpy(strDestination, strSource);
  149. return 0;
  150. }
  151. inline std::size_t strcat_s(
  152. char *strDestination,
  153. std::size_t sizeInBytes,
  154. const char *strSource
  155. )
  156. {
  157. if(std::strlen(strSource) + std::strlen(strDestination) + 1 > sizeInBytes)
  158. return 1;
  159. std::strcat(strDestination, strSource);
  160. return 0;
  161. }
  162. #endif
  163. inline void overflow_error_if_not_zero(std::size_t i)
  164. {
  165. if(i)
  166. {
  167. std::overflow_error e("String buffer too small");
  168. boost::throw_exception(e);
  169. }
  170. }
  171. }} // namespaces
  172. #endif // __cplusplus
  173. #endif // include guard