Skip to content

Commit

Permalink
add octal and binary string generation (#75)
Browse files Browse the repository at this point in the history
  • Loading branch information
cieslarmichal authored Aug 11, 2023
1 parent 22583b7 commit fe90cf0
Show file tree
Hide file tree
Showing 40 changed files with 6,781 additions and 6,663 deletions.
1 change: 1 addition & 0 deletions include/faker-cxx/Datatype.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once

#include <optional>

namespace faker
Expand Down
6 changes: 3 additions & 3 deletions include/faker-cxx/Finance.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
#include <optional>
#include <string>

#include "faker-cxx/types/BicCountry.h"
#include "faker-cxx/types/IbanCountry.h"
#include "faker-cxx/types/Precision.h"
#include "types/BicCountry.h"
#include "types/Country.h"
#include "types/IbanCountry.h"
#include "types/Precision.h"

namespace faker
{
Expand Down
33 changes: 15 additions & 18 deletions include/faker-cxx/Helper.h
Original file line number Diff line number Diff line change
@@ -1,18 +1,10 @@
#pragma once

#include <chrono>
#include <random>
#include <regex>
#include <span>
#include <string_view>
#include <vector>
#include <unordered_map>
#include <functional>
#include <span>

#include "../../src/common/LuhnCheck.h"
#include "../src/common/StringHelper.h"
#include "Number.h"
#include "Datatype.h"
#include "Number.h"

namespace faker
{
Expand Down Expand Up @@ -79,7 +71,7 @@ class Helper
* Helper::replaceSymbolWithNumber("Your pin is: !####") // "29841"
* @endcode
*/
static std::string replaceSymbolWithNumber(std::string str, const char & symbol = '#');
static std::string replaceSymbolWithNumber(std::string str, const char& symbol = '#');

/**
* @brief Returns credit card schema with replaced symbols and patterns in a credit card including Luhn checksum
Expand All @@ -97,7 +89,8 @@ class Helper
* Helper::replaceCreditCardSymbols("1234-[4-9]-##!!-L") // "1234-9-5298-2"
* @endcode
*/
static std::string replaceCreditCardSymbols(const std::string& inputString = "6453-####-####-####-###L", char symbol = '#');
static std::string replaceCreditCardSymbols(const std::string& inputString = "6453-####-####-####-###L",
char symbol = '#');

/**
* @brief Returns the replaced regex-like expression in the string with matching values.
Expand All @@ -121,7 +114,7 @@ class Helper
*/
static std::string regexpStyleStringParse(const std::string& input);

/**
/**
* @brief Returns a random key from given object.
*
* @tparam T The type of the object to select from.
Expand All @@ -144,12 +137,14 @@ class Helper
template <typename T>
static typename T::key_type objectKey(const T& object)
{
if (object.empty()) {
if (object.empty())
{
throw std::runtime_error("Object is empty.");
}

std::vector<typename T::key_type> keys;
for (const auto& entry : object) {
for (const auto& entry : object)
{
keys.push_back(entry.first);
}

Expand All @@ -158,7 +153,7 @@ class Helper

/**
* @brief Returns the result of the callback if the probability check was successful, otherwise empty string.
*
*
*
* @tparam TResult The type of result of the given callback.
*
Expand All @@ -173,8 +168,10 @@ class Helper
* @endcode
*/
template <typename TResult>
static TResult maybe(std::function<TResult()> callback, double probability = 0.5) {
if (Datatype::boolean(probability)) {
static TResult maybe(std::function<TResult()> callback, double probability = 0.5)
{
if (Datatype::boolean(probability))
{
return callback();
}
return TResult();
Expand Down
3 changes: 1 addition & 2 deletions include/faker-cxx/Internet.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
#include "types/EmojiType.h"
#include "types/Ipv4Address.h"
#include "types/Ipv4Class.h"
#include "faker-cxx/String.h"

namespace faker
{
Expand Down Expand Up @@ -200,7 +199,7 @@ class Internet
*/
static std::string ipv4(const IPv4Address& baseIpv4Address, const IPv4Address& generationMask);

/**
/**
* @brief Returns a generated random mac address.
*
* @param sep Separator to use. Defaults to ":". Also can be "-" or "".
Expand Down
12 changes: 5 additions & 7 deletions include/faker-cxx/Number.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@

#include <algorithm>
#include <concepts>
#include <optional>
#include <random>
#include <sstream>
#include <stdexcept>
#include <string>
#include <type_traits>
#include <utility>
#include <optional>
#include <sstream>

namespace faker
{
Expand Down Expand Up @@ -359,26 +359,24 @@ class Number
return distribution(pseudoRandomGenerator);
}

/**
/**
* @brief Returns a lowercase hexadecimal number.
*
* @param min Optional parameter for lower bound of generated number.
* @param max Optional parameter for upper bound of generated number.
*
* @return A lowercase hexadecimal number.
*
* @example
* @code
* Number::hex() // "b"
* Number::hex(0, 255) // "9d"
*
* @endcode
*/
static std::string hex(std::optional<int> min = std::nullopt, std::optional<int> max = std::nullopt);


private:
static std::random_device randomDevice;
static std::mt19937 pseudoRandomGenerator;
static std::string convertToHex(int number);

};
}
22 changes: 11 additions & 11 deletions include/faker-cxx/Phone.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include <optional>
#include <string>

#include "faker-cxx/types/PhoneNumberCountryFormat.h"
#include "types/PhoneNumberCountryFormat.h"

namespace faker
{
Expand All @@ -24,9 +24,9 @@ class Phone
* Phone::number("+48 91 ### ## ##") // "+48 91 463 61 70"
* @endcode
*/
static std::string number(std::optional<std::string> = std::nullopt);
static std::string number(std::optional<std::string> = std::nullopt);

/**
/**
* @brief Returns a random phone number based on country phone number template.
*
* @param format Enum country format, more details in PhoneNumberCountryFormat.h.
Expand All @@ -36,22 +36,22 @@ class Phone
* @code
* Phone::number(PhoneNumberCountryFormat::Usa) // "+1 (395) 714-1494"
* @endcode
*/
static std::string number(PhoneNumberCountryFormat format);
*/
static std::string number(PhoneNumberCountryFormat format);

/**
/**
* @brief Returns IMEI number.
*
* @returns IMEI number.
*
* @code
* Phone::imei() // "13-850175-913761-7"
* @endcode
*/
static std::string imei();
*/
static std::string imei();

private:
static std::map<PhoneNumberCountryFormat, std::string> createPhoneNumberFormatMap();
static std::map<PhoneNumberCountryFormat, std::string> phoneNumberFormatMap;
private:
static std::map<PhoneNumberCountryFormat, std::string> createPhoneNumberFormatMap();
static std::map<PhoneNumberCountryFormat, std::string> phoneNumberFormatMap;
};
}
55 changes: 55 additions & 0 deletions include/faker-cxx/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,35 @@ class String
*/
static std::string uuid();

/**
* @brief Returns a string containing UTF-16 chars between 33 and 125 (`!` to `}`).
*
* @param length The number of characters to generate. Defaults to `10`.
*
* @returns Sample string.
*
* @code
* String::sample() // "Zo!.:*e>wR"
* String::sample(5) // "6Bye8"
* @endcode
*/
static std::string sample(unsigned length = 10);

/**
* @brief Generates a string consisting of given characters.
*
* @param characters The characters to generate string with.
* @param length The number of characters to generate. Defaults to `1`.
*
* @returns String from characters.
*
* @code
* String::fromCharacters("abc") // "b"
* String::fromCharacters("qwerty", 5) // "qrwqt"
* @endcode
*/
static std::string fromCharacters(const std::string& characters, unsigned length = 1);

/**
* @brief Generates a string consisting of letters in the English alphabet.
*
Expand Down Expand Up @@ -92,5 +121,31 @@ class String
*/
static std::string hexadecimal(unsigned length = 1, HexCasing casing = HexCasing::Lower,
HexPrefix prefix = HexPrefix::ZeroX);

/**
* @brief Generates a binary string.
*
* @param length The number of digits to generate. Defaults to `1`.
*
* @returns Binary string.
*
* @code
* String::binary(8) // "0b01110101"
* @endcode
*/
static std::string binary(unsigned length = 1);

/**
* @brief Generates an octal string.
*
* @param length The number of digits to generate. Defaults to `1`.
*
* @returns Octal string.
*
* @code
* String::octal(8) // "0o52561721"
* @endcode
*/
static std::string octal(unsigned length = 1);
};
}
Loading

0 comments on commit fe90cf0

Please sign in to comment.