Untitled

mail@pastecode.io avatar
unknown
c_cpp
2 years ago
1.9 kB
3
Indexable
Never
namespace filter_functions
     {
          uint32_t SrcDstIpFunc(const std::string& s)
          {
               return static_cast<uint32_t>(StrToIp(s));
          }
          uint32_t SrcDstIdFunc(const std::string& s)
          {
               return static_cast<uint32_t>(std::stoull(s, nullptr, 16));
          }
     }
     template <typename T>
     std::set<Range<T>> GetSet(const std::string& s, std::function<T(const std::string)> func)
     {
          std::set<Range<T>> res;
          std::size_t k =0;
          for(std::size_t j =k; j<s.size(); j++)
          {
               if(s[j] == '-')
               {
                    T first_token = func(s.substr(k,j));             
                    k = j+1;
                    while(j < s.size() && s[j] != ',')
                         j++;
                    T second_token = func(s.substr(k,j));
                    k = j+1;
                    auto ret = res.insert(Range<T>{first_token, second_token});
                    if(!ret.second)
                    {
                         throw std::runtime_error{"GetSet: can't insert value"};
                    }
               }
               else if(s[j] == ',')
               {
                    T token = func(s.substr(k,j));
                    k = j+1;
                    auto ret = res.insert(Range<T>{token,token});
                    if(!ret.second)
                    {
                         throw std::runtime_error{"GetSet: can't insert value"};
                    }
               }
               else if(j == s.size() -1 && k<j)
               {
                    T token = func(s.substr(k,j+1));
                    auto ret = res.insert(Range<T>{token,token});
                    if(!ret.second)
                    {
                         throw std::runtime_error{"GetSet: can't insert value"};
                    }
               }
          }
          return res;
     }