include/boost/regex/v5/regex_workaround.hpp

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