From da024510c5686bc556ae1deaad3b10a72c20a787 Mon Sep 17 00:00:00 2001 From: Mark Gillard Date: Sun, 7 Jun 2020 16:28:08 +0300 Subject: [PATCH] fixed parse failure when parsing an empty file Blank files are in fact totally valid TOML and should not cause a parse failure (fixes #35). --- include/toml++/toml_parser.hpp | 23 +++++++------ include/toml++/toml_utf8_streams.h | 28 ++++++++++++++++ include/toml++/toml_version.h | 2 +- meson.build | 2 +- toml.hpp | 54 +++++++++++++++++++++++------- 5 files changed, 85 insertions(+), 24 deletions(-) diff --git a/include/toml++/toml_parser.hpp b/include/toml++/toml_parser.hpp index 375216b..db7444d 100644 --- a/include/toml++/toml_parser.hpp +++ b/include/toml++/toml_parser.hpp @@ -2557,18 +2557,21 @@ namespace toml::impl { root.source_ = { prev_pos, prev_pos, reader.source_path() }; - cp = reader.read_next(); - - #if !TOML_EXCEPTIONS - if (reader.error()) + if (!reader.peek_eof()) { - err = std::move(reader.error()); - return; - } - #endif + cp = reader.read_next(); - if (cp) - parse_document(); + #if !TOML_EXCEPTIONS + if (reader.error()) + { + err = std::move(reader.error()); + return; + } + #endif + + if (cp) + parse_document(); + } update_region_ends(root); } diff --git a/include/toml++/toml_utf8_streams.h b/include/toml++/toml_utf8_streams.h index 8da3e14..3ebd15a 100644 --- a/include/toml++/toml_utf8_streams.h +++ b/include/toml++/toml_utf8_streams.h @@ -46,6 +46,12 @@ namespace toml::impl return position >= source.length(); } + [[nodiscard]] TOML_ALWAYS_INLINE + constexpr bool peek_eof() const noexcept + { + return eof(); + } + [[nodiscard]] TOML_ALWAYS_INLINE constexpr bool error() const noexcept { @@ -97,6 +103,13 @@ namespace toml::impl return source->eof(); } + [[nodiscard]] TOML_ALWAYS_INLINE + bool peek_eof() const + { + using stream_traits = typename std::remove_pointer_t::traits_type; + return eof() || source->peek() == stream_traits::eof(); + } + [[nodiscard]] TOML_ALWAYS_INLINE bool error() const noexcept { @@ -190,6 +203,9 @@ namespace toml::impl [[nodiscard]] virtual const utf8_codepoint* read_next() = 0; + [[nodiscard]] + virtual bool peek_eof() const = 0; + #if !TOML_EXCEPTIONS [[nodiscard]] @@ -321,6 +337,12 @@ namespace toml::impl TOML_UNREACHABLE; } + [[nodiscard]] + bool peek_eof() const override + { + return stream.peek_eof(); + } + #if !TOML_EXCEPTIONS [[nodiscard]] @@ -432,6 +454,12 @@ namespace toml::impl : head; } + [[nodiscard]] + bool peek_eof() const override + { + return reader.peek_eof(); + } + #if !TOML_EXCEPTIONS [[nodiscard]] diff --git a/include/toml++/toml_version.h b/include/toml++/toml_version.h index 026b946..af445a0 100644 --- a/include/toml++/toml_version.h +++ b/include/toml++/toml_version.h @@ -7,7 +7,7 @@ #define TOML_LIB_MAJOR 1 #define TOML_LIB_MINOR 3 -#define TOML_LIB_PATCH 0 +#define TOML_LIB_PATCH 1 #define TOML_LANG_MAJOR 1 #define TOML_LANG_MINOR 0 diff --git a/meson.build b/meson.build index 36e5c67..c3981b0 100644 --- a/meson.build +++ b/meson.build @@ -1,7 +1,7 @@ project( 'tomlplusplus', 'cpp', - version : '1.3.0', + version : '1.3.1', license : 'MIT', default_options : [ 'cpp_std=c++17', diff --git a/toml.hpp b/toml.hpp index 081c813..cdce5bd 100644 --- a/toml.hpp +++ b/toml.hpp @@ -1,6 +1,6 @@ //---------------------------------------------------------------------------------------------------------------------- // -// toml++ v1.3.0 +// toml++ v1.3.1 // https://github.com/marzer/tomlplusplus // SPDX-License-Identifier: MIT // @@ -347,7 +347,7 @@ #endif #define TOML_LIB_MAJOR 1 #define TOML_LIB_MINOR 3 -#define TOML_LIB_PATCH 0 +#define TOML_LIB_PATCH 1 #define TOML_LANG_MAJOR 1 #define TOML_LANG_MINOR 0 @@ -5170,6 +5170,12 @@ namespace toml::impl return position >= source.length(); } + [[nodiscard]] TOML_ALWAYS_INLINE + constexpr bool peek_eof() const noexcept + { + return eof(); + } + [[nodiscard]] TOML_ALWAYS_INLINE constexpr bool error() const noexcept { @@ -5221,6 +5227,13 @@ namespace toml::impl return source->eof(); } + [[nodiscard]] TOML_ALWAYS_INLINE + bool peek_eof() const + { + using stream_traits = typename std::remove_pointer_t::traits_type; + return eof() || source->peek() == stream_traits::eof(); + } + [[nodiscard]] TOML_ALWAYS_INLINE bool error() const noexcept { @@ -5313,6 +5326,8 @@ namespace toml::impl [[nodiscard]] virtual const utf8_codepoint* read_next() = 0; + [[nodiscard]] + virtual bool peek_eof() const = 0; #if !TOML_EXCEPTIONS @@ -5445,6 +5460,12 @@ namespace toml::impl TOML_UNREACHABLE; } + [[nodiscard]] + bool peek_eof() const override + { + return stream.peek_eof(); + } + #if !TOML_EXCEPTIONS [[nodiscard]] @@ -5556,6 +5577,12 @@ namespace toml::impl : head; } + [[nodiscard]] + bool peek_eof() const override + { + return reader.peek_eof(); + } + #if !TOML_EXCEPTIONS [[nodiscard]] @@ -9130,18 +9157,21 @@ namespace toml::impl { root.source_ = { prev_pos, prev_pos, reader.source_path() }; - cp = reader.read_next(); - - #if !TOML_EXCEPTIONS - if (reader.error()) + if (!reader.peek_eof()) { - err = std::move(reader.error()); - return; - } - #endif + cp = reader.read_next(); - if (cp) - parse_document(); + #if !TOML_EXCEPTIONS + if (reader.error()) + { + err = std::move(reader.error()); + return; + } + #endif + + if (cp) + parse_document(); + } update_region_ends(root); }