Faker C++
Loading...
Searching...
No Matches
String.h
Go to the documentation of this file.
1#pragma once
2
3#include <limits>
4#include <map>
5#include <optional>
6#include <random>
7#include <set>
8#include <string>
9
10#include "faker-cxx/Export.h"
11#include "RandomGenerator.h"
12#include "types/Hex.h"
13
15{
16enum class StringCasing
17{
18 Mixed,
19 Lower,
20 Upper
21};
22
23struct FAKER_CXX_EXPORT CharCount
24{
25 unsigned int atLeastCount{(std::numeric_limits<unsigned int>::min)()};
26 unsigned int atMostCount{(std::numeric_limits<unsigned int>::max)()};
27};
28
32using GuaranteeMap = std::map<char, CharCount>;
33
50FAKER_CXX_EXPORT bool isValidGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters, unsigned int length);
51
64FAKER_CXX_EXPORT std::string generateAtLeastString(const GuaranteeMap& guarantee);
65
66 // namespace {
67 // std::string generateStringWithGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters,
68 // unsigned int length);
69 // }
81 template <typename T = std::mt19937>
83 {
84 static std::uniform_int_distribution<> dist(0, 15);
85 static std::uniform_int_distribution<> dist2(8, 11);
86 static std::string_view hexCharacters{"0123456789abcdef"};
87
88 std::string result;
89 result.reserve(36);
90
91 for (int i = 0; i < 8; i++)
92 {
93 result.append(1, hexCharacters[static_cast<size_t>(gen(dist))]);
94 }
95 result.append(1, '-');
96
97 for (int i = 0; i < 4; i++)
98 {
99 result.append(1, hexCharacters[static_cast<size_t>(gen(dist))]);
100 }
101 result.append(1, '-');
102
103 result.append(1, '4');
104 for (int i = 0; i < 3; i++)
105 {
106 result.append(1, hexCharacters[static_cast<size_t>(gen(dist))]);
107 }
108 result.append(1, '-');
109
110 result.append(1, hexCharacters[static_cast<size_t>(gen(dist2))]);
111
112 for (int i = 0; i < 3; i++)
113 {
114 result.append(1, hexCharacters[static_cast<size_t>(gen(dist))]);
115 }
116 result.append(1, '-');
117
118 for (int i = 0; i < 12; i++)
119 {
120 result.append(1, hexCharacters[static_cast<size_t>(gen(dist))]);
121 }
122
123 return result;
124 }
125
138 FAKER_CXX_EXPORT std::string sample(unsigned length = 10);
139
153 FAKER_CXX_EXPORT std::string sample(GuaranteeMap&& guarantee, unsigned length = 10);
154
168 FAKER_CXX_EXPORT std::string fromCharacters(const std::string& characters, unsigned length = 1);
169
184 FAKER_CXX_EXPORT std::string fromCharacters(GuaranteeMap&& guarantee, const std::string& characters, unsigned length = 1);
185
202 FAKER_CXX_EXPORT std::string alpha(unsigned length = 1, StringCasing casing = StringCasing::Mixed,
203 const std::string& excludeCharacters = "");
204
220 FAKER_CXX_EXPORT std::string alpha(GuaranteeMap&& guarantee, unsigned length = 1, StringCasing casing = StringCasing::Mixed);
221
238 FAKER_CXX_EXPORT std::string alphanumeric(unsigned length = 1, StringCasing casing = StringCasing::Mixed,
239 const std::string& excludeCharacters = "");
240
256 FAKER_CXX_EXPORT std::string alphanumeric(GuaranteeMap&& guarantee, unsigned length = 1,
258
273 FAKER_CXX_EXPORT std::string numeric(unsigned length = 1, bool allowLeadingZeros = true);
274
290 FAKER_CXX_EXPORT std::string numeric(GuaranteeMap&& guarantee, unsigned length = 1, bool allowLeadingZeros = true);
291
308 FAKER_CXX_EXPORT std::string hexadecimal(unsigned length = 1, HexCasing casing = HexCasing::Lower,
309 HexPrefix prefix = HexPrefix::ZeroX);
310
324 FAKER_CXX_EXPORT std::string hexadecimal(std::optional<int> min = std::nullopt, std::optional<int> max = std::nullopt);
325
343 FAKER_CXX_EXPORT std::string hexadecimal(GuaranteeMap&& guarantee, unsigned length = 1, HexCasing casing = HexCasing::Lower,
344 HexPrefix prefix = HexPrefix::ZeroX);
345
357 FAKER_CXX_EXPORT std::string binary(unsigned length = 1);
358
371 FAKER_CXX_EXPORT std::string binary(GuaranteeMap&& guarantee, unsigned length = 1);
372
384 FAKER_CXX_EXPORT std::string octal(unsigned length = 1);
385
398 FAKER_CXX_EXPORT std::string octal(GuaranteeMap&& guarantee, unsigned length = 1);
399}
Definition RandomGenerator.h:11
Definition String.h:15
FAKER_CXX_EXPORT bool isValidGuarantee(GuaranteeMap &guarantee, std::set< char > &targetCharacters, unsigned int length)
Checks if the given guarantee map is valid for given targetCharacters and length.
std::string uuid(RandomGenerator< T > gen=RandomGenerator< std::mt19937 >{})
Generates an Universally Unique Identifier with version 4.
Definition String.h:82
FAKER_CXX_EXPORT std::string sample(unsigned length=10)
Returns a string containing UTF-16 chars between 33 and 125 (`!` to `}`).
FAKER_CXX_EXPORT std::string hexadecimal(unsigned length=1, HexCasing casing=HexCasing::Lower, HexPrefix prefix=HexPrefix::ZeroX)
Generates a hexadecimal string.
StringCasing
Definition String.h:17
FAKER_CXX_EXPORT std::string binary(unsigned length=1)
Generates a binary string.
FAKER_CXX_EXPORT std::string generateAtLeastString(const GuaranteeMap &guarantee)
Generates the least required string for a given guarantee map.
FAKER_CXX_EXPORT std::string numeric(unsigned length=1, bool allowLeadingZeros=true)
Generates a given length string of digits.
FAKER_CXX_EXPORT std::string alphanumeric(unsigned length=1, StringCasing casing=StringCasing::Mixed, const std::string &excludeCharacters="")
Generates a string consisting of alpha characters and digits.
FAKER_CXX_EXPORT std::string alpha(unsigned length=1, StringCasing casing=StringCasing::Mixed, const std::string &excludeCharacters="")
Generates a string consisting of letters in the English alphabet.
FAKER_CXX_EXPORT std::string fromCharacters(const std::string &characters, unsigned length=1)
Generates a string consisting of given characters.
std::map< char, CharCount > GuaranteeMap
Definition String.h:32
FAKER_CXX_EXPORT std::string octal(unsigned length=1)
Generates an octal string.
Definition String.h:24