From cb577c7acd56430041427842c8e15261851ca206 Mon Sep 17 00:00:00 2001 From: Nav Date: Sat, 23 Jul 2022 15:36:05 +0100 Subject: [PATCH] String and YAML utilities --- src/CMakeLists.txt | 1 + src/Helpers/String.cpp | 29 +++++++++++++++++++++++++++++ src/Helpers/String.hpp | 16 ++++++++++++++++ src/Helpers/YamlUtilities.hpp | 21 +++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 src/Helpers/String.cpp create mode 100644 src/Helpers/String.hpp create mode 100644 src/Helpers/YamlUtilities.hpp diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index ab6af827..df5e0d9f 100755 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -7,6 +7,7 @@ target_sources( # Helpers & other ${CMAKE_CURRENT_SOURCE_DIR}/Logger/Logger.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Helpers/Paths.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/Helpers/String.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Helpers/EpollInstance.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Helpers/EventFdNotifier.cpp ${CMAKE_CURRENT_SOURCE_DIR}/Helpers/ConditionVariableNotifier.cpp diff --git a/src/Helpers/String.cpp b/src/Helpers/String.cpp new file mode 100644 index 00000000..be5d7257 --- /dev/null +++ b/src/Helpers/String.cpp @@ -0,0 +1,29 @@ +#include "String.hpp" + +#include +#include + +namespace Bloom +{ + std::string String::asciiToLower(std::string str) { + std::transform(str.begin(), str.end(), str.begin(), [] (unsigned char character) { + return std::tolower(character); + }); + + return str; + } + + std::string String::asciiToUpper(std::string str) { + std::transform(str.begin(), str.end(), str.begin(), [] (unsigned char character) { + return std::toupper(character); + }); + + return str; + } + + bool String::isAscii(const std::string& str) { + return !std::any_of(str.begin(), str.end(), [] (unsigned char character) { + return character > 127; + }); + } +} diff --git a/src/Helpers/String.hpp b/src/Helpers/String.hpp new file mode 100644 index 00000000..03689089 --- /dev/null +++ b/src/Helpers/String.hpp @@ -0,0 +1,16 @@ +#pragma once + +#include + +namespace Bloom +{ + class String + { + public: + static std::string asciiToLower(std::string str); + + static std::string asciiToUpper(std::string str); + + static bool isAscii(const std::string& str); + }; +} diff --git a/src/Helpers/YamlUtilities.hpp b/src/Helpers/YamlUtilities.hpp new file mode 100644 index 00000000..e87cba26 --- /dev/null +++ b/src/Helpers/YamlUtilities.hpp @@ -0,0 +1,21 @@ +#pragma once + +#include + +namespace Bloom +{ + class YamlUtilities + { + public: + template + static bool isType(const YAML::Node& node) { + try { + node.as(); + return true; + + } catch (YAML::BadConversion&) { + return false; + } + } + }; +}