Files
PowerToys/src/modules/powerrename/lib/Randomizer.h
Jaakko Hirvioja c148b51698 [PowerRename]Add random string values to file names (#32836)
* Add randomizer cheat sheet texts to UI tooltip

* Add randomizer icon (shuffle) + hint to main window

* Add randomizer logic + helpers, regex parsing

* Fix: remove unnecessary throw

* Fix: remove todo comment

* Fix: iffing logic

* Fix: add offset to randomizer onchange

* Update: guid generating to single function, handle bracket removing there

* Update: toggle off enum feat when random values are selected

* Update: main window UI tooltip texts to be more descriptive

* Update: remove unnecessary sstream include

* Fix: return empty string if chars has no value to avoid memory access violation

* Add unit tests

* Add PowerRename random string generating keywords

* Update: generating value names to be in line with POSIX conventions

* Allow to used with Enumerate at the same time

* Fix spellcheck

* Fix tests to take into account we no longer eat up empty expressions
with randomizer

---------

Co-authored-by: Jaime Bernardo <jaime@janeasystems.com>
2024-06-20 16:26:31 +01:00

76 lines
1.7 KiB
C++

#pragma once
#include "pch.h"
#include "Helpers.h"
#include <common\utils\string_utils.h>
struct ReplaceStrSpan
{
size_t offset = 0;
size_t length = 0;
};
struct RandomizerOptions
{
std::optional<int> length;
std::optional<boolean> alnum;
std::optional<boolean> alpha;
std::optional<boolean> digit;
std::optional<boolean> uuid;
ReplaceStrSpan replaceStrSpan;
bool isValid() const
{
return alnum.has_value() || alpha.has_value() || digit.has_value() || uuid.has_value();
}
};
std::vector<RandomizerOptions> parseRandomizerOptions(const std::wstring& replaceWith);
struct Randomizer
{
RandomizerOptions options;
inline Randomizer(RandomizerOptions opts) :
options(opts) {}
std::string randomize() const
{
std::string chars;
if (options.uuid.value_or(false))
{
return unwide(CreateGuidStringWithoutBrackets());
}
if (options.alnum.value_or(false))
{
chars += "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
}
if (options.alpha.value_or(false))
{
chars += "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
}
if (options.digit.value_or(false))
{
chars += "0123456789";
}
if (chars.empty())
{
return "";
}
std::string result;
std::random_device rd;
std::mt19937 generator(rd());
std::uniform_int_distribution<> distribution(0, static_cast<int>(chars.size()) - 1);
for (int i = 0; i < options.length.value_or(10); ++i)
{
result += chars[distribution(generator)];
}
return result;
}
};