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>::min)()};
25 unsigned int atMostCount{(std::numeric_limits<unsigned>::max)()};
26};
27
31using GuaranteeMap = std::map<char, CharCount>;
32
49FAKER_CXX_EXPORT bool isValidGuarantee(GuaranteeMap& guarantee, std::set<char>& targetCharacters, unsigned 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
163FAKER_CXX_EXPORT std::string symbol(unsigned minLength = 1, unsigned maxLength = 10);
164
178FAKER_CXX_EXPORT std::string fromCharacters(const std::string& characters, unsigned length = 1);
179
194FAKER_CXX_EXPORT std::string fromCharacters(GuaranteeMap&& guarantee, const std::string& characters,
195 unsigned length = 1);
196
212FAKER_CXX_EXPORT std::string alpha(unsigned length = 1, StringCasing casing = StringCasing::Mixed,
213 const std::string& excludeCharacters = "");
214
230FAKER_CXX_EXPORT std::string alpha(GuaranteeMap&& guarantee, unsigned length = 1,
232
249FAKER_CXX_EXPORT std::string alphanumeric(unsigned length = 1, StringCasing casing = StringCasing::Mixed,
250 const std::string& excludeCharacters = "");
251
267FAKER_CXX_EXPORT std::string alphanumeric(GuaranteeMap&& guarantee, unsigned length = 1,
269
284FAKER_CXX_EXPORT std::string numeric(unsigned length = 1, bool allowLeadingZeros = true);
285
301FAKER_CXX_EXPORT std::string numeric(GuaranteeMap&& guarantee, unsigned length = 1, bool allowLeadingZeros = true);
302
318FAKER_CXX_EXPORT std::string nanoId(int length);
319
333FAKER_CXX_EXPORT std::string nanoId();
334
352FAKER_CXX_EXPORT std::string nanoId(int minLength, int maxLength);
353}
Definition random_generator.h:12
Definition string.h:14
FAKER_CXX_EXPORT std::string nanoId()
Generates a unique, URL-safe string identifier of default length.
FAKER_CXX_EXPORT std::string symbol(unsigned minLength=1, unsigned maxLength=10)
Returns a string containing "~`!@#$%^&*()_-+={[}]|:;\"'<,>.?/".
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 bool isValidGuarantee(GuaranteeMap &guarantee, std::set< char > &targetCharacters, unsigned length)
Checks if the given guarantee map is valid for given targetCharacters and length.
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