LCOV - code coverage report
Current view: top level - src - Regex.cpp (source / functions) Hit Total Coverage
Test: lcov.info Lines: 9 25 36.0 %
Date: 2025-06-01 01:13:32 Functions: 3 4 75.0 %
Branches: 0 0 -

           Branch data     Line data    Source code
       1                 :            : /*
       2                 :            : ** Copyright (C) 2021 Sylvain Fargier
       3                 :            : **
       4                 :            : ** This software is provided 'as-is', without any express or implied
       5                 :            : ** warranty.  In no event will the authors be held liable for any damages
       6                 :            : ** arising from the use of this software.
       7                 :            : **
       8                 :            : ** Permission is granted to anyone to use this software for any purpose,
       9                 :            : ** including commercial applications, and to alter it and redistribute it
      10                 :            : ** freely, subject to the following restrictions:
      11                 :            : **
      12                 :            : ** 1. The origin of this software must not be misrepresented; you must not
      13                 :            : **    claim that you wrote the original software. If you use this software
      14                 :            : **    in a product, an acknowledgment in the product documentation would be
      15                 :            : **    appreciated but is not required.
      16                 :            : ** 2. Altered source versions must be plainly marked as such, and must not be
      17                 :            : **    misrepresented as being the original software.
      18                 :            : ** 3. This notice may not be removed or altered from any source distribution.
      19                 :            : **
      20                 :            : ** Created on: 2021-10-18T18:44:27+02:00
      21                 :            : **     Author: Sylvain Fargier <fargie_s> <fargier.sylvain@free.fr>
      22                 :            : **
      23                 :            : */
      24                 :            : 
      25                 :            : #include "Regex.hpp"
      26                 :            : 
      27                 :            : #include <stdexcept>
      28                 :            : 
      29                 :            : namespace logger {
      30                 :            : 
      31                 :          7 : Regex::Regex(const std::string &re, int flags)
      32                 :            : {
      33                 :          7 :     int code = regcomp(&m_regex, re.c_str(), flags);
      34                 :          7 :     if (code != 0)
      35                 :            :     {
      36                 :          0 :         size_t sz = regerror(code, &m_regex, nullptr, 0);
      37                 :          0 :         std::vector<char> err(sz + 1);
      38                 :          0 :         regerror(code, &m_regex, err.data(), err.size());
      39                 :          0 :         throw std::invalid_argument(std::string("invalid regex: ") + err.data());
      40                 :          0 :     }
      41                 :          7 : }
      42                 :            : 
      43                 :          7 : Regex::~Regex()
      44                 :            : {
      45                 :          7 :     ::regfree(&m_regex);
      46                 :          7 : }
      47                 :            : 
      48                 :          0 : bool Regex::match(const std::string &str,
      49                 :            :                   std::vector<std::string> &matches) const
      50                 :            : {
      51                 :          0 :     std::vector<regmatch_t> pmatch(matches.capacity() ? matches.capacity() :
      52                 :          0 :                                                         Regex::MaxMatchDefault);
      53                 :          0 :     matches.resize(0);
      54                 :          0 :     if (regexec(&m_regex, str.c_str(), pmatch.size(), pmatch.data(), 0) != 0)
      55                 :          0 :         return false;
      56                 :            : 
      57                 :          0 :     for (const regmatch_t &m : pmatch)
      58                 :            :     {
      59                 :          0 :         if (m.rm_so >= 0 && m.rm_eo >= 0)
      60                 :            :         {
      61                 :          0 :             matches.push_back(str.substr(m.rm_so, m.rm_eo - m.rm_so));
      62                 :            :         }
      63                 :            :     }
      64                 :          0 :     return true;
      65                 :          0 : }
      66                 :            : 
      67                 :         10 : bool Regex::search(const std::string &str) const
      68                 :            : {
      69                 :         10 :     return regexec(&m_regex, str.c_str(), 0, nullptr, 0) == 0;
      70                 :            : }
      71                 :            : 
      72                 :            : } // namespace logger

Generated by: LCOV version 1.14