From 604ca3f48eb21418c9ab38f89ccc93e4189f7e37 Mon Sep 17 00:00:00 2001 From: Joao Carreira Date: Wed, 13 Mar 2024 00:16:03 +0000 Subject: [PATCH] refactor: move sanitize to utils file --- BinaryOperatorFactory.tcc | 18 ++---------------- CMakeLists.txt | 1 + utils.cpp | 19 +++++++++++++++++++ utils.hpp | 8 ++++++++ 4 files changed, 30 insertions(+), 16 deletions(-) create mode 100644 utils.cpp create mode 100644 utils.hpp diff --git a/BinaryOperatorFactory.tcc b/BinaryOperatorFactory.tcc index ca5071f..a514cdc 100644 --- a/BinaryOperatorFactory.tcc +++ b/BinaryOperatorFactory.tcc @@ -1,5 +1,6 @@ #include "BinaryOperatorFactory.hpp" +#include "utils.hpp" template BinaryOperatorFactory::BinaryOperatorFactory() noexcept @@ -13,22 +14,7 @@ BinaryOperatorFactory::BinaryOperatorFactory() noexcept template auto BinaryOperatorFactory::make(std::string_view text) noexcept -> std::optional { - auto sanitize = [](std::string_view text) noexcept -> std::string { - constexpr char kSpace{' '}; - std::string_view view{text}; - - auto leading_spaces = view.find_first_not_of(kSpace); - if (leading_spaces == std::string::npos) return ""; - - auto sub_view = view.substr(leading_spaces); - - auto trailling_spaces = sub_view.find_first_of(kSpace); - if (trailling_spaces == std::string::npos) return std::string{sub_view}; - - return std::string{sub_view.substr(0, trailling_spaces)}; - }; - - auto found_op = _binary_operations.find(sanitize(text)); + auto found_op = _binary_operations.find(std::string{sanitize_text(text)}); if (found_op == _binary_operations.end()) return std::nullopt; return found_op->second; } diff --git a/CMakeLists.txt b/CMakeLists.txt index 929371c..6b22335 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,6 +32,7 @@ set(CMAKE_CXX_COMPILER /opt/homebrew/Cellar/llvm@16/16.0.6/bin/clang++) SET(CALCULATOR_CHALLENGE_SOURCES Calculator.cpp + utils.cpp ) add_executable(calculator-challege diff --git a/utils.cpp b/utils.cpp new file mode 100644 index 0000000..081f357 --- /dev/null +++ b/utils.cpp @@ -0,0 +1,19 @@ +#include "utils.hpp" + +#include + +auto sanitize_text(std::string_view text) noexcept -> std::string_view +{ + constexpr char kSpace{' '}; + std::string_view view{text}; + + auto leading_spaces = view.find_first_not_of(kSpace); + if (leading_spaces == std::string::npos) return ""; + + auto sub_view = view.substr(leading_spaces); + + auto trailling_spaces = sub_view.find_first_of(kSpace); + if (trailling_spaces == std::string::npos) return sub_view; + + return sub_view.substr(0, trailling_spaces); +} diff --git a/utils.hpp b/utils.hpp new file mode 100644 index 0000000..1e1e334 --- /dev/null +++ b/utils.hpp @@ -0,0 +1,8 @@ +#ifndef CALCULATOR_CHALLENGE_UTILS_HPP +#define CALCULATOR_CHALLENGE_UTILS_HPP + +#include + +auto sanitize_text(std::string_view text) noexcept -> std::string_view; + +#endif