diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 5a9a0b0..04ab80b 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -58,6 +58,7 @@ jobs: - {compiler: '5.0', standard: '20'} - {compiler: '6.0', standard: '20'} - {compiler: '7', standard: '20'} + - {compiler: '8', standard: '20'} steps: - name: Checkout uses: actions/checkout@v2 diff --git a/tests/test_comments.cpp b/tests/test_comments.cpp index 7438cae..635783a 100644 --- a/tests/test_comments.cpp +++ b/tests/test_comments.cpp @@ -190,7 +190,7 @@ BOOST_AUTO_TEST_CASE(test_discard_comment) )"; std::istringstream iss(file); - const auto v = toml::parse(iss); + const auto v = toml::parse(iss); const auto& a = toml::find(v, "a"); const auto& b = toml::find(v, "b"); diff --git a/tests/test_find.cpp b/tests/test_find.cpp index 5582da8..238390c 100644 --- a/tests/test_find.cpp +++ b/tests/test_find.cpp @@ -19,7 +19,7 @@ #include using test_value_types = std::tuple< - toml::value, + toml::basic_value, toml::basic_value, toml::basic_value, toml::basic_value diff --git a/tests/test_find_or.cpp b/tests/test_find_or.cpp index 434f943..72e5450 100644 --- a/tests/test_find_or.cpp +++ b/tests/test_find_or.cpp @@ -17,7 +17,7 @@ #endif using test_value_types = std::tuple< - toml::value, + toml::basic_value, toml::basic_value, toml::basic_value, toml::basic_value diff --git a/tests/test_get.cpp b/tests/test_get.cpp index 0749fd4..9a795f2 100644 --- a/tests/test_get.cpp +++ b/tests/test_get.cpp @@ -17,7 +17,7 @@ #endif using test_value_types = std::tuple< - toml::value, + toml::basic_value, toml::basic_value, toml::basic_value, toml::basic_value diff --git a/tests/test_get_or.cpp b/tests/test_get_or.cpp index 82e4839..a709ae7 100644 --- a/tests/test_get_or.cpp +++ b/tests/test_get_or.cpp @@ -17,7 +17,7 @@ #endif using test_value_types = std::tuple< - toml::value, + toml::basic_value, toml::basic_value, toml::basic_value, toml::basic_value diff --git a/tests/test_parse_array.cpp b/tests/test_parse_array.cpp index 2723b89..a75a6f1 100644 --- a/tests/test_parse_array.cpp +++ b/tests/test_parse_array.cpp @@ -71,62 +71,153 @@ BOOST_AUTO_TEST_CASE(test_oneline_array_value) BOOST_AUTO_TEST_CASE(test_multiline_array) { - TOML11_TEST_PARSE_EQUAL(parse_array, "[\n#comment\n]", array()); + TOML11_TEST_PARSE_EQUAL(parse_array>, "[\n#comment\n]", typename basic_value< discard_comments>::array_type()); + TOML11_TEST_PARSE_EQUAL(parse_array>, "[\n#comment\n]", typename basic_value::array_type()); + { - array a(5); - a[0] = toml::value(3); a[1] = toml::value(1); a[2] = toml::value(4); - a[3] = toml::value(1); a[4] = toml::value(5); - TOML11_TEST_PARSE_EQUAL(parse_array, "[3,\n1,\n4,\n1,\n5]", a); + typename basic_value::array_type a(5); + a[0] = basic_value(3); + a[1] = basic_value(1); + a[2] = basic_value(4); + a[3] = basic_value(1); + a[4] = basic_value(5); + TOML11_TEST_PARSE_EQUAL(parse_array>, "[3,\n1,\n4,\n1,\n5]", a); } { - array a(3); - a[0] = toml::value("foo"); a[1] = toml::value("bar"); - a[2] = toml::value("baz"); - TOML11_TEST_PARSE_EQUAL(parse_array, "[\"foo\",\n\"bar\",\n\"baz\"]", a); + typename basic_value::array_type a(5); + a[0] = basic_value(3); + a[1] = basic_value(1); + a[2] = basic_value(4); + a[3] = basic_value(1); + a[4] = basic_value(5); + TOML11_TEST_PARSE_EQUAL(parse_array>, "[3,\n1,\n4,\n1,\n5]", a); } { - array a(5); - a[0] = toml::value(3); a[1] = toml::value(1); a[2] = toml::value(4); - a[3] = toml::value(1); a[4] = toml::value(5); - TOML11_TEST_PARSE_EQUAL(parse_array, "[3,#comment\n1,#comment\n4,#comment\n1,#comment\n5]", a); + typename basic_value::array_type a(5); + a[0] = basic_value(3); + a[1] = basic_value(1); + a[2] = basic_value(4); + a[3] = basic_value(1); + a[4] = basic_value(5); + TOML11_TEST_PARSE_EQUAL(parse_array>, "[3,#comment\n1,#comment\n4,#comment\n1,#comment\n5 #comment\n]", a); } { - array a(3); - a[0] = toml::value("foo"); a[1] = toml::value("b#r"); - a[2] = toml::value("b#z"); - TOML11_TEST_PARSE_EQUAL(parse_array, "[\"foo\",#comment\n\"b#r\",#comment\n\"b#z\"#comment\n]", a); + typename basic_value::array_type a(5); + a[0] = basic_value(3, {"comment"}); + a[1] = basic_value(1, {"comment"}); + a[2] = basic_value(4, {"comment"}); + a[3] = basic_value(1, {"comment"}); + a[4] = basic_value(5, {"comment"}); + TOML11_TEST_PARSE_EQUAL(parse_array>, "[3,#comment\n1,#comment\n4,#comment\n1,#comment\n5 #comment\n]", a); + } + + + { + typename basic_value::array_type a(3); + a[0] = basic_value("foo"); + a[1] = basic_value("bar"); + a[2] = basic_value("baz"); + TOML11_TEST_PARSE_EQUAL(parse_array>, "[\"foo\",\n\"bar\",\n\"baz\"]", a); + } + { + typename basic_value::array_type a(3); + a[0] = basic_value("foo"); + a[1] = basic_value("bar"); + a[2] = basic_value("baz"); + TOML11_TEST_PARSE_EQUAL(parse_array>, "[\"foo\",\n\"bar\",\n\"baz\"]", a); + } + + { + typename basic_value::array_type a(3); + a[0] = basic_value("foo"); + a[1] = basic_value("b#r"); + a[2] = basic_value("b#z"); + TOML11_TEST_PARSE_EQUAL(parse_array>, "[\"foo\",#comment\n\"b#r\",#comment\n\"b#z\"#comment\n]", a); + } + { + typename basic_value::array_type a(3); + a[0] = basic_value("foo", {"comment"}); + a[1] = basic_value("b#r", {"comment"}); + a[2] = basic_value("b#z", {"comment"}); + TOML11_TEST_PARSE_EQUAL(parse_array>, "[\"foo\",#comment\n\"b#r\",#comment\n\"b#z\"#comment\n]", a); } } BOOST_AUTO_TEST_CASE(test_multiline_array_value) { - TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "[\n#comment\n]", toml::value(array())); + TOML11_TEST_PARSE_EQUAL_VALUE(parse_value>, "[\n#comment\n]", basic_value< discard_comments>(typename basic_value< discard_comments>::array_type())); + TOML11_TEST_PARSE_EQUAL_VALUE(parse_value>, "[\n#comment\n]", basic_value(typename basic_value::array_type())); + { - array a(5); - a[0] = toml::value(3); a[1] = toml::value(1); a[2] = toml::value(4); - a[3] = toml::value(1); a[4] = toml::value(5); - TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "[3,\n1,\n4,\n1,\n5]", toml::value(a)); + typename basic_value::array_type a(5); + a[0] = basic_value(3); + a[1] = basic_value(1); + a[2] = basic_value(4); + a[3] = basic_value(1); + a[4] = basic_value(5); + TOML11_TEST_PARSE_EQUAL_VALUE(parse_value>, "[3,\n1,\n4,\n1,\n5]", basic_value(a)); } { - array a(3); - a[0] = toml::value("foo"); a[1] = toml::value("bar"); - a[2] = toml::value("baz"); - TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "[\"foo\",\n\"bar\",\n\"baz\"]", toml::value(a)); + typename basic_value::array_type a(5); + a[0] = basic_value(3); + a[1] = basic_value(1); + a[2] = basic_value(4); + a[3] = basic_value(1); + a[4] = basic_value(5); + TOML11_TEST_PARSE_EQUAL_VALUE(parse_value>, "[3,\n1,\n4,\n1,\n5]", basic_value(a)); } { - array a(5); - a[0] = toml::value(3); a[1] = toml::value(1); a[2] = toml::value(4); - a[3] = toml::value(1); a[4] = toml::value(5); - TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "[3,#comment\n1,#comment\n4,#comment\n1,#comment\n5]", toml::value(a)); + typename basic_value::array_type a(5); + a[0] = basic_value(3); + a[1] = basic_value(1); + a[2] = basic_value(4); + a[3] = basic_value(1); + a[4] = basic_value(5); + TOML11_TEST_PARSE_EQUAL_VALUE(parse_value>, "[3,#comment\n1,#comment\n4,#comment\n1,#comment\n5 #comment\n]", basic_value(a)); } { - array a(3); - a[0] = toml::value("foo"); a[1] = toml::value("b#r"); - a[2] = toml::value("b#z"); - TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, "[\"foo\",#comment\n\"b#r\",#comment\n\"b#z\"#comment\n]", toml::value(a)); + typename basic_value::array_type a(5); + a[0] = basic_value(3, {"comment"}); + a[1] = basic_value(1, {"comment"}); + a[2] = basic_value(4, {"comment"}); + a[3] = basic_value(1, {"comment"}); + a[4] = basic_value(5, {"comment"}); + TOML11_TEST_PARSE_EQUAL_VALUE(parse_value>, "[3,#comment\n1,#comment\n4,#comment\n1,#comment\n5 #comment\n]", basic_value(a)); } + + + { + typename basic_value::array_type a(3); + a[0] = basic_value("foo"); + a[1] = basic_value("bar"); + a[2] = basic_value("baz"); + TOML11_TEST_PARSE_EQUAL_VALUE(parse_value>, "[\"foo\",\n\"bar\",\n\"baz\"]", basic_value(a)); + } + { + typename basic_value::array_type a(3); + a[0] = basic_value("foo"); + a[1] = basic_value("bar"); + a[2] = basic_value("baz"); + TOML11_TEST_PARSE_EQUAL_VALUE(parse_value>, "[\"foo\",\n\"bar\",\n\"baz\"]", basic_value(a)); + } + + { + typename basic_value::array_type a(3); + a[0] = basic_value("foo"); + a[1] = basic_value("b#r"); + a[2] = basic_value("b#z"); + TOML11_TEST_PARSE_EQUAL_VALUE(parse_value>, "[\"foo\",#comment\n\"b#r\",#comment\n\"b#z\"#comment\n]", basic_value(a)); + } + { + typename basic_value::array_type a(3); + a[0] = basic_value("foo", {"comment"}); + a[1] = basic_value("b#r", {"comment"}); + a[2] = basic_value("b#z", {"comment"}); + TOML11_TEST_PARSE_EQUAL_VALUE(parse_value>, "[\"foo\",#comment\n\"b#r\",#comment\n\"b#z\"#comment\n]", basic_value(a)); + } + } BOOST_AUTO_TEST_CASE(test_heterogeneous_array) @@ -176,14 +267,27 @@ BOOST_AUTO_TEST_CASE(test_heterogeneous_array) BOOST_AUTO_TEST_CASE(test_comments_after_comma) { { - array a; - a.push_back("foo"); - a.push_back("bar"); - a.push_back("baz"); - TOML11_TEST_PARSE_EQUAL_VALUE(parse_value, + typename basic_value::array_type a(3); + a[0] = basic_value("foo"); + a[1] = basic_value("bar"); + a[2] = basic_value("baz"); + TOML11_TEST_PARSE_EQUAL_VALUE(parse_value>, "[ \"foo\" # comment\n" ", \"bar\" # comment\n" ", \"baz\" # comment\n" - "]", toml::value(a)); + "]", basic_value(a)); } + + { + typename basic_value::array_type a(3); + a[0] = basic_value("foo", {" comment"}); + a[1] = basic_value("bar", {" comment"}); + a[2] = basic_value("baz", {" comment"}); + TOML11_TEST_PARSE_EQUAL_VALUE(parse_value>, + "[ \"foo\" # comment\n" + ", \"bar\" # comment\n" + ", \"baz\" # comment\n" + "]", basic_value(a)); + } + } diff --git a/tests/test_serialize_file.cpp b/tests/test_serialize_file.cpp index b5c408e..7e1d8e1 100644 --- a/tests/test_serialize_file.cpp +++ b/tests/test_serialize_file.cpp @@ -127,8 +127,8 @@ BOOST_AUTO_TEST_CASE(test_example_with_comment_nocomment) BOOST_TEST(!has_comment_inside(serialized)); } { - const auto data_nocomment = toml::parse("toml/tests/example.toml"); - auto serialized = toml::parse("tmp1_com_nocomment.toml"); + const auto data_nocomment = toml::parse("toml/tests/example.toml"); + auto serialized = toml::parse("tmp1_com_nocomment.toml"); { auto& owner = toml::find(serialized, "owner"); auto& bio = toml::find(owner, "bio"); @@ -182,8 +182,8 @@ BOOST_AUTO_TEST_CASE(test_example_with_comment_map_dq_nocomment) BOOST_TEST(!has_comment_inside(serialized)); } { - const auto data_nocomment = toml::parse("toml/tests/example.toml"); - auto serialized = toml::parse("tmp1_com_map_dq_nocomment.toml"); + const auto data_nocomment = toml::parse("toml/tests/example.toml"); + auto serialized = toml::parse("tmp1_com_map_dq_nocomment.toml"); { auto& owner = toml::find(serialized, "owner"); auto& bio = toml::find(owner, "bio"); diff --git a/toml/comments.hpp b/toml/comments.hpp index 92fc8e1..b9ce806 100644 --- a/toml/comments.hpp +++ b/toml/comments.hpp @@ -10,6 +10,12 @@ #include #include +#ifdef TOML11_PRESERVE_COMMENTS_BY_DEFAULT +# define TOML11_DEFAULT_COMMENT_STRATEGY ::toml::preserve_comments +#else +# define TOML11_DEFAULT_COMMENT_STRATEGY ::toml::discard_comments +#endif + // This file provides mainly two classes, `preserve_comments` and `discard_comments`. // Those two are a container that have the same interface as `std::vector` // but bahaves in the opposite way. `preserve_comments` is just the same as diff --git a/toml/literal.hpp b/toml/literal.hpp index 0824f83..1d338b7 100644 --- a/toml/literal.hpp +++ b/toml/literal.hpp @@ -12,11 +12,11 @@ inline namespace toml_literals { // implementation -inline ::toml::basic_value<::toml::discard_comments, std::unordered_map, std::vector> +inline ::toml::basic_value literal_internal_impl(::toml::detail::location loc) { using value_type = ::toml::basic_value< - ::toml::discard_comments, std::unordered_map, std::vector>; + TOML11_DEFAULT_COMMENT_STRATEGY, std::unordered_map, std::vector>; // if there are some comments or empty lines, skip them. using skip_line = ::toml::detail::repeat, @@ -81,7 +81,7 @@ literal_internal_impl(::toml::detail::location loc) } -inline ::toml::basic_value<::toml::discard_comments, std::unordered_map, std::vector> +inline ::toml::basic_value operator"" _toml(const char* str, std::size_t len) { ::toml::detail::location loc( @@ -95,7 +95,7 @@ operator"" _toml(const char* str, std::size_t len) #if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L // value of u8"" literal has been changed from char to char8_t and char8_t is // NOT compatible to char -inline ::toml::basic_value<::toml::discard_comments, std::unordered_map, std::vector> +inline ::toml::basic_value operator"" _toml(const char8_t* str, std::size_t len) { ::toml::detail::location loc( diff --git a/toml/parser.hpp b/toml/parser.hpp index c3df644..231df80 100644 --- a/toml/parser.hpp +++ b/toml/parser.hpp @@ -2076,7 +2076,7 @@ result parse_toml_file(location& loc) } // detail -template class Table = std::unordered_map, template class Array = std::vector> basic_value @@ -2127,7 +2127,7 @@ parse(std::istream& is, const std::string& fname = "unknown file") return data.unwrap(); } -template class Table = std::unordered_map, template class Array = std::vector> basic_value parse(const std::string& fname) @@ -2150,7 +2150,7 @@ basic_value parse(const std::string& fname) // // This function exactly matches to the invokation with c-string. // So this function is preferred than others and the ambiguity disappears. -template class Table = std::unordered_map, template class Array = std::vector> basic_value parse(const char* fname) @@ -2158,7 +2158,7 @@ basic_value parse(const char* fname) return parse(std::string(fname)); } -template class Table = std::unordered_map, template class Array = std::vector> basic_value parse(const std::filesystem::path& fpath) diff --git a/toml/value.hpp b/toml/value.hpp index e374c3b..f2e6c63 100644 --- a/toml/value.hpp +++ b/toml/value.hpp @@ -1739,7 +1739,8 @@ class basic_value }; // default toml::value and default array/table. -using value = basic_value; +// TOML11_DEFAULT_COMMENT_STRATEGY is defined in comments.hpp +using value = basic_value; using array = typename value::array_type; using table = typename value::table_type;