Faker C++
Loading...
Searching...
No Matches
word.h
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <optional>
5#include <string>
6#include <string_view>
7
8#include "faker-cxx/export.h"
9#include "faker-cxx/helper.h"
10#include "faker-cxx/types/locale.h"
11
12namespace faker::word
13{
28FAKER_CXX_EXPORT std::string_view sample(std::optional<unsigned> length = std::nullopt, Locale locale = Locale::en_US);
29
43FAKER_CXX_EXPORT std::string words(unsigned numberOfWords = 1, Locale locale = Locale::en_US);
44
59FAKER_CXX_EXPORT std::string_view adjective(std::optional<unsigned> length = std::nullopt,
60 Locale locale = Locale::en_US);
61
76FAKER_CXX_EXPORT std::string_view adjectiveLocale(unsigned length = 0, Locale locale = Locale::en_US);
77
92FAKER_CXX_EXPORT std::string_view adverb(std::optional<unsigned> length = std::nullopt, Locale locale = Locale::en_US);
93
108FAKER_CXX_EXPORT std::string_view conjunction(std::optional<unsigned> length = std::nullopt,
109 Locale locale = Locale::en_US);
110
125FAKER_CXX_EXPORT std::string_view interjection(std::optional<unsigned> length = std::nullopt,
126 Locale locale = Locale::en_US);
127
142FAKER_CXX_EXPORT std::string_view noun(std::optional<unsigned> length = std::nullopt, Locale locale = Locale::en_US);
143
158FAKER_CXX_EXPORT std::string_view preposition(std::optional<unsigned> length = std::nullopt,
159 Locale locale = Locale::en_US);
160
175FAKER_CXX_EXPORT std::string_view verb(std::optional<unsigned> length = std::nullopt, Locale locale = Locale::en_US);
176
191template <std::ranges::range Range>
192auto sortedSizeRandomElement(std::optional<unsigned int> length, Range&& range) -> decltype(auto)
193{
194 if (!length)
195 {
196 return helper::randomElement(range);
197 }
198
199 size_t length_64 = *length;
200 auto start = range.begin();
201 auto end = range.end();
202
203 auto lower_it = ::std::lower_bound(start, end, length_64,
204 [](const auto& lhs, const auto& value) { return lhs.size() < value; });
205
206 if (lower_it == end)
207 {
208 return helper::randomElement(range);
209 }
210
211 if (lower_it->size() != length)
212 {
213 return *lower_it;
214 }
215
216 auto upper_it = lower_it;
217
218 for (; upper_it != end; upper_it++)
219 {
220 if (upper_it->size() != lower_it->size())
221 {
222 break;
223 }
224 }
225
226 return helper::randomElement(std::ranges::subrange(lower_it, upper_it));
227}
228}
decltype(auto) randomElement(Range &&range)
Definition helper.h:17
Definition word.h:13
FAKER_CXX_EXPORT std::string words(unsigned numberOfWords=1, Locale locale=Locale::en_US)
Returns a string containing a number of space separated random words.
FAKER_CXX_EXPORT std::string_view conjunction(std::optional< unsigned > length=std::nullopt, Locale locale=Locale::en_US)
Returns a random conjunction.
FAKER_CXX_EXPORT std::string_view sample(std::optional< unsigned > length=std::nullopt, Locale locale=Locale::en_US)
Returns a random .
FAKER_CXX_EXPORT std::string_view verb(std::optional< unsigned > length=std::nullopt, Locale locale=Locale::en_US)
Returns a random verb.
FAKER_CXX_EXPORT std::string_view adjectiveLocale(unsigned length=0, Locale locale=Locale::en_US)
Returns a random adjective, using locale.
FAKER_CXX_EXPORT std::string_view adverb(std::optional< unsigned > length=std::nullopt, Locale locale=Locale::en_US)
Returns a random adverb.
FAKER_CXX_EXPORT std::string_view adjective(std::optional< unsigned > length=std::nullopt, Locale locale=Locale::en_US)
Returns a random adjective.
FAKER_CXX_EXPORT std::string_view interjection(std::optional< unsigned > length=std::nullopt, Locale locale=Locale::en_US)
Returns a random interjection.
FAKER_CXX_EXPORT std::string_view preposition(std::optional< unsigned > length=std::nullopt, Locale locale=Locale::en_US)
Returns a random preposition.
FAKER_CXX_EXPORT std::string_view noun(std::optional< unsigned > length=std::nullopt, Locale locale=Locale::en_US)
Returns a random noun.
auto sortedSizeRandomElement(std::optional< unsigned int > length, Range &&range) -> decltype(auto)
Returns random element of length.
Definition word.h:191