regex_workaround.hpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  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/regex/config.hpp>
  20. #include <algorithm>
  21. #include <stdexcept>
  22. #include <cstring>
  23. #ifndef BOOST_REGEX_STANDALONE
  24. #include <boost/detail/workaround.hpp>
  25. #include <boost/throw_exception.hpp>
  26. #endif
  27. #ifdef BOOST_REGEX_NO_BOOL
  28. # define BOOST_REGEX_MAKE_BOOL(x) static_cast<bool>((x) ? true : false)
  29. #else
  30. # define BOOST_REGEX_MAKE_BOOL(x) static_cast<bool>(x)
  31. #endif
  32. /*****************************************************************************
  33. *
  34. * helper functions pointer_construct/pointer_destroy:
  35. *
  36. ****************************************************************************/
  37. #ifdef __cplusplus
  38. namespace boost{ namespace BOOST_REGEX_DETAIL_NS{
  39. #ifdef BOOST_REGEX_MSVC
  40. #pragma warning (push)
  41. #pragma warning (disable : 4100)
  42. #endif
  43. template <class T>
  44. inline void pointer_destroy(T* p)
  45. { p->~T(); (void)p; }
  46. #ifdef BOOST_REGEX_MSVC
  47. #pragma warning (pop)
  48. #endif
  49. template <class T>
  50. inline void pointer_construct(T* p, const T& t)
  51. { new (p) T(t); }
  52. }} // namespaces
  53. #endif
  54. /*****************************************************************************
  55. *
  56. * helper function copy:
  57. *
  58. ****************************************************************************/
  59. #if defined(BOOST_WORKAROUND)
  60. #if BOOST_WORKAROUND(BOOST_REGEX_MSVC, >= 1400) && defined(__STDC_WANT_SECURE_LIB__) && __STDC_WANT_SECURE_LIB__
  61. #define BOOST_REGEX_HAS_STRCPY_S
  62. #endif
  63. #endif
  64. #ifdef __cplusplus
  65. namespace boost{ namespace BOOST_REGEX_DETAIL_NS{
  66. #if defined(BOOST_REGEX_MSVC) && (BOOST_REGEX_MSVC < 1910)
  67. //
  68. // MSVC 10 will either emit warnings or else refuse to compile
  69. // code that makes perfectly legitimate use of std::copy, when
  70. // the OutputIterator type is a user-defined class (apparently all user
  71. // defined iterators are "unsafe"). What's more Microsoft have removed their
  72. // non-standard "unchecked" versions, even though they are still in the MS
  73. // documentation!! Work around this as best we can:
  74. //
  75. template<class InputIterator, class OutputIterator>
  76. inline OutputIterator copy(
  77. InputIterator first,
  78. InputIterator last,
  79. OutputIterator dest
  80. )
  81. {
  82. while (first != last)
  83. *dest++ = *first++;
  84. return dest;
  85. }
  86. #else
  87. using std::copy;
  88. #endif
  89. #if defined(BOOST_REGEX_HAS_STRCPY_S)
  90. // use safe versions of strcpy etc:
  91. using ::strcpy_s;
  92. using ::strcat_s;
  93. #else
  94. inline std::size_t strcpy_s(
  95. char *strDestination,
  96. std::size_t sizeInBytes,
  97. const char *strSource
  98. )
  99. {
  100. std::size_t lenSourceWithNull = std::strlen(strSource) + 1;
  101. if (lenSourceWithNull > sizeInBytes)
  102. return 1;
  103. std::memcpy(strDestination, strSource, lenSourceWithNull);
  104. return 0;
  105. }
  106. inline std::size_t strcat_s(
  107. char *strDestination,
  108. std::size_t sizeInBytes,
  109. const char *strSource
  110. )
  111. {
  112. std::size_t lenSourceWithNull = std::strlen(strSource) + 1;
  113. std::size_t lenDestination = std::strlen(strDestination);
  114. if (lenSourceWithNull + lenDestination > sizeInBytes)
  115. return 1;
  116. std::memcpy(strDestination + lenDestination, strSource, lenSourceWithNull);
  117. return 0;
  118. }
  119. #endif
  120. inline void overflow_error_if_not_zero(std::size_t i)
  121. {
  122. if(i)
  123. {
  124. std::overflow_error e("String buffer too small");
  125. #ifndef BOOST_REGEX_STANDALONE
  126. boost::throw_exception(e);
  127. #else
  128. throw e;
  129. #endif
  130. }
  131. }
  132. }} // namespaces
  133. #endif // __cplusplus
  134. #endif // include guard