From a2b3d95df39ef251b7cf8a754101291af6199f46 Mon Sep 17 00:00:00 2001 From: brawlee Date: Mon, 20 Nov 2023 10:12:12 +0530 Subject: [PATCH] feature: Implemented `Helper::shuffleString()` in `Helper` module We already had a function `Helper::shuffle()` that can be use for shuffling a vector of objects but it wasn't possible to use it for a std::string and shuffle it chars `Helper::shuffleString()` can be use to generate random permutations of a given std::string --- include/faker-cxx/Helper.h | 13 +++++++++++++ src/modules/helper/Helper.cpp | 7 +++++++ 2 files changed, 20 insertions(+) diff --git a/include/faker-cxx/Helper.h b/include/faker-cxx/Helper.h index 0cfc1a98f..eae16f413 100644 --- a/include/faker-cxx/Helper.h +++ b/include/faker-cxx/Helper.h @@ -143,6 +143,19 @@ class Helper return data; } + /* + * @brief Returns shuffled std::string + * + * @param data String to be shuffled + * + * @return std::string with shuffled chars + * + * @code + * Helper::shuffleString("hello") // "eollh" + * @endcode + */ + static std::string shuffleString(std::string data); + // TODO: remove methods below from helper API, move to src/common /** diff --git a/src/modules/helper/Helper.cpp b/src/modules/helper/Helper.cpp index 74b400e42..e18a42c5d 100644 --- a/src/modules/helper/Helper.cpp +++ b/src/modules/helper/Helper.cpp @@ -1,5 +1,6 @@ #include "faker-cxx/Helper.h" +#include #include #include #include @@ -15,6 +16,12 @@ std::random_device Helper::randomDevice; std::mt19937 Helper::pseudoRandomGenerator(Helper::randomDevice()); +std::string Helper::shuffleString(std::string data) +{ + std::shuffle(data.begin(), data.end(), pseudoRandomGenerator); + return data; +} + std::string Helper::replaceSymbolWithNumber(std::string str, const char& symbol) { for (char& ch : str)