From 4de1e2fc433a4e587e81c007319395d2ff9735a4 Mon Sep 17 00:00:00 2001 From: Sven Czarnian Date: Tue, 10 Aug 2021 08:39:34 +0200 Subject: [PATCH] add an helper class to process strings --- include/aman/helper/String.h | 86 ++++++++++++++++++++++++++++++++++++ src/CMakeLists.txt | 5 +++ 2 files changed, 91 insertions(+) create mode 100644 include/aman/helper/String.h diff --git a/include/aman/helper/String.h b/include/aman/helper/String.h new file mode 100644 index 0000000..716794d --- /dev/null +++ b/include/aman/helper/String.h @@ -0,0 +1,86 @@ +/* + * @brief Defines and implements functions to handle strings + * @file aman/helper/String.h + * @author Sven Czarnian + * @copyright Copyright 2021 Sven Czarnian + * @license This project is published under the GNU General Public License v3 (GPLv3) + */ + +#pragma once + +#include +#include + +namespace aman { + /** + * @brief Implements and defines convinience functions for the string handling + * @ingroup helper + */ + class String { + private: + template + static auto splitAux(const std::string& value, Separator&& separator) -> std::vector { + std::vector result; + std::string::size_type p = 0; + std::string::size_type q; + while ((q = separator(value, p)) != std::string::npos) { + result.emplace_back(value, p, q - p); + p = q + 1; + } + result.emplace_back(value, p); + return result; + } + + public: + String() = delete; + String(const String&) = delete; + String(String&&) = delete; + String& operator=(const String&) = delete; + String& operator=(String&&) = delete; + + /** + * @brief Replaces all markers by replace in message + * @param[in,out] message The message which needs to be modified + * @param[in] marker The wildcard which needs to be found in message and which needs to be replaced + * @param[in] replace The replacement of marker in message + * @return + */ + static __inline void stringReplace(std::string& message, const std::string& marker, const std::string& replace) { + std::size_t pos = message.find(marker, 0); + while (std::string::npos != pos) { + auto it = message.cbegin() + pos; + message.replace(it, it + marker.length(), replace); + pos = message.find(marker, pos + marker.length()); + } + } + + /** + * @brief Splits value into chunks and the separator is defined in separators + * @param[in] value The string which needs to be splitted up + * @param[in] separators The separators which split up the value + * @return The list of splitted chunks + */ + static auto splitString(const std::string& value, const std::string& separators) -> std::vector { + return String::splitAux(value, [&](const std::string& v, std::string::size_type p) noexcept { + return v.find_first_of(separators, p); + }); + } + + /** + * @brief Removes leading and trailing whitespaces + * @param[in] value The trimable string + * @param[in] spaces The characters that need to be removed + * @return The trimmed version of value + */ + static auto trim(const std::string& value, const std::string& spaces = " \t") -> std::string { + const auto begin = value.find_first_not_of(spaces, 0); + if (std::string::npos == begin) + return ""; + + const auto end = value.find_last_not_of(spaces); + const auto range = end - begin + 1; + + return value.substr(begin, range); + } + }; +} diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 63c17f6..9e1c9f4 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -19,12 +19,16 @@ SET(SOURCE_FILES_RES ${CMAKE_SOURCE_DIR}/res/resource.h ${CMAKE_SOURCE_DIR}/res/targetver.h ) +SET(INCLUDE_HELPER_FILES + ${CMAKE_SOURCE_DIR}/include/aman/helper/String.h +) # define the plug in ADD_LIBRARY( ArrivalMANager SHARED ${SOURCE_FILES_RES} ${SOURCE_FILES} + ${INCLUDE_HELPER_FILES} ) # define the dependencies @@ -46,3 +50,4 @@ ENDIF() SOURCE_GROUP("Source Files" FILES ${SOURCE_FILES}) SOURCE_GROUP("Source Files\\res" FILES ${SOURCE_FILES_RES}) +SOURCE_GROUP("Header Files\\helper" FILES ${INCLUDE_HELPER_FILES})