Skip to content

Commit

Permalink
Fix patterns of kind [aab]; rnd.next(char*, ...)
Browse files Browse the repository at this point in the history
  • Loading branch information
ifsmirnov committed Mar 21, 2017
1 parent fbd2fd3 commit 7a922cf
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 16 deletions.
4 changes: 2 additions & 2 deletions common.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,15 @@ while (false)
namespace jngen {

template<typename ... Args>
std::string format(const std::string& format, Args... args) {
std::string format(const std::string& fmt, Args... args) {
constexpr static char BUF_SIZE = 64;
static char BUFFER[BUF_SIZE];

int bufSize = BUF_SIZE;
char *buf = BUFFER;

while (true) {
int ret = snprintf(buf, bufSize, format.c_str(), args...);
int ret = snprintf(buf, bufSize, fmt.c_str(), args...);
if (ret < bufSize) {
break;
}
Expand Down
22 changes: 14 additions & 8 deletions jngen.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,15 @@ while (false)
namespace jngen {

template<typename ... Args>
std::string format(const std::string& format, Args... args) {
std::string format(const std::string& fmt, Args... args) {
constexpr static char BUF_SIZE = 64;
static char BUFFER[BUF_SIZE];

int bufSize = BUF_SIZE;
char *buf = BUFFER;

while (true) {
int ret = snprintf(buf, bufSize, format.c_str(), args...);
int ret = snprintf(buf, bufSize, fmt.c_str(), args...);
if (ret < bufSize) {
break;
}
Expand Down Expand Up @@ -165,6 +165,7 @@ void getopts(int argc, char *argv[], Args& ...args) {
using jngen::getopts;


#include <algorithm>
#include <cctype>
#include <functional>
#include <set>
Expand Down Expand Up @@ -298,7 +299,7 @@ class Parser {
}

std::vector<char> parseBlock() {
std::set<char> allowed;
std::vector<char> allowed;
char last = -1;
bool inRange = false;
while (control(peek()) != ']') {
Expand All @@ -311,13 +312,13 @@ class Parser {
} else if (inRange) {
ensure(c >= last);
for (char i = last; i <= c; ++i) {
allowed.insert(i);
allowed.push_back(i);
}
inRange = false;
last = -1;
} else {
if (last != -1) {
allowed.insert(last);
allowed.push_back(last);
}
last = c;
}
Expand All @@ -327,10 +328,11 @@ class Parser {

ensure(!inRange);
if (last != -1) {
allowed.insert(last);
allowed.push_back(last);
}

return std::vector<char>(allowed.begin(), allowed.end());
std::sort(allowed.begin(), allowed.end());
return allowed;
}

Pattern parsePattern() {
Expand Down Expand Up @@ -546,6 +548,11 @@ class Random {
return Pattern(pattern).next([this](int n) { return next(n); });
}

template<typename ... Args>
std::string next(const std::string& pattern, Args... args) {
return next(format(pattern, args...));
}

template<typename T, typename ... Args>
T tnext(Args... args) {
return TypedRandom<T>{*this}.next(args...);
Expand Down Expand Up @@ -653,7 +660,6 @@ struct TypedRandom : public BaseTypedRandom {
T next(Args... args) { return random.next(args...); }
};


struct OrderedPairTag {} opair;

template<>
Expand Down
12 changes: 7 additions & 5 deletions pattern.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#include "common.h"

#include <algorithm>
#include <cctype>
#include <functional>
#include <set>
Expand Down Expand Up @@ -135,7 +136,7 @@ class Parser {
}

std::vector<char> parseBlock() {
std::set<char> allowed;
std::vector<char> allowed;
char last = -1;
bool inRange = false;
while (control(peek()) != ']') {
Expand All @@ -148,13 +149,13 @@ class Parser {
} else if (inRange) {
ensure(c >= last);
for (char i = last; i <= c; ++i) {
allowed.insert(i);
allowed.push_back(i);
}
inRange = false;
last = -1;
} else {
if (last != -1) {
allowed.insert(last);
allowed.push_back(last);
}
last = c;
}
Expand All @@ -164,10 +165,11 @@ class Parser {

ensure(!inRange);
if (last != -1) {
allowed.insert(last);
allowed.push_back(last);
}

return std::vector<char>(allowed.begin(), allowed.end());
std::sort(allowed.begin(), allowed.end());
return allowed;
}

Pattern parsePattern() {
Expand Down
6 changes: 5 additions & 1 deletion random.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,11 @@ class Random {
return Pattern(pattern).next([this](int n) { return next(n); });
}

template<typename ... Args>
std::string next(const std::string& pattern, Args... args) {
return next(format(pattern, args...));
}

template<typename T, typename ... Args>
T tnext(Args... args) {
return TypedRandom<T>{*this}.next(args...);
Expand Down Expand Up @@ -241,7 +246,6 @@ struct TypedRandom : public BaseTypedRandom {
T next(Args... args) { return random.next(args...); }
};


struct OrderedPairTag {} opair;

template<>
Expand Down

0 comments on commit 7a922cf

Please sign in to comment.