Skip to content

Commit

Permalink
Added rand.h implementation
Browse files Browse the repository at this point in the history
Supported types for now:
- Numbers: Int, Long, Long Long, Double
- Chars: Char, Lower, Upper, Alpha, Digit
- String
  • Loading branch information
NouemanKHAL authored and chermehdi committed Jan 14, 2021
1 parent 1e2afcc commit 58c0575
Showing 1 changed file with 129 additions and 3 deletions.
132 changes: 129 additions & 3 deletions templates/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package templates
import (
"errors"
"fmt"
"github.com/chermehdi/egor/config"
"io/ioutil"

"github.com/chermehdi/egor/config"
)

const BruteH = `
Expand All @@ -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 <ctime>
#include <string>
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 <char> (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;
}
};
`

Expand Down

0 comments on commit 58c0575

Please sign in to comment.