Skip to content

Commit 237e69c

Browse files
authored
Merge pull request #189 from boostorg/use_core_snprintf
Simplify snprintf workaround usage.
2 parents 1cad537 + 372d3f7 commit 237e69c

File tree

4 files changed

+29
-50
lines changed

4 files changed

+29
-50
lines changed

include/boost/regex/v4/regex_workaround.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -198,10 +198,10 @@ namespace boost{ namespace BOOST_REGEX_DETAIL_NS{
198198
const char *strSource
199199
)
200200
{
201-
std::size_t lenSourceWithNull = std::strlen(strSource) + 1;
202-
if (lenSourceWithNull > sizeInBytes)
201+
std::size_t lenSourceWithNull = std::strlen(strSource) + 1;
202+
if (lenSourceWithNull > sizeInBytes)
203203
return 1;
204-
std::memcpy(strDestination, strSource, lenSourceWithNull);
204+
std::memcpy(strDestination, strSource, lenSourceWithNull);
205205
return 0;
206206
}
207207
inline std::size_t strcat_s(
@@ -210,11 +210,11 @@ namespace boost{ namespace BOOST_REGEX_DETAIL_NS{
210210
const char *strSource
211211
)
212212
{
213-
std::size_t lenSourceWithNull = std::strlen(strSource) + 1;
214-
std::size_t lenDestination = std::strlen(strDestination);
215-
if (lenSourceWithNull + lenDestination > sizeInBytes)
213+
std::size_t lenSourceWithNull = std::strlen(strSource) + 1;
214+
std::size_t lenDestination = std::strlen(strDestination);
215+
if (lenSourceWithNull + lenDestination > sizeInBytes)
216216
return 1;
217-
std::memcpy(strDestination + lenDestination, strSource, lenSourceWithNull);
217+
std::memcpy(strDestination + lenDestination, strSource, lenSourceWithNull);
218218
return 0;
219219
}
220220

include/boost/regex/v5/regex_workaround.hpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -116,10 +116,10 @@ namespace boost{ namespace BOOST_REGEX_DETAIL_NS{
116116
const char *strSource
117117
)
118118
{
119-
std::size_t lenSourceWithNull = std::strlen(strSource) + 1;
120-
if (lenSourceWithNull > sizeInBytes)
119+
std::size_t lenSourceWithNull = std::strlen(strSource) + 1;
120+
if (lenSourceWithNull > sizeInBytes)
121121
return 1;
122-
std::memcpy(strDestination, strSource, lenSourceWithNull);
122+
std::memcpy(strDestination, strSource, lenSourceWithNull);
123123
return 0;
124124
}
125125
inline std::size_t strcat_s(
@@ -128,11 +128,11 @@ namespace boost{ namespace BOOST_REGEX_DETAIL_NS{
128128
const char *strSource
129129
)
130130
{
131-
std::size_t lenSourceWithNull = std::strlen(strSource) + 1;
132-
std::size_t lenDestination = std::strlen(strDestination);
133-
if (lenSourceWithNull + lenDestination > sizeInBytes)
131+
std::size_t lenSourceWithNull = std::strlen(strSource) + 1;
132+
std::size_t lenDestination = std::strlen(strDestination);
133+
if (lenSourceWithNull + lenDestination > sizeInBytes)
134134
return 1;
135-
std::memcpy(strDestination + lenDestination, strSource, lenSourceWithNull);
135+
std::memcpy(strDestination + lenDestination, strSource, lenSourceWithNull);
136136
return 0;
137137
}
138138

src/posix_api.cpp

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -22,19 +22,15 @@
2222
#include <boost/cregex.hpp>
2323
#include <cstdio>
2424

25-
#if defined(BOOST_NO_STDC_NAMESPACE)
26-
namespace std{
27-
using ::sprintf;
28-
using ::strcpy;
29-
using ::strcmp;
30-
}
31-
#endif
32-
3325
#ifndef BOOST_WORKAROUND
3426
#define BOOST_WORKAROUND(x, y) false
3527
#endif
3628

37-
29+
#ifndef BOOST_REGEX_STANDALONE
30+
#include <boost/core/snprintf.hpp>
31+
#else
32+
namespace boost { namespace core { using std::snprintf; } }
33+
#endif
3834

3935
namespace boost{
4036

@@ -180,27 +176,15 @@ BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorA(int code, const regex_tA*
180176
// We're converting an integer i to a string, and since i <= REG_E_UNKNOWN
181177
// a five character string is *always* large enough:
182178
//
183-
#if !defined(BOOST_CXX_VERSION) || (BOOST_CXX_VERSION >= 201103)
184-
int r = (std::snprintf)(localbuf, 5, "%d", i);
185-
#elif BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
186-
int r = (::sprintf_s)(localbuf, 5, "%d", i);
187-
#else
188-
int r = (std::sprintf)(localbuf, "%d", i);
189-
#endif
179+
int r = (boost::core::snprintf)(localbuf, 5, "%d", i);
190180
if(r < 0)
191181
return 0; // sprintf failed
192182
if(std::strlen(localbuf) < buf_size)
193183
BOOST_REGEX_DETAIL_NS::strcpy_s(buf, buf_size, localbuf);
194184
return std::strlen(localbuf) + 1;
195185
}
196186
}
197-
#if !defined(BOOST_CXX_VERSION) || (BOOST_CXX_VERSION >= 201103)
198-
int r = (::snprintf)(localbuf, 5, "%d", 0);
199-
#elif BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
200-
int r = (::sprintf_s)(localbuf, 5, "%d", 0);
201-
#else
202-
int r = (std::sprintf)(localbuf, "%d", 0);
203-
#endif
187+
int r = (boost::core::snprintf)(localbuf, 5, "%d", 0);
204188
if(r < 0)
205189
return 0; // sprintf failed
206190
if(std::strlen(localbuf) < buf_size)

src/wide_posix_api.cpp

Lines changed: 8 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@
2525
#include <boost/regex.hpp>
2626
#include <boost/cregex.hpp>
2727

28+
#ifndef BOOST_REGEX_STANDALONE
29+
#include <boost/core/snprintf.hpp>
30+
#else
31+
namespace boost { namespace core { using std::swprintf; } }
32+
#endif
33+
2834
#ifndef BOOST_WORKAROUND
2935
#define BOOST_WORKAROUND(x, y) false
3036
#endif
@@ -37,15 +43,6 @@
3743
#pragma warning(disable:981)
3844
#endif
3945

40-
#if defined(BOOST_NO_STDC_NAMESPACE) || defined(__NetBSD__)
41-
namespace std{
42-
# ifndef BOOST_NO_SWPRINTF
43-
using ::swprintf;
44-
# endif
45-
}
46-
#endif
47-
48-
4946
namespace boost{
5047

5148
namespace {
@@ -181,7 +178,6 @@ BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorW(int code, const regex_tW*
181178
}
182179
return result;
183180
}
184-
#if !defined(BOOST_NO_SWPRINTF)
185181
if(code == REG_ATOI)
186182
{
187183
wchar_t localbuf[5];
@@ -194,7 +190,7 @@ BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorW(int code, const regex_tW*
194190
#if defined(_WIN32_WCE) && !defined(UNDER_CE)
195191
(std::swprintf)(localbuf, L"%d", i);
196192
#else
197-
(std::swprintf)(localbuf, 5, L"%d", i);
193+
(boost::core::swprintf)(localbuf, 5, L"%d", i);
198194
#endif
199195
if(std::wcslen(localbuf) < buf_size)
200196
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
@@ -208,7 +204,7 @@ BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorW(int code, const regex_tW*
208204
#if defined(_WIN32_WCE) && !defined(UNDER_CE)
209205
(std::swprintf)(localbuf, L"%d", 0);
210206
#else
211-
(std::swprintf)(localbuf, 5, L"%d", 0);
207+
(boost::core::swprintf)(localbuf, 5, L"%d", 0);
212208
#endif
213209
if(std::wcslen(localbuf) < buf_size)
214210
#if BOOST_WORKAROUND(BOOST_MSVC, >= 1400) && !defined(_WIN32_WCE) && !defined(UNDER_CE)
@@ -218,7 +214,6 @@ BOOST_REGEX_DECL regsize_t BOOST_REGEX_CCALL regerrorW(int code, const regex_tW*
218214
#endif
219215
return std::wcslen(localbuf) + 1;
220216
}
221-
#endif
222217
if(code <= (int)REG_E_UNKNOWN)
223218
{
224219
std::string p;

0 commit comments

Comments
 (0)