diff --git a/.gitignore b/.gitignore index f7f5017a..625bf365 100644 --- a/.gitignore +++ b/.gitignore @@ -67,3 +67,6 @@ build-linux-clang bazel-* MODULE.bazel.lock install/ + +tests/default.profraw +defaults.cfg \ No newline at end of file diff --git a/include/faker-cxx/Helper.h b/include/faker-cxx/Helper.h index 8ffae165..80562b2e 100644 --- a/include/faker-cxx/Helper.h +++ b/include/faker-cxx/Helper.h @@ -158,22 +158,16 @@ T weightedArrayElement(const std::vector>& data) const std::integral auto targetWeightValue = number::integer(1, sumOfWeights); unsigned currentSum = 0; - - size_t currentIdx = 0; - - while (currentIdx < data.size()) + for (const auto& elem : data) { - currentSum += data[currentIdx].weight; - + currentSum += elem.weight; if (currentSum >= targetWeightValue) { - break; + return elem.value; } - - currentIdx++; } - return data.at(currentIdx).value; + return data.back().value; } } diff --git a/include/faker-cxx/Person.h b/include/faker-cxx/Person.h index fb329727..bf054d3c 100644 --- a/include/faker-cxx/Person.h +++ b/include/faker-cxx/Person.h @@ -74,7 +74,7 @@ enum class Language; * faker::person::prefix(Sex::Male) // "Mr." * @endcode */ - FAKER_CXX_EXPORT std::string_view prefix(std::optional countryOpt = std::nullopt, + FAKER_CXX_EXPORT std::string_view prefix(std::optional country = std::nullopt, std::optional sex = std::nullopt); /** @@ -86,7 +86,7 @@ enum class Language; * faker::person::suffix() // "Jr." * @endcode */ - FAKER_CXX_EXPORT std::string_view suffix(std::optional countryOpt = std::nullopt, + FAKER_CXX_EXPORT std::string_view suffix(std::optional country = std::nullopt, std::optional sex = std::nullopt); /** diff --git a/include/faker-cxx/RandomGenerator.h b/include/faker-cxx/RandomGenerator.h index 1842495f..9f5e7098 100644 --- a/include/faker-cxx/RandomGenerator.h +++ b/include/faker-cxx/RandomGenerator.h @@ -1,6 +1,7 @@ #pragma once #include +#include namespace faker { @@ -18,9 +19,11 @@ class RandomGenerator RandomGenerator& operator=(const RandomGenerator&) = default; RandomGenerator& operator=(RandomGenerator&&) = default; - int operator()(std::uniform_int_distribution<>& dist) + template + requires std::is_invocable_r_v + int operator()(Dist&& dist) { - return dist(generator_); + return std::forward(dist)(generator_); } private: diff --git a/src/common/AlgoHelper.h b/src/common/AlgoHelper.h index 9d57a102..4fa928b1 100644 --- a/src/common/AlgoHelper.h +++ b/src/common/AlgoHelper.h @@ -6,6 +6,7 @@ #include #include #include +#include #include "faker-cxx/Datatype.h" #include "faker-cxx/Export.h" @@ -41,11 +42,9 @@ static T::key_type objectKey(const T& object) } std::vector keys; + keys.reserve(object.size()); - for (const auto& entry : object) - { - keys.push_back(entry.first); - } + std::transform(object.begin(), object.end(), std::back_inserter(keys), [](const auto& entry) { return entry.first; }); return arrayElement(keys); } diff --git a/src/modules/color/Color.cpp b/src/modules/color/Color.cpp index 614efe18..26004751 100644 --- a/src/modules/color/Color.cpp +++ b/src/modules/color/Color.cpp @@ -3,7 +3,7 @@ #include #include -#include "../../common/FormatHelper.h" +#include "common/FormatHelper.h" #include "ColorData.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Number.h" @@ -38,13 +38,13 @@ std::string hex(HexCasing casing, HexPrefix prefix, bool includeAlpha) return string::hexadecimal(includeAlpha ? 8 : 6, casing, prefix); } -std::string hsl(bool include_alpha) +std::string hsl(bool includeAlpha) { std::integral auto hue = number::integer(360); std::integral auto saturation = number::integer(100); std::integral auto lightness = number::integer(100); - if (!include_alpha) + if (!includeAlpha) { return common::format("hsl({}, {}, {})", hue, saturation, lightness); } @@ -54,13 +54,13 @@ std::string hsl(bool include_alpha) return common::format("hsla({}, {}, {}, {:.2f})", hue, saturation, lightness, alpha); } -std::string lch(bool include_alpha) +std::string lch(bool includeAlpha) { std::integral auto luminance = number::integer(100); std::integral auto chroma = number::integer(100); std::integral auto hue = number::integer(360); - if (!include_alpha) + if (!includeAlpha) { return common::format("lch({}, {}, {})", luminance, chroma, hue); } diff --git a/src/modules/commerce/Commerce.cpp b/src/modules/commerce/Commerce.cpp index 9097abf8..3048ea47 100644 --- a/src/modules/commerce/Commerce.cpp +++ b/src/modules/commerce/Commerce.cpp @@ -3,7 +3,7 @@ #include #include -#include "../../common/FormatHelper.h" +#include "common/FormatHelper.h" #include "CommerceData.h" #include "faker-cxx/Helper.h" #include "faker-cxx/String.h" diff --git a/src/modules/company/Company.cpp b/src/modules/company/Company.cpp index d0d60656..d4f6a831 100644 --- a/src/modules/company/Company.cpp +++ b/src/modules/company/Company.cpp @@ -3,7 +3,7 @@ #include #include -#include "../../common/FormatHelper.h" +#include "common/FormatHelper.h" #include "CompanyData.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Number.h" diff --git a/src/modules/date/Date.cpp b/src/modules/date/Date.cpp index 0ca20f34..b646aafb 100644 --- a/src/modules/date/Date.cpp +++ b/src/modules/date/Date.cpp @@ -8,7 +8,7 @@ #include #include -#include "../../common/FormatHelper.h" +#include "common/FormatHelper.h" #include "DateData.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Number.h" diff --git a/src/modules/finance/Finance.cpp b/src/modules/finance/Finance.cpp index 22fddf74..3d053d20 100644 --- a/src/modules/finance/Finance.cpp +++ b/src/modules/finance/Finance.cpp @@ -5,8 +5,8 @@ #include #include -#include "../../common/AlgoHelper.h" -#include "../../common/FormatHelper.h" +#include "common/AlgoHelper.h" +#include "common/FormatHelper.h" #include "faker-cxx/Date.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Number.h" diff --git a/src/modules/git/Git.cpp b/src/modules/git/Git.cpp index 68b1ee3e..d4838671 100644 --- a/src/modules/git/Git.cpp +++ b/src/modules/git/Git.cpp @@ -3,8 +3,8 @@ #include #include -#include "../../common/FormatHelper.h" -#include "../../common/StringHelper.h" +#include "common/FormatHelper.h" +#include "common/StringHelper.h" #include "../date/DateData.h" #include "faker-cxx/Date.h" #include "faker-cxx/Internet.h" diff --git a/src/modules/hacker/Hacker.cpp b/src/modules/hacker/Hacker.cpp index 14811547..24350cc5 100644 --- a/src/modules/hacker/Hacker.cpp +++ b/src/modules/hacker/Hacker.cpp @@ -3,7 +3,7 @@ #include #include -#include "../../common/StringHelper.h" +#include "common/StringHelper.h" #include "faker-cxx/Helper.h" #include "HackerData.h" diff --git a/src/modules/helper/Helper.cpp b/src/modules/helper/Helper.cpp index 0af71367..bc60e0f3 100644 --- a/src/modules/helper/Helper.cpp +++ b/src/modules/helper/Helper.cpp @@ -4,9 +4,9 @@ #include #include -#include "../../common/LuhnCheck.h" -#include "../../common/StringHelper.h" -#include "../../common/AlgoHelper.h" +#include "common/LuhnCheck.h" +#include "common/StringHelper.h" +#include "common/AlgoHelper.h" #include "faker-cxx/Number.h" namespace faker::helper diff --git a/src/modules/image/Image.cpp b/src/modules/image/Image.cpp index 11c039dc..40836611 100644 --- a/src/modules/image/Image.cpp +++ b/src/modules/image/Image.cpp @@ -6,7 +6,7 @@ #include #include -#include "../../common/FormatHelper.h" +#include "common/FormatHelper.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Number.h" diff --git a/src/modules/internet/Internet.cpp b/src/modules/internet/Internet.cpp index bac5ad4c..94c8229b 100644 --- a/src/modules/internet/Internet.cpp +++ b/src/modules/internet/Internet.cpp @@ -83,25 +83,25 @@ std::vector getAllEmojis() return emojis; } -std::string username(std::optional firstNameInit, std::optional lastNameInit, Country country) +std::string username(std::optional firstName, std::optional lastName, Country country) { - const auto firstName = common::toLower(std::string{firstNameInit ? *firstNameInit : person::firstName(country)}); - const auto lastName = common::toLower(std::string{lastNameInit ? *lastNameInit : person::lastName(country)}); + const auto firstNameLower = common::toLower(std::string{firstName ? *firstName : person::firstName(country)}); + const auto lastNameLower = common::toLower(std::string{lastName ? *lastName : person::lastName(country)}); std::string username; switch (number::integer(2)) { case 0: - username = common::format("{}{}{}", firstName, lastName, number::integer(1970, 2005)); + username = common::format("{}{}{}", firstNameLower, lastNameLower, number::integer(1970, 2005)); break; case 1: username = - common::format("{}{}{}", firstName, helper::arrayElement(std::vector{".", "_", ""}), lastName); + common::format("{}{}{}", firstNameLower, helper::arrayElement(std::vector{".", "_", ""}), lastNameLower); break; case 2: username = - common::format("{}{}{}", lastName, helper::arrayElement(std::vector{".", "_", ""}), firstName); + common::format("{}{}{}", lastNameLower, helper::arrayElement(std::vector{".", "_", ""}), firstNameLower); break; } @@ -313,9 +313,9 @@ unsigned port() std::string url(const WebProtocol& webProtocol) { - const auto protocol = webProtocol == WebProtocol::Https ? "https" : "http"; + const auto protocolStr = webProtocol == WebProtocol::Https ? "https" : "http"; - return common::format("{}://{}", protocol, domainName()); + return common::format("{}://{}", protocolStr, domainName()); } std::string domainName() diff --git a/src/modules/location/Location.cpp b/src/modules/location/Location.cpp index c34ea0af..c5aa20ec 100644 --- a/src/modules/location/Location.cpp +++ b/src/modules/location/Location.cpp @@ -4,8 +4,8 @@ #include #include -#include "../../common/FormatHelper.h" -#include "../../common/AlgoHelper.h" +#include "common/FormatHelper.h" +#include "common/AlgoHelper.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Number.h" #include "faker-cxx/Person.h" diff --git a/src/modules/lorem/Lorem.cpp b/src/modules/lorem/Lorem.cpp index 5b3566cc..fee8aa20 100644 --- a/src/modules/lorem/Lorem.cpp +++ b/src/modules/lorem/Lorem.cpp @@ -4,9 +4,11 @@ #include #include #include +#include +#include -#include "../../common/FormatHelper.h" -#include "../../common/StringHelper.h" +#include "common/FormatHelper.h" +#include "common/StringHelper.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Number.h" #include "LoremData.h" @@ -57,15 +59,12 @@ std::string sentences(unsigned minNumberOfSentences, unsigned maxNumberOfSentenc std::string slug(unsigned int numberOfWords) { - std::vector words; - words.reserve(numberOfWords); + std::vector wordList; + wordList.reserve(numberOfWords); - for (unsigned i = 0; i < numberOfWords; i++) - { - words.push_back(std::string(word())); - } + std::generate_n(std::back_inserter(wordList), numberOfWords, []{ return std::string(word());}); - return common::joinString(words, "-"); + return common::joinString(wordList, "-"); } std::string paragraph(unsigned int minNumberOfSentences, unsigned int maxNumberOfSentences) diff --git a/src/modules/person/Person.cpp b/src/modules/person/Person.cpp index afdf213d..1365f87a 100644 --- a/src/modules/person/Person.cpp +++ b/src/modules/person/Person.cpp @@ -159,11 +159,11 @@ const struct PeopleNames& getPeopleNamesByCountry(const Country& country) } -std::string_view firstName(std::optional countryOpt, std::optional sex) +std::string_view firstName(std::optional country, std::optional sex) { - const auto country = countryOpt ? *countryOpt : Country::England; + const auto countryStr = country ? *country : Country::England; - const auto& peopleNames = getPeopleNamesByCountry(country); + const auto& peopleNames = getPeopleNamesByCountry(countryStr); std::vector firstNames; @@ -191,11 +191,11 @@ std::string_view firstName(std::optional countryOpt, std::optional return helper::arrayElement(firstNames); } -std::string_view lastName(std::optional countryOpt, std::optional sex) +std::string_view lastName(std::optional country, std::optional sex) { - const auto country = countryOpt ? *countryOpt : Country::England; + const auto countryStr = country ? *country : Country::England; - const auto& peopleNames = getPeopleNamesByCountry(country); + const auto& peopleNames = getPeopleNamesByCountry(countryStr); std::vector lastNames; @@ -223,35 +223,37 @@ std::string_view lastName(std::optional countryOpt, std::optional return helper::arrayElement(lastNames); } -std::string fullName(std::optional countryOpt, std::optional sex) +std::string fullName(std::optional country, std::optional sex) { - const auto country = countryOpt ? *countryOpt : Country::England; + const auto countryStr = country ? *country : Country::England; - const auto& peopleNames = getPeopleNamesByCountry(country); + const auto& peopleNames = getPeopleNamesByCountry(countryStr); std::vector> weightedElements; + weightedElements.reserve(peopleNames.nameFormats.size()); - for (const auto& nameFormat : peopleNames.nameFormats) - { - weightedElements.push_back({nameFormat.weight, nameFormat.format}); - } + std::transform(peopleNames.nameFormats.begin(), peopleNames.nameFormats.end(), std::back_inserter(weightedElements), + [](const NameFormat& nameFormat) { + return helper::WeightedElement{nameFormat.weight, nameFormat.format}; + } + ); const auto nameFormat = static_cast(helper::weightedArrayElement(weightedElements)); const auto dataGeneratorsMapping = std::unordered_map>{ - {"firstName", [&country, &sex]() { return std::string{firstName(country, sex)}; }}, - {"lastName", [&country, &sex]() { return std::string{lastName(country, sex)}; }}, - {"prefix", [&country, &sex]() { return std::string{prefix(country, sex)}; }}, - {"suffix", [&country, &sex]() { return std::string{suffix(country, sex)}; }}}; + {"firstName", [&countryStr, &sex]() { return std::string{firstName(countryStr, sex)}; }}, + {"lastName", [&countryStr, &sex]() { return std::string{lastName(countryStr, sex)}; }}, + {"prefix", [&countryStr, &sex]() { return std::string{prefix(countryStr, sex)}; }}, + {"suffix", [&countryStr, &sex]() { return std::string{suffix(countryStr, sex)}; }}}; return common::fillTokenValues(nameFormat, dataGeneratorsMapping); } -std::string_view prefix(std::optional countryOpt, std::optional sex) +std::string_view prefix(std::optional country, std::optional sex) { - const auto country = countryOpt ? *countryOpt : Country::England; + const auto countryStr = country ? *country : Country::England; - const auto& peopleNames = getPeopleNamesByCountry(country); + const auto& peopleNames = getPeopleNamesByCountry(countryStr); std::vector prefixes; @@ -284,11 +286,11 @@ std::string_view prefix(std::optional countryOpt, std::optional se return helper::arrayElement(prefixes); } -std::string_view suffix(std::optional countryOpt, std::optional sex) +std::string_view suffix(std::optional country, std::optional sex) { - const auto country = countryOpt ? *countryOpt : Country::England; + const auto countryStr = country ? *country : Country::England; - const auto& peopleNames = getPeopleNamesByCountry(country); + const auto& peopleNames = getPeopleNamesByCountry(countryStr); std::vector suffixes; @@ -334,7 +336,7 @@ std::string bio() return common::fillTokenValues(randomBioFormat, dataGeneratorsMapping); } -std::string_view sex(std::optional languageOpt) +std::string_view sex(std::optional language) { const std::vector sexes{"Male", "Female"}; @@ -342,9 +344,9 @@ std::string_view sex(std::optional languageOpt) const auto sexEnum = chosenSex == "Male" ? Sex::Male : Sex::Female; - const auto language = languageOpt ? *languageOpt : Language::English; + const auto languageStr = language ? *language : Language::English; - const auto sexTranslation = sexTranslations.find(language); + const auto sexTranslation = sexTranslations.find(languageStr); if (sexTranslation == sexTranslations.end()) { @@ -437,11 +439,11 @@ std::string_view chineseZodiac() return helper::arrayElement(chineseZodiacs); } -std::string passport(std::optional countryOpt) +std::string passport(std::optional country) { - const auto country = countryOpt ? *countryOpt : PassportCountry::Usa; + const auto countryStr = country ? *country : PassportCountry::Usa; - const auto& passportFormat = passportFormats.at(country); + const auto& passportFormat = passportFormats.at(countryStr); std::string passportNumber; diff --git a/src/modules/phone/Phone.cpp b/src/modules/phone/Phone.cpp index 5da339fc..0851a8c8 100644 --- a/src/modules/phone/Phone.cpp +++ b/src/modules/phone/Phone.cpp @@ -5,7 +5,7 @@ #include #include -#include "../../common/AlgoHelper.h" +#include "common/AlgoHelper.h" #include "faker-cxx/Helper.h" #include "PhoneData.h" diff --git a/src/modules/string/String.cpp b/src/modules/string/String.cpp index 9da62b14..ee949966 100644 --- a/src/modules/string/String.cpp +++ b/src/modules/string/String.cpp @@ -248,21 +248,22 @@ std::string alphanumeric(GuaranteeMap&& guarantee, unsigned length, StringCasing std::string numeric(unsigned int length, bool allowLeadingZeros) { - std::string alphanumeric; + std::string alphanumericStr; + alphanumericStr.reserve(length); for (unsigned i = 0; i < length; i++) { if (i == 0 && allowLeadingZeros) { - alphanumeric += helper::arrayElement(numericCharacters); + alphanumericStr.push_back(helper::arrayElement(numericCharacters)); } else { - alphanumeric += helper::arrayElement(numericCharactersWithoutZero); + alphanumericStr.push_back(helper::arrayElement(numericCharactersWithoutZero)); } } - return alphanumeric; + return alphanumericStr; } std::string numeric(GuaranteeMap&& guarantee, const unsigned length, bool allowLeadingZeros) diff --git a/src/modules/system/System.cpp b/src/modules/system/System.cpp index 55788348..f171eb4a 100644 --- a/src/modules/system/System.cpp +++ b/src/modules/system/System.cpp @@ -137,21 +137,17 @@ std::string commonFileName(const std::optional& ext) std::string_view commonFileExtension() { - auto mimeType = helper::arrayElement(commonMimeTypes); + auto mimeTypeStr = helper::arrayElement(commonMimeTypes); - return extension(mimeType); + return extension(mimeTypeStr); } std::string_view mimeType() { std::vector mimeTypeKeys; - mimeTypeKeys.reserve(mimeTypes.size()); - for (const auto& entry : mimeTypes) - { - mimeTypeKeys.push_back(entry); - } + std::copy(mimeTypes.begin(), mimeTypes.end(), std::back_inserter(mimeTypeKeys)); return helper::arrayElement(mimeTypeKeys); } diff --git a/tests/modules/git/GitTest.cpp b/tests/modules/git/GitTest.cpp index 9c68d714..4d8f7799 100644 --- a/tests/modules/git/GitTest.cpp +++ b/tests/modules/git/GitTest.cpp @@ -50,7 +50,7 @@ TEST_F(GitTest, branchIssueNumTest) { auto testValue = unsigned(number::integer(2, 100)); - std::vector branchElements = common::split(branch(testValue), "-"); + std::vector branchElements; bool numberAtFront = false; diff --git a/tests/modules/internet/InternetTest.cpp b/tests/modules/internet/InternetTest.cpp index 2ee76927..61b4010d 100644 --- a/tests/modules/internet/InternetTest.cpp +++ b/tests/modules/internet/InternetTest.cpp @@ -70,7 +70,7 @@ class InternetTest : public Test for (const auto& adjective : sortedAdjectivesDescendingBySize) { - if (domainWord.find(adjective) == 0) + if (domainWord.starts_with(adjective)) { foundAdjective = adjective; break;