From 9e7ec1fb5d5142edc63856e5ccd2fd84295ddde0 Mon Sep 17 00:00:00 2001 From: Eric Floyd Date: Mon, 9 Oct 2023 20:52:56 -0400 Subject: [PATCH] Removed uneeded functions and moved some to StringHelper --- include/faker-cxx/Helper.h | 41 ------------------------------- src/common/StringHelper.cpp | 14 +++++++++++ src/common/StringHelper.h | 2 ++ src/common/StringHelperTest.cpp | 22 +++++++++++++++++ src/modules/hacker/Hacker.cpp | 5 ++-- src/modules/hacker/HackerTest.cpp | 2 -- src/modules/helper/Helper.cpp | 24 ------------------ src/modules/helper/HelperTest.cpp | 31 ----------------------- 8 files changed, 41 insertions(+), 100 deletions(-) diff --git a/include/faker-cxx/Helper.h b/include/faker-cxx/Helper.h index 794069347..f1f20056b 100644 --- a/include/faker-cxx/Helper.h +++ b/include/faker-cxx/Helper.h @@ -3,7 +3,6 @@ #include #include #include -#include #include "Datatype.h" #include "Number.h" @@ -179,46 +178,6 @@ class Helper return TResult(); } - /* - * @brief Returns a split string into words. - * - * @param input The string to split. - * - * @return A vector of words. - * - * @code - * Helper::splitIntoWords("Hello World!") // {"Hello", "World!"} - * @endcode - */ - static std::vector splitIntoWords(const std::string& input); - - /* - * @brief Returns true if the given character is punctuation. - * - * @param c The character to check. - * - * @return True if the given character is punctuation. - * - * @code - * Helper::isPunctuation('!') // true - * Helper::isPunctuation('a') // false - * @endcode - */ - static bool isPunctuation(char c); - - /* - * @brief Returns the given word without punctuation. - * - * @param word The word to remove punctuation. - * - * @return The given word without punctuation. - * - * @code - * Helper::removePunctuation("Hello!") // "Hello" - * @endcode - */ - static std::string removePunctuation(const std::string& word); - private: static std::random_device randomDevice; static std::mt19937 pseudoRandomGenerator; diff --git a/src/common/StringHelper.cpp b/src/common/StringHelper.cpp index 3b4c87f30..173c56c10 100644 --- a/src/common/StringHelper.cpp +++ b/src/common/StringHelper.cpp @@ -63,4 +63,18 @@ std::string StringHelper::toLower(const std::string& data) return lowerData; } + +// Function to check if a character is punctuation +bool StringHelper::isPunctuation(char c) +{ + return (c == '.' || c == ',' || c == '!' || c == '?' || c == ';' || c == ':'); +} + +// Function to remove punctuation from a word +std::string StringHelper::removePunctuation(const std::string& word) +{ + std::string result = word; + result.erase(std::remove_if(result.begin(), result.end(), isPunctuation), result.end()); + return result; +} } diff --git a/src/common/StringHelper.h b/src/common/StringHelper.h index 3c22433b3..163bcacad 100644 --- a/src/common/StringHelper.h +++ b/src/common/StringHelper.h @@ -12,5 +12,7 @@ class StringHelper static std::string join(const std::vector& data, const std::string& separator = " "); static std::string repeat(const std::string& data, int repetition); static std::string toLower(const std::string& data); + static bool isPunctuation(char c); + static std::string removePunctuation(const std::string& word); }; } diff --git a/src/common/StringHelperTest.cpp b/src/common/StringHelperTest.cpp index f905b2ccc..ec58c75bb 100644 --- a/src/common/StringHelperTest.cpp +++ b/src/common/StringHelperTest.cpp @@ -70,3 +70,25 @@ TEST_F(StringHelperTest, toLower) ASSERT_EQ(result, "hello!"); } + +TEST_F(StringHelperTest, IsPunctuation) +{ + std::string punctuation = ".,;:!?"; + for (char c : punctuation) + { + EXPECT_TRUE(StringHelper::isPunctuation(c)); + } + + std::string notPunctuation = "abc123"; + for (char c : notPunctuation) + { + EXPECT_FALSE(StringHelper::isPunctuation(c)); + } +} + +TEST_F(StringHelperTest, RemovePunctuation) +{ + std::string input = "Hello, World!"; + std::string result = StringHelper::removePunctuation(input); + EXPECT_EQ(result, "Hello World"); +} diff --git a/src/modules/hacker/Hacker.cpp b/src/modules/hacker/Hacker.cpp index 5dfdfe0a8..b61eb059d 100644 --- a/src/modules/hacker/Hacker.cpp +++ b/src/modules/hacker/Hacker.cpp @@ -1,4 +1,5 @@ #include "faker-cxx/Hacker.h" +#include "../../common/StringHelper.h" #include #include @@ -39,12 +40,12 @@ std::string Hacker::ingverb() std::string Hacker::phrase() { - auto splitRandomPhrase = Helper::splitIntoWords(faker::Helper::arrayElement(faker::phrases)); + auto splitRandomPhrase = StringHelper::split(faker::Helper::arrayElement(faker::phrases)); std::string ret; for (auto& word : splitRandomPhrase) { - word = Helper::removePunctuation(word); + word = StringHelper::removePunctuation(word); if (word == "{abbreviation}") { word = abbreviation(); diff --git a/src/modules/hacker/HackerTest.cpp b/src/modules/hacker/HackerTest.cpp index f6f4aac53..1b1a5df52 100644 --- a/src/modules/hacker/HackerTest.cpp +++ b/src/modules/hacker/HackerTest.cpp @@ -9,9 +9,7 @@ #include "data/Adjectives.h" #include "data/Ingverbs.h" #include "data/Nouns.h" -#include "data/Phrases.h" #include "data/Verbs.h" -#include "faker-cxx/Helper.h" using namespace ::testing; using namespace faker; diff --git a/src/modules/helper/Helper.cpp b/src/modules/helper/Helper.cpp index a76864768..74b400e42 100644 --- a/src/modules/helper/Helper.cpp +++ b/src/modules/helper/Helper.cpp @@ -1,11 +1,9 @@ #include "faker-cxx/Helper.h" #include -#include #include #include #include -#include #include #include "../src/common/LuhnCheck.h" @@ -107,26 +105,4 @@ std::string Helper::regexpStyleStringParse(const std::string& input) return string; } - -// helper functions for phrases -std::vector Helper::splitIntoWords(const std::string& input) -{ - std::istringstream iss(input); - std::vector words(std::istream_iterator{iss}, std::istream_iterator()); - return words; -} - -// Function to check if a character is punctuation -bool Helper::isPunctuation(char c) -{ - return (c == '.' || c == ',' || c == '!' || c == '?' || c == ';' || c == ':'); -} - -// Function to remove punctuation from a word -std::string Helper::removePunctuation(const std::string& word) -{ - std::string result = word; - result.erase(std::remove_if(result.begin(), result.end(), isPunctuation), result.end()); - return result; -} } diff --git a/src/modules/helper/HelperTest.cpp b/src/modules/helper/HelperTest.cpp index e851719e9..b58053a1c 100644 --- a/src/modules/helper/HelperTest.cpp +++ b/src/modules/helper/HelperTest.cpp @@ -107,35 +107,4 @@ TEST_F(HelperTest, MaybeDouble) result = Helper::maybe([]() { return 3.14; }, lowProbability); EXPECT_EQ(result, 0.0); } - -TEST_F(HelperTest, IsPunctuation) -{ - std::string punctuation = ".,;:!?"; - for (char c : punctuation) - { - EXPECT_TRUE(Helper::isPunctuation(c)); - } - - std::string notPunctuation = "abc123"; - for (char c : notPunctuation) - { - EXPECT_FALSE(Helper::isPunctuation(c)); - } -} - -TEST_F(HelperTest, RemovePunctuation) -{ - std::string input = "Hello, World!"; - std::string result = Helper::removePunctuation(input); - EXPECT_EQ(result, "Hello World"); -} - -TEST_F(HelperTest, SplitIntoWords) -{ - std::string input = "Hello World!"; - std::vector result = Helper::splitIntoWords(input); - EXPECT_EQ(result.size(), 2); - EXPECT_EQ(result[0], "Hello"); - EXPECT_EQ(result[1], "World!"); -} }