Commit 26569483 authored by David Faure's avatar David Faure
Browse files

autocorrect wrong use of ';' where ',' was expected

We can detect the common case of
- Mo-Sa 12:00-15:00; 18:00-24:00
+ Mo-Sa 12:00-15:00,18:00-24:00
based on the fact that the second rule has no time selector.

The JS evaluation_tool doesn't do this autocorrect though, and
adds the two time ranges (as if "Mo-Sa 12:00-15:00, Mo-Su 18:00-24:00")
rather than overwriting the first rule with the second as I thought would happen.

It's still very unlikely that this is what the author meant though...

Do the same in case of identical weekdays (and no other selector):
- Mo-Sa 12:00-15:00; Mo-Sa 18:00-24:00
+ Mo-Sa 12:00-15:00,18:00-24:00
parent f7180c97
Pipeline #54705 passed with stage
in 23 seconds
......@@ -309,6 +309,9 @@ private Q_SLOTS:
T2("Mo 07:00-12:00,Tu 15:00-20:00,We 07:00-12:00,Fr 15:00-20:00", "Mo 07:00-12:00, Tu 15:00-20:00, We 07:00-12:00, Fr 15:00-20:00");
T2("Mo-Fr 09:00-17:00 Sa 09:00-14:00", "Mo-Fr 09:00-17:00; Sa 09:00-14:00");
T2("Friday 11AM–2:30AM Saturday 10AM–3:30AM Sunday 9AM–4:30AM", "Fr 11:00-02:30; Sa 10:00-03:30; Su 09:00-04:30");
T2("Mo-Sa 12:00-15:00; 18:00-24:00", "Mo-Sa 12:00-15:00,18:00-24:00");
T2("Mo-Sa 12:00-15:00; Mo-Sa 18:00-24:00", "Mo-Sa 12:00-15:00,18:00-24:00");
T2("Mo 12:00-15:00; Mo 18:00-24:00", "Mo 12:00-15:00,18:00-24:00");
// recovery from wrong time selector separators
T2("Dimanche Fermé Lundi 08:00 – 12:30 14:00 – 19:00 Mardi 08:00 – 12:30 14:00 – 19:00 Mercredi 08:00 – 12:30 14:00 – 19:00 Jeudi 08:00 – 12:30 14:00 – 19:00 Vendredi 08:00 – 12:30 14:00 – 19:00 Samedi 08:00 – 12:30 14:30 – 18:00", "Su closed; Mo 08:00-12:30,14:00-19:00; Tu 08:00-12:30,14:00-19:00; We 08:00-12:30,14:00-19:00; Th 08:00-12:30,14:00-19:00; Fr 08:00-12:30,14:00-19:00; Sa 08:00-12:30,14:30-18:00");
......
......@@ -52,45 +52,68 @@ void OpeningHoursPrivate::autocorrect()
auto rule = (*it).get();
auto prevRule = (*(std::prev(it))).get();
if (rule->m_ruleType != Rule::AdditionalRule || rule->hasComment() || prevRule->hasComment() || !prevRule->hasImplicitState()) {
if (rule->hasComment() || prevRule->hasComment() || !prevRule->hasImplicitState()) {
continue;
}
const auto prevRuleSingleSelector = prevRule->selectorCount() == 1;
const auto curRuleSingleSelector = rule->selectorCount() == 1;
// the previous rule has no time selector, the current rule only has a weekday selector
// so we fold the two rules together
if (!prevRule->m_timeSelector && prevRule->m_weekdaySelector && rule->m_weekdaySelector && !rule->hasWideRangeSelector()) {
auto tmp = std::move(rule->m_weekdaySelector);
rule->m_weekdaySelector = std::move(prevRule->m_weekdaySelector);
rule->m_weekSelector = std::move(prevRule->m_weekSelector);
rule->m_monthdaySelector = std::move(prevRule->m_monthdaySelector);
rule->m_yearSelector = std::move(prevRule->m_yearSelector);
rule->m_colonAfterWideRangeSelector = prevRule->m_colonAfterWideRangeSelector;
auto *selector = rule->m_weekdaySelector.get();
while (selector->rhsAndSelector)
selector = selector->rhsAndSelector.get();
appendSelector(selector, std::move(tmp));
rule->m_ruleType = prevRule->m_ruleType;
std::swap(*it, *std::prev(it));
it = std::prev(m_rules.erase(it));
}
if (rule->m_ruleType == Rule::AdditionalRule) {
// the previous rule has no time selector, the current rule only has a weekday selector
// so we fold the two rules together
if (!prevRule->m_timeSelector && prevRule->m_weekdaySelector && rule->m_weekdaySelector && !rule->hasWideRangeSelector()) {
auto tmp = std::move(rule->m_weekdaySelector);
rule->m_weekdaySelector = std::move(prevRule->m_weekdaySelector);
rule->m_weekSelector = std::move(prevRule->m_weekSelector);
rule->m_monthdaySelector = std::move(prevRule->m_monthdaySelector);
rule->m_yearSelector = std::move(prevRule->m_yearSelector);
rule->m_colonAfterWideRangeSelector = prevRule->m_colonAfterWideRangeSelector;
auto *selector = rule->m_weekdaySelector.get();
while (selector->rhsAndSelector)
selector = selector->rhsAndSelector.get();
appendSelector(selector, std::move(tmp));
rule->m_ruleType = prevRule->m_ruleType;
std::swap(*it, *std::prev(it));
it = std::prev(m_rules.erase(it));
}
// the current rule only has a time selector, so we append that to the previous rule
else if (curRuleSingleSelector && rule->m_timeSelector && prevRule->m_timeSelector) {
appendSelector(prevRule->m_timeSelector.get(), std::move(rule->m_timeSelector));
it = std::prev(m_rules.erase(it));
}
// the current rule only has a time selector, so we append that to the previous rule
else if (curRuleSingleSelector && rule->m_timeSelector && prevRule->m_timeSelector) {
appendSelector(prevRule->m_timeSelector.get(), std::move(rule->m_timeSelector));
it = std::prev(m_rules.erase(it));
}
// previous is a single monthday selector
else if (rule->m_monthdaySelector && prevRuleSingleSelector && prevRule->m_monthdaySelector && !isWiderThan(prevRule, rule)) {
auto tmp = std::move(rule->m_monthdaySelector);
rule->m_monthdaySelector = std::move(prevRule->m_monthdaySelector);
appendSelector(rule->m_monthdaySelector.get(), std::move(tmp));
rule->m_ruleType = prevRule->m_ruleType;
std::swap(*it, *std::prev(it));
it = std::prev(m_rules.erase(it));
// previous is a single monthday selector
else if (rule->m_monthdaySelector && prevRuleSingleSelector && prevRule->m_monthdaySelector && !isWiderThan(prevRule, rule)) {
auto tmp = std::move(rule->m_monthdaySelector);
rule->m_monthdaySelector = std::move(prevRule->m_monthdaySelector);
appendSelector(rule->m_monthdaySelector.get(), std::move(tmp));
rule->m_ruleType = prevRule->m_ruleType;
std::swap(*it, *std::prev(it));
it = std::prev(m_rules.erase(it));
}
} else if (rule->m_ruleType == Rule::NormalRule) {
// Previous rule has time and other selectors
// Current rule is only a time selector
// "Mo-Sa 12:00-15:00; 18:00-24:00" => "Mo-Sa 12:00-15:00,18:00-24:00"
if (curRuleSingleSelector && rule->m_timeSelector
&& prevRule->selectorCount() > 1 && prevRule->m_timeSelector) {
appendSelector(prevRule->m_timeSelector.get(), std::move(rule->m_timeSelector));
it = std::prev(m_rules.erase(it));
}
// Both rules have exactly the same selector apart from time
// For now this only supports weekday selectors, could be extended
else if (rule->selectorCount() == prevRule->selectorCount()
&& rule->m_timeSelector
&& prevRule->m_timeSelector
&& rule->selectorCount() == 2 && rule->m_weekdaySelector
// slower than writing an operator==, but so much easier to write :)
&& rule->m_weekdaySelector->toExpression() == prevRule->m_weekdaySelector->toExpression()
) {
appendSelector(prevRule->m_timeSelector.get(), std::move(rule->m_timeSelector));
it = std::prev(m_rules.erase(it));
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment