Skip to content

Commit 46c8f2f

Browse files
committed
Cleaned up lingering static locale issues
1 parent b949bfe commit 46c8f2f

File tree

2 files changed

+23
-46
lines changed

2 files changed

+23
-46
lines changed

include/scn/detail/locale.h

Lines changed: 18 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -26,38 +26,6 @@
2626
#include <cwchar>
2727
#include <string>
2828

29-
#ifndef SCN_DEFAULT_LOCALE_TRUE
30-
#define SCN_DEFAULT_LOCALE_TRUE "true"
31-
#endif
32-
33-
#ifndef SCN_DEFAULT_LOCALE_FALSE
34-
#define SCN_DEFAULT_LOCALE_FALSE "false"
35-
#endif
36-
37-
#ifndef SCN_DEFAULT_LOCALE_DECIMAL
38-
#define SCN_DEFAULT_LOCALE_DECIMAL '.'
39-
#endif
40-
41-
#ifndef SCN_DEFAULT_LOCALE_SEPARATOR
42-
#define SCN_DEFAULT_LOCALE_SEPARATOR ','
43-
#endif
44-
45-
#ifndef SCN_DEFAULT_LOCALE_TRUE_WIDE
46-
#define SCN_DEFAULT_LOCALE_TRUE_WIDE L"true"
47-
#endif
48-
49-
#ifndef SCN_DEFAULT_LOCALE_FALSE_WIDE
50-
#define SCN_DEFAULT_LOCALE_FALSE_WIDE L"false"
51-
#endif
52-
53-
#ifndef SCN_DEFAULT_LOCALE_DECIMAL_WIDE
54-
#define SCN_DEFAULT_LOCALE_DECIMAL_WIDE L'.'
55-
#endif
56-
57-
#ifndef SCN_DEFAULT_LOCALE_SEPARATOR_WIDE
58-
#define SCN_DEFAULT_LOCALE_SEPARATOR_WIDE L','
59-
#endif
60-
6129
namespace scn {
6230
SCN_BEGIN_NAMESPACE
6331

@@ -144,38 +112,38 @@ namespace scn {
144112
struct locale_defaults<char> {
145113
static constexpr string_view truename()
146114
{
147-
return {SCN_DEFAULT_LOCALE_TRUE};
115+
return {"true"};
148116
}
149117
static constexpr string_view falsename()
150118
{
151-
return {SCN_DEFAULT_LOCALE_FALSE};
119+
return {"false"};
152120
}
153121
static constexpr char decimal_point() noexcept
154122
{
155-
return SCN_DEFAULT_LOCALE_DECIMAL;
123+
return '.';
156124
}
157125
static constexpr char thousands_separator() noexcept
158126
{
159-
return SCN_DEFAULT_LOCALE_SEPARATOR;
127+
return ',';
160128
}
161129
};
162130
template <>
163131
struct locale_defaults<wchar_t> {
164132
static constexpr wstring_view truename()
165133
{
166-
return {SCN_DEFAULT_LOCALE_TRUE_WIDE};
134+
return {L"true"};
167135
}
168136
static constexpr wstring_view falsename()
169137
{
170-
return {SCN_DEFAULT_LOCALE_FALSE_WIDE};
138+
return {L"false"};
171139
}
172140
static constexpr wchar_t decimal_point() noexcept
173141
{
174-
return SCN_DEFAULT_LOCALE_DECIMAL_WIDE;
142+
return L'.';
175143
}
176144
static constexpr wchar_t thousands_separator() noexcept
177145
{
178-
return SCN_DEFAULT_LOCALE_SEPARATOR_WIDE;
146+
return L',';
179147
}
180148
};
181149
} // namespace detail
@@ -502,6 +470,15 @@ namespace scn {
502470

503471
// default
504472
constexpr basic_locale_ref() = default;
473+
474+
// hardcoded "C", constexpr, should be preferred whenever possible
475+
constexpr static_type get_static() const
476+
{
477+
return {};
478+
}
479+
480+
#if !SCN_USE_STATIC_LOCALE
481+
505482
// nullptr = global
506483
constexpr basic_locale_ref(const void* p) : m_payload(p) {}
507484

@@ -515,12 +492,6 @@ namespace scn {
515492
return m_payload != nullptr;
516493
}
517494

518-
// hardcoded "C", constexpr, should be preferred whenever possible
519-
constexpr static_type get_static() const
520-
{
521-
return {};
522-
}
523-
524495
// hardcoded "C", not constexpr
525496
default_type& get_default()
526497
{
@@ -599,6 +570,7 @@ namespace scn {
599570
mutable detail::unique_ptr<custom_type> m_custom{nullptr};
600571
const void* m_payload{nullptr};
601572
default_type m_default{};
573+
#endif
602574
};
603575

604576
template <typename CharT, typename Locale>

src/reader_float.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ namespace scn {
8282
size_t& chars,
8383
uint8_t options)
8484
{
85+
#if !SCN_USE_STATIC_LOCALE
8586
// Get current C locale
8687
const auto loc = std::setlocale(LC_NUMERIC, nullptr);
8788
// For whatever reason, this cannot be stored in the heap if
@@ -93,15 +94,19 @@ namespace scn {
9394
std::strcpy(locbuf, loc);
9495

9596
std::setlocale(LC_NUMERIC, "C");
97+
#endif
9698

9799
CharT* end{};
98100
errno = 0;
99101
T f = f_strtod(str, &end);
100102
chars = static_cast<size_t>(end - str);
101103
auto err = errno;
104+
105+
#if !SCN_USE_STATIC_LOCALE
102106
// Reset locale
103107
std::setlocale(LC_NUMERIC, locbuf);
104108
errno = 0;
109+
#endif
105110

106111
SCN_GCC_COMPAT_PUSH
107112
SCN_GCC_COMPAT_IGNORE("-Wfloat-equal")

0 commit comments

Comments
 (0)