Explorar el Código

add an helper class to process strings

Sven Czarnian hace 3 años
padre
commit
4de1e2fc43
Se han modificado 2 ficheros con 91 adiciones y 0 borrados
  1. 86 0
      include/aman/helper/String.h
  2. 5 0
      src/CMakeLists.txt

+ 86 - 0
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 <devel@svcz.de>
+ * @copyright Copyright 2021 Sven Czarnian
+ * @license This project is published under the GNU General Public License v3 (GPLv3)
+ */
+
+#pragma once
+
+#include <string>
+#include <vector>
+
+namespace aman {
+    /**
+     * @brief Implements and defines convinience functions for the string handling
+     * @ingroup helper
+     */
+    class String {
+    private:
+        template <typename Separator>
+        static auto splitAux(const std::string& value, Separator&& separator) -> std::vector<std::string> {
+            std::vector<std::string> 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<std::string> {
+            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);
+        }
+    };
+}

+ 5 - 0
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})