Skip to content

Commit

Permalink
refactor: string module (#743)
Browse files Browse the repository at this point in the history
- string module migration from class to functions within string namespace

- modified reference to functions and variables from string module in all dependent modules

Signed-off-by: Guru Mehar Rachaputi <gurumeharrachaputi@gmail.com>
  • Loading branch information
00thirdeye00 committed Jun 27, 2024
1 parent 90bf7bc commit ac53456
Show file tree
Hide file tree
Showing 21 changed files with 504 additions and 507 deletions.
2 changes: 1 addition & 1 deletion examples/basic/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

int main()
{
const auto id = faker::String::uuid();
const auto id = faker::string::uuid();
const auto email = faker::internet::email();
const auto password = faker::internet::password();
const auto city = faker::location::city();
Expand Down
136 changes: 66 additions & 70 deletions include/faker-cxx/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#include "RandomGenerator.h"
#include "types/Hex.h"

namespace faker
namespace faker::string
{
enum class StringCasing
{
Expand Down Expand Up @@ -43,7 +43,7 @@ using GuaranteeMap = std::map<char, CharCount>;
* GuaranteeMap guarantee = {{'0',{5,10}},{'1',{6,10}}};
* std::string targetCharacters = "01";
* unsigned int length = 10;
* faker::isValidGuarantee(guarantee,targetCharacters,length) // false
* faker::string::isValidGuarantee(guarantee,targetCharacters,length) // false
* @endcode
*/
bool isValidGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters, unsigned int length);
Expand All @@ -57,18 +57,15 @@ bool isValidGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters,
*
* @code
* GuaranteeMap guarantee { {'0',{3,10}},{'a',{6,8}} }; // "000aaaaaa"
* faker::generateAtLeastString(guarantee);
* faker::string::generateAtLeastString(guarantee);
* @endcode
*/
std::string generateAtLeastString(const GuaranteeMap& guarantee);

class String
{
private:
static std::string generateStringWithGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters,
unsigned int length);

public:
// namespace {
// std::string generateStringWithGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters,
// unsigned int length);
// }
/**
* @brief Generates an Universally Unique Identifier with version 4.
*
Expand All @@ -77,11 +74,11 @@ class String
* @param gen A random number generator (type RandomGenerator)
*
* @code
* String::uuid() // "27666229-cedb-4a45-8018-98b1e1d921e2"
* string::uuid() // "27666229-cedb-4a45-8018-98b1e1d921e2"
* @endcode
*/
template <typename T = std::mt19937>
static std::string uuid(RandomGenerator<T> gen = RandomGenerator<std::mt19937>{})
std::string uuid(RandomGenerator<T> gen = RandomGenerator<std::mt19937>{})
{
static std::uniform_int_distribution<> dist(0, 15);
static std::uniform_int_distribution<> dist2(8, 11);
Expand Down Expand Up @@ -133,11 +130,11 @@ class String
* @returns Sample string.
*
* @code
* String::sample() // "Zo!.:*e>wR"
* String::sample(5) // "6Bye8"
* string::sample() // "Zo!.:*e>wR"
* string::sample(5) // "6Bye8"
* @endcode
*/
static std::string sample(unsigned length = 10);
std::string sample(unsigned length = 10);

/**
* @brief Returns a string containing UTF-16 chars between 33 and 125 (`!` to `}`).
Expand All @@ -148,11 +145,11 @@ class String
* @returns Sample string.
*
* @code
* String::sample({}) // "Zo!.:*e>wR"
* String::sample({{'|' ,{2,2}},{'^',{0,0}},{':',{1,8}}}, 8) // "|6Bye8:|"
* string::sample({}) // "Zo!.:*e>wR"
* string::sample({{'|' ,{2,2}},{'^',{0,0}},{':',{1,8}}}, 8) // "|6Bye8:|"
* @endcode
*/
static std::string sample(GuaranteeMap&& guarantee, unsigned length = 10);
std::string sample(GuaranteeMap&& guarantee, unsigned length = 10);

/**
* @brief Generates a string consisting of given characters.
Expand All @@ -163,11 +160,11 @@ class String
* @returns String from characters.
*
* @code
* String::fromCharacters("abc") // "b"
* String::fromCharacters("qwerty", 5) // "qrwqt"
* string::fromCharacters("abc") // "b"
* string::fromCharacters("qwerty", 5) // "qrwqt"
* @endcode
*/
static std::string fromCharacters(const std::string& characters, unsigned length = 1);
std::string fromCharacters(const std::string& characters, unsigned length = 1);

/**
* @brief Generates a string consisting of given characters.
Expand All @@ -179,11 +176,11 @@ class String
* @returns String from characters.
*
* @code
* String::fromCharacters({}, "abc") // "b"
* String::fromCharacters({{'q',{2,2}},{'e',{1,5}}}, "qwerty", 8) // "yqreqety"
* string::fromCharacters({}, "abc") // "b"
* string::fromCharacters({{'q',{2,2}},{'e',{1,5}}}, "qwerty", 8) // "yqreqety"
* @endcode
*/
static std::string fromCharacters(GuaranteeMap&& guarantee, const std::string& characters, unsigned length = 1);
std::string fromCharacters(GuaranteeMap&& guarantee, const std::string& characters, unsigned length = 1);

/**
* @brief Generates a string consisting of letters in the English alphabet.
Expand All @@ -196,12 +193,12 @@ class String
* @returns Alpha string.
*
* @code
* String::alpha() // "b"
* String::alpha(5, StringCasing::Upper) // "DTCIC"
* String::alpha(4, StringCasing::Lower) // "brpt"
* string::alpha() // "b"
* string::alpha(5, StringCasing::Upper) // "DTCIC"
* string::alpha(4, StringCasing::Lower) // "brpt"
* @endcode
*/
static std::string alpha(unsigned length = 1, StringCasing casing = StringCasing::Mixed,
std::string alpha(unsigned length = 1, StringCasing casing = StringCasing::Mixed,
const std::string& excludeCharacters = "");

/**
Expand All @@ -214,12 +211,12 @@ class String
* @returns Alpha string.
*
* @code
* String::alpha({}) // "b"
* String::alpha({{'A',{2,2}}, 5, StringCasing::Upper) // "DACAC"
* String::alpha({{'a',{0,0}},{'b',{3,3}},{'c', {0,2}}}, 10, StringCasing::Lower) // "bicnmmkbbp"
* string::alpha({}) // "b"
* string::alpha({{'A',{2,2}}, 5, StringCasing::Upper) // "DACAC"
* string::alpha({{'a',{0,0}},{'b',{3,3}},{'c', {0,2}}}, 10, StringCasing::Lower) // "bicnmmkbbp"
* @endcode
*/
static std::string alpha(GuaranteeMap&& guarantee, unsigned length = 1, StringCasing casing = StringCasing::Mixed);
std::string alpha(GuaranteeMap&& guarantee, unsigned length = 1, StringCasing casing = StringCasing::Mixed);

/**
* @brief Generates a string consisting of alpha characters and digits.
Expand All @@ -232,12 +229,12 @@ class String
* @returns Alphanumeric string.
*
* @code
* String::alphanumeric() // "4"
* String::alphanumeric(5, StringCasing::Upper) // "3e5V7"
* String::alphanumeric(4, StringCasing::Lower) // "1nrq"
* string::alphanumeric() // "4"
* string::alphanumeric(5, StringCasing::Upper) // "3e5V7"
* string::alphanumeric(4, StringCasing::Lower) // "1nrq"
* @endcode
*/
static std::string alphanumeric(unsigned length = 1, StringCasing casing = StringCasing::Mixed,
std::string alphanumeric(unsigned length = 1, StringCasing casing = StringCasing::Mixed,
const std::string& excludeCharacters = "");

/**
Expand All @@ -250,12 +247,12 @@ class String
* @returns Alphanumeric string.
*
* @code
* String::alphanumeric({}) // "4"
* String::alphanumeric({{'A', {3,6}},{'1', {1,1}}, 5, StringCasing::Upper) // "1EAAA"
* String::alphanumeric({{'a',{0,2}},{'2',{0,3}},{'z',{3,5}}}, 10, StringCasing::Lower) // "z1naazrqz0"
* string::alphanumeric({}) // "4"
* string::alphanumeric({{'A', {3,6}},{'1', {1,1}}, 5, StringCasing::Upper) // "1EAAA"
* string::alphanumeric({{'a',{0,2}},{'2',{0,3}},{'z',{3,5}}}, 10, StringCasing::Lower) // "z1naazrqz0"
* @endcode
*/
static std::string alphanumeric(GuaranteeMap&& guarantee, unsigned length = 1,
std::string alphanumeric(GuaranteeMap&& guarantee, unsigned length = 1,
StringCasing casing = StringCasing::Mixed);

/**
Expand All @@ -267,12 +264,12 @@ class String
* @returns Numeric string.
*
* @code
* String::numeric() // "1"
* String::numeric(6) // "035742"
* String::numeric(6, false) // "254429"
* string::numeric() // "1"
* string::numeric(6) // "035742"
* string::numeric(6, false) // "254429"
* @endcode
*/
static std::string numeric(unsigned length = 1, bool allowLeadingZeros = true);
std::string numeric(unsigned length = 1, bool allowLeadingZeros = true);

/**
* @brief Generates a given length string of digits.
Expand All @@ -284,12 +281,12 @@ class String
* @returns Numeric string.
*
* @code
* String::numeric({}) // "1"
* String::numeric({'5',{3,6}}, 6) // "055542"
* String::numeric({'0',{0,0}}, {'4',{1,1}}, 6, false) // "854829"
* string::numeric({}) // "1"
* string::numeric({'5',{3,6}}, 6) // "055542"
* string::numeric({'0',{0,0}}, {'4',{1,1}}, 6, false) // "854829"
* @endcode
*/
static std::string numeric(GuaranteeMap&& guarantee, unsigned length = 1, bool allowLeadingZeros = true);
std::string numeric(GuaranteeMap&& guarantee, unsigned length = 1, bool allowLeadingZeros = true);

/**
* @brief Generates a hexadecimal string.
Expand All @@ -301,13 +298,13 @@ class String
* @returns Hexadecimal string.
*
* @code
* String::hexadecimal() // "0xb"
* String::hexadecimal(10) // "0xae13d044cb"
* String::hexadecimal(6, HexCasing::Upper, HexPrefix::Hash) // "#E3F380"
* String::hexadecimal(6, HexCasing::Lower, HexPrefix::None) // "e3f380"
* string::hexadecimal() // "0xb"
* string::hexadecimal(10) // "0xae13d044cb"
* string::hexadecimal(6, HexCasing::Upper, HexPrefix::Hash) // "#E3F380"
* string::hexadecimal(6, HexCasing::Lower, HexPrefix::None) // "e3f380"
* @endcode
*/
static std::string hexadecimal(unsigned length = 1, HexCasing casing = HexCasing::Lower,
std::string hexadecimal(unsigned length = 1, HexCasing casing = HexCasing::Lower,
HexPrefix prefix = HexPrefix::ZeroX);

/**
Expand All @@ -319,11 +316,11 @@ class String
* @return A lowercase hexadecimal number.
*
* @code
* String::hexadecimal() // "b"
* String::hexadecimal(0, 255) // "9d"
* string::hexadecimal() // "b"
* string::hexadecimal(0, 255) // "9d"
* @endcode
*/
static std::string hexadecimal(std::optional<int> min = std::nullopt, std::optional<int> max = std::nullopt);
std::string hexadecimal(std::optional<int> min = std::nullopt, std::optional<int> max = std::nullopt);

/**
* @brief Generates a hexadecimal string.
Expand All @@ -336,13 +333,13 @@ class String
* @returns Hexadecimal string.
*
* @code
* String::hexadecimal({}) // "0xb"
* String::hexadecimal({'a',{2,2}}, 10) // "0xae13d04acb"
* String::hexadecimal({'F', {2,4}}, 6, HexCasing::Upper, HexPrefix::Hash) // "#E3FFF0"
* String::hexadecimal({'1', {1,4}, {'2', {1, 4}, {'c', {1,1}}, 6, HexCasing::Lower, HexPrefix::None) // "121a1c"
* string::hexadecimal({}) // "0xb"
* string::hexadecimal({'a',{2,2}}, 10) // "0xae13d04acb"
* string::hexadecimal({'F', {2,4}}, 6, HexCasing::Upper, HexPrefix::Hash) // "#E3FFF0"
* string::hexadecimal({'1', {1,4}, {'2', {1, 4}, {'c', {1,1}}, 6, HexCasing::Lower, HexPrefix::None) // "121a1c"
* @endcode
*/
static std::string hexadecimal(GuaranteeMap&& guarantee, unsigned length = 1, HexCasing casing = HexCasing::Lower,
std::string hexadecimal(GuaranteeMap&& guarantee, unsigned length = 1, HexCasing casing = HexCasing::Lower,
HexPrefix prefix = HexPrefix::ZeroX);

/**
Expand All @@ -353,10 +350,10 @@ class String
* @returns Binary string.
*
* @code
* String::binary(8) // "0b01110101"
* string::binary(8) // "0b01110101"
* @endcode
*/
static std::string binary(unsigned length = 1);
std::string binary(unsigned length = 1);

/**
* @brief Generates a binary string.
Expand All @@ -367,10 +364,10 @@ class String
* @returns Binary string.
*
* @code
* String::binary({'1',{7,8}}, 8) // "0b11110111"
* string::binary({'1',{7,8}}, 8) // "0b11110111"
* @endcode
*/
static std::string binary(GuaranteeMap&& guarantee, unsigned length = 1);
std::string binary(GuaranteeMap&& guarantee, unsigned length = 1);

/**
* @brief Generates an octal string.
Expand All @@ -380,10 +377,10 @@ class String
* @returns Octal string.
*
* @code
* String::octal(8) // "0o52561721"
* string::octal(8) // "0o52561721"
* @endcode
*/
static std::string octal(unsigned length = 1);
std::string octal(unsigned length = 1);

/**
* @brief Generates an octal string.
Expand All @@ -394,9 +391,8 @@ class String
* @returns Octal string.
*
* @code
* String::octal({'4',{4,5}}, 8) // "0o42444041"
* string::octal({'4',{4,5}}, 8) // "0o42444041"
* @endcode
*/
static std::string octal(GuaranteeMap&& guarantee, unsigned length = 1);
};
std::string octal(GuaranteeMap&& guarantee, unsigned length = 1);
}
12 changes: 6 additions & 6 deletions src/modules/airline/Airline.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,29 +40,29 @@ std::string recordLocator(bool allowNumerics)
{
if (allowNumerics)
{
return String::alphanumeric(6, StringCasing::Upper);
return string::alphanumeric(6, string::StringCasing::Upper);
}

return String::alpha(6, StringCasing::Upper);
return string::alpha(6, string::StringCasing::Upper);
}

std::string flightNumber(bool addLeadingZeros, unsigned int length)
{
if (addLeadingZeros)
{
return String::numeric(length, true);
return string::numeric(length, true);
}

return String::numeric(length, false);
return string::numeric(length, false);
}

std::string flightNumberByRange(bool addLeadingZeros, Range length)
{
if (addLeadingZeros)
{
return String::numeric(number::integer(length.min, length.max), true);
return string::numeric(number::integer(length.min, length.max), true);
}

return String::numeric(number::integer(length.min, length.max), false);
return string::numeric(number::integer(length.min, length.max), false);
}
}
2 changes: 1 addition & 1 deletion src/modules/color/Color.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ std::string rgb(bool includeAlpha)

std::string hex(HexCasing casing, HexPrefix prefix, bool includeAlpha)
{
return String::hexadecimal(includeAlpha ? 8 : 6, casing, prefix);
return string::hexadecimal(includeAlpha ? 8 : 6, casing, prefix);
}

std::string hsl(bool include_alpha)
Expand Down
Loading

0 comments on commit ac53456

Please sign in to comment.