xaizek / dit (License: GPLv3) (since 2018-12-07)
Command-line task keeper that remembers all old values and is meant to combine several orthogonal features to be rather flexible in managing items.
Commit efb8eada2ce6d9456727593c666d0bdb94573df3

Refactor ItemFilter::passes()
Code structure with the `switch`-statement is much less obvious and more
verbose.
Author: xaizek
Author date (UTC): 2018-10-31 21:16
Committer name: xaizek
Committer date (UTC): 2018-10-31 21:17
Parent(s): e297f7142899961d71d3570e23015720cd8b0dfd
Signing key: 99DC5E4DB05F6BE2
Tree: a3842e4dab69c05e20290f835cacafa4550a8e55
File Lines added Lines deleted
src/ItemFilter.cpp 13 23
File src/ItemFilter.cpp changed (mode: 100644) (index df4eba0..46c3eff)
... ... ItemFilter::passes(std::function<std::string(const std::string &)> accessor,
70 70 { {
71 71 error.clear(); error.clear();
72 72
73 auto test = [](const Cond &cond, const std::string &val) {
74 switch (cond.op) {
75 case Op::eq: return (val == cond.value);
76 case Op::ne: return (val != cond.value);
77 case Op::iccontains: return boost::icontains(val, cond.value);
78 case Op::icnotcontain: return !boost::icontains(val, cond.value);
79 }
80 assert(false && "Unhandled operation type.");
81 return false;
82 };
83
73 84 auto err = [&error](const Cond &cond) { auto err = [&error](const Cond &cond) {
74 85 if (!error.empty()) { if (!error.empty()) {
75 86 error += '\n'; error += '\n';
 
... ... ItemFilter::passes(std::function<std::string(const std::string &)> accessor,
78 89 }; };
79 90
80 91 for (const Cond &cond : conds) { for (const Cond &cond : conds) {
81 const std::string &val = accessor(cond.key);
82 switch (cond.op) {
83 case Op::eq:
84 if (val != cond.value) {
85 err(cond);
86 }
87 continue;
88 case Op::ne:
89 if (val == cond.value) {
90 err(cond);
91 }
92 continue;
93 case Op::iccontains:
94 if (!boost::icontains(val, cond.value)) {
95 err(cond);
96 }
97 continue;
98 case Op::icnotcontain:
99 if (boost::icontains(val, cond.value)) {
100 err(cond);
101 }
102 continue;
92 if (!test(cond, accessor(cond.key))) {
93 err(cond);
103 94 } }
104 assert(false && "Unhandled operation type.");
105 95 } }
106 96
107 97 return error.empty(); return error.empty();
Hints

Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://code.reversed.top/user/xaizek/dit

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@code.reversed.top/user/xaizek/dit

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a pull request:
... clone the repository ...
... make some changes and some commits ...
git push origin master