From 62cf4373bde43e402b7d2213472bdbc7e4802bc9 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Mon, 22 Apr 2019 23:17:30 +0900 Subject: [PATCH 01/12] feat: conversion toml::string <-> string_view --- toml/string.hpp | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/toml/string.hpp b/toml/string.hpp index 89e6d39..abd58b5 100644 --- a/toml/string.hpp +++ b/toml/string.hpp @@ -4,6 +4,11 @@ #define TOML11_STRING_HPP #include #include +#if __cplusplus >= 201703L +#if __has_include() +#include +#endif +#endif namespace toml { @@ -40,6 +45,17 @@ struct string operator std::string const& () const& noexcept {return str;} operator std::string&& () && noexcept {return std::move(str);} +#if __cplusplus >= 201703L + explicit string(std::string_view s): kind(string_t::basic), str(s){} + string(std::string_view s, string_t k): kind(k), str(s){} + + string& operator=(std::string_view s) + {kind = string_t::basic; str = s; return *this;} + + explicit operator std::string_view() const noexcept + {return std::string_view(str);} +#endif + string_t kind; std::string str; }; From 0c7d2d07d4b16bcf751766ebac7ae9eb16875b20 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Tue, 23 Apr 2019 23:23:57 +0900 Subject: [PATCH 02/12] feat: do not consider string_view as a container it is a kind of string. --- toml/types.hpp | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/toml/types.hpp b/toml/types.hpp index db93320..b879176 100644 --- a/toml/types.hpp +++ b/toml/types.hpp @@ -7,6 +7,11 @@ #include "traits.hpp" #include #include +#if __cplusplus >= 201703L +#if __has_include() +#include +#endif +#endif namespace toml { @@ -172,6 +177,9 @@ template struct is_container : conjunction< negation>, negation>, +#if __cplusplus >= 201703L + negation>, +#endif has_iterator, has_value_type >{}; From d061c33a1695b637aca5f3fdc6b348bc314a1605 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Tue, 23 Apr 2019 23:24:23 +0900 Subject: [PATCH 03/12] feat: enable toml::get with std::string_view --- toml/get.hpp | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/toml/get.hpp b/toml/get.hpp index c0e47bb..344519f 100644 --- a/toml/get.hpp +++ b/toml/get.hpp @@ -108,6 +108,18 @@ inline std::string get(value&& v) return std::move(v.cast().str); } +// ============================================================================ +// std::string_view + +#if __cplusplus >= 201703L +template::value, std::nullptr_t>::type = nullptr> +inline std::string_view get(const value& v) +{ + return std::string_view(v.cast().str); +} +#endif + // ============================================================================ // std::chrono::duration from toml::local_time. From 37aa2739a51fe45e24d303fc38569bad0126f58c Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Tue, 23 Apr 2019 23:27:53 +0900 Subject: [PATCH 04/12] chore: add description about string_view to README --- README.md | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/README.md b/README.md index 0097f9b..2f7701b 100644 --- a/README.md +++ b/README.md @@ -187,6 +187,12 @@ See also [underlying types](#underlying-types). NOTE: To enable to get a reference, conversions between Float and Integer are not supported. +After C++17, you can use `std::string_view` to get a string from a `toml::value`. + +```cpp +const auto sv = toml::get(tab.at("key")); +``` + ### In the case of type error If you pass an invalid type to `toml::get`, `toml::type_error` will be thrown. From 2967cebfb317a7ee88ac8874b251989f4e85d6ed Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 26 Apr 2019 16:31:59 +0900 Subject: [PATCH 05/12] test: add test to get a toml::value as string_view --- tests/test_get.cpp | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/tests/test_get.cpp b/tests/test_get.cpp index b9c5941..833c479 100644 --- a/tests/test_get.cpp +++ b/tests/test_get.cpp @@ -12,6 +12,9 @@ #include #include #include +#if __cplusplus >= 201703L +#include +#endif BOOST_AUTO_TEST_CASE(test_get_exact) @@ -166,6 +169,17 @@ BOOST_AUTO_TEST_CASE(test_get_string_type) toml::get(v) += "bar"; BOOST_CHECK_EQUAL("foobar", toml::get(v)); } + +#if __cplusplus >= 201703L + { + toml::value v("foo", toml::string_t::basic); + BOOST_CHECK_EQUAL("foo", toml::get(v)); + } + { + toml::value v("foo", toml::string_t::literal); + BOOST_CHECK_EQUAL("foo", toml::get(v)); + } +#endif } BOOST_AUTO_TEST_CASE(test_get_toml_array) From 819351f5a4e691038451a7014e2a80d43301c607 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 26 Apr 2019 16:32:23 +0900 Subject: [PATCH 06/12] test: add test for init toml::value by string_view --- tests/test_value.cpp | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/tests/test_value.cpp b/tests/test_value.cpp index a0b38e4..fe874b9 100644 --- a/tests/test_value.cpp +++ b/tests/test_value.cpp @@ -9,6 +9,11 @@ #include #include +#if __cplusplus >= 201703L +#include +#endif + + BOOST_AUTO_TEST_CASE(test_value_boolean) { toml::value v1(true); @@ -355,6 +360,25 @@ BOOST_AUTO_TEST_CASE(test_value_string) BOOST_CHECK_EQUAL(v1.cast(), true); BOOST_CHECK_EQUAL(v2.cast(), true); BOOST_CHECK_EQUAL(v3.cast(), true); + +#if __cplusplus >= 201703L + std::string_view sv = "foo" + + toml::value v7(sv); + toml::value v8(sv, toml::string_t::literal); + + BOOST_CHECK_EQUAL(v7.type(), toml::value_t::String); + BOOST_CHECK_EQUAL(v8.type(), toml::value_t::String); + BOOST_CHECK(v7.is(toml::value_t::String)); + BOOST_CHECK(v8.is(toml::value_t::String)); + BOOST_CHECK(v7.is()); + BOOST_CHECK(v8.is()); + BOOST_CHECK(v7.is_string()); + BOOST_CHECK(v8.is_string()); + + BOOST_CHECK_EQUAL(v7.cast(), sv); + BOOST_CHECK_EQUAL(v8.cast(), sv); +#endif } BOOST_AUTO_TEST_CASE(test_value_local_date) From 01aa2ef5b2c61a430b6397e53a4eae323bfbeac7 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 26 Apr 2019 16:33:09 +0900 Subject: [PATCH 07/12] feat: add ctor to value to init with string_view --- toml/value.hpp | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/toml/value.hpp b/toml/value.hpp index ef4bd15..edfaab4 100644 --- a/toml/value.hpp +++ b/toml/value.hpp @@ -14,6 +14,9 @@ #include #include #include +#if __cplusplus >= 201703L +#include +#endif namespace toml { @@ -293,6 +296,29 @@ class value assigner(this->string_, toml::string(std::string(s), kind)); } +#if __cplusplus >= 201703L + value(std::string_view s) + : type_(value_t::String), + region_info_(std::make_shared(region_base{})) + { + assigner(this->string_, toml::string(s)); + } + value& operator=(std::string_view s) + { + this->cleanup(); + this->type_ = value_t::String; + this->region_info_ = std::make_shared(region_base{}); + assigner(this->string_, toml::string(s)); + return *this; + } + value(std::string_view s, string_t kind) + : type_(value_t::String), + region_info_(std::make_shared(region_base{})) + { + assigner(this->string_, toml::string(s, kind)); + } +#endif + // local date =========================================================== value(const local_date& ld) From 6383a93ce7b3ada3e17dd1c67970ba76a0c46cd3 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 26 Apr 2019 16:33:48 +0900 Subject: [PATCH 08/12] chore: check CXX_STANDARD exists or not --- CMakeLists.txt | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 05dbef1..f76b700 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -4,7 +4,10 @@ project(toml11) include(CheckCXXCompilerFlag) if("${CMAKE_VERSION}" VERSION_GREATER 3.1) - set(CMAKE_CXX_STANDARD 11) + set(CMAKE_CXX_EXTENSIONS OFF) + if(NOT DEFINED CMAKE_CXX_STANDARD) + set(CMAKE_CXX_STANDARD 11) + endif() set(CXX_STANDARD_REQUIRED ON) else() # Manually check for C++11 compiler flag. From 1ce54a9cf9f19577cf9e59eb64726e58e70ee126 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 26 Apr 2019 16:35:03 +0900 Subject: [PATCH 09/12] chore: add auto test with c++17 + latest compilers --- .travis.yml | 55 ++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 44 insertions(+), 11 deletions(-) diff --git a/.travis.yml b/.travis.yml index 840eabe..2cd7b9b 100644 --- a/.travis.yml +++ b/.travis.yml @@ -5,7 +5,7 @@ matrix: - os: linux language: cpp compiler: gcc - env: COMPILER="g++-5" + env: COMPILER="g++-5" CXX_STANDARD=11 addons: apt: sources: @@ -16,7 +16,7 @@ matrix: - os: linux language: cpp compiler: gcc - env: COMPILER="g++-6" + env: COMPILER="g++-6" CXX_STANDARD=11 addons: apt: sources: @@ -27,7 +27,7 @@ matrix: - os: linux language: cpp compiler: gcc - env: COMPILER="g++-7" + env: COMPILER="g++-7" CXX_STANDARD=11 addons: apt: sources: @@ -38,7 +38,18 @@ matrix: - os: linux language: cpp compiler: gcc - env: COMPILER="g++-8" + env: COMPILER="g++-8" CXX_STANDARD=11 + addons: + apt: + sources: + - ubuntu-toolchain-r-test + packages: + - g++-8 + - libboost-all-dev + - os: linux + language: cpp + compiler: gcc + env: COMPILER="g++-8" CXX_STANDARD=17 addons: apt: sources: @@ -49,7 +60,7 @@ matrix: - os: linux language: cpp compiler: clang - env: COMPILER="clang++-3.7" + env: COMPILER="clang++-3.7" CXX_STANDARD=11 addons: apt: sources: @@ -61,7 +72,7 @@ matrix: - os: linux language: cpp compiler: clang - env: COMPILER="clang++-4.0" + env: COMPILER="clang++-4.0" CXX_STANDARD=11 addons: apt: sources: @@ -73,7 +84,7 @@ matrix: - os: linux language: cpp compiler: clang - env: COMPILER="clang++-5.0" + env: COMPILER="clang++-5.0" CXX_STANDARD=11 addons: apt: sources: @@ -85,7 +96,7 @@ matrix: - os: linux language: cpp compiler: clang - env: COMPILER="clang++-6.0" + env: COMPILER="clang++-6.0" CXX_STANDARD=11 addons: apt: sources: @@ -97,7 +108,7 @@ matrix: - os: linux language: cpp compiler: clang - env: COMPILER="clang++-7" + env: COMPILER="clang++-7" CXX_STANDARD=11 addons: apt: sources: @@ -109,7 +120,19 @@ matrix: - os: linux language: cpp compiler: clang - env: COMPILER="clang++-8" + env: COMPILER="clang++-8" CXX_STANDARD=11 + addons: + apt: + sources: + - ubuntu-toolchain-r-test + - llvm-toolchain-trusty-8 + packages: + - clang-8 + - libboost-all-dev + - os: linux + language: cpp + compiler: clang + env: COMPILER="clang++-8" CXX_STANDARD=17 addons: apt: sources: @@ -123,9 +146,19 @@ matrix: compiler: clang script: +- | + if [[ "${TRAVIS_OS_NAME}" == "linux" ]]; then + mkdir -p cmake + travis_retry wget "https://cmake.org/files/v3.11/cmake-3.11.2-Linux-x86_64.tar.gz" + tar xf cmake-3.11.2-Linux-x86_64.tar.gz -C cmake --strip-components=1 + export PATH=${TRAVIS_BUILD_DIR}/cmake/bin:${PATH} + else + brew upgrade cmake + fi +- cmake --version - mkdir build - cd build - git clone https://github.com/toml-lang/toml.git -- cmake -DCMAKE_CXX_COMPILER=$COMPILER .. +- cmake -DCMAKE_CXX_COMPILER=$COMPILER -DCMAKE_CXX_STANDARD=$CXX_STANDARD .. - make - ctest --output-on-failure From f3bdf083fe4d59538ddfcf7de926f4e5797172be Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 26 Apr 2019 16:51:23 +0900 Subject: [PATCH 10/12] fix: fix typo in test code for string_view --- tests/test_value.cpp | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/tests/test_value.cpp b/tests/test_value.cpp index fe874b9..2faaa99 100644 --- a/tests/test_value.cpp +++ b/tests/test_value.cpp @@ -362,7 +362,7 @@ BOOST_AUTO_TEST_CASE(test_value_string) BOOST_CHECK_EQUAL(v3.cast(), true); #if __cplusplus >= 201703L - std::string_view sv = "foo" + std::string_view sv = "foo"; toml::value v7(sv); toml::value v8(sv, toml::string_t::literal); @@ -376,8 +376,8 @@ BOOST_AUTO_TEST_CASE(test_value_string) BOOST_CHECK(v7.is_string()); BOOST_CHECK(v8.is_string()); - BOOST_CHECK_EQUAL(v7.cast(), sv); - BOOST_CHECK_EQUAL(v8.cast(), sv); + BOOST_CHECK_EQUAL(v7.cast(), "foo"); + BOOST_CHECK_EQUAL(v8.cast(), "foo"); #endif } From ebaa5dfb517d68b3c70da5d9ef87d6993f887a9f Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 26 Apr 2019 21:10:29 +0900 Subject: [PATCH 11/12] chore: fix build settings for OS X on Travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index 2cd7b9b..ea889e0 100644 --- a/.travis.yml +++ b/.travis.yml @@ -144,6 +144,7 @@ matrix: - os: osx language: cpp compiler: clang + env: CXX_STANDARD=11 script: - | From 80ea736b3f2686ea3c2174e2b85096d2e00abd7a Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sat, 27 Apr 2019 14:46:40 +0900 Subject: [PATCH 12/12] ci: try to update standard library on travis --- .travis.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.travis.yml b/.travis.yml index ea889e0..08de4ac 100644 --- a/.travis.yml +++ b/.travis.yml @@ -140,6 +140,7 @@ matrix: - llvm-toolchain-trusty-8 packages: - clang-8 + - g++-8 - libboost-all-dev - os: osx language: cpp