refactor: restrict retrace dist == 1

to simplify the implementation
This commit is contained in:
ToruNiina
2024-10-18 02:24:11 +09:00
parent f06ad06ad7
commit befe379241
2 changed files with 9 additions and 9 deletions

View File

@@ -41,7 +41,7 @@ class location
~location() = default; ~location() = default;
void advance(std::size_t n = 1) noexcept; 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<bool>(this->source_); } bool is_ok() const noexcept { return static_cast<bool>(this->source_); }
@@ -68,7 +68,7 @@ class location
private: private:
void advance_impl(const std::size_t n); void advance_impl(const std::size_t n);
void retrace_impl(const std::size_t n); void retrace_impl();
private: private:

View File

@@ -24,18 +24,18 @@ TOML11_INLINE void location::advance(std::size_t n) noexcept
this->location_ = this->source_->size(); 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()); assert(this->is_ok());
if(this->location_ < n) if(this->location_ == 0)
{ {
this->location_ = 0; this->location_ = 0;
this->line_number_ = 1; this->line_number_ = 1;
} }
else else
{ {
this->retrace_impl(n); this->retrace_impl();
this->location_ -= n; this->location_ -= 1;
} }
} }
@@ -102,14 +102,14 @@ TOML11_INLINE void location::advance_impl(const std::size_t n)
return; 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(this->is_ok());
assert(n <= this->location_); // loc - n >= 0 assert(this->location_ != 0);
const auto iter = this->source_->cbegin(); const auto iter = this->source_->cbegin();
const auto dline_num = static_cast<std::size_t>(std::count( const auto dline_num = static_cast<std::size_t>(std::count(
std::next(iter, static_cast<difference_type>(this->location_ - n)), std::next(iter, static_cast<difference_type>(this->location_ - 1)),
std::next(iter, static_cast<difference_type>(this->location_)), std::next(iter, static_cast<difference_type>(this->location_)),
char_type('\n'))); char_type('\n')));