diff --git a/src/modules/word.cpp b/src/modules/word.cpp index 5eb4adea..42505370 100644 --- a/src/modules/word.cpp +++ b/src/modules/word.cpp @@ -12,8 +12,6 @@ namespace faker::word { - - std::string_view sample( std::optional length,const Locale locale) { unsigned int aux_length{0}; @@ -34,7 +32,6 @@ std::string_view sample( std::optional length,const Locale locale) auto sorted= _allWords_map.at(localeLocal); return sortedSizeRandomElement(aux_length, sorted); - } std::string words(unsigned numberOfWords,const Locale locale) @@ -50,44 +47,19 @@ std::string words(unsigned numberOfWords,const Locale locale) localeExt = Locale::en_US; } std::string combined_words; - if (numberOfWords <= 256) - { - std::array tmp{}; // fitting 1024 bytes worth of integers* - const size_t last_index = ((_allWords_map.at(localeExt)).size()) - 1; - size_t reserve_size = 0; - - for (unsigned i = 0; i < numberOfWords; i++) - { - tmp[i] = number::integer(last_index); - auto vw = (_allWords_map.at(localeExt))[tmp[i]]; - reserve_size += vw.size(); - } - - unsigned space_words = (numberOfWords - 1); - combined_words.reserve(reserve_size + (numberOfWords - 1)); - for (unsigned i = 0; i < space_words; i++) - { - auto vw = (_allWords_map.at(localeExt))[tmp[i]]; - combined_words.append(vw.begin(), vw.end()); - combined_words.push_back(' '); - } - auto vw = (_allWords_map.at(localeExt))[tmp[numberOfWords - 1]]; - combined_words.append(vw.begin(), vw.end()); - } - else + + unsigned space_words = (numberOfWords - 1); + for (unsigned i = 0; i < space_words; i++) { - unsigned space_words = (numberOfWords - 1); - for (unsigned i = 0; i < space_words; i++) - { - auto s = sample(1, localeExt); - combined_words.append(s.begin(), s.end()); - combined_words.push_back(' '); - } - auto s = sample(1, localeExt); combined_words.append(s.begin(), s.end()); + combined_words.push_back(' '); } + auto s = sample(1, localeExt); + combined_words.append(s.begin(), s.end()); + + return combined_words; } diff --git a/tests/modules/word_test.cpp b/tests/modules/word_test.cpp index 2fd57e4c..93b1096b 100644 --- a/tests/modules/word_test.cpp +++ b/tests/modules/word_test.cpp @@ -606,7 +606,7 @@ TEST_P(WordTestLocale, shouldGenerateSampleWithNonExistingLength) TEST_P(WordTestLocale, shouldGenerateWords) { - const auto locale = GetParam(); + const auto locale = GetParam(); Locale extra=locale; if(!checkLocale(locale)) @@ -614,12 +614,14 @@ TEST_P(WordTestLocale, shouldGenerateWords) extra=Locale::en_US; } - const auto generatedWords = words(5,locale); - + const auto generatedWords = words(5,locale); + const auto separatedWords = common::split(generatedWords, " "); - ASSERT_TRUE(std::ranges::all_of(separatedWords, [extra](const std::string& separatedWord) - { return std::ranges::find(_allWords_map.at(extra), separatedWord) !=_allWords_map.at(extra).end(); })); + const auto &datamap=_allWords_map.at(extra); + + ASSERT_TRUE(std::ranges::all_of(separatedWords, [&datamap](const std::string& separatedWord) + { return std::ranges::find(datamap, separatedWord) !=datamap.end(); })); } TEST_F(WordTestLocale, shouldReturnRandomElementWhenExactLengthNotFound)