Skip to content

Commit fa73cde

Browse files
committed
Use from_chars when supported in GCC and VS
1 parent 25c5766 commit fa73cde

File tree

10 files changed

+34
-28
lines changed

10 files changed

+34
-28
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
master
2+
-----
3+
4+
- Use `std::from_chars` for chars to double conversion
5+
when supported in GCC and VC.
6+
17
0.168.7
28
-------
39

include/jsoncons/basic_json.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4030,7 +4030,7 @@ namespace jsoncons {
40304030
case json_storage_kind::short_string_value:
40314031
case json_storage_kind::long_string_value:
40324032
{
4033-
jsoncons::detail::to_double_t to_double;
4033+
jsoncons::detail::chars_to to_double;
40344034
// to_double() throws std::invalid_argument if conversion fails
40354035
return to_double(as_cstring(), as_string_view().length());
40364036
}

include/jsoncons/detail/parse_number.hpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -912,7 +912,7 @@ base16_to_integer(const CharT* s, std::size_t length, T& n)
912912

913913
#if defined(JSONCONS_HAS_STD_FROM_CHARS)
914914

915-
class to_double_t
915+
class chars_to
916916
{
917917
public:
918918

@@ -929,7 +929,7 @@ class to_double_t
929929
const auto res = std::from_chars(s, s+len, val);
930930
if (res.ec != std::errc())
931931
{
932-
JSONCONS_THROW(json_runtime_error<std::invalid_argument>("Convert string to double failed"));
932+
JSONCONS_THROW(json_runtime_error<std::invalid_argument>("Convert chars to double failed"));
933933
}
934934
return val;
935935
}
@@ -948,33 +948,33 @@ class to_double_t
948948
const auto res = std::from_chars(input.data(), input.data() + len, val);
949949
if (res.ec != std::errc())
950950
{
951-
JSONCONS_THROW(json_runtime_error<std::invalid_argument>("Convert string to double failed"));
951+
JSONCONS_THROW(json_runtime_error<std::invalid_argument>("Convert chars to double failed"));
952952
}
953953
return val;
954954
}
955955
};
956956
#elif defined(JSONCONS_HAS_MSC_STRTOD_L)
957957

958-
class to_double_t
958+
class chars_to
959959
{
960960
private:
961961
_locale_t locale_;
962962
public:
963-
to_double_t()
963+
chars_to()
964964
{
965965
locale_ = _create_locale(LC_NUMERIC, "C");
966966
}
967-
~to_double_t() noexcept
967+
~chars_to() noexcept
968968
{
969969
_free_locale(locale_);
970970
}
971971

972-
to_double_t(const to_double_t&)
972+
chars_to(const chars_to&)
973973
{
974974
locale_ = _create_locale(LC_NUMERIC, "C");
975975
}
976976

977-
to_double_t& operator=(const to_double_t&)
977+
chars_to& operator=(const chars_to&)
978978
{
979979
// Don't assign locale
980980
return *this;
@@ -1014,26 +1014,26 @@ class to_double_t
10141014

10151015
#elif defined(JSONCONS_HAS_STRTOLD_L)
10161016

1017-
class to_double_t
1017+
class chars_to
10181018
{
10191019
private:
10201020
locale_t locale_;
10211021
public:
1022-
to_double_t()
1022+
chars_to()
10231023
{
10241024
locale_ = newlocale(LC_ALL_MASK, "C", (locale_t) 0);
10251025
}
1026-
~to_double_t() noexcept
1026+
~chars_to() noexcept
10271027
{
10281028
freelocale(locale_);
10291029
}
10301030

1031-
to_double_t(const to_double_t&)
1031+
chars_to(const chars_to&)
10321032
{
10331033
locale_ = newlocale(LC_ALL_MASK, "C", (locale_t) 0);
10341034
}
10351035

1036-
to_double_t& operator=(const to_double_t&)
1036+
chars_to& operator=(const chars_to&)
10371037
{
10381038
return *this;
10391039
}
@@ -1071,13 +1071,13 @@ class to_double_t
10711071
};
10721072

10731073
#else
1074-
class to_double_t
1074+
class chars_to
10751075
{
10761076
private:
10771077
std::vector<char> buffer_;
10781078
char decimal_point_;
10791079
public:
1080-
to_double_t()
1080+
chars_to()
10811081
: buffer_()
10821082
{
10831083
struct lconv * lc = localeconv();
@@ -1092,8 +1092,8 @@ class to_double_t
10921092
buffer_.reserve(100);
10931093
}
10941094

1095-
to_double_t(const to_double_t&) = default;
1096-
to_double_t& operator=(const to_double_t&) = default;
1095+
chars_to(const chars_to&) = default;
1096+
chars_to& operator=(const chars_to&) = default;
10971097

10981098
char get_decimal_point() const
10991099
{

include/jsoncons/detail/write_number.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ namespace detail {
294294
return true;
295295
}
296296

297-
jsoncons::detail::to_double_t to_double_;
297+
jsoncons::detail::chars_to to_double_;
298298

299299
char buffer[100];
300300
int precision = std::numeric_limits<double>::digits10;
@@ -327,7 +327,7 @@ namespace detail {
327327
return true;
328328
}
329329

330-
jsoncons::detail::to_double_t to_double_;
330+
jsoncons::detail::chars_to to_double_;
331331

332332
char buffer[100];
333333
int precision = std::numeric_limits<double>::digits10;
@@ -394,7 +394,7 @@ namespace detail {
394394
return true;
395395
}
396396

397-
jsoncons::detail::to_double_t to_double_;
397+
jsoncons::detail::chars_to to_double_;
398398

399399
char buffer[100];
400400
int precision = std::numeric_limits<double>::digits10;
@@ -463,7 +463,7 @@ namespace detail {
463463
class write_double
464464
{
465465
private:
466-
to_double_t to_double_;
466+
chars_to to_double_;
467467
float_chars_format float_format_;
468468
int precision_;
469469
char decimal_point_;

include/jsoncons/json_parser.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ class basic_json_parser : public ser_context
156156
bool done_;
157157

158158
std::basic_string<char_type,std::char_traits<char_type>,char_allocator_type> string_buffer_;
159-
jsoncons::detail::to_double_t to_double_;
159+
jsoncons::detail::chars_to to_double_;
160160

161161
std::vector<json_parse_state,parse_state_allocator_type> state_stack_;
162162
std::vector<std::pair<string_view_type,double>> string_double_map_;

include/jsoncons/staj_cursor.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -404,7 +404,7 @@ class basic_staj_event
404404
case staj_event_type::key:
405405
case staj_event_type::string_value:
406406
{
407-
jsoncons::detail::to_double_t f;
407+
jsoncons::detail::chars_to f;
408408
return f(value_.string_data_, length_);
409409
}
410410
case staj_event_type::double_value:

include/jsoncons_ext/csv/csv_parser.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -520,7 +520,7 @@ class basic_csv_parser : public ser_context
520520
std::size_t column_index_;
521521
std::size_t level_;
522522
std::size_t offset_;
523-
jsoncons::detail::to_double_t to_double_;
523+
jsoncons::detail::chars_to to_double_;
524524
const CharT* begin_input_;
525525
const CharT* input_end_;
526526
const CharT* input_ptr_;

include/jsoncons_ext/jmespath/jmespath.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1829,7 +1829,7 @@ namespace jmespath {
18291829
{
18301830
return *resources.create_json(sval);
18311831
}
1832-
jsoncons::detail::to_double_t to_double;
1832+
jsoncons::detail::chars_to to_double;
18331833
try
18341834
{
18351835
auto s = arg0.as_string();

include/jsoncons_ext/jsonpath/expression.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1527,7 +1527,7 @@ namespace detail {
15271527
{
15281528
return value_type(sn);
15291529
}
1530-
jsoncons::detail::to_double_t to_double;
1530+
jsoncons::detail::chars_to to_double;
15311531
try
15321532
{
15331533
auto s = arg0.as_string();

test/src/string_to_double_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ TEST_CASE("test_string_to_double")
3636

3737
TEST_CASE("test_exponent")
3838
{
39-
jsoncons::detail::to_double_t reader;
39+
jsoncons::detail::chars_to reader;
4040
const char* begin = "1.15507e-173";
4141
char* endptr = nullptr;
4242
const double value1 = 1.15507e-173;

0 commit comments

Comments
 (0)