Skip to content

Commit

Permalink
feature: Implemented string guarantee for String::hexadecimal()
Browse files Browse the repository at this point in the history
Overloaded `String::hexadecimal()` so users can pass `GuaranteeMap` as
arguments.
Added std::set<char> for hexadecimal as per need.
  • Loading branch information
braw-lee committed Nov 24, 2023
1 parent adef940 commit 4138ce1
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
20 changes: 20 additions & 0 deletions include/faker-cxx/String.h
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,26 @@ class String
static std::string hexadecimal(unsigned length = 1, HexCasing casing = HexCasing::Lower,
HexPrefix prefix = HexPrefix::ZeroX);

/**
* @brief Generates a hexadecimal string.
*
* @param guarantee A map specifying char count constraints if any
* @param length The number of digits to generate. Defaults to `1`.
* @param casing Casing of the generated string. Defaults to `HexCasing::Lower`.
* @param prefix Prefix for the generated string. Defaults to `0x`.
*
* @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"
* @endcode
*/
static std::string hexadecimal(GuaranteeMap&& guarantee, unsigned length = 1, HexCasing casing = HexCasing::Lower,
HexPrefix prefix = HexPrefix::ZeroX);

/**
* @brief Generates a binary string.
*
Expand Down
17 changes: 17 additions & 0 deletions src/modules/string/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@ const std::map<HexPrefix, std::string> hexPrefixToStringMapping{
{HexPrefix::Hash, "#"},
{HexPrefix::None, ""},
};

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

bool isValidGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters, unsigned int length)
Expand Down Expand Up @@ -206,6 +211,18 @@ std::string String::hexadecimal(unsigned int length, HexCasing casing, HexPrefix
return hexadecimal;
}

std::string String::hexadecimal(GuaranteeMap&& guarantee, unsigned int length, HexCasing casing, HexPrefix prefix)
{
std::set<char> targetCharacters = hexCasingToCharSetMapping.at(casing);
// throw if guarantee is invalid
if (!isValidGuarantee(guarantee, targetCharacters, length))
{
throw std::invalid_argument{"Invalid guarantee."};
}
const auto& hexadecimalPrefix = hexPrefixToStringMapping.at(prefix);
return hexadecimalPrefix + generateStringWithGuarantee(guarantee, targetCharacters, length);
}

std::string String::binary(unsigned int length)
{
std::string binaryNumber;
Expand Down
4 changes: 4 additions & 0 deletions src/modules/string/data/Characters.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <set>
#include <string>

namespace faker
Expand All @@ -15,4 +16,7 @@ const std::string mixedAlphanumericCharacters = upperCharacters + lowerCharacter
const std::string hexUpperCharacters = "0123456789ABCDEF";
const std::string hexLowerCharacters = "0123456789abcdef";
const std::string symbolCharacters = "~`!@#$%^&*()_-+={[}]|:;\"'<,>.?/";

const std::set<char> hexUpperCharSet{'A', 'B', 'C', 'D', 'E', 'F', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
const std::set<char> hexLowerCharSet{'a', 'b', 'c', 'd', 'e', 'f', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};
}

0 comments on commit 4138ce1

Please sign in to comment.