Skip to content

Commit d451114

Browse files
committed
support platforms without wchar_t
closes #51
1 parent ac84c4f commit d451114

File tree

3 files changed

+100
-20
lines changed

3 files changed

+100
-20
lines changed

include/boost/static_string/config.hpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -228,6 +228,24 @@ defined(BOOST_STATIC_STRING_CPP14)
228228
#define BOOST_STATIC_STRING_GCC5_BAD_CONSTEXPR
229229
#endif
230230

231+
#ifndef BOOST_STATIC_STRING_STANDALONE
232+
#if ! defined(BOOST_NO_CWCHAR) && ! defined(BOOST_NO_SWPRINTF)
233+
#define BOOST_STATIC_STRING_HAS_WCHAR
234+
#endif
235+
#else
236+
#ifndef __has_include
237+
// If we don't have __has_include in standalone,
238+
// we will assume that <cwchar> exists.
239+
#define BOOST_STATIC_STRING_HAS_WCHAR
240+
#elif __has_include(<cwchar>)
241+
#define BOOST_STATIC_STRING_HAS_WCHAR
242+
#endif
243+
#endif
244+
245+
#ifdef BOOST_STATIC_STRING_HAS_WCHAR
246+
#include <cwchar>
247+
#endif
248+
231249
// Define the basic string_view type used by the library
232250
// Conversions to and from other available string_view types
233251
// are still defined.

include/boost/static_string/static_string.hpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
#include <algorithm>
3535
#include <cstdint>
3636
#include <cstdio>
37-
#include <cwchar>
3837
#include <functional>
3938
#include <initializer_list>
4039
#include <limits>
@@ -58,9 +57,11 @@ template<std::size_t N>
5857
using static_string =
5958
basic_static_string<N, char, std::char_traits<char>>;
6059

60+
#ifdef BOOST_STATIC_STRING_HAS_WCHAR
6161
template<std::size_t N>
6262
using static_wstring =
6363
basic_static_string<N, wchar_t, std::char_traits<wchar_t>>;
64+
#endif
6465

6566
template<std::size_t N>
6667
using static_u16string =
@@ -550,6 +551,7 @@ to_static_string_int_impl(Integer value) noexcept
550551
return static_string<N>(digits_begin, std::distance(digits_begin, digits_end));
551552
}
552553

554+
#ifdef BOOST_STATIC_STRING_HAS_WCHAR
553555
template<std::size_t N, typename Integer>
554556
inline
555557
static_wstring<N>
@@ -561,6 +563,7 @@ to_static_wstring_int_impl(Integer value) noexcept
561563
digits_end, value, std::is_signed<Integer>{});
562564
return static_wstring<N>(digits_begin, std::distance(digits_begin, digits_end));
563565
}
566+
#endif
564567

565568
BOOST_STATIC_STRING_CPP11_CONSTEXPR
566569
inline
@@ -638,6 +641,7 @@ to_static_string_float_impl(long double value) noexcept
638641
return static_string<N>(buffer);
639642
}
640643

644+
#ifdef BOOST_STATIC_STRING_HAS_WCHAR
641645
template<std::size_t N>
642646
inline
643647
static_wstring<N>
@@ -711,6 +715,7 @@ to_static_wstring_float_impl(long double value) noexcept
711715
// this will not throw
712716
return static_wstring<N>(buffer);
713717
}
718+
#endif
714719

715720
#if defined(__GNUC__) && __GNUC__ >= 7
716721
#pragma GCC diagnostic pop
@@ -6202,6 +6207,7 @@ to_static_string(long double value) noexcept
62026207
std::numeric_limits<long double>::max_digits10 + 4>(value);
62036208
}
62046209

6210+
#ifdef BOOST_STATIC_STRING_HAS_WCHAR
62056211
/// Converts `value` to a `static_wstring`
62066212
static_wstring<std::numeric_limits<int>::digits10 + 2>
62076213
inline
@@ -6282,6 +6288,7 @@ to_static_wstring(long double value) noexcept
62826288
return detail::to_static_wstring_float_impl<
62836289
std::numeric_limits<long double>::max_digits10 + 4>(value);
62846290
}
6291+
#endif
62856292

62866293
//------------------------------------------------------------------------------
62876294
//
@@ -6322,7 +6329,9 @@ hash_value(
63226329
//------------------------------------------------------------------------------
63236330

63246331
using static_strings::static_string;
6332+
#ifdef BOOST_STATIC_STRING_HAS_WCHAR
63256333
using static_strings::static_wstring;
6334+
#endif
63266335
using static_strings::static_u16string;
63276336
using static_strings::static_u32string;
63286337
} // boost

test/static_string.cpp

Lines changed: 72 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -245,29 +245,52 @@ testR(S s, typename S::size_type pos, typename S::size_type n1, const typename S
245245

246246
template<typename Arithmetic>
247247
bool
248-
testTS(Arithmetic value, const char* str_expected = "", const wchar_t* wstr_expected = L"", bool test_expected = false)
248+
testTS(Arithmetic value, const char* str_expected = "", bool test_expected = false)
249249
{
250250
const auto str = to_static_string(value);
251-
const auto wstr = to_static_wstring(value);
252251
if (std::is_floating_point<Arithmetic>::value)
253252
{
254253
const auto std_res = std::to_string(value);
254+
return str == std_res.data();
255+
}
256+
else
257+
{
258+
if (std::is_signed<Arithmetic>::value)
259+
{
260+
return
261+
Arithmetic(std::strtoll(str.begin(), nullptr, 10)) == value &&
262+
(! test_expected || str == str_expected);
263+
}
264+
else
265+
{
266+
return Arithmetic(std::strtoull(str.begin(), nullptr, 10)) == value &&
267+
(! test_expected || str == str_expected);
268+
}
269+
}
270+
}
271+
272+
template<typename Arithmetic>
273+
bool
274+
testTWS(Arithmetic value, const wchar_t* wstr_expected = L"", bool test_expected = false)
275+
{
276+
const auto wstr = to_static_wstring(value);
277+
if (std::is_floating_point<Arithmetic>::value)
278+
{
255279
const auto wstd_res = std::to_wstring(value);
256-
return str == std_res.data() && wstr == wstd_res.data();
280+
return wstr == wstd_res.data();
257281
}
258282
else
259283
{
260284
if (std::is_signed<Arithmetic>::value)
261285
{
262-
return Arithmetic(std::strtoll(str.begin(), nullptr, 10)) == value &&
286+
return
263287
Arithmetic(std::wcstoll(wstr.begin(), nullptr, 10)) == value &&
264-
(test_expected ? str == str_expected && wstr == wstr_expected : true);
288+
(! test_expected || wstr == wstr_expected);
265289
}
266290
else
267291
{
268-
return Arithmetic(std::strtoull(str.begin(), nullptr, 10)) == value &&
269-
Arithmetic(std::wcstoull(wstr.begin(), nullptr, 10)) == value &&
270-
(test_expected ? str == str_expected && wstr == wstr_expected : true);
292+
return Arithmetic(std::wcstoull(wstr.begin(), nullptr, 10)) == value &&
293+
(! test_expected || wstr == wstr_expected);
271294
}
272295
}
273296
}
@@ -3913,16 +3936,16 @@ testGeneral()
39133936
void
39143937
testToStaticString()
39153938
{
3916-
BOOST_TEST(testTS(0, "0", L"0", true));
3917-
BOOST_TEST(testTS(0u, "0", L"0", true));
3918-
BOOST_TEST(testTS(0xffff, "65535", L"65535", true));
3919-
BOOST_TEST(testTS(0x10000, "65536", L"65536", true));
3920-
BOOST_TEST(testTS(0xffffffff, "4294967295", L"4294967295", true));
3921-
BOOST_TEST(testTS(-65535, "-65535", L"-65535", true));
3922-
BOOST_TEST(testTS(-65536, "-65536", L"-65536", true));
3923-
BOOST_TEST(testTS(-4294967295ll, "-4294967295", L"-4294967295", true));
3924-
BOOST_TEST(testTS(1, "1", L"1", true));
3925-
BOOST_TEST(testTS(-1, "-1", L"-1", true));
3939+
BOOST_TEST(testTS(0, "0", true));
3940+
BOOST_TEST(testTS(0u, "0", true));
3941+
BOOST_TEST(testTS(0xffff, "65535", true));
3942+
BOOST_TEST(testTS(0x10000, "65536", true));
3943+
BOOST_TEST(testTS(0xffffffff, "4294967295", true));
3944+
BOOST_TEST(testTS(-65535, "-65535", true));
3945+
BOOST_TEST(testTS(-65536, "-65536", true));
3946+
BOOST_TEST(testTS(-4294967295ll, "-4294967295", true));
3947+
BOOST_TEST(testTS(1, "1", true));
3948+
BOOST_TEST(testTS(-1, "-1", true));
39263949
BOOST_TEST(testTS(0.1));
39273950
BOOST_TEST(testTS(0.0000001));
39283951
BOOST_TEST(testTS(-0.0000001));
@@ -3956,6 +3979,35 @@ testToStaticString()
39563979
BOOST_TEST(str.find('e') != static_string<0>::npos || str.find('.') !=
39573980
static_string<0>::npos || str == "infinity" || str == "inf");
39583981
}
3982+
3983+
#ifdef BOOST_STATIC_STRING_HAS_WCHAR
3984+
3985+
BOOST_TEST(testTWS(0, L"0", true));
3986+
BOOST_TEST(testTWS(0u, L"0", true));
3987+
BOOST_TEST(testTWS(0xffff, L"65535", true));
3988+
BOOST_TEST(testTWS(0x10000, L"65536", true));
3989+
BOOST_TEST(testTWS(0xffffffff, L"4294967295", true));
3990+
BOOST_TEST(testTWS(-65535, L"-65535", true));
3991+
BOOST_TEST(testTWS(-65536, L"-65536", true));
3992+
BOOST_TEST(testTWS(-4294967295ll, L"-4294967295", true));
3993+
BOOST_TEST(testTWS(1, L"1", true));
3994+
BOOST_TEST(testTWS(-1, L"-1", true));
3995+
BOOST_TEST(testTWS(0.1));
3996+
BOOST_TEST(testTWS(0.0000001));
3997+
BOOST_TEST(testTWS(-0.0000001));
3998+
BOOST_TEST(testTWS(-0.1));
3999+
BOOST_TEST(testTWS(1234567890.0001));
4000+
BOOST_TEST(testTWS(1.123456789012345));
4001+
BOOST_TEST(testTWS(-1234567890.1234));
4002+
BOOST_TEST(testTWS(-1.123456789012345));
4003+
4004+
BOOST_TEST(testTWS(std::numeric_limits<long long>::max()));
4005+
BOOST_TEST(testTWS(std::numeric_limits<long long>::min()));
4006+
BOOST_TEST(testTWS(std::numeric_limits<unsigned long long>::max()));
4007+
BOOST_TEST(testTWS(std::numeric_limits<unsigned long long>::max()));
4008+
BOOST_TEST(testTWS(std::numeric_limits<long double>::min()));
4009+
BOOST_TEST(testTWS(std::numeric_limits<float>::min()));
4010+
39594011
{
39604012
auto str = to_static_wstring(std::numeric_limits<float>::max());
39614013
BOOST_TEST(str.find('e') != static_string<0>::npos || str.find('.') !=
@@ -3971,6 +4023,7 @@ testToStaticString()
39714023
BOOST_TEST(str.find('e') != static_string<0>::npos || str.find('.') !=
39724024
static_string<0>::npos || str == L"infinity" || str == L"inf");
39734025
}
4026+
#endif
39744027
}
39754028

39764029
// done
@@ -6007,7 +6060,7 @@ testFind()
60076060
void
60086061
testReplace()
60096062
{
6010-
// replace(size_type pos1, size_type n1, const charT* s, size_type n2);
6063+
// replace(size_type pos1, size_type n1, const charT* s, size_type n2);
60116064
{
60126065
static_string<20> fs1 = "helloworld";
60136066
BOOST_TEST(fs1.replace(5, 2, fs1.data() + 1, 8) == "helloelloworlrld");

0 commit comments

Comments
 (0)