From 58c05754abed4578f165d469873ff34a96b9880e Mon Sep 17 00:00:00 2001 From: NouemanKHAL Date: Tue, 12 Jan 2021 01:55:20 +0100 Subject: [PATCH] Added rand.h implementation Supported types for now: - Numbers: Int, Long, Long Long, Double - Chars: Char, Lower, Upper, Alpha, Digit - String --- templates/resolver.go | 132 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 129 insertions(+), 3 deletions(-) diff --git a/templates/resolver.go b/templates/resolver.go index 0df558f..598220f 100644 --- a/templates/resolver.go +++ b/templates/resolver.go @@ -3,8 +3,9 @@ package templates import ( "errors" "fmt" - "github.com/chermehdi/egor/config" "io/ioutil" + + "github.com/chermehdi/egor/config" ) const BruteH = ` @@ -22,10 +23,135 @@ int main() { // We could consider using some new Go feature to embed it as a static resource. // At the time of creation of this, this is not a priority. const RandH = ` +#pragma once +#include +#include + +const std::string LOWERCASE_ALPHABET = "abcdefghijklmnopqrstuvwyxz"; +const std::string UPPERCASE_ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWYXZ"; +const std::string DIGITS_SET = "0123456789"; + +// Used to determine the set of allowed characters +// for the String method of the Rand class +const int LOWER = 1; +const int UPPER = 1 << 1; +const int DIGITS = 1 << 2; + +// Returns a random value of the following types +// int, long long, double, char, string +// given main arguments +// Example: +// Rand rand(argc, argv); +// int randomInt = rand.Int(); +// string randomString = rand.String(10, DIGITS); class Rand { + unsigned int seed; + + unsigned int ReadSeedFromArgs(int argc, char * argv[]) { + if (argc > 0) { + return std::stoi(argv[1]); + } + return 0; + } + public: - Rand(int argc, char** argv) { - } + + Rand(unsigned int _seed = 0) : seed(_seed) { + srand(seed); + } + + Rand(int argc, char * argv[]) { + unsigned int seed = ReadSeedFromArgs(argc, argv); + Rand{ seed }; + } + + // Returns an random int value in the range [from, to] inclusive + int Int(int from, int to) { + return rand() % (to - from) + from; + } + + // Returns an random long long value in the range [from, to] inclusive + long long Long(long long from, long long to) { + return rand() % (to - from) + from; + } + + // Returns an random double value in the range [from, to] inclusive + double Double(double from, double to) { + double tmp = (double)rand() / RAND_MAX; + return from + tmp * (to - from); + } + + // Returns an random char value in the range [from, to] inclusive + // Parameters are optional + char Char(char from = CHAR_MIN, char to = CHAR_MAX) { + return static_cast (Int(from, to)); + } + + // Returns an random char value in the range [from, to] inclusive + // Parameters are optional, by default returns a random lowercase letter + char Lower(char from = 'a', char to = 'z') { + return Char(from, to); + } + + // Returns an random char value in the range [from, to] inclusive + // Parameters are optional, by default returns a random uppercase letter + char Upper(char from = 'A', char to = 'Z') { + return Char(from, to); + } + + // Returns an random letter either in lowercase or uppercase + char Alpha() { + int k = rand() % 52; + if (k < 26) return Lower('a', 'a' + k); + return Upper('a', 'a' + k % 26); + } + + // Returns an random digit character + // Parameters are optional, by default returns a random digit character in the range ['0', '9'] inclusive + char Digit(char from = '0', char to = '9') { + return Char(from, to); + } + + // Returns a random alphanumerical character + char AlphaNum() { + if (rand() & 1) return Alpha(); + return Digit(); + } + + + // Returns an std::string of length size consisting only of characters allowed in the given mask + // using the constants LOWER, UPPER, DIGITS + // Example: + // Rand rand(argc, argv); + // std::string str = rand.String(10, LOWER | DIGITS); + // str is an std::string of size 10 consisting only of lowercase letters and digits + std::string String(size_t size, const int mask = LOWER | UPPER) { + std::string charset; + + // Building the set of allowed charset from the mask + if (mask & LOWER) { + charset += LOWERCASE_ALPHABET; + } + if (mask & UPPER) { + charset += UPPERCASE_ALPHABET; + } + if (mask & DIGITS) { + charset += DIGITS_SET; + } + + std::random_shuffle(charset.begin(), charset.end()); + + std::string res(size, ' '); + + int len = charset.size(); + + for (char& c : res) { + size_t randomIndex = Int(0, len - 1); + c = charset[randomIndex]; + } + + return res; + } }; `