1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
|
// Copyright 2017 Elias Kosunen
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
// This file is a part of scnlib:
// https://github.com/eliaskosunen/scnlib
#ifndef SCN_DETAIL_LOCALE_H
#define SCN_DETAIL_LOCALE_H
#include "../unicode/unicode.h"
#include "../util/array.h"
#include "../util/string_view.h"
#include "../util/unique_ptr.h"
#include <cwchar>
#include <string>
namespace scn {
SCN_BEGIN_NAMESPACE
namespace detail {
constexpr bool has_zero(uint64_t v)
{
return (v - UINT64_C(0x0101010101010101)) & ~v &
UINT64_C(0x8080808080808080);
}
template <typename CharT>
CharT ascii_widen(char ch);
template <>
constexpr char ascii_widen(char ch)
{
return ch;
}
template <>
constexpr wchar_t ascii_widen(char ch)
{
return static_cast<wchar_t>(ch);
}
// Hand write to avoid C locales and thus noticeable performance losses
inline bool is_space(char ch) noexcept
{
static constexpr detail::array<bool, 256> lookup = {
{false, false, false, false, false, false, false, false, false,
true, true, true, true, true, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, true, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false, false, false, false, false, false,
false, false, false, false}};
return lookup[static_cast<size_t>(static_cast<unsigned char>(ch))];
}
constexpr inline bool is_space(wchar_t ch) noexcept
{
return ch == 0x20 || (ch >= 0x09 && ch <= 0x0d);
}
constexpr inline bool is_space(code_point cp) noexcept
{
return cp == 0x20 || (cp >= 0x09 && cp <= 0x0d);
}
constexpr inline bool is_digit(char ch) noexcept
{
return ch >= '0' && ch <= '9';
}
constexpr inline bool is_digit(wchar_t ch) noexcept
{
return ch >= L'0' && ch <= L'9';
}
constexpr inline bool is_digit(code_point cp) noexcept
{
return cp >= '0' && cp <= '9';
}
template <typename CharT>
struct locale_defaults;
template <>
struct locale_defaults<char> {
static constexpr string_view truename()
{
return {"true"};
}
static constexpr string_view falsename()
{
return {"false"};
}
static constexpr char decimal_point() noexcept
{
return '.';
}
static constexpr char thousands_separator() noexcept
{
return ',';
}
};
template <>
struct locale_defaults<wchar_t> {
static constexpr wstring_view truename()
{
return {L"true"};
}
static constexpr wstring_view falsename()
{
return {L"false"};
}
static constexpr wchar_t decimal_point() noexcept
{
return L'.';
}
static constexpr wchar_t thousands_separator() noexcept
{
return L',';
}
};
} // namespace detail
SCN_CLANG_PUSH_IGNORE_UNDEFINED_TEMPLATE
SCN_CLANG_PUSH
SCN_CLANG_IGNORE("-Wpadded")
// scn::scan:
// - no L flag -> use hard-coded defaults, akin to "C"
// locale_ref.default() -> default_locale_ref
// - L flag -> use global C++ locale
// locale_ref.localized() -> custom_locale_ref (global C++)
// scn::scan_localized:
// - no L flag -> use hard-coded defaults, akin to "C"
// locale_ref.default() -> default_locale_ref
// - L flag -> use given C++ locale
// locale_ref.localized() -> custom_locale_ref (given locale)
namespace detail {
// constexpr locale
template <typename CharT, typename SV, typename Def>
struct basic_static_locale_ref_base {
using char_type = CharT;
using string_view_type = SV;
using defaults = Def;
static constexpr bool is_static = true;
constexpr basic_static_locale_ref_base() = default;
static constexpr bool is_space(char_type ch)
{
return detail::is_space(ch);
}
static constexpr bool is_digit(char_type ch)
{
return detail::is_digit(ch);
}
static SCN_CONSTEXPR14 bool is_space(span<const char_type> ch)
{
SCN_EXPECT(ch.size() >= 1);
return detail::is_space(ch[0]);
}
static SCN_CONSTEXPR14 bool is_digit(span<const char_type> ch)
{
SCN_EXPECT(ch.size() >= 1);
return detail::is_digit(ch[0]);
}
static constexpr char_type decimal_point()
{
return defaults::decimal_point();
}
static constexpr char_type thousands_separator()
{
return defaults::thousands_separator();
}
static constexpr string_view_type truename()
{
return defaults::truename();
}
static constexpr string_view_type falsename()
{
return defaults::falsename();
}
};
template <typename CharT>
struct basic_static_locale_ref
: basic_static_locale_ref_base<CharT,
basic_string_view<CharT>,
locale_defaults<CharT>> {
};
template <>
struct basic_static_locale_ref<code_point>
: basic_static_locale_ref_base<code_point,
string_view,
locale_defaults<char>> {
};
// base class
template <typename CharT>
class basic_locale_ref_impl_base {
public:
using char_type = CharT;
using string_type = std::basic_string<char_type>;
using string_view_type = basic_string_view<char_type>;
static constexpr bool is_static = false;
basic_locale_ref_impl_base() = default;
basic_locale_ref_impl_base(const basic_locale_ref_impl_base&) =
default;
basic_locale_ref_impl_base(basic_locale_ref_impl_base&&) = default;
basic_locale_ref_impl_base& operator=(
const basic_locale_ref_impl_base&) = default;
basic_locale_ref_impl_base& operator=(
basic_locale_ref_impl_base&&) = default;
#define SCN_DEFINE_LOCALE_REF_CTYPE(f) \
bool is_##f(char_type ch) const \
{ \
return do_is_##f(ch); \
} \
bool is_##f(span<const char_type> ch) const \
{ \
return do_is_##f(ch); \
}
SCN_DEFINE_LOCALE_REF_CTYPE(space)
SCN_DEFINE_LOCALE_REF_CTYPE(digit)
// SCN_DEFINE_LOCALE_REF_CTYPE(alnum)
// SCN_DEFINE_LOCALE_REF_CTYPE(alpha)
// SCN_DEFINE_LOCALE_REF_CTYPE(blank)
// SCN_DEFINE_LOCALE_REF_CTYPE(cntrl)
// SCN_DEFINE_LOCALE_REF_CTYPE(graph)
// SCN_DEFINE_LOCALE_REF_CTYPE(lower)
// SCN_DEFINE_LOCALE_REF_CTYPE(print)
// SCN_DEFINE_LOCALE_REF_CTYPE(punct)
// SCN_DEFINE_LOCALE_REF_CTYPE(upper)
// SCN_DEFINE_LOCALE_REF_CTYPE(xdigit)
#undef SCN_DEFINE_LOCALE_REF_CTYPE
char_type decimal_point() const
{
return do_decimal_point();
}
char_type thousands_separator() const
{
return do_thousands_separator();
}
string_view_type truename() const
{
return do_truename();
}
string_view_type falsename() const
{
return do_falsename();
}
protected:
~basic_locale_ref_impl_base() = default;
private:
#define SCN_DECLARE_LOCALE_REF_CTYPE_DO(f) \
virtual bool do_is_##f(char_type) const = 0; \
virtual bool do_is_##f(span<const char_type>) const = 0;
SCN_DECLARE_LOCALE_REF_CTYPE_DO(space)
SCN_DECLARE_LOCALE_REF_CTYPE_DO(digit)
// SCN_DECLARE_LOCALE_REF_CTYPE_DO(alnum)
// SCN_DECLARE_LOCALE_REF_CTYPE_DO(alpha)
// SCN_DECLARE_LOCALE_REF_CTYPE_DO(blank)
// SCN_DECLARE_LOCALE_REF_CTYPE_DO(cntrl)
// SCN_DECLARE_LOCALE_REF_CTYPE_DO(graph)
// SCN_DECLARE_LOCALE_REF_CTYPE_DO(lower)
// SCN_DECLARE_LOCALE_REF_CTYPE_DO(print)
// SCN_DECLARE_LOCALE_REF_CTYPE_DO(punct)
// SCN_DECLARE_LOCALE_REF_CTYPE_DO(upper)
// SCN_DECLARE_LOCALE_REF_CTYPE_DO(xdigit)
#undef SCN_DECLARE_LOCALE_REF_CTYPE_DO
virtual char_type do_decimal_point() const = 0;
virtual char_type do_thousands_separator() const = 0;
virtual string_view_type do_truename() const = 0;
virtual string_view_type do_falsename() const = 0;
};
// hardcoded "C", using static_locale_ref
template <typename CharT>
class basic_default_locale_ref final
: public basic_locale_ref_impl_base<CharT> {
using base = basic_locale_ref_impl_base<CharT>;
public:
using char_type = typename base::char_type;
using string_view_type = typename base::string_view_type;
basic_default_locale_ref() = default;
private:
using static_type = basic_static_locale_ref<char_type>;
bool do_is_space(char_type ch) const override
{
return static_type::is_space(ch);
}
bool do_is_digit(char_type ch) const override
{
return static_type::is_digit(ch);
}
bool do_is_space(span<const char_type> ch) const override
{
return static_type::is_space(ch);
}
bool do_is_digit(span<const char_type> ch) const override
{
return static_type::is_digit(ch);
}
char_type do_decimal_point() const override
{
return static_type::decimal_point();
}
char_type do_thousands_separator() const override
{
return static_type::thousands_separator();
}
string_view_type do_truename() const override
{
return static_type::truename();
}
string_view_type do_falsename() const override
{
return static_type::falsename();
}
};
// custom
template <typename CharT>
class basic_custom_locale_ref final
: public basic_locale_ref_impl_base<CharT> {
using base = basic_locale_ref_impl_base<CharT>;
public:
using char_type = typename base::char_type;
using string_type = typename base::string_type;
using string_view_type = typename base::string_view_type;
basic_custom_locale_ref();
basic_custom_locale_ref(const void* locale);
basic_custom_locale_ref(const basic_custom_locale_ref&) = delete;
basic_custom_locale_ref& operator=(const basic_custom_locale_ref&) =
delete;
basic_custom_locale_ref(basic_custom_locale_ref&&);
basic_custom_locale_ref& operator=(basic_custom_locale_ref&&);
~basic_custom_locale_ref();
static basic_custom_locale_ref make_classic();
const void* get_locale() const
{
return m_locale;
}
void convert_to_global();
void convert_to_classic();
// narrow: locale multibyte -> locale wide
// wide: identity
error convert_to_wide(const CharT* from_begin,
const CharT* from_end,
const CharT*& from_next,
wchar_t* to_begin,
wchar_t* to_end,
wchar_t*& to_next) const;
expected<wchar_t> convert_to_wide(const CharT* from_begin,
const CharT* from_end) const;
#define SCN_DEFINE_CUSTOM_LOCALE_CTYPE(f) \
bool is_##f(char_type) const; \
bool is_##f(span<const char_type>) const; \
bool is_##f(code_point) const;
SCN_DEFINE_CUSTOM_LOCALE_CTYPE(alnum)
SCN_DEFINE_CUSTOM_LOCALE_CTYPE(alpha)
SCN_DEFINE_CUSTOM_LOCALE_CTYPE(blank)
SCN_DEFINE_CUSTOM_LOCALE_CTYPE(cntrl)
SCN_DEFINE_CUSTOM_LOCALE_CTYPE(graph)
SCN_DEFINE_CUSTOM_LOCALE_CTYPE(lower)
SCN_DEFINE_CUSTOM_LOCALE_CTYPE(print)
SCN_DEFINE_CUSTOM_LOCALE_CTYPE(punct)
SCN_DEFINE_CUSTOM_LOCALE_CTYPE(upper)
SCN_DEFINE_CUSTOM_LOCALE_CTYPE(xdigit)
#undef SCN_DEFINE_CUSTOM_LOCALE_CTYPE
bool is_space(code_point) const;
using base::is_space;
bool is_digit(code_point) const;
using base::is_digit;
template <typename T>
expected<std::ptrdiff_t> read_num(T& val,
const string_type& buf,
int base) const;
private:
SCN_CLANG_PUSH_IGNORE_UNDEFINED_TEMPLATE
bool do_is_space(char_type ch) const override;
bool do_is_digit(char_type ch) const override;
bool do_is_space(span<const char_type> ch) const override;
bool do_is_digit(span<const char_type> ch) const override;
SCN_CLANG_POP_IGNORE_UNDEFINED_TEMPLATE
char_type do_decimal_point() const override;
char_type do_thousands_separator() const override;
string_view_type do_truename() const override;
string_view_type do_falsename() const override;
void _initialize();
const void* m_locale{nullptr};
void* m_data{nullptr};
};
} // namespace detail
template <typename CharT>
class basic_locale_ref {
public:
using char_type = CharT;
using impl_base = detail::basic_locale_ref_impl_base<char_type>;
using static_type = detail::basic_static_locale_ref<char_type>;
using default_type = detail::basic_default_locale_ref<char_type>;
using custom_type = detail::basic_custom_locale_ref<char_type>;
// default
constexpr basic_locale_ref() = default;
// nullptr = global
constexpr basic_locale_ref(const void* p) : m_payload(p) {}
basic_locale_ref clone() const
{
return {m_payload};
}
constexpr bool has_custom() const
{
return m_payload != nullptr;
}
// hardcoded "C", constexpr, should be preferred whenever possible
constexpr static_type get_static() const
{
return {};
}
// hardcoded "C", not constexpr
default_type& get_default()
{
return m_default;
}
const default_type& get_default() const
{
return m_default;
}
// global locale or given locale
custom_type& get_localized()
{
_construct_custom();
return *m_custom;
}
const custom_type& get_localized() const
{
_construct_custom();
return *m_custom;
}
custom_type make_localized_classic() const
{
return custom_type::make_classic();
}
custom_type* get_localized_unsafe()
{
return m_custom.get();
}
const custom_type* get_localized_unsafe() const
{
return m_custom.get();
}
// virtual interface
impl_base& get(bool localized)
{
if (localized) {
return get_localized();
}
return get_default();
}
const impl_base& get(bool localized) const
{
if (localized) {
return get_localized();
}
return get_default();
}
void prepare_localized() const
{
_construct_custom();
}
void reset_locale(const void* payload)
{
m_custom.reset();
m_payload = payload;
_construct_custom();
}
private:
void _construct_custom() const
{
if (m_custom) {
// already constructed
return;
}
SCN_CLANG_PUSH_IGNORE_UNDEFINED_TEMPLATE
m_custom = detail::make_unique<custom_type>(m_payload);
SCN_CLANG_POP_IGNORE_UNDEFINED_TEMPLATE
}
mutable detail::unique_ptr<custom_type> m_custom{nullptr};
const void* m_payload{nullptr};
default_type m_default{};
};
template <typename CharT, typename Locale>
basic_locale_ref<CharT> make_locale_ref(const Locale& loc)
{
return {std::addressof(loc)};
}
template <typename CharT>
basic_locale_ref<CharT> make_default_locale_ref()
{
return {};
}
using locale_ref = basic_locale_ref<char>;
using wlocale_ref = basic_locale_ref<wchar_t>;
SCN_CLANG_POP // -Wpadded
SCN_CLANG_POP_IGNORE_UNDEFINED_TEMPLATE
SCN_END_NAMESPACE
} // namespace scn
#if defined(SCN_HEADER_ONLY) && SCN_HEADER_ONLY && !defined(SCN_LOCALE_CPP)
#include "locale.cpp"
#endif
#endif // SCN_DETAIL_LOCALE_H
|