Skip to content

Commit

Permalink
Fix cppcheck errors
Browse files Browse the repository at this point in the history
Signed-off-by: Uilian Ries <[email protected]>
  • Loading branch information
uilianries committed Jul 6, 2024
1 parent f0d5119 commit d24374c
Show file tree
Hide file tree
Showing 12 changed files with 73 additions and 77 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,6 @@ build-linux-clang
bazel-*
MODULE.bazel.lock
install/

tests/default.profraw
defaults.cfg
14 changes: 4 additions & 10 deletions include/faker-cxx/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -158,22 +158,16 @@ T weightedArrayElement(const std::vector<WeightedElement<T>>& data)
const std::integral auto targetWeightValue = number::integer<unsigned>(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;
}

}
4 changes: 2 additions & 2 deletions include/faker-cxx/Person.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ enum class Language;
* faker::person::prefix(Sex::Male) // "Mr."
* @endcode
*/
FAKER_CXX_EXPORT std::string_view prefix(std::optional<Country> countryOpt = std::nullopt,
FAKER_CXX_EXPORT std::string_view prefix(std::optional<Country> country = std::nullopt,
std::optional<Sex> sex = std::nullopt);

/**
Expand All @@ -86,7 +86,7 @@ enum class Language;
* faker::person::suffix() // "Jr."
* @endcode
*/
FAKER_CXX_EXPORT std::string_view suffix(std::optional<Country> countryOpt = std::nullopt,
FAKER_CXX_EXPORT std::string_view suffix(std::optional<Country> country = std::nullopt,
std::optional<Sex> sex = std::nullopt);

/**
Expand Down
7 changes: 5 additions & 2 deletions include/faker-cxx/RandomGenerator.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#pragma once

#include <random>
#include <type_traits>

namespace faker
{
Expand All @@ -18,9 +19,11 @@ class RandomGenerator
RandomGenerator& operator=(const RandomGenerator&) = default;
RandomGenerator& operator=(RandomGenerator&&) = default;

int operator()(std::uniform_int_distribution<>& dist)
template <typename Dist>
requires std::is_invocable_r_v<int, Dist&, T&>
int operator()(Dist&& dist)
{
return dist(generator_);
return std::forward<Dist>(dist)(generator_);
}

private:
Expand Down
6 changes: 2 additions & 4 deletions src/common/AlgoHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
#include <set>
#include <stdexcept>
#include <string>
#include <iterator>

#include "faker-cxx/Datatype.h"
#include "faker-cxx/Export.h"
Expand Down Expand Up @@ -43,10 +44,7 @@ static T::key_type objectKey(const T& object)
std::vector<typename T::key_type> 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<typename T::key_type>(keys);
}
Expand Down
16 changes: 8 additions & 8 deletions src/modules/internet/Internet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,25 +83,25 @@ std::vector<std::string_view> getAllEmojis()
return emojis;
}

std::string username(std::optional<std::string> firstNameInit, std::optional<std::string> lastNameInit, Country country)
std::string username(std::optional<std::string> firstName, std::optional<std::string> 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<int>(2))
{
case 0:
username = common::format("{}{}{}", firstName, lastName, number::integer<int>(1970, 2005));
username = common::format("{}{}{}", firstNameLower, lastNameLower, number::integer<int>(1970, 2005));
break;
case 1:
username =
common::format("{}{}{}", firstName, helper::arrayElement(std::vector<std::string>{".", "_", ""}), lastName);
common::format("{}{}{}", firstNameLower, helper::arrayElement(std::vector<std::string>{".", "_", ""}), lastNameLower);
break;
case 2:
username =
common::format("{}{}{}", lastName, helper::arrayElement(std::vector<std::string>{".", "_", ""}), firstName);
common::format("{}{}{}", lastNameLower, helper::arrayElement(std::vector<std::string>{".", "_", ""}), firstNameLower);
break;
}

Expand Down Expand Up @@ -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()
Expand Down
17 changes: 8 additions & 9 deletions src/modules/lorem/Lorem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@
#include <string>
#include <string_view>
#include <vector>
#include <algorithm>
#include <iterator>

#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"
Expand Down Expand Up @@ -57,15 +59,12 @@ std::string sentences(unsigned minNumberOfSentences, unsigned maxNumberOfSentenc

std::string slug(unsigned int numberOfWords)
{
std::vector<std::string> words;
words.reserve(numberOfWords);
std::vector<std::string> 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)
Expand Down
60 changes: 31 additions & 29 deletions src/modules/person/Person.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -159,11 +159,11 @@ const struct PeopleNames& getPeopleNamesByCountry(const Country& country)

}

std::string_view firstName(std::optional<Country> countryOpt, std::optional<Sex> sex)
std::string_view firstName(std::optional<Country> country, std::optional<Sex> 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<std::string_view> firstNames;

Expand Down Expand Up @@ -191,11 +191,11 @@ std::string_view firstName(std::optional<Country> countryOpt, std::optional<Sex>
return helper::arrayElement(firstNames);
}

std::string_view lastName(std::optional<Country> countryOpt, std::optional<Sex> sex)
std::string_view lastName(std::optional<Country> country, std::optional<Sex> 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<std::string_view> lastNames;

Expand Down Expand Up @@ -223,35 +223,37 @@ std::string_view lastName(std::optional<Country> countryOpt, std::optional<Sex>
return helper::arrayElement(lastNames);
}

std::string fullName(std::optional<Country> countryOpt, std::optional<Sex> sex)
std::string fullName(std::optional<Country> country, std::optional<Sex> 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<helper::WeightedElement<std::string_view>> 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<std::string_view>{nameFormat.weight, nameFormat.format};
}
);

const auto nameFormat = static_cast<std::string>(helper::weightedArrayElement<std::string_view>(weightedElements));

const auto dataGeneratorsMapping = std::unordered_map<std::string, std::function<std::string()>>{
{"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<Country> countryOpt, std::optional<Sex> sex)
std::string_view prefix(std::optional<Country> country, std::optional<Sex> 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<std::string_view> prefixes;

Expand Down Expand Up @@ -284,11 +286,11 @@ std::string_view prefix(std::optional<Country> countryOpt, std::optional<Sex> se
return helper::arrayElement(prefixes);
}

std::string_view suffix(std::optional<Country> countryOpt, std::optional<Sex> sex)
std::string_view suffix(std::optional<Country> country, std::optional<Sex> 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<std::string_view> suffixes;

Expand Down Expand Up @@ -334,17 +336,17 @@ std::string bio()
return common::fillTokenValues(randomBioFormat, dataGeneratorsMapping);
}

std::string_view sex(std::optional<Language> languageOpt)
std::string_view sex(std::optional<Language> language)
{
const std::vector<std::string> sexes{"Male", "Female"};

const auto chosenSex = helper::arrayElement(sexes);

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())
{
Expand Down Expand Up @@ -437,11 +439,11 @@ std::string_view chineseZodiac()
return helper::arrayElement(chineseZodiacs);
}

std::string passport(std::optional<PassportCountry> countryOpt)
std::string passport(std::optional<PassportCountry> 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;

Expand Down
9 changes: 5 additions & 4 deletions src/modules/string/String.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char>(numericCharacters);
alphanumericStr.push_back(helper::arrayElement<char>(numericCharacters));
}
else
{
alphanumeric += helper::arrayElement<char>(numericCharactersWithoutZero);
alphanumericStr.push_back(helper::arrayElement<char>(numericCharactersWithoutZero));
}
}

return alphanumeric;
return alphanumericStr;
}

std::string numeric(GuaranteeMap&& guarantee, const unsigned length, bool allowLeadingZeros)
Expand Down
10 changes: 3 additions & 7 deletions src/modules/system/System.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,21 +137,17 @@ std::string commonFileName(const std::optional<std::string>& 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<std::string_view> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion tests/modules/git/GitTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ TEST_F(GitTest, branchIssueNumTest)
{
auto testValue = unsigned(number::integer(2, 100));

std::vector<std::string> branchElements = common::split(branch(testValue), "-");
std::vector<std::string> branchElements;

bool numberAtFront = false;

Expand Down
2 changes: 1 addition & 1 deletion tests/modules/internet/InternetTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit d24374c

Please sign in to comment.