Skip to content

Commit

Permalink
Removed uneeded functions and moved some to StringHelper
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-bodhi committed Oct 10, 2023
1 parent d6e26e1 commit 9e7ec1f
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 100 deletions.
41 changes: 0 additions & 41 deletions include/faker-cxx/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
#include <functional>
#include <span>
#include <string>
#include <vector>

#include "Datatype.h"
#include "Number.h"
Expand Down Expand Up @@ -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<std::string> 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;
Expand Down
14 changes: 14 additions & 0 deletions src/common/StringHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}
2 changes: 2 additions & 0 deletions src/common/StringHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,7 @@ class StringHelper
static std::string join(const std::vector<std::string>& 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);
};
}
22 changes: 22 additions & 0 deletions src/common/StringHelperTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
5 changes: 3 additions & 2 deletions src/modules/hacker/Hacker.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "faker-cxx/Hacker.h"
#include "../../common/StringHelper.h"

#include <string>
#include <vector>
Expand Down Expand Up @@ -39,12 +40,12 @@ std::string Hacker::ingverb()

std::string Hacker::phrase()
{
auto splitRandomPhrase = Helper::splitIntoWords(faker::Helper::arrayElement<std::string>(faker::phrases));
auto splitRandomPhrase = StringHelper::split(faker::Helper::arrayElement<std::string>(faker::phrases));
std::string ret;

for (auto& word : splitRandomPhrase)
{
word = Helper::removePunctuation(word);
word = StringHelper::removePunctuation(word);
if (word == "{abbreviation}")
{
word = abbreviation();
Expand Down
2 changes: 0 additions & 2 deletions src/modules/hacker/HackerTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
24 changes: 0 additions & 24 deletions src/modules/helper/Helper.cpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
#include "faker-cxx/Helper.h"

#include <chrono>
#include <iterator>
#include <random>
#include <regex>
#include <string>
#include <string_view>
#include <vector>

#include "../src/common/LuhnCheck.h"
Expand Down Expand Up @@ -107,26 +105,4 @@ std::string Helper::regexpStyleStringParse(const std::string& input)

return string;
}

// helper functions for phrases
std::vector<std::string> Helper::splitIntoWords(const std::string& input)
{
std::istringstream iss(input);
std::vector<std::string> words(std::istream_iterator<std::string>{iss}, std::istream_iterator<std::string>());
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;
}
}
31 changes: 0 additions & 31 deletions src/modules/helper/HelperTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,35 +107,4 @@ TEST_F(HelperTest, MaybeDouble)
result = Helper::maybe<double>([]() { 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<std::string> result = Helper::splitIntoWords(input);
EXPECT_EQ(result.size(), 2);
EXPECT_EQ(result[0], "Hello");
EXPECT_EQ(result[1], "World!");
}
}

0 comments on commit 9e7ec1f

Please sign in to comment.