diff --git a/libcron/Cron.cpp b/libcron/Cron.cpp index 6824421..9b97917 100644 --- a/libcron/Cron.cpp +++ b/libcron/Cron.cpp @@ -1,8 +1,3 @@ -// -// Created by permal on 3/8/18. -// - - #include #include "Cron.h" diff --git a/libcron/CronTime.h b/libcron/CronTime.h index 76ffcb1..ad8c013 100644 --- a/libcron/CronTime.h +++ b/libcron/CronTime.h @@ -96,6 +96,9 @@ namespace libcron template bool get_range(const std::string& s, T& low, T& high); + template + bool get_step(const std::string& s, uint8_t& start, uint8_t& step); + template 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(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 + 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(raw_start, raw_start) && raw_step > 0) + { + start = static_cast(raw_start); + step = static_cast(raw_step); + res = true; + } + } + + return res; + } + template void CronTime::add_full_range(std::set& set) { diff --git a/test/test.cpp b/test/test.cpp index d46f5a0..8b96ff8 100644 --- a/test/test.cpp +++ b/test/test.cpp @@ -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)); + } + + } + } } \ No newline at end of file