Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: stringhelper module #765

Merged
merged 2 commits into from
Jul 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions src/common/StringHelper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@
#include <string>
#include <vector>

namespace faker
namespace faker::common
{
std::vector<std::string> StringHelper::split(const std::string& data, const std::string& separator)
std::vector<std::string> split(const std::string& data, const std::string& separator)
{
size_t positionStart = 0;
size_t positionEnd;
Expand All @@ -28,7 +28,7 @@ std::vector<std::string> StringHelper::split(const std::string& data, const std:
return result;
}

std::string StringHelper::joinString(const std::vector<std::string>& data, const std::string& separator)
std::string joinString(const std::vector<std::string>& data, const std::string& separator)
{
switch (data.size())
{
Expand All @@ -51,7 +51,7 @@ std::string StringHelper::joinString(const std::vector<std::string>& data, const
}
}

std::string StringHelper::join(const std::vector<std::string_view>& data, const std::string& separator)
std::string join(const std::vector<std::string_view>& data, const std::string& separator)
{
switch (data.size())
{
Expand All @@ -74,7 +74,7 @@ std::string StringHelper::join(const std::vector<std::string_view>& data, const
}
}

std::string StringHelper::repeat(const std::string& data, int repetition)
std::string repeat(const std::string& data, int repetition)
{
std::string result;

Expand All @@ -88,7 +88,7 @@ std::string StringHelper::repeat(const std::string& data, int repetition)
return result;
}

std::string StringHelper::toLower(const std::string& data)
std::string toLower(const std::string& data)
{
std::string lowerData{data};

Expand All @@ -97,12 +97,12 @@ std::string StringHelper::toLower(const std::string& data)
return lowerData;
}

bool StringHelper::isPunctuation(char c)
bool isPunctuation(char c)
{
return (c == '.' || c == ',' || c == '!' || c == '?' || c == ';' || c == ':');
}

std::string StringHelper::removePunctuation(const std::string& word)
std::string removePunctuation(const std::string& word)
{
std::string result{word};

Expand Down
20 changes: 8 additions & 12 deletions src/common/StringHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
#include <vector>
#include "faker-cxx/Export.h"

namespace faker
namespace faker::common
{
class StringHelper
{
public:
FAKER_CXX_EXPORT static std::vector<std::string> split(const std::string& data, const std::string& separator = " ");
FAKER_CXX_EXPORT static std::string joinString(const std::vector<std::string>& data, const std::string& separator = " ");
FAKER_CXX_EXPORT static std::string join(const std::vector<std::string_view>& data, const std::string& separator = " ");
FAKER_CXX_EXPORT static std::string repeat(const std::string& data, int repetition);
FAKER_CXX_EXPORT static std::string toLower(const std::string& data);
FAKER_CXX_EXPORT static bool isPunctuation(char c);
FAKER_CXX_EXPORT static std::string removePunctuation(const std::string& word);
};
FAKER_CXX_EXPORT std::vector<std::string> split(const std::string& data, const std::string& separator = " ");
FAKER_CXX_EXPORT std::string joinString(const std::vector<std::string>& data, const std::string& separator = " ");
FAKER_CXX_EXPORT std::string join(const std::vector<std::string_view>& data, const std::string& separator = " ");
FAKER_CXX_EXPORT std::string repeat(const std::string& data, int repetition);
FAKER_CXX_EXPORT std::string toLower(const std::string& data);
FAKER_CXX_EXPORT bool isPunctuation(char c);
FAKER_CXX_EXPORT std::string removePunctuation(const std::string& word);
}
6 changes: 3 additions & 3 deletions src/modules/git/Git.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,17 @@ std::string commitDate(unsigned years)
{
const auto date = faker::date::pastDate(int(years));

const auto dateSplit = StringHelper::split(date, "-");
const auto dateSplit = common::split(date, "-");

const auto& year = dateSplit[0];
const auto& month = dateSplit[1];
const auto& rest = dateSplit[2];

const auto restSplit = StringHelper::split(rest, "T");
const auto restSplit = common::split(rest, "T");

const auto& day = restSplit[0];

const auto time = StringHelper::split(restSplit[1], "Z")[0];
const auto time = common::split(restSplit[1], "Z")[0];

int timeZone = number::integer(0, 12);

Expand Down
4 changes: 2 additions & 2 deletions src/modules/hacker/Hacker.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,13 @@ std::string_view ingverb()

std::string phrase()
{
const auto splitRandomPhrase = StringHelper::split(static_cast<std::string>(helper::arrayElement(phrases)));
const auto splitRandomPhrase = common::split(static_cast<std::string>(helper::arrayElement(phrases)));

std::string phrase;

for (const auto& word : splitRandomPhrase)
{
const auto normalizedWord = StringHelper::removePunctuation(word);
const auto normalizedWord = common::removePunctuation(word);

if (normalizedWord == "{abbreviation}")
{
Expand Down
4 changes: 2 additions & 2 deletions src/modules/helper/Helper.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ std::string regexpStyleStringParse(const std::string& input)

int repetitions = number::integer(min, max);
data = data.substr(0, static_cast<unsigned long>(token.position())) +
StringHelper::repeat(token[1], repetitions) +
common::repeat(token[1], repetitions) +
data.substr(static_cast<unsigned long>(token.position() + token.length()));
}

Expand All @@ -93,7 +93,7 @@ std::string regexpStyleStringParse(const std::string& input)
{
int repetitions = std::stoi(token[2]);
data = data.substr(0, static_cast<unsigned long>(token.position())) +
StringHelper::repeat(token[1], repetitions) +
common::repeat(token[1], repetitions) +
data.substr(static_cast<unsigned long>(token.position() + token.length()));
}

Expand Down
4 changes: 2 additions & 2 deletions src/modules/internet/Internet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ std::string ipv6()
ipv6Parts.push_back(string::hexadecimal(4, HexCasing::Lower, HexPrefix::None));
}

return StringHelper::joinString(ipv6Parts, ":");
return common::joinString(ipv6Parts, ":");
}

std::string mac(const std::string& sep)
Expand Down Expand Up @@ -321,7 +321,7 @@ std::string domainName()

std::string domainWord()
{
return StringHelper::toLower(FormatHelper::format("{}-{}", word::adjective(), word::noun()));
return common::toLower(FormatHelper::format("{}-{}", word::adjective(), word::noun()));
}

std::string_view domainSuffix()
Expand Down
8 changes: 4 additions & 4 deletions src/modules/lorem/Lorem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ std::string words(unsigned numberOfWords)
words.push_back(word());
}

return StringHelper::join(words, " ");
return common::join(words, " ");
}

std::string sentence(unsigned minNumberOfWords, unsigned maxNumberOfWords)
Expand All @@ -52,7 +52,7 @@ std::string sentences(unsigned minNumberOfSentences, unsigned maxNumberOfSentenc
sentences.push_back(sentence());
}

return StringHelper::joinString(sentences, " ");
return common::joinString(sentences, " ");
}

std::string slug(unsigned int numberOfWords)
Expand All @@ -65,7 +65,7 @@ std::string slug(unsigned int numberOfWords)
words.push_back(std::string(word()));
}

return StringHelper::joinString(words, "-");
return common::joinString(words, "-");
}

std::string paragraph(unsigned int minNumberOfSentences, unsigned int maxNumberOfSentences)
Expand All @@ -85,7 +85,7 @@ std::string paragraphs(unsigned int minNumberOfParagraphs, unsigned int maxNumbe
paragraphs.push_back(paragraph());
}

return StringHelper::joinString(paragraphs, "\n");
return common::joinString(paragraphs, "\n");
}

}
4 changes: 2 additions & 2 deletions src/modules/system/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ std::string fileName(const FileOptions& options)
randomExtensions.push_back(fileExtension());
}

extensionsStr = "." + StringHelper::joinString(randomExtensions, ".");
extensionsStr = "." + common::joinString(randomExtensions, ".");
}
else
{
Expand All @@ -72,7 +72,7 @@ std::string fileName(const FileOptions& options)
randomExtensions.push_back(fileExtension());
}

extensionsStr = "." + StringHelper::joinString(randomExtensions, ".");
extensionsStr = "." + common::joinString(randomExtensions, ".");
}
}

Expand Down
22 changes: 11 additions & 11 deletions tests/common/StringHelperTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class StringHelperTest : public Test

TEST_F(StringHelperTest, splitStringBySpace)
{
const auto result = StringHelper::split("faker cxx open source");
const auto result = common::split("faker cxx open source");

ASSERT_EQ(result.size(), 4);
ASSERT_EQ(result[0], "faker");
Expand All @@ -25,7 +25,7 @@ TEST_F(StringHelperTest, splitStringBySpace)

TEST_F(StringHelperTest, splitStringByNewLine)
{
const auto result = StringHelper::split("faker\ncxx\nopen\nsource", "\n");
const auto result = common::split("faker\ncxx\nopen\nsource", "\n");

ASSERT_EQ(result.size(), 4);
ASSERT_EQ(result[0], "faker");
Expand All @@ -38,7 +38,7 @@ TEST_F(StringHelperTest, joinStringViewsIntoVectorBySpace)
{
const std::vector<std::string_view> input{"Join", "faker", "development!"};

const auto result = StringHelper::join(input);
const auto result = common::join(input);

ASSERT_EQ(result, "Join faker development!");
}
Expand All @@ -47,7 +47,7 @@ TEST_F(StringHelperTest, joinStringViewsIntoVectorByNewLine)
{
const std::vector<std::string_view> input{"Join", "faker", "development!"};

const auto result = StringHelper::join(input, "\n");
const auto result = common::join(input, "\n");

ASSERT_EQ(result, "Join\nfaker\ndevelopment!");
}
Expand All @@ -56,7 +56,7 @@ TEST_F(StringHelperTest, joinStringsIntoVectorBySpace)
{
const std::vector<std::string> input{"Join", "faker", "development!"};

const auto result = StringHelper::joinString(input);
const auto result = common::joinString(input);

ASSERT_EQ(result, "Join faker development!");
}
Expand All @@ -65,7 +65,7 @@ TEST_F(StringHelperTest, joinStringsIntoVectorByNewLine)
{
const std::vector<std::string> input{"Join", "faker", "development!"};

const auto result = StringHelper::joinString(input, "\n");
const auto result = common::joinString(input, "\n");

ASSERT_EQ(result, "Join\nfaker\ndevelopment!");
}
Expand All @@ -75,7 +75,7 @@ TEST_F(StringHelperTest, repeatString)
const std::string data = "hello ";
const int repetition = 3;

const std::string result = StringHelper::repeat(data, repetition);
const std::string result = common::repeat(data, repetition);

ASSERT_EQ(result, "hello hello hello ");
}
Expand All @@ -84,7 +84,7 @@ TEST_F(StringHelperTest, toLower)
{
const std::string data = "HeLlo!";

const std::string result = StringHelper::toLower(data);
const std::string result = common::toLower(data);

ASSERT_EQ(result, "hello!");
}
Expand All @@ -94,19 +94,19 @@ TEST_F(StringHelperTest, IsPunctuation)
std::string punctuation = ".,;:!?";
for (char c : punctuation)
{
EXPECT_TRUE(StringHelper::isPunctuation(c));
EXPECT_TRUE(common::isPunctuation(c));
}

std::string notPunctuation = "abc123";
for (char c : notPunctuation)
{
EXPECT_FALSE(StringHelper::isPunctuation(c));
EXPECT_FALSE(common::isPunctuation(c));
}
}

TEST_F(StringHelperTest, RemovePunctuation)
{
std::string input = "Hello, World!";
std::string result = StringHelper::removePunctuation(input);
std::string result = common::removePunctuation(input);
EXPECT_EQ(result, "Hello World");
}
Loading
Loading