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 "random_generator.h"
12
14{
15enum class StringCasing
16{
17 Mixed,
18 Lower,
19 Upper
20};
21
22struct FAKER_CXX_EXPORT CharCount
23{
24 unsigned int atLeastCount{(std::numeric_limits<unsigned int>::min)()};
25 unsigned int atMostCount{(std::numeric_limits<unsigned int>::max)()};
26};
27
31using GuaranteeMap = std::map<char, CharCount>;
32
49FAKER_CXX_EXPORT bool isValidGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters, unsigned int length);
50
63FAKER_CXX_EXPORT std::string generateAtLeastString(const GuaranteeMap& guarantee);
64
76template <typename T = std::mt19937>
78{
79 static std::uniform_int_distribution<> dist(0, 15);
80 static std::uniform_int_distribution<> dist2(8, 11);
81 static std::string_view hexCharacters{"0123456789abcdef"};
82
83 std::string result;
84 result.reserve(36);
85
86 for (int i = 0; i < 8; i++)
87 {
88 result.append(1, hexCharacters[static_cast<size_t>(gen(dist))]);
89 }
90 result.append(1, '-');
91
92 for (int i = 0; i < 4; i++)
93 {
94 result.append(1, hexCharacters[static_cast<size_t>(gen(dist))]);
95 }
96 result.append(1, '-');
97
98 result.append(1, '4');
99 for (int i = 0; i < 3; i++)
100 {
101 result.append(1, hexCharacters[static_cast<size_t>(gen(dist))]);
102 }
103 result.append(1, '-');
104
105 result.append(1, hexCharacters[static_cast<size_t>(gen(dist2))]);
106
107 for (int i = 0; i < 3; i++)
108 {
109 result.append(1, hexCharacters[static_cast<size_t>(gen(dist))]);
110 }
111 result.append(1, '-');
112
113 for (int i = 0; i < 12; i++)
114 {
115 result.append(1, hexCharacters[static_cast<size_t>(gen(dist))]);
116 }
117
118 return result;
119}
120
133FAKER_CXX_EXPORT std::string sample(unsigned length = 10);
134
148FAKER_CXX_EXPORT std::string sample(GuaranteeMap&& guarantee, unsigned length = 10);
149
162FAKER_CXX_EXPORT std::string symbol(unsigned length = 10);
163
177FAKER_CXX_EXPORT std::string symbol(unsigned int minLength, unsigned int maxLength);
178
192FAKER_CXX_EXPORT std::string fromCharacters(const std::string& characters, unsigned length = 1);
193
208FAKER_CXX_EXPORT std::string fromCharacters(GuaranteeMap&& guarantee, const std::string& characters,
209 unsigned length = 1);
210
227FAKER_CXX_EXPORT std::string alpha(unsigned length = 1, StringCasing casing = StringCasing::Mixed,
228 const std::string& excludeCharacters = "");
229
245FAKER_CXX_EXPORT std::string alpha(GuaranteeMap&& guarantee, unsigned length = 1,
247
264FAKER_CXX_EXPORT std::string alphanumeric(unsigned length = 1, StringCasing casing = StringCasing::Mixed,
265 const std::string& excludeCharacters = "");
266
282FAKER_CXX_EXPORT std::string alphanumeric(GuaranteeMap&& guarantee, unsigned length = 1,
284
299FAKER_CXX_EXPORT std::string numeric(unsigned length = 1, bool allowLeadingZeros = true);
300
316FAKER_CXX_EXPORT std::string numeric(GuaranteeMap&& guarantee, unsigned length = 1, bool allowLeadingZeros = true);
317}
Definition random_generator.h:11
Definition string.h:14
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:77
FAKER_CXX_EXPORT std::string sample(unsigned length=10)
Returns a string containing UTF-16 chars between 33 and 125 (`!` to `}`).
StringCasing
Definition string.h:16
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 symbol(unsigned length=10)
Returns a string containing "~`!@#$%^&*()_-+={[}]|:;\"'<,>.?/".
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:31
Definition string.h:23