Skip to content

Commit

Permalink
Refactor String (#618)
Browse files Browse the repository at this point in the history
* Refactor String data

Signed-off-by: Uilian Ries <[email protected]>

* Add String Data

Signed-off-by: Uilian Ries <[email protected]>

* update tests

Signed-off-by: Uilian Ries <[email protected]>

* move all to string data header

Signed-off-by: Uilian Ries <[email protected]>

* remove string data from cmake file

Signed-off-by: Uilian Ries <[email protected]>

---------

Signed-off-by: Uilian Ries <[email protected]>
  • Loading branch information
uilianries authored May 31, 2024
1 parent dc9ff1c commit 0a07883
Show file tree
Hide file tree
Showing 12 changed files with 140 additions and 124 deletions.
10 changes: 5 additions & 5 deletions include/faker-cxx/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include <set>
#include <sstream>
#include <string>
#include <unordered_map>
#include <map>

#include "RandomGenerator.h"
#include "types/Hex.h"
Expand All @@ -27,16 +27,16 @@ struct CharCount
};

/**
* A std::unordered_map where user can specify the count required for specific chars
* A std::map where user can specify the count required for specific chars
*/
using GuaranteeMap = std::unordered_map<char, CharCount>;
using GuaranteeMap = std::map<char, CharCount>;

/**
* @brief Checks if the given guarantee map is valid for given targetCharacters and length.
*
* @returns a bool.
*
* @param guarantee A std::unordered_map that maps the count range of specific characters required
* @param guarantee A std::map that maps the count range of specific characters required
* @param targetCharacters A std::string consisting of all chars available for that string generating function
* @param length The number of characters to generate.
*
Expand All @@ -54,7 +54,7 @@ bool isValidGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters,
*
* @returns least required std::string
*
* @param guarantee A std::unordered_map<char,CharCount> which stores the guarantee specified by the user
* @param guarantee A std::map<char,CharCount> which stores the guarantee specified by the user
*
* @code
* GuaranteeMap guarantee { {'0',{3,10}},{'a',{6,8}} }; // "000aaaaaa"
Expand Down
10 changes: 5 additions & 5 deletions src/modules/internet/Internet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

#include "common/FormatHelper.h"
#include "common/StringHelper.h"
#include "modules/string/data/Characters.h"
#include "modules/string/StringData.h"
#include "InternetData.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Person.h"
Expand Down Expand Up @@ -126,22 +126,22 @@ std::string Internet::password(int length, const PasswordOptions& options)

if (options.upperLetters)
{
characters += faker::upperCharacters;
characters += faker::string::upperCharacters;
}

if (options.lowerLetters)
{
characters += faker::lowerCharacters;
characters += faker::string::lowerCharacters;
}

if (options.numbers)
{
characters += faker::numericCharacters;
characters += faker::string::numericCharacters;
}

if (options.symbols)
{
characters += faker::symbolCharacters;
characters += faker::string::symbolCharacters;
}

std::string password;
Expand Down
56 changes: 30 additions & 26 deletions src/modules/string/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,44 +4,46 @@
#include <stdexcept>
#include <string>
#include <unordered_map>
#include <map>
#include <algorithm>

#include "data/Characters.h"
#include "StringData.h"
#include "faker-cxx/Helper.h"
#include "faker-cxx/Number.h"

namespace faker
{
namespace
{
const std::unordered_map<StringCasing, std::string> stringCasingToAlphaCharactersMapping{
{StringCasing::Lower, lowerCharacters},
{StringCasing::Upper, upperCharacters},
{StringCasing::Mixed, mixedAlphaCharacters},
const std::map<StringCasing, std::string> stringCasingToAlphaCharactersMapping{
{StringCasing::Lower, string::lowerCharacters},
{StringCasing::Upper, string::upperCharacters},
{StringCasing::Mixed, string::mixedAlphaCharacters},
};
const std::unordered_map<StringCasing, std::string> stringCasingToAlphanumericCharactersMapping{
{StringCasing::Lower, lowerAlphanumericCharacters},
{StringCasing::Upper, upperAlphanumericCharacters},
{StringCasing::Mixed, mixedAlphanumericCharacters},
const std::map<StringCasing, std::string> stringCasingToAlphanumericCharactersMapping{
{StringCasing::Lower, string::lowerAlphanumericCharacters},
{StringCasing::Upper, string::upperAlphanumericCharacters},
{StringCasing::Mixed, string::mixedAlphanumericCharacters},
};
const std::unordered_map<HexCasing, std::string> hexCasingToCharactersMapping{
{HexCasing::Lower, hexLowerCharacters},
{HexCasing::Upper, hexUpperCharacters},
const std::map<HexCasing, std::string> hexCasingToCharactersMapping{
{HexCasing::Lower, string::hexLowerCharacters},
{HexCasing::Upper, string::hexUpperCharacters},
};
const std::unordered_map<HexPrefix, std::string> hexPrefixToStringMapping{
const std::map<HexPrefix, std::string> hexPrefixToStringMapping{
{HexPrefix::ZeroX, "0x"},
{HexPrefix::Hash, "#"},
{HexPrefix::None, ""},
};

const std::unordered_map<StringCasing, std::set<char>> stringCasingToAlphaCharSetMapping{
{StringCasing::Lower, lowerCharSet},
{StringCasing::Upper, upperCharSet},
{StringCasing::Mixed, mixedAlphaCharSet},
const std::map<StringCasing, std::set<char>> stringCasingToAlphaCharSetMapping{
{StringCasing::Lower, string::lowerCharSet},
{StringCasing::Upper, string::upperCharSet},
{StringCasing::Mixed, string::mixedAlphaCharSet},
};

const std::unordered_map<HexCasing, std::set<char>> hexCasingToCharSetMapping{
{HexCasing::Lower, hexLowerCharSet},
{HexCasing::Upper, hexUpperCharSet},
const std::map<HexCasing, std::set<char>> hexCasingToCharSetMapping{
{HexCasing::Lower, string::hexLowerCharSet},
{HexCasing::Upper, string::hexUpperCharSet},
};
}

Expand All @@ -52,8 +54,10 @@ bool isValidGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters,
for (auto& it : guarantee)
{
// if a char in guarantee is not in char set, it is an invalid guarantee
if (targetCharacters.find(it.first) == targetCharacters.end())
if (std::find(targetCharacters.begin(), targetCharacters.end(), it.first) == targetCharacters.end())
{
return false;
}
atleastCountSum += it.second.atLeastCount;
atmostCountSum += it.second.atMostCount;
}
Expand Down Expand Up @@ -131,7 +135,7 @@ std::string String::sample(unsigned int length)

std::string String::sample(GuaranteeMap&& guarantee, unsigned int length)
{
auto targetCharacters = utf16CharSet;
auto targetCharacters = string::utf16CharSet;
// throw if guarantee is invalid
if (!isValidGuarantee(guarantee, targetCharacters, length))
{
Expand Down Expand Up @@ -228,7 +232,7 @@ std::string String::alphanumeric(unsigned int length, StringCasing casing, const

std::string String::alphanumeric(GuaranteeMap&& guarantee, unsigned length, StringCasing casing)
{
auto targetCharacters = digitSet;
auto targetCharacters = string::digitSet;
auto charSet = stringCasingToAlphaCharSetMapping.at(casing);
targetCharacters.merge(charSet);
// throw if guarantee is invalid
Expand All @@ -247,11 +251,11 @@ std::string String::numeric(unsigned int length, bool allowLeadingZeros)
{
if (i == 0 && allowLeadingZeros)
{
alphanumeric += Helper::arrayElement<char>(numericCharacters);
alphanumeric += Helper::arrayElement<char>(string::numericCharacters);
}
else
{
alphanumeric += Helper::arrayElement<char>(numericCharactersWithoutZero);
alphanumeric += Helper::arrayElement<char>(string::numericCharactersWithoutZero);
}
}

Expand All @@ -269,7 +273,7 @@ std::string String::numeric(GuaranteeMap&& guarantee, const unsigned length, boo
throw std::invalid_argument{"Invalid guarantee."};
}
}
auto targetCharacters = digitSet;
auto targetCharacters = string::digitSet;
// throw if guarantee is invalid
if (!isValidGuarantee(guarantee, targetCharacters, length))
{
Expand Down
55 changes: 55 additions & 0 deletions src/modules/string/StringData.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#pragma once

#include <set>
#include <string>

namespace faker::string {

static const std::string upperCharacters{"ABCDEFGHIJKLMNOPQRSTUVWXYZ"};
static const std::string lowerCharacters{"abcdefghijklmnopqrstuvwxyz"};
static const std::string numericCharactersWithoutZero{"123456789"};
static const std::string numericCharacters{"0123456789"};
static const std::string mixedAlphaCharacters{upperCharacters + lowerCharacters};
static const std::string lowerAlphanumericCharacters{lowerCharacters + numericCharacters};
static const std::string upperAlphanumericCharacters{upperCharacters + numericCharacters};
static const std::string mixedAlphanumericCharacters{upperCharacters + lowerCharacters + numericCharacters};
static const std::string hexUpperCharacters{"0123456789ABCDEF"};
static const std::string hexLowerCharacters{"0123456789abcdef"};
static const std::string symbolCharacters{"~`!@#$%^&*()_-+={[}]|:;\"'<,>.?/"};

static const std::set<char> lowerCharSet{
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z',
};

static const std::set<char> upperCharSet{
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M',
'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
};

static const std::set<char> mixedAlphaCharSet{
'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z',
};

static const std::set<char> hexUpperCharSet{
'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
};

static const std::set<char> hexLowerCharSet{
'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
};

static const std::set<char> digitSet{
'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
};

static const std::set<char> utf16CharSet{
'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', '0', '1', '2', '3',
'4', '5', '6', '7', '8', '9', ':', ';', '<', '=', '>', '?', '@', 'A', 'B', 'C', 'D', 'E', 'F',
'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y',
'Z', '[', '\\', ']', '^', '_', '`', 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l',
'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', '{', '|', '}',
};
}
43 changes: 0 additions & 43 deletions src/modules/string/data/Characters.h

This file was deleted.

6 changes: 3 additions & 3 deletions tests/modules/color/ColorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

#include "color/ColorData.h"
#include "common/StringHelper.h"
#include "string/data/Characters.h"
#include "string/StringData.h"

using namespace ::testing;
using namespace faker;
Expand Down Expand Up @@ -73,7 +73,7 @@ TEST_F(ColorTest, shouldGenerateHexColorWithoutAlpha)
ASSERT_EQ(hexadecimal.size(), 7);
ASSERT_EQ(prefix, "#");
ASSERT_TRUE(std::ranges::any_of(hexNumber, [hexNumber](char hexNumberCharacter)
{ return hexLowerCharacters.find(hexNumberCharacter) != std::string::npos; }));
{ return string::hexLowerCharacters.find(hexNumberCharacter) != std::string::npos; }));
}

TEST_F(ColorTest, shouldGenerateHexColorWithAlpha)
Expand All @@ -86,7 +86,7 @@ TEST_F(ColorTest, shouldGenerateHexColorWithAlpha)
ASSERT_EQ(hexadecimal.size(), 10);
ASSERT_EQ(prefix, "0x");
ASSERT_TRUE(std::ranges::any_of(hexNumber, [hexNumber](char hexNumberCharacter)
{ return hexUpperCharacters.find(hexNumberCharacter) != std::string::npos; }));
{ return string::hexUpperCharacters.find(hexNumberCharacter) != std::string::npos; }));
}

TEST_F(ColorTest, shouldGenerateHslWithoutAlpha)
Expand Down
10 changes: 5 additions & 5 deletions tests/modules/commerce/CommerceTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

#include "commerce/CommerceData.h"
#include "common/StringHelper.h"
#include "string/data/Characters.h"
#include "string/StringData.h"

using namespace ::testing;
using namespace faker;
Expand Down Expand Up @@ -47,7 +47,7 @@ TEST_F(CommerceTest, shouldGenerateSku)
ASSERT_TRUE(std::ranges::all_of(sku,
[](char skuCharacter)
{
return std::ranges::any_of(numericCharacters,
return std::ranges::any_of(string::numericCharacters,
[skuCharacter](char numericCharacter)
{ return skuCharacter == numericCharacter; });
}));
Expand All @@ -63,7 +63,7 @@ TEST_F(CommerceTest, shouldGenerateSkuWithSpecifiedLength)
ASSERT_TRUE(std::ranges::all_of(sku,
[](char skuCharacter)
{
return std::ranges::any_of(numericCharacters,
return std::ranges::any_of(string::numericCharacters,
[skuCharacter](char numericCharacter)
{ return skuCharacter == numericCharacter; });
}));
Expand Down Expand Up @@ -279,7 +279,7 @@ TEST_F(CommerceTest, shouldGenerateDiscountCode)
[](char generatedDiscountCodeCharacter)
{
return std::ranges::any_of(
upperAlphanumericCharacters,
string::upperAlphanumericCharacters,
[generatedDiscountCodeCharacter](char upperAlphanumericCharacter)
{ return upperAlphanumericCharacter == generatedDiscountCodeCharacter; });
}));
Expand Down Expand Up @@ -310,7 +310,7 @@ TEST_F(CommerceTest, shouldGenerateOrderNumber)
TEST_F(CommerceTest, shouldGenerateOrderStatus)
{
const auto generatedOrderStatus = Commerce::orderStatus();

ASSERT_TRUE(std::ranges::any_of(orderStatuses, [generatedOrderStatus](const std::string_view& orderStatus)
{ return orderStatus == generatedOrderStatus; }));
}
Expand Down
4 changes: 2 additions & 2 deletions tests/modules/database/DatabaseTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#include "gtest/gtest.h"

#include "database/DatabaseData.h"
#include "string/data/Characters.h"
#include "string/StringData.h"

using namespace ::testing;
using namespace faker;
Expand Down Expand Up @@ -53,5 +53,5 @@ TEST_F(DatabaseTest, shouldGenerateMongoDbObjectId)

ASSERT_EQ(mongoDbObjectId.size(), 24);
ASSERT_TRUE(std::ranges::any_of(mongoDbObjectId, [](char hexNumberCharacter)
{ return hexLowerCharacters.find(hexNumberCharacter) != std::string::npos; }));
{ return string::hexLowerCharacters.find(hexNumberCharacter) != std::string::npos; }));
}
Loading

0 comments on commit 0a07883

Please sign in to comment.