浏览代码

Added RegexUtils class

- added RegexUtils class to provide additional basic
regex functionality
- added test for RegexUtils class
Patrick 3 年之前
父节点
当前提交
bdd2f82bfc
共有 3 个文件被更改,包括 67 次插入2 次删除
  1. 4 2
      CMakeLists.txt
  2. 31 0
      source/utils/RegexUtils.hpp
  3. 32 0
      test/cases/utils/RegexUtilsTest.cpp

+ 4 - 2
CMakeLists.txt

@@ -65,7 +65,8 @@ set(SOURCE_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/source/io/IReader.hpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/io/FileReader.hpp
         ${CMAKE_CURRENT_SOURCE_DIR}/source/io/FileReader.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/source/io/NewLine.hpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/io/NewLine.hpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/source/utils/RegexUtils.hpp)
 
 set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/IntegerTest.cpp
@@ -80,7 +81,8 @@ set(TEST_FILES
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/utils/STLUtilsTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/boxing/LongTest.cpp
         ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/FileWriterTest.cpp
-        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/FileReaderTest.cpp)
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/io/FileReaderTest.cpp
+        ${CMAKE_CURRENT_SOURCE_DIR}/test/cases/utils/RegexUtilsTest.cpp)
 
 ##########################################################
 # Build

+ 31 - 0
source/utils/RegexUtils.hpp

@@ -0,0 +1,31 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-18
+ * Changed:         2020-08-18
+ *
+ * */
+
+#ifndef REGEX_UTILS_HPP
+#define REGEX_UTILS_HPP
+
+#include <string>
+#include <regex>
+
+namespace ls_std {
+  class RegexUtils {
+    public:
+
+      RegexUtils() = default;
+      ~RegexUtils() = default;
+
+      static std::string escapeString(const std::string &_text)
+      {
+        static std::regex regexMetaEscape(R"(([\^\$\\\.\*\+\?\(\)\[\]\{\}\|]))");
+        return std::regex_replace(_text, regexMetaEscape, R"(\$1)");
+      }
+  };
+}
+
+#endif

+ 32 - 0
test/cases/utils/RegexUtilsTest.cpp

@@ -0,0 +1,32 @@
+/*
+ * Author:          Patrick-Christopher Mattulat
+ * Company:         Lynar Studios
+ * E-Mail:          webmaster@lynarstudios.com
+ * Created:         2020-08-18
+ * Changed:         2020-08-18
+ *
+ * */
+
+#include <gtest/gtest.h>
+#include "../../../source/utils/RegexUtils.hpp"
+
+namespace {
+  class RegexUtilsTest : public ::testing::Test {
+    protected:
+
+      RegexUtilsTest() = default;
+      ~RegexUtilsTest() override = default;
+
+      void SetUp() override {}
+      void TearDown() override {}
+  };
+
+  TEST_F(RegexUtilsTest, escapeString)
+  {
+    std::string escapedString = ls_std::RegexUtils::escapeString("Hello?!");
+    ASSERT_STREQ(R"(Hello\?!)", escapedString.c_str());
+
+    escapedString = ls_std::RegexUtils::escapeString(R"(\)");
+    ASSERT_STREQ(R"(\\)", escapedString.c_str());
+  }
+}