• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

antonvw / wex / 20407431017

21 Dec 2025 08:54AM UTC coverage: 64.344% (-0.002%) from 64.346%
20407431017

push

github

antonvw
use boost trace if log level is trace

18617 of 31694 branches covered (58.74%)

Branch coverage included in aggregate %.

14779 of 20208 relevant lines covered (73.13%)

1510.99 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

62.5
/src/factory/unified-diff-parser.cpp
1
////////////////////////////////////////////////////////////////////////////////
2
// Name:      unified-diff-parser.cpp
3
// Purpose:   Implementation of unified_diff_parser
4
//            https://www.gnu.org/software/diffutils/manual/html_node/Detailed-Unified.html
5
// Author:    Anton van Wezenbeek
6
// Copyright: (c) 2025 Anton van Wezenbeek
7
////////////////////////////////////////////////////////////////////////////////
8

9
#include <boost/parser/parser.hpp>
10
#include <wex/core/log.h>
11
#include <wex/factory/unified-diff.h>
12

13
#include "unified-diff-parser.h"
14

15
namespace bp = boost::parser;
16

17
enum action_t_str
18
{
19
  ACTION_DEL,
20
  ACTION_ADD,
21
  ACTION_BOTH,
22
};
23

24
struct diff
25
{
26
  std::string path_a, path_b;
27

28
  std::vector<std::vector<std::variant<std::pair<int, int>, int>>> hunks;
29

30
  std::vector<std::string> files;
31
};
32

33
wex::factory::unified_diff_parser::unified_diff_parser(unified_diff* diff)
3✔
34
  : m_diff(diff)
3✔
35
{
36
  m_diff->m_diffs = 0;
3✔
37
  m_diff->m_type  = unified_diff::diff_t::UNKNOWN;
3✔
38
}
3✔
39

40
bool wex::factory::unified_diff_parser::parse()
3✔
41
{
42
  bp::symbols<int> const actions = {
43
    {"-", ACTION_DEL},
×
44
    {"+", ACTION_ADD},
×
45
    {" ", ACTION_BOTH}};
3✔
46

47
  auto const action = [this](auto& ctx)
2✔
48
  {
49
    std::cout << "Got one!\n";
2✔
50
    diff x;
2✔
51

52
    m_diff->m_path[0] = wex::path(x.path_a);
2✔
53
    m_diff->m_path[1] = wex::path(x.path_b);
2✔
54

55
    for (const auto& hunk : x.hunks)
2!
56
    {
57
      int index = 0;
×
58

59
      for (const auto& number : hunk)
×
60
      {
61
        if (const auto* val = std::get_if<std::pair<int, int>>(&number); val)
×
62
        {
63
          m_diff->m_range[index]     = val->first;
×
64
          m_diff->m_range[index + 1] = val->second;
×
65
        }
66
        else
67
        {
68
          m_diff->m_range[index]     = std::get<int>(number);
×
69
          m_diff->m_range[index + 1] = 1;
×
70
        }
71

72
        index += 2;
×
73
      }
74

75
      m_diff->m_type =
×
76
        (m_diff->m_type == unified_diff::diff_t::UNKNOWN ?
×
77
           unified_diff::diff_t::FIRST :
78
           unified_diff::diff_t::OTHER);
79

80
      m_diff->report_diff();
×
81
    }
82
  };
5✔
83

84
  // (Skip the first lines)
85
  // The unified output format starts with a two-line header:
86
  // --- from-file from-file-modification-time
87
  // +++ to-file to-file-modification-time
88
  // Next come one or more hunks of differences:
89
  // @@ from-file-line-numbers to-file-line-numbers @@
90
  // line-from-either-file
91
  // line-from-either-file...
92

93
  auto const skip_lines = bp::omit[*(bp::char_ - "--- a/")];
3✔
94

95
  auto const parser_diff =
3✔
96
    bp::lit("--- a/") >> +(bp::char_ - "+++ b/") >> bp::lit("+++ b/") >>
97
    +(bp::char_ - "@@") >>
98
    +(bp::lit("@@") >> bp::repeat(2)[bp::int_ >> ',' >> bp::int_ | bp::int_] >>
99
      bp::lit("@@")) >>
100
    *(bp::char_ - bp::eol); // >> bp::eol >>
101
                            //    +(bp::char_);
102
                            //    +(actions >> +bp::char_);
103

104
  auto const action_parser = skip_lines >> +(parser_diff[action]);
3✔
105

106
  if (const auto result = bp::parse(
6!
107
        m_diff->input(),
3✔
108
        action_parser,
109
        bp::ws,
110
        log::get_level() == log::level_t::TRACE ? bp::trace::on : bp::trace::off);
3✔
111
      result)
112
  {
113
    return true;
2✔
114
  }
115

116
  return false;
1✔
117
}
3✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc