forked from Alexhuszagh/rust-lexical
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlexical.hpp
1871 lines (1653 loc) · 81.1 KB
/
lexical.hpp
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
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* lexical_core
* ============
*
* Access lexical-core functionality from C++.
*
* License
* -------
*
* This is free and unencumbered software released into the public domain.
*
* Anyone is free to copy, modify, publish, use, compile, sell, or
* distribute this software, either in source code form or as a compiled
* binary, for any purpose, commercial or non-commercial, and by any
* means.
*
* In jurisdictions that recognize copyright laws, the author or authors
* of this software dedicate any and all copyright interest in the
* software to the public domain. We make this dedication for the benefit
* of the public at large and to the detriment of our heirs and
* successors. We intend this dedication to be an overt act of
* relinquishment in perpetuity of all present and future rights to this
* software under copyright law.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
* OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* For more information, please refer to <http://unlicense.org/>
*/
#ifndef LEXICAL_HPP_
#define LEXICAL_HPP_
#include "lexical.h"
#include <cassert>
#include <cstdint>
#include <cstring>
#include <iterator>
#include <stdexcept>
#include <string>
#include <tuple>
#include <type_traits>
#if __cplusplus >= 201703L
# include <string_view>
#endif
namespace lexical {
// Features must be enabled through the following macro definitions:
// 1. HAVE_RADIX
// 2. HAVE_ROUNDING
// CONFIG
// ------
inline uint8_t get_exponent_default_char()
{
return ::lexical_get_exponent_default_char();
}
inline void set_exponent_default_char(uint8_t ch)
{
::lexical_set_exponent_default_char(ch);
}
#ifdef HAVE_RADIX
inline uint8_t get_exponent_backup_char()
{
return ::lexical_get_exponent_backup_char();
}
inline void set_exponent_backup_char(uint8_t ch)
{
::lexical_set_exponent_backup_char(ch);
}
#endif // HAVE_RADIX
#ifdef HAVE_ROUNDING
// Rounding type for float-parsing.
enum class rounding_kind: uint32_t {
nearest_tie_even = ::lexical_nearest_tie_even,
nearest_tie_away_zero = ::lexical_nearest_tie_away_zero,
toward_positive_infinity = ::lexical_toward_positive_infinity,
toward_negative_infinity = ::lexical_toward_negative_infinity,
toward_zero = ::lexical_toward_zero,
};
inline rounding_kind get_float_rounding()
{
return static_cast<rounding_kind>(::lexical_get_float_rounding());
}
inline void set_float_rounding(rounding_kind rounding)
{
::lexical_set_float_rounding(static_cast<uint32_t>(rounding));
}
#endif // HAVE_ROUNDING
#ifdef HAVE_FORMAT
// BITFLAGS
// Bitflags for a serialized number format.
enum class number_format: uint64_t {
// FLAGS
required_integer_digits = lexical_required_integer_digits,
required_fraction_digits = lexical_required_fraction_digits,
required_exponent_digits = lexical_required_exponent_digits,
no_positive_mantissa_sign = lexical_no_positive_mantissa_sign,
required_mantissa_sign = lexical_required_mantissa_sign,
no_exponent_notation = lexical_no_exponent_notation,
no_positive_exponent_sign = lexical_no_positive_exponent_sign,
required_exponent_sign = lexical_required_exponent_sign,
no_exponent_without_fraction = lexical_no_exponent_without_fraction,
no_special = lexical_no_special,
case_sensitive_special = lexical_case_sensitive_special,
no_integer_leading_zeros = lexical_no_integer_leading_zeros,
no_float_leading_zeros = lexical_no_float_leading_zeros,
integer_internal_digit_separator = lexical_integer_internal_digit_separator,
fraction_internal_digit_separator = lexical_fraction_internal_digit_separator,
exponent_internal_digit_separator = lexical_exponent_internal_digit_separator,
integer_leading_digit_separator = lexical_integer_leading_digit_separator,
fraction_leading_digit_separator = lexical_fraction_leading_digit_separator,
exponent_leading_digit_separator = lexical_exponent_leading_digit_separator,
integer_trailing_digit_separator = lexical_integer_trailing_digit_separator,
fraction_trailing_digit_separator = lexical_fraction_trailing_digit_separator,
exponent_trailing_digit_separator = lexical_exponent_trailing_digit_separator,
integer_consecutive_digit_separator = lexical_integer_consecutive_digit_separator,
fraction_consecutive_digit_separator = lexical_fraction_consecutive_digit_separator,
exponent_consecutive_digit_separator = lexical_exponent_consecutive_digit_separator,
special_digit_separator = lexical_special_digit_separator,
required_digits = lexical_required_digits,
internal_digit_separator = lexical_internal_digit_separator,
leading_digit_separator = lexical_leading_digit_separator,
trailing_digit_separator = lexical_trailing_digit_separator,
consecutive_digit_separator = lexical_consecutive_digit_separator,
// MASKS
digit_separator_flag_mask = lexical_digit_separator_flag_mask,
integer_digit_separator_flag_mask = lexical_integer_digit_separator_flag_mask,
fraction_digit_separator_flag_mask = lexical_fraction_digit_separator_flag_mask,
exponent_digit_separator_flag_mask = lexical_exponent_digit_separator_flag_mask,
exponent_flag_mask = lexical_exponent_flag_mask,
flag_mask = lexical_flag_mask,
// PRE-DEFINED
// Note:
// The pre-defined enum definitions are the public API for
// lexical_number_format.
rust_literal = lexical_rust_literal,
rust_string = lexical_rust_string,
rust_string_strict = lexical_rust_string_strict,
python_literal = lexical_python_literal,
python_string = lexical_python_string,
cxx17_literal = lexical_cxx17_literal,
cxx17_string = lexical_cxx17_string,
cxx14_literal = lexical_cxx14_literal,
cxx14_string = lexical_cxx14_string,
cxx11_literal = lexical_cxx11_literal,
cxx11_string = lexical_cxx11_string,
cxx03_literal = lexical_cxx03_literal,
cxx03_string = lexical_cxx03_string,
cxx98_literal = lexical_cxx98_literal,
cxx98_string = lexical_cxx98_string,
c18_literal = lexical_c18_literal,
c18_string = lexical_c18_string,
c11_literal = lexical_c11_literal,
c11_string = lexical_c11_string,
c99_literal = lexical_c99_literal,
c99_string = lexical_c99_string,
c90_literal = lexical_c90_literal,
c90_string = lexical_c90_string,
c89_literal = lexical_c89_literal,
c89_string = lexical_c89_string,
ruby_literal = lexical_ruby_literal,
ruby_string = lexical_ruby_string,
swift_literal = lexical_swift_literal,
swift_string = lexical_swift_string,
go_literal = lexical_go_literal,
go_string = lexical_go_string,
haskell_literal = lexical_haskell_literal,
haskell_string = lexical_haskell_string,
javascript_literal = lexical_javascript_literal,
javascript_string = lexical_javascript_string,
perl_literal = lexical_perl_literal,
perl_string = lexical_perl_string,
php_literal = lexical_php_literal,
php_string = lexical_php_string,
java_literal = lexical_java_literal,
java_string = lexical_java_string,
r_literal = lexical_r_literal,
r_string = lexical_r_string,
kotlin_literal = lexical_kotlin_literal,
kotlin_string = lexical_kotlin_string,
julia_literal = lexical_julia_literal,
julia_string = lexical_julia_string,
csharp7_literal = lexical_csharp7_literal,
csharp7_string = lexical_csharp7_string,
csharp6_literal = lexical_csharp6_literal,
csharp6_string = lexical_csharp6_string,
csharp5_literal = lexical_csharp5_literal,
csharp5_string = lexical_csharp5_string,
csharp4_literal = lexical_csharp4_literal,
csharp4_string = lexical_csharp4_string,
csharp3_literal = lexical_csharp3_literal,
csharp3_string = lexical_csharp3_string,
csharp2_literal = lexical_csharp2_literal,
csharp2_string = lexical_csharp2_string,
csharp1_literal = lexical_csharp1_literal,
csharp1_string = lexical_csharp1_string,
kawa_literal = lexical_kawa_literal,
kawa_string = lexical_kawa_string,
gambitc_literal = lexical_gambitc_literal,
gambitc_string = lexical_gambitc_string,
guile_literal = lexical_guile_literal,
guile_string = lexical_guile_string,
clojure_literal = lexical_clojure_literal,
clojure_string = lexical_clojure_string,
erlang_literal = lexical_erlang_literal,
erlang_string = lexical_erlang_string,
elm_literal = lexical_elm_literal,
elm_string = lexical_elm_string,
scala_literal = lexical_scala_literal,
scala_string = lexical_scala_string,
elixir_literal = lexical_elixir_literal,
elixir_string = lexical_elixir_string,
fortran_literal = lexical_fortran_literal,
fortran_string = lexical_fortran_string,
d_literal = lexical_d_literal,
d_string = lexical_d_string,
coffeescript_literal = lexical_coffeescript_literal,
coffeescript_string = lexical_coffeescript_string,
cobol_literal = lexical_cobol_literal,
cobol_string = lexical_cobol_string,
fsharp_literal = lexical_fsharp_literal,
fsharp_string = lexical_fsharp_string,
vb_literal = lexical_vb_literal,
vb_string = lexical_vb_string,
ocaml_literal = lexical_ocaml_literal,
ocaml_string = lexical_ocaml_string,
objectivec_literal = lexical_objectivec_literal,
objectivec_string = lexical_objectivec_string,
reasonml_literal = lexical_reasonml_literal,
reasonml_string = lexical_reasonml_string,
octave_literal = lexical_octave_literal,
octave_string = lexical_octave_string,
matlab_literal = lexical_matlab_literal,
matlab_string = lexical_matlab_string,
zig_literal = lexical_zig_literal,
zig_string = lexical_zig_string,
sage_literal = lexical_sage_literal,
sage_string = lexical_sage_string,
json = lexical_json,
toml = lexical_toml,
yaml = lexical_yaml,
xml = lexical_xml,
sqlite = lexical_sqlite,
postgresql = lexical_postgresql,
mysql = lexical_mysql,
mongodb = lexical_mongodb,
// HIDDEN DEFAULTS
permissive = lexical_permissive,
standard = lexical_standard,
ignore = lexical_ignore,
};
// OPTION TAG
// Tag for the option type in the tagged enum.
enum class option_tag: uint32_t {
some = ::lexical_some,
none = ::lexical_none,
};
// OPTION
// Option type for number format compilation.
template <typename T>
struct option {
option_tag tag;
T data;
// Safely convert from a C-style result to a C++ one.
// This is to prevent layout differences from causing UB.
template <typename ResultT>
static inline option from(ResultT c_opt)
{
// Ensure we likely have a similar layout.
// We're technically invoking UB, since the layout isn't
// guaranteed to be the same, but it would take a
// very malicious compiler to do so.
// Pretty much any approach would option in UB, even the platform-
// specific bindings, since the structs aren't **guaranteed**
// to be the same as what we're using.
static_assert(sizeof(ResultT) == sizeof(option), "Invalid sizes");
static_assert(std::is_standard_layout<ResultT>::value, "Not std layout");
static_assert(std::is_standard_layout<option>::value, "Not std layout");
option cc_opt;
std::memcpy(&cc_opt, &c_opt, sizeof(option));
return cc_opt;
}
inline bool is_some()
{
return tag == option_tag::some;
}
inline bool is_none()
{
return tag == option_tag::none;
}
inline T unwrap()
{
assert(is_some());
return std::move(data);
}
inline friend bool operator==(const option& lhs, const option& rhs)
{
if (lhs.tag != rhs.tag) {
return false;
} else if (lhs.tag == option_tag::some) {
return lhs.data == rhs.data;
} else {
return true;
}
}
inline friend bool operator!=(const option& lhs, const option& rhs)
{
return !(lhs == rhs);
}
};
// Compile float format value from specifications.
//
// * `digit_separator` - Character to separate digits.
// * `required_integer_digits` - If digits are required before the decimal point.
// * `required_fraction_digits` - If digits are required after the decimal point.
// * `required_exponent_digits` - If digits are required after the exponent character.
// * `no_positive_mantissa_sign` - If positive sign before the mantissa is not allowed.
// * `required_mantissa_sign` - If positive sign before the mantissa is required.
// * `no_exponent_notation` - If exponent notation is not allowed.
// * `no_positive_exponent_sign` - If positive sign before the exponent is not allowed.
// * `required_exponent_sign` - If sign before the exponent is required.
// * `no_exponent_without_fraction` - If exponent without fraction is not allowed.
// * `no_special` - If special (non-finite) values are not allowed.
// * `case_sensitive_special` - If special (non-finite) values are case-sensitive.
// * `no_integer_leading_zeros` - If leading zeros before an integer are not allowed.
// * `no_float_leading_zeros` - If leading zeros before a float are not allowed.
// * `integer_internal_digit_separator` - If digit separators are allowed between integer digits.
// * `fraction_internal_digit_separator` - If digit separators are allowed between fraction digits.
// * `exponent_internal_digit_separator` - If digit separators are allowed between exponent digits.
// * `integer_leading_digit_separator` - If a digit separator is allowed before any integer digits.
// * `fraction_leading_digit_separator` - If a digit separator is allowed before any fraction digits.
// * `exponent_leading_digit_separator` - If a digit separator is allowed before any exponent digits.
// * `integer_trailing_digit_separator` - If a digit separator is allowed after any integer digits.
// * `fraction_trailing_digit_separator` - If a digit separator is allowed after any fraction digits.
// * `exponent_trailing_digit_separator` - If a digit separator is allowed after any exponent digits.
// * `integer_consecutive_digit_separator` - If multiple consecutive integer digit separators are allowed.
// * `fraction_consecutive_digit_separator` - If multiple consecutive fraction digit separators are allowed.
// * `special_digit_separator` - If any digit separators are allowed in special (non-finite) values.
//
// Returns the value if it was able to compile the format,
// otherwise, returns None. Digit separators must not be
// in the character group `[A-Za-z0-9+.-]`, nor be equal to
// `get_exponent_default_char` or `get_exponent_backup_char`.
inline option<number_format> number_format_compile(
char digit_separator = '_',
bool required_integer_digits = false,
bool required_fraction_digits = false,
bool required_exponent_digits = false,
bool no_positive_mantissa_sign = false,
bool required_mantissa_sign = false,
bool no_exponent_notation = false,
bool no_positive_exponent_sign = false,
bool required_exponent_sign = false,
bool no_exponent_without_fraction = false,
bool no_special = false,
bool case_sensitive_special = false,
bool no_integer_leading_zeros = false,
bool no_float_leading_zeros = false,
bool integer_internal_digit_separator = false,
bool fraction_internal_digit_separator = false,
bool exponent_internal_digit_separator = false,
bool integer_leading_digit_separator = false,
bool fraction_leading_digit_separator = false,
bool exponent_leading_digit_separator = false,
bool integer_trailing_digit_separator = false,
bool fraction_trailing_digit_separator = false,
bool exponent_trailing_digit_separator = false,
bool integer_consecutive_digit_separator = false,
bool fraction_consecutive_digit_separator = false,
bool exponent_consecutive_digit_separator = false,
bool special_digit_separator = false
)
{
return option<number_format>::from(::lexical_number_format_compile(
digit_separator,
required_integer_digits,
required_fraction_digits,
required_exponent_digits,
no_positive_mantissa_sign,
required_mantissa_sign,
no_exponent_notation,
no_positive_exponent_sign,
required_exponent_sign,
no_exponent_without_fraction,
no_special,
case_sensitive_special,
no_integer_leading_zeros,
no_float_leading_zeros,
integer_internal_digit_separator,
fraction_internal_digit_separator,
exponent_internal_digit_separator,
integer_leading_digit_separator,
fraction_leading_digit_separator,
exponent_leading_digit_separator,
integer_trailing_digit_separator,
fraction_trailing_digit_separator,
exponent_trailing_digit_separator,
integer_consecutive_digit_separator,
fraction_consecutive_digit_separator,
exponent_consecutive_digit_separator,
special_digit_separator
));
}
// Compile permissive number format.
//
// The permissive number format does not require any control
// grammar, besides the presence of mantissa digits.
inline option<number_format> number_format_permissive()
{
return option<number_format>::from(::lexical_number_format_permissive());
}
// Compile standard number format.
//
// The standard number format is guaranteed to be identical
// to the format expected by Rust's string to number parsers.
inline option<number_format> number_format_standard()
{
return option<number_format>::from(::lexical_number_format_standard());
}
// Compile ignore number format.
//
// The ignore number format ignores all digit separators,
// and is permissive for all other control grammar, so
// implements a fast parser.
//
// * `digit_separator` - Character to separate digits.
inline option<number_format> number_format_ignore(uint8_t digit_separator)
{
return option<number_format>::from(::lexical_number_format_ignore(digit_separator));
}
// Get the flag bits from the compiled float format.
inline uint64_t number_format_flags(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_flags(f);
}
// Get the digit separator from the compiled float format.
inline uint8_t number_format_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_digit_separator(f);
}
// Get if digits are required before the decimal point.
inline bool number_format_required_integer_digits(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_required_integer_digits(f);
}
// Get if digits are required after the decimal point.
inline bool number_format_required_fraction_digits(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_required_fraction_digits(f);
}
// Get if digits are required after the exponent character.
inline bool number_format_required_exponent_digits(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_required_exponent_digits(f);
}
// Get if digits are required before or after the decimal point.
inline bool number_format_required_digits(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_required_digits(f);
}
// Get if positive sign before the mantissa is not allowed.
inline bool number_format_no_positive_mantissa_sign(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_no_positive_mantissa_sign(f);
}
// Get if positive sign before the mantissa is required.
inline bool number_format_required_mantissa_sign(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_required_mantissa_sign(f);
}
// Get if exponent notation is not allowed.
inline bool number_format_no_exponent_notation(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_no_exponent_notation(f);
}
// Get if positive sign before the exponent is not allowed.
inline bool number_format_no_positive_exponent_sign(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_no_positive_exponent_sign(f);
}
// Get if sign before the exponent is required.
inline bool number_format_required_exponent_sign(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_required_exponent_sign(f);
}
// Get if exponent without fraction is not allowed.
inline bool number_format_no_exponent_without_fraction(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_no_exponent_without_fraction(f);
}
// Get if special (non-finite) values are not allowed.
inline bool number_format_no_special(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_no_special(f);
}
// Get if special (non-finite) values are case-sensitive.
inline bool number_format_case_sensitive_special(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_case_sensitive_special(f);
}
// Get if leading zeros before an integer are not allowed.
inline bool number_format_no_integer_leading_zeros(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_no_integer_leading_zeros(f);
}
// Get if leading zeros before a float are not allowed.
inline bool number_format_no_float_leading_zeros(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_no_float_leading_zeros(f);
}
// Get if digit separators are allowed between integer digits.
inline bool number_format_integer_internal_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_integer_internal_digit_separator(f);
}
// Get if digit separators are allowed between fraction digits.
inline bool number_format_fraction_internal_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_fraction_internal_digit_separator(f);
}
// Get if digit separators are allowed between exponent digits.
inline bool number_format_exponent_internal_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_exponent_internal_digit_separator(f);
}
// Get if digit separators are allowed between digits.
inline bool number_format_internal_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_internal_digit_separator(f);
}
// Get if a digit separator is allowed before any integer digits.
inline bool number_format_integer_leading_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_integer_leading_digit_separator(f);
}
// Get if a digit separator is allowed before any fraction digits.
inline bool number_format_fraction_leading_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_fraction_leading_digit_separator(f);
}
// Get if a digit separator is allowed before any exponent digits.
inline bool number_format_exponent_leading_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_exponent_leading_digit_separator(f);
}
// Get if a digit separator is allowed before any digits.
inline bool number_format_leading_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_leading_digit_separator(f);
}
// Get if a digit separator is allowed after any integer digits.
inline bool number_format_integer_trailing_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_integer_trailing_digit_separator(f);
}
// Get if a digit separator is allowed after any fraction digits.
inline bool number_format_fraction_trailing_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_fraction_trailing_digit_separator(f);
}
// Get if a digit separator is allowed after any exponent digits.
inline bool number_format_exponent_trailing_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_exponent_trailing_digit_separator(f);
}
// Get if a digit separator is allowed after any digits.
inline bool number_format_trailing_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_trailing_digit_separator(f);
}
// Get if multiple consecutive integer digit separators are allowed.
inline bool number_format_integer_consecutive_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_integer_consecutive_digit_separator(f);
}
// Get if multiple consecutive fraction digit separators are allowed.
inline bool number_format_fraction_consecutive_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_fraction_consecutive_digit_separator(f);
}
// Get if multiple consecutive exponent digit separators are allowed.
inline bool number_format_exponent_consecutive_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_exponent_consecutive_digit_separator(f);
}
// Get if multiple consecutive digit separators are allowed.
inline bool number_format_consecutive_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_consecutive_digit_separator(f);
}
// Get if any digit separators are allowed in special (non-finite) values.
inline bool number_format_special_digit_separator(number_format format)
{
auto f = static_cast<uint64_t>(format);
return ::lexical_number_format_special_digit_separator(f);
}
#endif // HAVE_FORMAT
// C++ wrapper for get_*_string.
#define lexical_get_string(cb) \
uint8_t* ptr; \
size_t size; \
if (::cb(&ptr, &size) != 0) { \
throw std::runtime_error("Unexpected runtime error."); \
} \
return std::string(reinterpret_cast<char*>(ptr), size);
// C++ wrapper for set_*_string.
#define lexical_set_string(cb) \
auto* ptr = reinterpret_cast<uint8_t const*>(string.data()); \
auto size = string.length(); \
if (::cb(ptr, size) != 0) { \
throw std::runtime_error("Unexpected runtime error."); \
}
inline std::string get_nan_string()
{
lexical_get_string(lexical_get_nan_string)
}
inline void set_nan_string(const std::string& string)
{
lexical_set_string(lexical_set_nan_string)
}
inline std::string get_inf_string()
{
lexical_get_string(lexical_get_inf_string)
}
inline void set_inf_string(const std::string& string)
{
lexical_set_string(lexical_set_inf_string)
}
inline std::string get_infinity_string()
{
lexical_get_string(lexical_get_infinity_string)
}
inline void set_infinity_string(const std::string& string)
{
lexical_set_string(lexical_set_infinity_string)
}
// CONSTANTS
// ---------
static const size_t I8_FORMATTED_SIZE = ::LEXICAL_I8_FORMATTED_SIZE;
static const size_t I16_FORMATTED_SIZE = ::LEXICAL_I16_FORMATTED_SIZE;
static const size_t I32_FORMATTED_SIZE = ::LEXICAL_I32_FORMATTED_SIZE;
static const size_t I64_FORMATTED_SIZE = ::LEXICAL_I64_FORMATTED_SIZE;
static const size_t ISIZE_FORMATTED_SIZE = ::LEXICAL_ISIZE_FORMATTED_SIZE;
static const size_t U8_FORMATTED_SIZE = ::LEXICAL_U8_FORMATTED_SIZE;
static const size_t U16_FORMATTED_SIZE = ::LEXICAL_U16_FORMATTED_SIZE;
static const size_t U32_FORMATTED_SIZE = ::LEXICAL_U32_FORMATTED_SIZE;
static const size_t U64_FORMATTED_SIZE = ::LEXICAL_U64_FORMATTED_SIZE;
static const size_t USIZE_FORMATTED_SIZE = ::LEXICAL_USIZE_FORMATTED_SIZE;
static const size_t F32_FORMATTED_SIZE = ::LEXICAL_F32_FORMATTED_SIZE;
static const size_t F64_FORMATTED_SIZE = ::LEXICAL_F64_FORMATTED_SIZE;
static const size_t I8_FORMATTED_SIZE_DECIMAL = ::LEXICAL_I8_FORMATTED_SIZE_DECIMAL;
static const size_t I16_FORMATTED_SIZE_DECIMAL = ::LEXICAL_I16_FORMATTED_SIZE_DECIMAL;
static const size_t I32_FORMATTED_SIZE_DECIMAL = ::LEXICAL_I32_FORMATTED_SIZE_DECIMAL;
static const size_t I64_FORMATTED_SIZE_DECIMAL = ::LEXICAL_I64_FORMATTED_SIZE_DECIMAL;
static const size_t ISIZE_FORMATTED_SIZE_DECIMAL = ::LEXICAL_ISIZE_FORMATTED_SIZE_DECIMAL;
static const size_t U8_FORMATTED_SIZE_DECIMAL = ::LEXICAL_U8_FORMATTED_SIZE_DECIMAL;
static const size_t U16_FORMATTED_SIZE_DECIMAL = ::LEXICAL_U16_FORMATTED_SIZE_DECIMAL;
static const size_t U32_FORMATTED_SIZE_DECIMAL = ::LEXICAL_U32_FORMATTED_SIZE_DECIMAL;
static const size_t U64_FORMATTED_SIZE_DECIMAL = ::LEXICAL_U64_FORMATTED_SIZE_DECIMAL;
static const size_t USIZE_FORMATTED_SIZE_DECIMAL = ::LEXICAL_USIZE_FORMATTED_SIZE_DECIMAL;
static const size_t F32_FORMATTED_SIZE_DECIMAL = ::LEXICAL_F32_FORMATTED_SIZE_DECIMAL;
static const size_t F64_FORMATTED_SIZE_DECIMAL = ::LEXICAL_F64_FORMATTED_SIZE_DECIMAL;
static const size_t BUFFER_SIZE = ::LEXICAL_BUFFER_SIZE;
// Buffer size used internally for the to_string implementations.
// Avoid malloc whenever possible.
static constexpr size_t WRITE_SIZE = 256;
// TYPES
// -----
// ALIAS
using u8 = ::lexical_u8;
using u16 = ::lexical_u16;
using u32 = ::lexical_u32;
using u64 = ::lexical_u64;
using usize = ::lexical_usize;
using i8 = ::lexical_i8;
using i16 = ::lexical_i16;
using i32 = ::lexical_i32;
using i64 = ::lexical_i64;
using isize = ::lexical_isize;
using f32 = ::lexical_f32;
using f64 = ::lexical_f64;
// Internal typedef for the parsers.
// If we have C++17, we can use `std::string_view` by value.
// Otherwise, use a const ref to `std::string`.
#if __cplusplus >= 201703L
using string_type = std::string_view;
#else // !CPP_17
using string_type = const std::string&;
#endif // CPP_17
// ERROR
// Error code, indicating failure type.
enum class error_code: int32_t {
overflow = ::lexical_overflow,
underflow = ::lexical_underflow,
invalid_digit = ::lexical_invalid_digit,
empty = ::lexical_empty,
empty_mantissa = ::lexical_empty_mantissa,
empty_exponent = ::lexical_empty_exponent,
empty_integer = ::lexical_empty_integer,
empty_fraction = ::lexical_empty_fraction,
invalid_positive_mantissa_sign = ::lexical_invalid_positive_mantissa_sign,
missing_mantissa_sign = ::lexical_missing_mantissa_sign,
invalid_exponent = ::lexical_invalid_exponent,
invalid_positive_exponent_sign = ::lexical_invalid_positive_exponent_sign,
missing_exponent_sign = ::lexical_missing_exponent_sign,
exponent_without_fraction = ::lexical_exponent_without_fraction,
invalid_leading_zeros = ::lexical_invalid_leading_zeros,
};
// Determine if an error code matches the desired code.
#define lexical_is_error(type) \
inline bool is_##type() \
{ \
return code == error_code::type; \
}
// C-compatible error type.
struct error
{
error_code code;
size_t index;
lexical_is_error(overflow);
lexical_is_error(underflow);
lexical_is_error(invalid_digit);
lexical_is_error(empty);
lexical_is_error(empty_mantissa);
lexical_is_error(empty_exponent);
lexical_is_error(empty_integer);
lexical_is_error(empty_fraction);
lexical_is_error(invalid_positive_mantissa_sign);
lexical_is_error(missing_mantissa_sign);
lexical_is_error(invalid_exponent);
lexical_is_error(invalid_positive_exponent_sign);
lexical_is_error(missing_exponent_sign);
lexical_is_error(exponent_without_fraction);
lexical_is_error(invalid_leading_zeros);
inline friend bool operator==(const error& lhs, const error& rhs)
{
return std::make_tuple(lhs.code, lhs.index) == std::make_tuple(rhs.code, rhs.index);
}
inline friend bool operator!=(const error& lhs, const error& rhs)
{
return !(lhs == rhs);
}
};
// RESULT TAG
// Tag for the result type in the tagged enum.
enum class result_tag: uint32_t {
ok = ::lexical_ok,
err = ::lexical_err,
};
// COMPLETE UNIONS
// Union for the lexical result type.
template <typename T>
union result_union {
T value;
struct error error;
};
// COMPLETE RESULTS
// Result type for parser functions.
template <typename T>
struct result {
result_tag tag;
result_union<T> data;
// Safely convert from a C-style result to a C++ one.
// This is to prevent layout differences from causing UB.
template <typename ResultT>
static inline result from(ResultT c_res)
{
// Ensure we likely have a similar layout.
// We're technically invoking UB, since the layout isn't
// guaranteed to be the same, but it would take a
// very malicious compiler to do so.
// Pretty much any approach would result in UB, even the platform-
// specific bindings, since the structs aren't **guaranteed**
// to be the same as what we're using.
static_assert(sizeof(ResultT) == sizeof(result), "Invalid sizes");
static_assert(std::is_standard_layout<ResultT>::value, "Not std layout");
static_assert(std::is_standard_layout<result>::value, "Not std layout");
result cc_res;
std::memcpy(&cc_res, &c_res, sizeof(result));
return cc_res;
}
inline bool is_ok()
{
return tag == result_tag::ok;
}
inline bool is_err()
{
return tag == result_tag::err;
}
inline T ok()
{
assert(is_ok());
return std::move(data.value);
}
inline error err()
{
assert(is_err());
return std::move(data.error);
}
inline friend bool operator==(const result& lhs, const result& rhs)
{
if (lhs.tag != rhs.tag) {
return false;
} else if (lhs.tag == result_tag::ok) {
return lhs.data.value == rhs.data.value;
} else {
return lhs.data.error == rhs.data.error;
}
}
inline friend bool operator!=(const result& lhs, const result& rhs)
{
return !(lhs == rhs);
}
};
// PARTIAL RESULT TUPLES
// Result value type for the partial parsers.
template <typename T>
struct partial_result_tuple {
T x;
size_t y;
inline friend bool operator==(const partial_result_tuple& lhs, const partial_result_tuple& rhs)
{
return std::make_tuple(lhs.x, lhs.y) == std::make_tuple(rhs.x, rhs.y);
}
inline friend bool operator!=(const partial_result_tuple& lhs, const partial_result_tuple& rhs)
{
return !(lhs == rhs);
}
};
// PARTIAL RESULT UNIONS
// Union for the lexical partial result type.
template <typename T>
union partial_result_union {
partial_result_tuple<T> value;
struct error error;
};
// PARTIAL RESULTS
// Result type for partial parser functions.
template <typename T>
struct partial_result {
result_tag tag;
partial_result_union<T> data;
// Safely convert from a C-style result to a C++ one.
// This is to prevent layout differences from causing UB.
template <typename ResultT>
static inline partial_result from(ResultT c_res)
{
// Ensure we likely have a similar layout.
// We're technically invoking UB, since the layout isn't
// guaranteed to be the same, but it would take a
// very malicious compiler to do so.
// Pretty much any approach would result in UB, even the platform-