From befe37924105bf6f144d246167f51f2e4597b923 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Fri, 18 Oct 2024 02:24:11 +0900 Subject: [PATCH] refactor: restrict retrace dist == 1 to simplify the implementation --- include/toml11/fwd/location_fwd.hpp | 4 ++-- include/toml11/impl/location_impl.hpp | 14 +++++++------- 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/include/toml11/fwd/location_fwd.hpp b/include/toml11/fwd/location_fwd.hpp index 401d857..7fc7eca 100644 --- a/include/toml11/fwd/location_fwd.hpp +++ b/include/toml11/fwd/location_fwd.hpp @@ -41,7 +41,7 @@ class location ~location() = default; void advance(std::size_t n = 1) noexcept; - void retrace(std::size_t n = 1) noexcept; + void retrace() noexcept; bool is_ok() const noexcept { return static_cast(this->source_); } @@ -68,7 +68,7 @@ class location private: void advance_impl(const std::size_t n); - void retrace_impl(const std::size_t n); + void retrace_impl(); private: diff --git a/include/toml11/impl/location_impl.hpp b/include/toml11/impl/location_impl.hpp index a7751a1..a909ee4 100644 --- a/include/toml11/impl/location_impl.hpp +++ b/include/toml11/impl/location_impl.hpp @@ -24,18 +24,18 @@ TOML11_INLINE void location::advance(std::size_t n) noexcept this->location_ = this->source_->size(); } } -TOML11_INLINE void location::retrace(std::size_t n) noexcept +TOML11_INLINE void location::retrace(/*restricted to n=1*/) noexcept { assert(this->is_ok()); - if(this->location_ < n) + if(this->location_ == 0) { this->location_ = 0; this->line_number_ = 1; } else { - this->retrace_impl(n); - this->location_ -= n; + this->retrace_impl(); + this->location_ -= 1; } } @@ -102,14 +102,14 @@ TOML11_INLINE void location::advance_impl(const std::size_t n) return; } -TOML11_INLINE void location::retrace_impl(const std::size_t n) +TOML11_INLINE void location::retrace_impl(/*n == 1*/) { assert(this->is_ok()); - assert(n <= this->location_); // loc - n >= 0 + assert(this->location_ != 0); const auto iter = this->source_->cbegin(); const auto dline_num = static_cast(std::count( - std::next(iter, static_cast(this->location_ - n)), + std::next(iter, static_cast(this->location_ - 1)), std::next(iter, static_cast(this->location_)), char_type('\n')));