refactor: move sanitize to utils file
This commit is contained in:
1 parent
ed02a8bd1d
commit
604ca3f48e
4 files changed
+30
-16
No files matched your search
@@ -1,5 +1,6 @@
|
||||
|
||||
#include "BinaryOperatorFactory.hpp"
|
||||
#include "utils.hpp"
|
||||
|
||||
template<typename T>
|
||||
BinaryOperatorFactory<T>::BinaryOperatorFactory() noexcept
|
||||
@@ -13,22 +14,7 @@ BinaryOperatorFactory<T>::BinaryOperatorFactory() noexcept
|
||||
template<typename T>
|
||||
auto BinaryOperatorFactory<T>::make(std::string_view text) noexcept -> std::optional<Operator>
|
||||
{
|
||||
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;
|
||||
}
|
||||
@@ -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
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#include "utils.hpp"
|
||||
|
||||
#include <string>
|
||||
|
||||
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);
|
||||
}
|
||||
Reference in new issue
Block a user