Skip to content

Commit

Permalink
refactor words
Browse files Browse the repository at this point in the history
Use std::string_view where possible except for words().
Avoid allocating and shuffling data! Adding a header to combine all data together, organized by length once. Functions rewritten to retrieve by length (binary search) if possible later.
  • Loading branch information
Andersama committed Jun 12, 2024
1 parent ff95aa8 commit 56e0bab
Show file tree
Hide file tree
Showing 13 changed files with 312 additions and 173 deletions.
13 changes: 13 additions & 0 deletions include/faker-cxx/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,19 @@ class Helper
return data[index];
}

template <typename It>
static auto arrayElement(It start, It end) -> decltype(*::std::declval<It>())
{
size_t size = end - start;
if (size==0)
{
throw std::invalid_argument{"Range [start,end) is empty."};
}

const auto index = Number::integer<size_t>(size - 1);
return start[index];
}

/**
* @brief Get a random element from a vector.
*
Expand Down
17 changes: 9 additions & 8 deletions include/faker-cxx/Word.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <optional>
#include <string>
#include <string_view>

namespace faker
{
Expand All @@ -21,7 +22,7 @@ class Word
* Word::sample(5) // "spell"
* @endcode
*/
static std::string sample(std::optional<unsigned> length = std::nullopt);
static std::string_view sample(std::optional<unsigned> length = std::nullopt);

/**
* @brief Returns a string containing a number of space separated random words.
Expand Down Expand Up @@ -50,7 +51,7 @@ class Word
* Word::adjective(3) // "bad"
* @endcode
*/
static std::string adjective(std::optional<unsigned> length = std::nullopt);
static std::string_view adjective(std::optional<unsigned> length = std::nullopt);

/**
* @brief Returns a random adverb.
Expand All @@ -65,7 +66,7 @@ class Word
* Word::adverb(5) // "almost"
* @endcode
*/
static std::string adverb(std::optional<unsigned> length = std::nullopt);
static std::string_view adverb(std::optional<unsigned> length = std::nullopt);

/**
* @brief Returns a random conjunction.
Expand All @@ -80,7 +81,7 @@ class Word
* Word::conjunction(6) // "indeed"
* @endcode
*/
static std::string conjunction(std::optional<unsigned> length = std::nullopt);
static std::string_view conjunction(std::optional<unsigned> length = std::nullopt);

/**
* @brief Returns a random interjection.
Expand All @@ -95,7 +96,7 @@ class Word
* Word::interjection(4) // "yuck"
* @endcode
*/
static std::string interjection(std::optional<unsigned> length = std::nullopt);
static std::string_view interjection(std::optional<unsigned> length = std::nullopt);

/**
* @brief Returns a random noun.
Expand All @@ -110,7 +111,7 @@ class Word
* Word::noun(8) // "distance"
* @endcode
*/
static std::string noun(std::optional<unsigned> length = std::nullopt);
static std::string_view noun(std::optional<unsigned> length = std::nullopt);

/**
* @brief Returns a random preposition.
Expand All @@ -125,7 +126,7 @@ class Word
* Word::preposition(4) // "from"
* @endcode
*/
static std::string preposition(std::optional<unsigned> length = std::nullopt);
static std::string_view preposition(std::optional<unsigned> length = std::nullopt);

/**
* @brief Returns a random verb.
Expand All @@ -140,6 +141,6 @@ class Word
* Word::verb(9) // "stabilise"
* @endcode
*/
static std::string verb(std::optional<unsigned> length = std::nullopt);
static std::string_view verb(std::optional<unsigned> length = std::nullopt);
};
}
4 changes: 4 additions & 0 deletions src/common/FormatHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include <functional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <vector>

Expand Down Expand Up @@ -33,5 +34,8 @@ class FormatHelper
static std::string
fillTokenValues(const std::string& format,
std::unordered_map<std::string, std::function<std::string()>> tokenValueGenerators);

static std::string fillTokenValues(const std::string& format,
std::unordered_map<std::string_view, std::function<std::string_view()>> tokenValueGenerators);
};
}
8 changes: 4 additions & 4 deletions src/modules/person/Person.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,11 +330,11 @@ std::string Person::bio()
{
const auto randomBioFormat = static_cast<std::string>(Helper::arrayElement(bioFormats));

const std::unordered_map<std::string, std::function<std::string()>> dataGeneratorsMapping{
{"bio_part", []() { return std::string{Helper::arrayElement(bioParts)}; }},
{"bio_supporter", []() { return std::string{Helper::arrayElement(bioSupporters)}; }},
const std::unordered_map<std::string_view, std::function<std::string_view()>> dataGeneratorsMapping{
{"bio_part", []() { return Helper::arrayElement(bioParts); }},
{"bio_supporter", []() { return Helper::arrayElement(bioSupporters); }},
{"noun", []() { return Word::noun(); }},
{"emoji", []() { return std::string{Internet::emoji()}; }}};
{"emoji", []() { return Internet::emoji(); }}};

return FormatHelper::fillTokenValues(randomBioFormat, dataGeneratorsMapping);
}
Expand Down
Loading

0 comments on commit 56e0bab

Please sign in to comment.