|
|
- #pragma once
- #include "tool.h"
- #include <deque>
- #include <regex>
-
- class filter_numbers : public tool
- {
- std::optional<std::shared_ptr<tool>> n;
- tool_env me;
- std::deque<std::string> queue;
- bool filter_in = true;
- public:
- filter_numbers(tool_env t)
- : me{t}
- {}
-
- virtual void init(std::optional<std::shared_ptr<tool>> next)
- {
- n = next;
- if(me.find("reverse")!=me.end())
- filter_in = false;
- }
- virtual void execute()
- {
- if(has_queue())
- {
- std::string v = queue.front();
- queue.pop_front();
- bool point_sigil = false;
- auto it = v.begin();
- if(*it=='-' || *it=='+')
- {
- ++it;
- }
- for(;it!=v.end();++it)
- {
- if(!isdigit(*it))
- {
- if(*it == '.')
- {
- if(point_sigil)
- {
- if(n.has_value() && !filter_in)
- n.value()->enqueue(v);
- return;
- }
- else
- {
- point_sigil = true;
- }
- }
- else
- {
- if(n.has_value() && !filter_in)
- n.value()->enqueue(v);
- return;
- }
- }
- }
-
- if(n.has_value() && filter_in)
- n.value()->enqueue(v);
- }
- }
- virtual void enqueue(std::string v)
- {
- queue.push_back(v);
- }
- virtual bool has_queue()
- {
- return queue.size()!=0;
- }
- };
-
- class filter_in : public tool
- {
- std::optional<std::shared_ptr<tool>> n;
- tool_env me;
- std::deque<std::string> queue;
- std::regex pattern;
- public:
- filter_in(tool_env t)
- : me{t}
- {}
-
- virtual void init(std::optional<std::shared_ptr<tool>> next)
- {
- n = next;
- pattern = std::regex(me["pattern"]);
- }
- virtual void execute()
- {
- if(has_queue())
- {
- if(n.has_value() && std::regex_match(queue.front(), pattern))
- n.value()->enqueue(queue.front());
- queue.pop_front();
- }
- }
- virtual void enqueue(std::string v)
- {
- queue.push_back(v);
- }
- virtual bool has_queue()
- {
- return queue.size()!=0;
- }
- };
|