Merge pull request #60 from ToruNiina/string-view

support std::string_view
This commit is contained in:
Toru Niina
2019-04-27 18:33:59 +09:00
committed by GitHub
9 changed files with 156 additions and 12 deletions

View File

@@ -5,7 +5,7 @@ matrix:
- os: linux - os: linux
language: cpp language: cpp
compiler: gcc compiler: gcc
env: COMPILER="g++-5" env: COMPILER="g++-5" CXX_STANDARD=11
addons: addons:
apt: apt:
sources: sources:
@@ -16,7 +16,7 @@ matrix:
- os: linux - os: linux
language: cpp language: cpp
compiler: gcc compiler: gcc
env: COMPILER="g++-6" env: COMPILER="g++-6" CXX_STANDARD=11
addons: addons:
apt: apt:
sources: sources:
@@ -27,7 +27,7 @@ matrix:
- os: linux - os: linux
language: cpp language: cpp
compiler: gcc compiler: gcc
env: COMPILER="g++-7" env: COMPILER="g++-7" CXX_STANDARD=11
addons: addons:
apt: apt:
sources: sources:
@@ -38,7 +38,18 @@ matrix:
- os: linux - os: linux
language: cpp language: cpp
compiler: gcc 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: addons:
apt: apt:
sources: sources:
@@ -49,7 +60,7 @@ matrix:
- os: linux - os: linux
language: cpp language: cpp
compiler: clang compiler: clang
env: COMPILER="clang++-3.7" env: COMPILER="clang++-3.7" CXX_STANDARD=11
addons: addons:
apt: apt:
sources: sources:
@@ -61,7 +72,7 @@ matrix:
- os: linux - os: linux
language: cpp language: cpp
compiler: clang compiler: clang
env: COMPILER="clang++-4.0" env: COMPILER="clang++-4.0" CXX_STANDARD=11
addons: addons:
apt: apt:
sources: sources:
@@ -73,7 +84,7 @@ matrix:
- os: linux - os: linux
language: cpp language: cpp
compiler: clang compiler: clang
env: COMPILER="clang++-5.0" env: COMPILER="clang++-5.0" CXX_STANDARD=11
addons: addons:
apt: apt:
sources: sources:
@@ -85,7 +96,7 @@ matrix:
- os: linux - os: linux
language: cpp language: cpp
compiler: clang compiler: clang
env: COMPILER="clang++-6.0" env: COMPILER="clang++-6.0" CXX_STANDARD=11
addons: addons:
apt: apt:
sources: sources:
@@ -97,7 +108,7 @@ matrix:
- os: linux - os: linux
language: cpp language: cpp
compiler: clang compiler: clang
env: COMPILER="clang++-7" env: COMPILER="clang++-7" CXX_STANDARD=11
addons: addons:
apt: apt:
sources: sources:
@@ -109,7 +120,7 @@ matrix:
- os: linux - os: linux
language: cpp language: cpp
compiler: clang compiler: clang
env: COMPILER="clang++-8" env: COMPILER="clang++-8" CXX_STANDARD=11
addons: addons:
apt: apt:
sources: sources:
@@ -118,14 +129,38 @@ matrix:
packages: packages:
- clang-8 - clang-8
- libboost-all-dev - libboost-all-dev
- os: linux
language: cpp
compiler: clang
env: COMPILER="clang++-8" CXX_STANDARD=17
addons:
apt:
sources:
- ubuntu-toolchain-r-test
- llvm-toolchain-trusty-8
packages:
- clang-8
- g++-8
- libboost-all-dev
- os: osx - os: osx
language: cpp language: cpp
compiler: clang compiler: clang
env: CXX_STANDARD=11
script: 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 - mkdir build
- cd build - cd build
- git clone https://github.com/toml-lang/toml.git - 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 - make
- ctest --output-on-failure - ctest --output-on-failure

View File

@@ -4,7 +4,10 @@ project(toml11)
include(CheckCXXCompilerFlag) include(CheckCXXCompilerFlag)
if("${CMAKE_VERSION}" VERSION_GREATER 3.1) if("${CMAKE_VERSION}" VERSION_GREATER 3.1)
set(CMAKE_CXX_EXTENSIONS OFF)
if(NOT DEFINED CMAKE_CXX_STANDARD)
set(CMAKE_CXX_STANDARD 11) set(CMAKE_CXX_STANDARD 11)
endif()
set(CXX_STANDARD_REQUIRED ON) set(CXX_STANDARD_REQUIRED ON)
else() else()
# Manually check for C++11 compiler flag. # Manually check for C++11 compiler flag.

View File

@@ -188,6 +188,12 @@ See also [underlying types](#underlying-types).
NOTE: To enable to get a reference, conversions between Float and Integer are not supported. 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<std::string_view>(tab.at("key"));
```
### In the case of type error ### In the case of type error
If you pass an invalid type to `toml::get`, `toml::type_error` will be thrown. If you pass an invalid type to `toml::get`, `toml::type_error` will be thrown.

View File

@@ -12,6 +12,9 @@
#include <list> #include <list>
#include <deque> #include <deque>
#include <array> #include <array>
#if __cplusplus >= 201703L
#include <string_view>
#endif
BOOST_AUTO_TEST_CASE(test_get_exact) BOOST_AUTO_TEST_CASE(test_get_exact)
@@ -166,6 +169,17 @@ BOOST_AUTO_TEST_CASE(test_get_string_type)
toml::get<std::string>(v) += "bar"; toml::get<std::string>(v) += "bar";
BOOST_CHECK_EQUAL("foobar", toml::get<std::string>(v)); BOOST_CHECK_EQUAL("foobar", toml::get<std::string>(v));
} }
#if __cplusplus >= 201703L
{
toml::value v("foo", toml::string_t::basic);
BOOST_CHECK_EQUAL("foo", toml::get<std::string_view>(v));
}
{
toml::value v("foo", toml::string_t::literal);
BOOST_CHECK_EQUAL("foo", toml::get<std::string_view>(v));
}
#endif
} }
BOOST_AUTO_TEST_CASE(test_get_toml_array) BOOST_AUTO_TEST_CASE(test_get_toml_array)

View File

@@ -9,6 +9,11 @@
#include <map> #include <map>
#include <list> #include <list>
#if __cplusplus >= 201703L
#include <string_view>
#endif
BOOST_AUTO_TEST_CASE(test_value_boolean) BOOST_AUTO_TEST_CASE(test_value_boolean)
{ {
toml::value v1(true); toml::value v1(true);
@@ -355,6 +360,25 @@ BOOST_AUTO_TEST_CASE(test_value_string)
BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean>(), true); BOOST_CHECK_EQUAL(v1.cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Boolean>(), true); BOOST_CHECK_EQUAL(v2.cast<toml::value_t::Boolean>(), true);
BOOST_CHECK_EQUAL(v3.cast<toml::value_t::Boolean>(), true); BOOST_CHECK_EQUAL(v3.cast<toml::value_t::Boolean>(), 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<toml::String>());
BOOST_CHECK(v8.is<toml::String>());
BOOST_CHECK(v7.is_string());
BOOST_CHECK(v8.is_string());
BOOST_CHECK_EQUAL(v7.cast<toml::value_t::String>(), "foo");
BOOST_CHECK_EQUAL(v8.cast<toml::value_t::String>(), "foo");
#endif
} }
BOOST_AUTO_TEST_CASE(test_value_local_date) BOOST_AUTO_TEST_CASE(test_value_local_date)

View File

@@ -108,6 +108,18 @@ inline std::string get(value&& v)
return std::move(v.cast<value_t::String>().str); return std::move(v.cast<value_t::String>().str);
} }
// ============================================================================
// std::string_view
#if __cplusplus >= 201703L
template<typename T, typename std::enable_if<
std::is_same<T, std::string_view>::value, std::nullptr_t>::type = nullptr>
inline std::string_view get(const value& v)
{
return std::string_view(v.cast<value_t::String>().str);
}
#endif
// ============================================================================ // ============================================================================
// std::chrono::duration from toml::local_time. // std::chrono::duration from toml::local_time.

View File

@@ -4,6 +4,11 @@
#define TOML11_STRING_HPP #define TOML11_STRING_HPP
#include <string> #include <string>
#include <cstdint> #include <cstdint>
#if __cplusplus >= 201703L
#if __has_include(<string_view>)
#include <string_view>
#endif
#endif
namespace toml namespace toml
{ {
@@ -40,6 +45,17 @@ struct string
operator std::string const& () const& noexcept {return str;} operator std::string const& () const& noexcept {return str;}
operator std::string&& () && noexcept {return std::move(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; string_t kind;
std::string str; std::string str;
}; };

View File

@@ -7,6 +7,11 @@
#include "traits.hpp" #include "traits.hpp"
#include <vector> #include <vector>
#include <unordered_map> #include <unordered_map>
#if __cplusplus >= 201703L
#if __has_include(<string_view>)
#include <string_view>
#endif
#endif
namespace toml namespace toml
{ {
@@ -172,6 +177,9 @@ template<typename T>
struct is_container : conjunction< struct is_container : conjunction<
negation<is_map<T>>, negation<is_map<T>>,
negation<std::is_same<T, std::string>>, negation<std::is_same<T, std::string>>,
#if __cplusplus >= 201703L
negation<std::is_same<T, std::string_view>>,
#endif
has_iterator<T>, has_iterator<T>,
has_value_type<T> has_value_type<T>
>{}; >{};

View File

@@ -14,6 +14,9 @@
#include <unordered_map> #include <unordered_map>
#include <cassert> #include <cassert>
#include <cstdint> #include <cstdint>
#if __cplusplus >= 201703L
#include <string_view>
#endif
namespace toml namespace toml
{ {
@@ -293,6 +296,29 @@ class value
assigner(this->string_, toml::string(std::string(s), kind)); 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>(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>(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>(region_base{}))
{
assigner(this->string_, toml::string(s, kind));
}
#endif
// local date =========================================================== // local date ===========================================================
value(const local_date& ld) value(const local_date& ld)