From 20a758c42db4085ef4315a33ff1e4e0a4031752c Mon Sep 17 00:00:00 2001 From: Pedro Bianchini de Quadros Date: Fri, 24 Jan 2025 16:32:27 -0300 Subject: [PATCH] feat(exceptions): Including a new file that standardizes the output --- lib/CMakeLists.txt | 1 + .../exceptions/invalid_pointer_exception.h | 16 +++++++++++++++ lib/include/preprocessing/stemmer.h | 2 ++ .../exceptions/invalid_pointer_exception.cpp | 13 ++++++++++++ lib/src/preprocessing/stemmer.cpp | 20 +++++++++++-------- 5 files changed, 44 insertions(+), 8 deletions(-) create mode 100644 lib/include/exceptions/invalid_pointer_exception.h create mode 100644 lib/src/exceptions/invalid_pointer_exception.cpp diff --git a/lib/CMakeLists.txt b/lib/CMakeLists.txt index 89a8b1d..9f60196 100644 --- a/lib/CMakeLists.txt +++ b/lib/CMakeLists.txt @@ -7,6 +7,7 @@ add_library( src/page_rank.cpp src/inverted_index.cpp src/preprocessing/stemmer.cpp + src/exceptions/invalid_pointer_exception.cpp ) target_include_directories(search_engine PUBLIC include) diff --git a/lib/include/exceptions/invalid_pointer_exception.h b/lib/include/exceptions/invalid_pointer_exception.h new file mode 100644 index 0000000..f7513a1 --- /dev/null +++ b/lib/include/exceptions/invalid_pointer_exception.h @@ -0,0 +1,16 @@ +#ifndef INVALID_POINTER_H +#define INVALID_POINTER_H + +#include +#include + +namespace exceptions { +class invalid_pointer_exception : public std::exception { + public: + invalid_pointer_exception(); + + const char* what() const noexcept override; +}; +} // namespace exceptions + +#endif \ No newline at end of file diff --git a/lib/include/preprocessing/stemmer.h b/lib/include/preprocessing/stemmer.h index 9626bdf..23d82d3 100644 --- a/lib/include/preprocessing/stemmer.h +++ b/lib/include/preprocessing/stemmer.h @@ -11,6 +11,8 @@ #include #include +#include "exceptions/invalid_pointer_exception.h" + namespace stemmer { /** diff --git a/lib/src/exceptions/invalid_pointer_exception.cpp b/lib/src/exceptions/invalid_pointer_exception.cpp new file mode 100644 index 0000000..48ad4a5 --- /dev/null +++ b/lib/src/exceptions/invalid_pointer_exception.cpp @@ -0,0 +1,13 @@ +#include "exceptions/invalid_pointer_exception.h" + +namespace exceptions { + +// Definição do construtor com mensagem padrão +invalid_pointer_exception::invalid_pointer_exception() {} + +// Definição do método what(), retornando a mensagem padrão +const char* invalid_pointer_exception::what() const noexcept { + return "Invalid pointer exception occurred."; +} + +} // namespace exceptions diff --git a/lib/src/preprocessing/stemmer.cpp b/lib/src/preprocessing/stemmer.cpp index c4b6e80..b1c884d 100644 --- a/lib/src/preprocessing/stemmer.cpp +++ b/lib/src/preprocessing/stemmer.cpp @@ -199,14 +199,18 @@ std::string RSPL::removeAccents(const std::string& input) { } void RSPL::shrinkString(std::string* input) { - if (!input) - return; // Verifica se o ponteiro é válido - - icu::UnicodeString ustr(input->c_str(), "UTF-8"); - ustr.toLower(); - std::string result; - ustr.toUTF8String(result); - *input = result; + try { + if (!input) + throw exceptions::invalid_pointer_exception(); + + icu::UnicodeString ustr(input->c_str(), "UTF-8"); + ustr.toLower(); + std::string result; + ustr.toUTF8String(result); + *input = result; + } catch (const std::exception& e) { + std::cerr << e.what() << '\n'; + } } bool RSPL::applyRules(std::string& word, const std::vector& rules) {