From 97c0aa666efa89cedb6c812ef0804e76aefa8740 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Cie=C5=9Blar?= Date: Tue, 12 Sep 2023 23:22:10 +0100 Subject: [PATCH] refactor location class to use regex generator for address formats (#91) --- include/faker-cxx/Number.h | 3 +- src/modules/location/Location.cpp | 71 ++++++++----------------------- src/modules/number/Number.cpp | 9 ++-- 3 files changed, 24 insertions(+), 59 deletions(-) diff --git a/include/faker-cxx/Number.h b/include/faker-cxx/Number.h index 4bb336c27..554f82b2a 100644 --- a/include/faker-cxx/Number.h +++ b/include/faker-cxx/Number.h @@ -375,8 +375,9 @@ class Number static std::string hex(std::optional min = std::nullopt, std::optional max = std::nullopt); private: + static std::string convertToHex(int number); + static std::random_device randomDevice; static std::mt19937 pseudoRandomGenerator; - static std::string convertToHex(int number); }; } diff --git a/src/modules/location/Location.cpp b/src/modules/location/Location.cpp index c27d66674..c65f8997a 100644 --- a/src/modules/location/Location.cpp +++ b/src/modules/location/Location.cpp @@ -6,14 +6,14 @@ #include "../../common/StringHelper.h" #include "data/Countries.h" #include "data/Directions.h" +#include "data/russia/RussiaAddressFormat.h" +#include "data/russia/RussiaCities.h" +#include "data/russia/RussiaStreetPrefixes.h" #include "data/States.h" #include "data/TimeZones.h" #include "data/usa/UsaAddressFormat.h" #include "data/usa/UsaCities.h" #include "data/usa/UsaStreetSuffixes.h" -#include "data/russia/RussiaAddressFormat.h" -#include "data/russia/RussiaCities.h" -#include "data/russia/RussiaStreetPrefixes.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Person.h" #include "faker-cxx/String.h" @@ -23,19 +23,23 @@ namespace faker namespace { const std::map> countryToCitiesMapping{ - {Country::Usa, usaCities}, {Country::Russia, russiaCities}, + {Country::Usa, usaCities}, + {Country::Russia, russiaCities}, }; const std::map countryToZipCodeFormatMapping{ - {Country::Usa, usaZipCodeFormat}, {Country::Russia, russiaZipCodeFormat}, + {Country::Usa, usaZipCodeFormat}, + {Country::Russia, russiaZipCodeFormat}, }; const std::map> countryToBuildingNumberFormatsMapping{ - {Country::Usa, usaBuildingNumberFormats}, {Country::Russia, russiaBuildingNumberFormats}, + {Country::Usa, usaBuildingNumberFormats}, + {Country::Russia, russiaBuildingNumberFormats}, }; const std::map> countryToStreetFormatsMapping{ - {Country::Usa, usaStreetFormats}, {Country::Russia, russiaStreetFormats}, + {Country::Usa, usaStreetFormats}, + {Country::Russia, russiaStreetFormats}, }; const std::map> countryToSecondaryAddressFormatsMapping{ @@ -43,7 +47,8 @@ const std::map> countryToSecondaryAddressForma }; const std::map countryToAddressFormatMapping{ - {Country::Usa, usaAddressFormat}, {Country::Russia, russiaAddressFormat}, + {Country::Usa, usaAddressFormat}, + {Country::Russia, russiaAddressFormat}, }; const std::map> countryToStreetSuffixesMapping{ @@ -81,21 +86,7 @@ std::string Location::zipCode(Country country) { const auto& zipCodeFormat = countryToZipCodeFormatMapping.at(country); - std::string zipCode; - - for (const auto& zipCodeFormatCharacter : zipCodeFormat) - { - if (zipCodeFormatCharacter == '#') - { - zipCode += String::numeric(1); - } - else - { - zipCode += zipCodeFormatCharacter; - } - } - - return zipCode; + return Helper::replaceSymbolWithNumber(zipCodeFormat); } std::string Location::streetAddress(Country country) @@ -115,7 +106,7 @@ std::string Location::streetAddress(Country country) else if (addressFormatElement == "{street}") { addressElements.push_back(street(country)); - } + } } return StringHelper::join(addressElements, " "); @@ -168,21 +159,7 @@ std::string Location::buildingNumber(Country country) const auto buildingNumberFormat = Helper::arrayElement(buildingNumberFormats); - std::string buildingNumber; - - for (const auto& buildingNumberFormatCharacter : buildingNumberFormat) - { - if (buildingNumberFormatCharacter == '#') - { - buildingNumber += String::numeric(1); - } - else - { - buildingNumber += buildingNumberFormatCharacter; - } - } - - return buildingNumber; + return Helper::replaceSymbolWithNumber(buildingNumberFormat); } std::string Location::secondaryAddress(Country country) @@ -191,21 +168,7 @@ std::string Location::secondaryAddress(Country country) const auto secondaryAddressFormat = Helper::arrayElement(secondaryAddressFormats); - std::string secondaryAddress; - - for (const auto& secondaryAddressFormatCharacter : secondaryAddressFormat) - { - if (secondaryAddressFormatCharacter == '#') - { - secondaryAddress += String::numeric(1); - } - else - { - secondaryAddress += secondaryAddressFormatCharacter; - } - } - - return secondaryAddress; + return Helper::replaceSymbolWithNumber(secondaryAddressFormat); } std::string Location::latitude(Precision precision) diff --git a/src/modules/number/Number.cpp b/src/modules/number/Number.cpp index 6e61c0c52..bac700e47 100644 --- a/src/modules/number/Number.cpp +++ b/src/modules/number/Number.cpp @@ -28,10 +28,11 @@ std::string Number::hex(std::optional min, std::optional max) std::string Number::convertToHex(int number) { - std::stringstream sstream; - sstream << std::hex << number; - std::string hexString = sstream.str(); - return hexString; + std::stringstream stream; + + stream << std::hex << number; + + return stream.str(); } }