Added support for step-syntax.

This commit is contained in:
Per Malmberg 2018-03-08 14:53:29 -08:00
parent 2071035acf
commit 40b418942f
3 changed files with 68 additions and 5 deletions

View File

@ -1,8 +1,3 @@
//
// Created by permal on 3/8/18.
//
#include <functional>
#include "Cron.h"

View File

@ -96,6 +96,9 @@ namespace libcron
template<typename T>
bool get_range(const std::string& s, T& low, T& high);
template<typename T>
bool get_step(const std::string& s, uint8_t& start, uint8_t& step);
template<typename T>
uint8_t value_of(T t)
{
@ -167,6 +170,8 @@ namespace libcron
T left;
T right;
uint8_t step_start;
uint8_t step;
for (const auto& p : parts)
{
@ -204,6 +209,14 @@ namespace libcron
}
}
}
else if (get_step<T>(p, step_start, step))
{
// Add from step_start to T::Last with a step of 'step'
for (auto v = step_start; v <= value_of(T::Last); v += step)
{
res &= add_number(numbers, v);
}
}
else
{
res = false;
@ -240,6 +253,33 @@ namespace libcron
return res;
}
template<typename T>
bool CronTime::get_step(const std::string& s, uint8_t& start, uint8_t& step)
{
bool res = false;
auto value_range = R"#((\d+)/(\d+))#";
std::regex range(value_range, std::regex_constants::ECMAScript);
std::smatch match;
if (std::regex_match(s.begin(), s.end(), match, range))
{
auto raw_start = std::stoi(match[1].str().c_str());
auto raw_step = std::stoi(match[2].str().c_str());
if(is_within_limits<T>(raw_start, raw_start) && raw_step > 0)
{
start = static_cast<uint8_t>(raw_start);
step = static_cast<uint8_t>(raw_step);
res = true;
}
}
return res;
}
template<typename T>
void CronTime::add_full_range(std::set<T>& set)
{

View File

@ -195,4 +195,32 @@ SCENARIO("Literal input")
}
}
}
}
SCENARIO("Using step syntax")
{
GIVEN("Step inputs")
{
WHEN("Using literal ranges")
{
THEN("Range is valid")
{
auto c = CronTime::create("* * * * JAN/2 *");
REQUIRE(c.is_valid());
REQUIRE(has_value_range(c.months, 1, 1));
REQUIRE(has_value_range(c.months, 3, 3));
REQUIRE(has_value_range(c.months, 5, 5));
REQUIRE(has_value_range(c.months, 7, 7));
REQUIRE(has_value_range(c.months, 9, 9));
REQUIRE(has_value_range(c.months, 11, 11));
REQUIRE_FALSE(has_any_in_range(c.months, 2, 2));
REQUIRE_FALSE(has_any_in_range(c.months, 4, 4));
REQUIRE_FALSE(has_any_in_range(c.months, 6, 6));
REQUIRE_FALSE(has_any_in_range(c.months, 8, 8));
REQUIRE_FALSE(has_any_in_range(c.months, 10, 10));
REQUIRE_FALSE(has_any_in_range(c.months, 12, 12));
}
}
}
}