From 2b2a05148e76c73cb194214c4ec1aa863794d43f Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 8 Dec 2018 20:18:50 +0900 Subject: [PATCH] add from_string to utility --- tests/test_utility.cpp | 12 ++++++++++++ toml/utility.hpp | 11 +++++++++++ 2 files changed, 23 insertions(+) diff --git a/tests/test_utility.cpp b/tests/test_utility.cpp index dd08f2f..b879c54 100644 --- a/tests/test_utility.cpp +++ b/tests/test_utility.cpp @@ -68,3 +68,15 @@ BOOST_AUTO_TEST_CASE(test_concat_to_string) const std::string cat = toml::concat_to_string("foo", "bar", 42); BOOST_CHECK(cat == "foobar42"); } + +BOOST_AUTO_TEST_CASE(test_from_string) +{ + { + const std::string str("123"); + BOOST_CHECK_EQUAL(toml::from_string(str, 0), 123); + } + { + const std::string str("01"); + BOOST_CHECK_EQUAL(toml::from_string(str, 0), 1); + } +} diff --git a/toml/utility.hpp b/toml/utility.hpp index 2e7b0f0..26eff2d 100644 --- a/toml/utility.hpp +++ b/toml/utility.hpp @@ -62,5 +62,16 @@ std::string concat_to_string(Ts&& ... args) return detail::concat_to_string_impl(oss, std::forward(args) ...); } +template +T from_string(const std::string& str, U&& opt) +{ + T v(std::forward(opt)); + std::istringstream iss(str); + iss >> v; + return v; +} + + + }// toml #endif // TOML11_UTILITY