Skip to content

Commit

Permalink
feature: Implemented Helper::shuffleString() in Helper module
Browse files Browse the repository at this point in the history
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
  • Loading branch information
braw-lee committed Nov 20, 2023
1 parent ac58ac2 commit a2b3d95
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 0 deletions.
13 changes: 13 additions & 0 deletions include/faker-cxx/Helper.h
Original file line number Diff line number Diff line change
Expand Up @@ -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

/**
Expand Down
7 changes: 7 additions & 0 deletions src/modules/helper/Helper.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "faker-cxx/Helper.h"

#include <algorithm>
#include <chrono>
#include <random>
#include <regex>
Expand All @@ -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)
Expand Down

0 comments on commit a2b3d95

Please sign in to comment.