From 64c2d4d33b472e77b025f1cdd2f56f513de57538 Mon Sep 17 00:00:00 2001 From: Shikhar Vashistha Date: Mon, 4 Oct 2021 05:29:50 +0000 Subject: [PATCH] Refactored the code --- liblangutil/Exceptions.cpp | 11 +++++++++++ liblangutil/Exceptions.h | 15 --------------- liblangutil/SourceReferenceFormatter.cpp | 10 ++-------- 3 files changed, 13 insertions(+), 23 deletions(-) diff --git a/liblangutil/Exceptions.cpp b/liblangutil/Exceptions.cpp index 34aa69803..dd0de9d0a 100644 --- a/liblangutil/Exceptions.cpp +++ b/liblangutil/Exceptions.cpp @@ -71,3 +71,14 @@ Error::Error( if (!_description.empty()) *this << util::errinfo_comment(_description); } + +static std::optional severityFromString(std::string _severity) +{ + boost::algorithm::to_lower(_severity); + + _severity = boost::algorithm::trim_copy(_severity); + + _severity[0] = toupper(_severity[0]); + + return std::make_optional(_severity).has_value() ? std::optional(_severity).value() : std::nullopt; +} \ No newline at end of file diff --git a/liblangutil/Exceptions.h b/liblangutil/Exceptions.h index 74d506f47..69a098d52 100644 --- a/liblangutil/Exceptions.h +++ b/liblangutil/Exceptions.h @@ -33,8 +33,6 @@ #include #include -#include -#include #include #include #include @@ -244,19 +242,6 @@ public: return "Error"; } - static std::optional stringToSeverity(std::string _severity) - { - _severity = tolower(_severity); - - _severity = std::regex_replace(_severity, std::regex("^ +| +$|( ) +"), "$1"); - - _severity.erase(_severity.begin(), std::find_if(_severity.begin(), _severity.end(), std::bind1st(std::not_equal_to(), ' '))); - - _severity[0] = toupper(_severity[0]); - - return std::make_optional(_severity).has_value() ? std::optional(_severity).value() : std::nullopt; - } - static std::string formatErrorSeverityLowercase(Severity _severity) { switch (_severity) diff --git a/liblangutil/SourceReferenceFormatter.cpp b/liblangutil/SourceReferenceFormatter.cpp index 437e3a1d1..8ca479b6b 100644 --- a/liblangutil/SourceReferenceFormatter.cpp +++ b/liblangutil/SourceReferenceFormatter.cpp @@ -55,13 +55,6 @@ std::string SourceReferenceFormatter::formatErrorInformation(Error const& _error ); } -enum class Severity//have to define if used in switch -{ - Error, - Warning, - Info -}; - AnsiColorized SourceReferenceFormatter::normalColored() const { return AnsiColorized(m_stream, m_colored, {WHITE}); @@ -188,7 +181,7 @@ void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtracto // exception header line if (_msg.errorId.has_value()) { - switch(stringToSeverity(_msg.severity)) + switch(severityFromString(_msg.severity)) { case Severity::Error : errorColored() << " (" << _msg.errorId.value().error << ")"; break; @@ -201,6 +194,7 @@ void SourceReferenceFormatter::printExceptionInformation(SourceReferenceExtracto default : unknownColored() << " (" << _msg.errorId.value().error << ")"; } + } printSourceLocation(_msg.primary);