From 7df96d95c77c664e3757c5e432202feacc89671c Mon Sep 17 00:00:00 2001 From: ado Date: Sun, 6 Aug 2023 19:56:28 +0200 Subject: [PATCH] Make line method work on errors too --- include/ss/parser.hpp | 18 ++++++++---------- ssp.hpp | 7 +++---- 2 files changed, 11 insertions(+), 14 deletions(-) diff --git a/include/ss/parser.hpp b/include/ss/parser.hpp index 9fc249d..54126bb 100644 --- a/include/ss/parser.hpp +++ b/include/ss/parser.hpp @@ -84,9 +84,9 @@ public: return to_object(get_next()); } - // TODO make the method work with if valid() returns false size_t line() const { - return valid() ? reader_.line_number_ - 1 : 0; + return reader_.line_number_ > 1 ? reader_.line_number_ - 1 + : reader_.line_number_; } template @@ -193,7 +193,7 @@ public: reader_.next_line_converter_.set_column_mapping(column_mappings, header_.size()); - if (line() == 0) { + if (line() == 1) { ignore_next(); } } @@ -622,13 +622,11 @@ private: void decorate_rethrow(const ss::exception& e) const { static_assert(throw_on_error, "throw_on_error needs to be enabled to use this method"); - auto line = reader_.line_number_; - throw ss::exception{ - std::string{file_name_} - .append(" ") - .append(std::to_string(line > 1 ? line - 1 : line)) - .append(": ") - .append(e.what())}; + throw ss::exception{std::string{file_name_} + .append(" ") + .append(std::to_string(line())) + .append(": ") + .append(e.what())}; } //////////////// diff --git a/ssp.hpp b/ssp.hpp index 90494e9..d1d0158 100644 --- a/ssp.hpp +++ b/ssp.hpp @@ -2386,9 +2386,9 @@ public: return to_object(get_next()); } - // TODO make the method work with if valid() returns false size_t line() const { - return valid() ? reader_.line_number_ - 1 : 0; + return reader_.line_number_ > 1 ? reader_.line_number_ - 1 + : reader_.line_number_; } template @@ -2924,11 +2924,10 @@ private: void decorate_rethrow(const ss::exception& e) const { static_assert(throw_on_error, "throw_on_error needs to be enabled to use this method"); - auto line = reader_.line_number_; throw ss::exception{ std::string{file_name_} .append(" ") - .append(std::to_string(line > 1 ? line - 1 : line)) + .append(std::to_string(line())) .append(": ") .append(e.what())}; }