Skip to content

Commit

Permalink
[PROJECT] sync
Browse files Browse the repository at this point in the history
  • Loading branch information
Martin Onufrak committed Oct 29, 2024
2 parents 18c605f + e730d22 commit 5a13b93
Show file tree
Hide file tree
Showing 12 changed files with 774 additions and 136 deletions.
30 changes: 15 additions & 15 deletions include/faker-cxx/animal.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace faker::animal
* @brief Returns a random species of bear.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
*
* @returns Species of bear.
*
* @code
Expand All @@ -24,7 +24,7 @@ FAKER_CXX_EXPORT std::string_view bear(Locale locale = Locale::en_US);
* @brief Returns a random species of bird.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
*
* @returns Species of bird.
*
* @code
Expand All @@ -37,7 +37,7 @@ FAKER_CXX_EXPORT std::string_view bird(Locale locale = Locale::en_US);
* @brief Returns a random species of cat.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
*
* @returns Species of cat.
*
* @code
Expand All @@ -50,7 +50,7 @@ FAKER_CXX_EXPORT std::string_view cat(Locale locale = Locale::en_US);
* @brief Returns a random species of cetacean.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
*
* @returns Species of cetacean.
*
* @code
Expand All @@ -63,7 +63,7 @@ FAKER_CXX_EXPORT std::string_view cetacean(Locale locale = Locale::en_US);
* @brief Returns a random species of cow.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
*
* @returns Species of cow.
*
* @code
Expand All @@ -76,7 +76,7 @@ FAKER_CXX_EXPORT std::string_view cow(Locale locale = Locale::en_US);
* @brief Returns a random species of crocodilia.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
*
* @returns Species of crocodilia.
*
* @code
Expand All @@ -89,7 +89,7 @@ FAKER_CXX_EXPORT std::string_view crocodile(Locale locale = Locale::en_US);
* @brief Returns a random species of dog.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
*
* @returns Species of dog.
*
* @code
Expand All @@ -102,7 +102,7 @@ FAKER_CXX_EXPORT std::string_view dog(Locale locale = Locale::en_US);
* @brief Returns a random species of fish.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
*
* @returns Species of fish.
*
* @code
Expand All @@ -115,7 +115,7 @@ FAKER_CXX_EXPORT std::string_view fish(Locale locale = Locale::en_US);
* @brief Returns a random species of horse.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
*
* @returns Species of horse.
*
* @code
Expand All @@ -128,7 +128,7 @@ FAKER_CXX_EXPORT std::string_view horse(Locale locale = Locale::en_US);
* @brief Returns a random species of insect.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
*
* @returns Species of insect.
*
* @code
Expand All @@ -141,7 +141,7 @@ FAKER_CXX_EXPORT std::string_view insect(Locale locale = Locale::en_US);
* @brief Returns a random species of lion.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
*
* @returns Species of lion.
*
* @code
Expand All @@ -154,7 +154,7 @@ FAKER_CXX_EXPORT std::string_view lion(Locale locale = Locale::en_US);
* @brief Returns a random species of rabbit.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
*
* @returns Species of rabbit.
*
* @code
Expand All @@ -167,7 +167,7 @@ FAKER_CXX_EXPORT std::string_view rabbit(Locale locale = Locale::en_US);
* @brief Returns a random species of rodent.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
*
* @returns Species of rodent.
*
* @code
Expand All @@ -180,7 +180,7 @@ FAKER_CXX_EXPORT std::string_view rodent(Locale locale = Locale::en_US);
* @brief Returns a random species of snake.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
*
* @returns Species of snake.
*
* @code
Expand All @@ -193,7 +193,7 @@ FAKER_CXX_EXPORT std::string_view snake(Locale locale = Locale::en_US);
* @brief Returns a random type of animal.
*
* @param locale The locale. Defaults to `Locale::en_US`.
*
*
* @returns Type of animal.
*
* @code
Expand Down
4 changes: 2 additions & 2 deletions include/faker-cxx/helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ namespace faker::helper
{

template <typename T>
concept input_range_with_faster_size_compute_than_linear_rng =
std::ranges::input_range<T> && (std::ranges::sized_range<T> || std::ranges::forward_range<T>);
concept input_range_with_faster_size_compute_than_linear_rng = std::ranges::input_range<T> &&
(std::ranges::sized_range<T> || std::ranges::forward_range<T>);

template <input_range_with_faster_size_compute_than_linear_rng Range>
decltype(auto) randomElement(Range&& range)
Expand Down
36 changes: 0 additions & 36 deletions include/faker-cxx/internet.h
Original file line number Diff line number Diff line change
Expand Up @@ -390,39 +390,3 @@ getJWTToken(const std::optional<std::map<std::string, std::string>>& header = st
*/
FAKER_CXX_EXPORT std::string_view getJWTAlgorithm();
}

namespace faker::internet::utility
{
/**
* @brief Encodes a given string to Base64 URL format.
*
* This function takes an input string and converts it into a Base64 URL encoded string.
* Base64 URL encoding is a variant of Base64 encoding that is URL-safe.
*
* @param input The string to be encoded.
*
* @returns A Base64 URL encoded string.
*
* @code
* std::string input = "Hello, World!";
* faker::internet::toBase64UrlEncode(input); // "SGVsbG8sIFdvcmxkIQ"
* @endcode
*/
std::string toBase64UrlEncode(const std::string& input);

/**
* @brief Converts a map of key-value pairs to a JSON string.
*
* This function takes a map where both keys and values are strings and converts it into a JSON formatted string.
*
* @param data The map containing key-value pairs to be converted to JSON.
*
* @returns A JSON formatted string representing the input map.
*
* @code
* std::map<std::string, std::string> data = {{"name", "John"}, {"age", "30"}};
* faker::internet::toJSON(data); // json is now "{\"name\":\"John\",\"age\":\"30\"}"
* @endcode
*/
std::string toJSON(std::map<std::string, std::string>& data);
}
6 changes: 4 additions & 2 deletions include/faker-cxx/random_generator.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
namespace faker
{
template <typename T>
requires std::uniform_random_bit_generator<T>
requires std::uniform_random_bit_generator<T>

class RandomGenerator
{
public:
Expand All @@ -20,7 +21,8 @@ class RandomGenerator
RandomGenerator& operator=(RandomGenerator&&) = default;

template <typename Dist>
requires std::is_invocable_r_v<int, Dist&, T&>
requires std::is_invocable_r_v<int, Dist&, T&>

int operator()(Dist&& dist)
{
return std::forward<Dist>(dist)(generator_);
Expand Down
1 change: 1 addition & 0 deletions src/modules/animal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const struct AnimalDefinition& getAnimalDefinition(Locale locale)
}
}
}

std::string_view bear(Locale locale)
{
const auto& animalDefinition = getAnimalDefinition(locale);
Expand Down
15 changes: 5 additions & 10 deletions src/modules/esport_data.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,11 @@ const auto enUSEvents =
"IEM"});

const auto enUSGames = std::to_array<std::string_view>({"Valorant", "League Of Legend", "Rocket League", "Fifa",
"Overwatch", "CSGO", "Fortnite", "Dota 2",
"Street Fighter,"
"Apex Legends",
"TFT", "PUBG", "Call of Duty"});
"Overwatch", "CSGO", "Fortnite", "Dota 2",
"Street Fighter,"
"Apex Legends",
"TFT", "PUBG", "Call of Duty"});

const EsportDefinition enUSEsportDefinition = {
.players = enUSPlayers,
.teams = enUSTeams,
.leagues = enUSLeagues,
.events = enUSEvents,
.games = enUSGames
};
.players = enUSPlayers, .teams = enUSTeams, .leagues = enUSLeagues, .events = enUSEvents, .games = enUSGames};
}
136 changes: 84 additions & 52 deletions src/modules/internet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,89 @@
#include "internet_data.h"
#include "modules/string_data.h"

namespace faker::internet::utility
{
/**
* @brief Encodes a given string to Base64 URL format.
*
* This function takes an input string and converts it into a Base64 URL encoded string.
* Base64 URL encoding is a variant of Base64 encoding that is URL-safe.
*
* @param input The string to be encoded.
*
* @returns A Base64 URL encoded string.
*
* @code
* std::string input = "Hello, World!";
* faker::internet::toBase64UrlEncode(input); // "SGVsbG8sIFdvcmxkIQ"
* @endcode
*/
std::string toBase64UrlEncode(const std::string& input)
{
const std::string base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string encodedInput;

int value = 0;
int validBits = -6;

for (unsigned char character : input)
{
value = (value << 8) + character;
validBits += 8;
while (validBits >= 0)
{
encodedInput.push_back(base64Chars[(value >> validBits) & 0x3F]);
validBits -= 6;
}
}

if (validBits > -6)
{
encodedInput.push_back(base64Chars[((value << 8) >> validBits) & 0x3F]);
}

while (encodedInput.size() % 4)
{
encodedInput.push_back('=');
}

std::replace(encodedInput.begin(), encodedInput.end(), '+', '-');
std::replace(encodedInput.begin(), encodedInput.end(), '/', '_');
encodedInput = std::regex_replace(encodedInput, std::regex{"=+$"}, "");

return encodedInput;
}

/**
* @brief Converts a map of key-value pairs to a JSON string.
*
* This function takes a map where both keys and values are strings and converts it into a JSON formatted string.
*
* @param data The map containing key-value pairs to be converted to JSON.
*
* @returns A JSON formatted string representing the input map.
*
* @code
* std::map<std::string, std::string> data = {{"name", "John"}, {"age", "30"}};
* faker::internet::toJSON(data); // json is now "{\"name\":\"John\",\"age\":\"30\"}"
* @endcode
*/
std::string toJSON(std::map<std::string, std::string>& data)
{
std::string json = "{";
for (auto it = data.begin(); it != data.end(); ++it)
{
if (it != data.begin())
{
json += ",";
}
json += "\"" + it->first + "\":\"" + it->second + "\"";
}
json += "}";
return json;
}
}

namespace faker::internet
{
namespace
Expand Down Expand Up @@ -352,57 +435,6 @@ std::string anonymousUsername(unsigned maxLength)
return common::format("{}{}", word::adjective(adjectiveLength), word::noun(nounLength));
}

std::string utility::toBase64UrlEncode(const std::string& input)
{
const std::string base64Chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
std::string encodedInput;

int value = 0;
int validBits = -6;

for (unsigned char character : input)
{
value = (value << 8) + character;
validBits += 8;
while (validBits >= 0)
{
encodedInput.push_back(base64Chars[(value >> validBits) & 0x3F]);
validBits -= 6;
}
}

if (validBits > -6)
{
encodedInput.push_back(base64Chars[((value << 8) >> validBits) & 0x3F]);
}

while (encodedInput.size() % 4)
{
encodedInput.push_back('=');
}

std::replace(encodedInput.begin(), encodedInput.end(), '+', '-');
std::replace(encodedInput.begin(), encodedInput.end(), '/', '_');
encodedInput = std::regex_replace(encodedInput, std::regex{"=+$"}, "");

return encodedInput;
}

std::string utility::toJSON(std::map<std::string, std::string>& data)
{
std::string json = "{";
for (auto it = data.begin(); it != data.end(); ++it)
{
if (it != data.begin())
{
json += ",";
}
json += "\"" + it->first + "\":\"" + it->second + "\"";
}
json += "}";
return json;
}

std::string_view getJWTAlgorithm()
{
return helper::randomElement(jwtAlgorithms);
Expand Down Expand Up @@ -450,5 +482,5 @@ std::string getJWTToken(const std::optional<std::map<std::string, std::string>>&

return encodedHeader + "." + encodedPayload + "." + signature;
}

}
Loading

0 comments on commit 5a13b93

Please sign in to comment.