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

antonvw / wex / 20440555753

22 Dec 2025 06:26PM UTC coverage: 64.31% (-0.01%) from 64.322%
20440555753

push

github

antonvw
final updates

18531 of 31589 branches covered (58.66%)

Branch coverage included in aggregate %.

14764 of 20184 relevant lines covered (73.15%)

1512.67 hits per line

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

91.89
/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/factory/unified-diff.h>
11

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

14
namespace bp = boost::parser;
15

16
wex::factory::unified_diff_parser::unified_diff_parser(unified_diff* diff)
14✔
17
  : m_diff(diff)
14✔
18
{
19
  m_diff->m_range.fill({0});
14✔
20
  m_diff->m_diffs    = 0;
14✔
21
  m_diff->m_is_first = true;
14✔
22
  m_diff->m_is_last  = false;
14✔
23
  m_diff->m_type     = unified_diff::diff_t::UNKNOWN;
14✔
24
}
14✔
25

26
bool wex::factory::unified_diff_parser::parse()
14✔
27
{
28
  auto const action_diff = [this](const auto& ctx)
14✔
29
  {
30
    const auto tpl    = _attr(ctx);
14✔
31
    m_diff->m_path[0] = wex::path(std::get<0>(tpl));
14✔
32
    m_diff->m_path[1] = wex::path(std::get<1>(tpl));
14✔
33
    m_diff->m_range.fill({0});
14✔
34

35
    for (const auto& hunk : std::get<2>(tpl))
34✔
36
    {
37
      int index = 0;
20✔
38
      m_hunk_no++;
20✔
39

40
      for (const auto& number : std::get<0>(hunk))
60✔
41
      {
42
        if (const auto* val = std::get_if<std::tuple<int, int>>(&number); val)
40✔
43
        {
44
          const int range            = std::get<1>(*val);
14✔
45
          m_diff->m_range[index]     = std::abs(std::get<0>(*val));
14✔
46
          m_diff->m_range[index + 1] = range;
14✔
47

48
          if (range > 0)
14✔
49
          {
50
            m_diff->m_diffs++;
4✔
51
          }
52
        }
53
        else
54
        {
55
          m_diff->m_range[index]     = std::abs(std::get<int>(number));
26✔
56
          m_diff->m_range[index + 1] = 1;
26✔
57
          m_diff->m_diffs++;
26✔
58
        }
59

60
        index += 2;
40✔
61
      }
62

63
      if (m_hunk_no > 1)
20✔
64
      {
65
        m_diff->m_is_first = false;
13✔
66
      }
67

68
      m_diff->m_text.fill({});
20✔
69

70
      for (const auto& line : std::get<1>(hunk))
68✔
71
      {
72
        auto fix(line);
48✔
73

74
        if (line.starts_with("\n"))
48✔
75
        {
76
          fix = fix.substr(1);
8✔
77
        }
78

79
        switch (fix[0])
48!
80
        {
81
          case '+':
13✔
82
            m_diff->m_text[1].push_back(fix.substr(1));
13✔
83
            break;
13✔
84
          case '-':
20✔
85
            m_diff->m_text[0].push_back(fix.substr(1));
20✔
86
            break;
20✔
87
          case ' ':
×
88
            m_diff->m_text[0].push_back(fix.substr(1));
×
89
            m_diff->m_text[1].push_back(fix.substr(1));
×
90
            break;
×
91
        }
92
      }
93

94
      m_diff->m_type =
20✔
95
        (m_diff->m_type == unified_diff::diff_t::UNKNOWN ?
20✔
96
           unified_diff::diff_t::FIRST :
97
           unified_diff::diff_t::OTHER);
98

99
      m_diff->report_diff();
20✔
100
      m_diff->trace("found");
60✔
101
    }
102
  };
28✔
103

104
  auto const action_eoi = [this](const auto& ctx)
7✔
105
  {
106
    m_diff->m_is_last = true;
7✔
107
    m_diff->m_type    = unified_diff::diff_t::LAST;
7✔
108
    m_diff->report_diff_finish();
7✔
109
    m_diff->trace("finish");
14✔
110
  };
21✔
111

112
  // (Skip the first lines)
113
  // The unified output format starts with a two-line header:
114
  // --- from-file from-file-modification-time
115
  // +++ to-file to-file-modification-time
116
  // Next come one or more hunks of differences:
117
  // @@ from-file-line-numbers to-file-line-numbers @@
118
  // line-from-either-file
119
  // line-from-either-file...
120

121
  auto const parser_diff_lines = bp::lexeme[+(
14✔
122
    bp::char_ >> +(bp::char_ - bp::eol - "--- a/" - "@@" - "diff --"))];
123

124
  auto const parser_hunk =
125
    bp::lit("@@") >> bp::repeat(2)[bp::int_ >> ',' >> bp::int_ | bp::int_] >>
14✔
126
    bp::lit("@@") >> bp::lexeme[+(bp::char_ - bp::eol)] >> parser_diff_lines;
14✔
127

128
  auto const parser_diff = bp::lit("--- a/") >> +(bp::char_ - "+++ b/") >>
14✔
129
                           bp::lit("+++ b/") >> +(bp::char_ - "@@") >>
14✔
130
                           +parser_hunk;
14✔
131

132
  auto const parser_skip = bp::omit[*(bp::char_ - "--- a/")];
14✔
133

134
  auto const parser_all =
135
    +(parser_skip >> +parser_diff[action_diff]) >> bp::eoi[action_eoi];
14✔
136

137
  return bp::parse(m_diff->input(), parser_all, bp::ws);
28✔
138
}
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