Skip to content

Commit b949bfe

Browse files
committed
Added SCN_USE_STATIC_LOCALE
1 parent a56b64f commit b949bfe

File tree

7 files changed

+40
-2
lines changed

7 files changed

+40
-2
lines changed

CMakeLists.txt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ option(SCN_BUILD_BUILDTIME "Generate build time test target" OFF)
3636

3737
option(SCN_USE_BUNDLED_FAST_FLOAT "Use lemire/fast_float bundled with scnlib" ON)
3838

39+
option(SCN_USE_STATIC_LOCALE "Disable all localization" OFF)
40+
3941
file(READ include/scn/detail/config.h config_h)
4042
if (NOT config_h MATCHES "SCN_VERSION SCN_COMPILER\\(([0-9]+), ([0-9]+), ([0-9]+)\\)")
4143
message(FATAL_ERROR "Cannot get SCN_VERSION from config.h")
@@ -83,6 +85,9 @@ function(generate_library_target target_name)
8385
target_compile_features(${target_name} PUBLIC cxx_std_11)
8486
set_private_flags(${target_name})
8587

88+
target_compile_definitions(${target_name} PUBLIC
89+
-DSCN_USE_STATIC_LOCALE=$<IF:$<BOOL:${SCN_USE_STATIC_LOCALE}>,1,0>)
90+
8691
if (SCN_USE_BUNDLED_FAST_FLOAT)
8792
target_include_directories(${target_name} PRIVATE
8893
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src/deps/fast_float/single_include>)
@@ -100,6 +105,9 @@ function(generate_header_only_target target_name)
100105
-DSCN_HEADER_ONLY=1)
101106
target_compile_features(${target_name} INTERFACE cxx_std_11)
102107

108+
target_compile_definitions(${target_name} INTERFACE
109+
-DSCN_USE_STATIC_LOCALE=$<IF:$<BOOL:${SCN_USE_STATIC_LOCALE}>,1,0>)
110+
103111
if (SCN_USE_BUNDLED_FAST_FLOAT)
104112
target_include_directories(${target_name} INTERFACE
105113
$<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}/src/deps/fast_float/single_include>)

include/scn/reader/common.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -962,10 +962,12 @@ namespace scn {
962962
m_width{width},
963963
m_fn{get_fn(localized, width != 0)}
964964
{
965+
#if !SCN_USE_STATIC_LOCALE
965966
if (localized) {
966967
l.prepare_localized();
967968
m_locale = l.get_localized_unsafe();
968969
}
970+
#endif
969971
}
970972

971973
/**
@@ -1056,9 +1058,11 @@ namespace scn {
10561058

10571059
static SCN_CONSTEXPR14 fn_type get_fn(bool localized, bool counting)
10581060
{
1061+
#if !SCN_USE_STATIC_LOCALE
10591062
if (localized) {
10601063
return counting ? localized_call_counting : localized_call;
10611064
}
1065+
#endif
10621066
return counting ? call_counting : call;
10631067
}
10641068
};

include/scn/reader/float.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,13 @@ namespace scn {
131131
auto do_parse_float = [&](span<const char_type> s) -> error {
132132
T tmp = 0;
133133
expected<std::ptrdiff_t> ret{0};
134+
#if SCN_USE_STATIC_LOCALE
135+
ret = _read_float(
136+
tmp, s,
137+
ctx.locale()
138+
.get_static()
139+
.decimal_point());
140+
#else
134141
if (SCN_UNLIKELY((format_options & localized_digits) != 0 ||
135142
((common_options & localized) != 0 &&
136143
(format_options & allow_hex) != 0))) {
@@ -151,6 +158,7 @@ namespace scn {
151158
.get((common_options & localized) != 0)
152159
.decimal_point());
153160
}
161+
#endif
154162

155163
if (!ret) {
156164
return ret.error();

include/scn/reader/int.h

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,7 @@ namespace scn {
231231
auto do_parse_int = [&](span<const char_type> s) -> error {
232232
T tmp = 0;
233233
expected<std::ptrdiff_t> ret{0};
234+
#if !SCN_USE_STATIC_LOCALE
234235
if (SCN_UNLIKELY((format_options & localized_digits) !=
235236
0)) {
236237
SCN_CLANG_PUSH_IGNORE_UNDEFINED_TEMPLATE
@@ -270,7 +271,9 @@ namespace scn {
270271
}
271272
SCN_CLANG_POP_IGNORE_UNDEFINED_TEMPLATE
272273
}
273-
else {
274+
else
275+
#endif
276+
{
274277
SCN_CLANG_PUSH_IGNORE_UNDEFINED_TEMPLATE
275278
ret = _parse_int(tmp, s);
276279
SCN_CLANG_POP_IGNORE_UNDEFINED_TEMPLATE
@@ -404,7 +407,11 @@ namespace scn {
404407
return e;
405408
}
406409
auto thsep = ctx.locale()
410+
#if SCN_USE_STATIC_LOCALE
411+
.get_static()
412+
#else
407413
.get((common_options & localized) != 0)
414+
#endif
408415
.thousands_separator();
409416

410417
auto it = tmp.begin();

include/scn/reader/string.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ namespace scn {
116116

117117
if (get_option(flag::use_specifiers) &&
118118
!get_option(flag::accept_all)) {
119+
#if !SCN_USE_STATIC_LOCALE
119120
if (localized) {
120121
if (get_option(specifier::letters)) {
121122
get_option(specifier::letters) = false;
@@ -135,7 +136,9 @@ namespace scn {
135136
get_option(specifier::digit) = true;
136137
}
137138
}
138-
else {
139+
else
140+
#endif
141+
{
139142
auto do_range = [&](char a, char b) {
140143
for (; a < b; ++a) {
141144
get_option(a) = true;
@@ -299,6 +302,7 @@ namespace scn {
299302
return not_inverted;
300303
}
301304

305+
#if !SCN_USE_STATIC_LOCALE
302306
if (get_option(flag::use_specifiers)) {
303307
SCN_EXPECT(localized); // ensured by sanitize()
304308
SCN_UNUSED(localized);
@@ -353,6 +357,7 @@ namespace scn {
353357
}
354358
SCN_CLANG_POP_IGNORE_UNDEFINED_TEMPLATE
355359
}
360+
#endif
356361
if (get_option(flag::use_chars) && (ch >= 0 && ch <= 0x7f)) {
357362
if (get_option(static_cast<char>(ch))) {
358363
return not_inverted;

include/scn/reader/types.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -107,10 +107,12 @@ namespace scn {
107107
if ((format_options & allow_string) != 0) {
108108
auto truename = ctx.locale().get_static().truename();
109109
auto falsename = ctx.locale().get_static().falsename();
110+
#if !SCN_USE_STATIC_LOCALE
110111
if ((common_options & localized) != 0) {
111112
truename = ctx.locale().get_localized().truename();
112113
falsename = ctx.locale().get_localized().falsename();
113114
}
115+
#endif
114116
const auto max_len =
115117
detail::max(truename.size(), falsename.size());
116118
std::basic_string<char_type> buf;
@@ -155,6 +157,7 @@ namespace scn {
155157
}
156158

157159
if ((format_options & allow_int) != 0) {
160+
#if !SCN_USE_STATIC_LOCALE
158161
if ((format_options & localized_digits) != 0) {
159162
int i{};
160163
auto s = integer_scanner<int>{};
@@ -179,6 +182,7 @@ namespace scn {
179182
}
180183
return {};
181184
}
185+
#endif
182186

183187
unsigned char buf[4] = {0};
184188
auto cp = read_code_point(ctx.range(), make_span(buf, 4));

src/reader_float.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,11 @@ namespace scn {
288288
flags.format = static_cast<::fast_float::chars_format>(
289289
flags.format | ::fast_float::scientific);
290290
}
291+
#if !SCN_USE_STATIC_LOCALE
291292
if ((options & detail::float_scanner<T>::localized) != 0) {
292293
flags.decimal_point = locale_decimal_point;
293294
}
295+
#endif
294296

295297
const auto result = ::fast_float::from_chars_advanced(
296298
str, str + len, value, flags);

0 commit comments

Comments
 (0)