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

antonvw / wex / 20878323683

10 Jan 2026 12:29PM UTC coverage: 64.282% (-0.01%) from 64.295%
20878323683

push

github

antonvw
vcs_diff update

18536 of 31612 branches covered (58.64%)

Branch coverage included in aggregate %.

14760 of 20185 relevant lines covered (73.12%)

1516.12 hits per line

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

92.31
/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-2026 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
wex::factory::unified_diff_parser::unified_diff_parser(unified_diff* diff)
14✔
18
  : m_diff(diff)
14✔
19
{
20
  m_diff->m_range.fill({0});
14✔
21
  m_diff->m_diffs    = 0;
14✔
22
  m_diff->m_is_first = true;
14✔
23
  m_diff->m_is_last  = true;
14✔
24
  m_diff->m_type     = unified_diff::diff_t::UNKNOWN;
14✔
25
}
14✔
26

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

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

40
      m_diff->m_is_first = (hunk == *std::get<2>(tpl).begin());
20✔
41
      m_diff->m_is_last  = (hunk == std::get<2>(tpl).back());
20✔
42

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

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

63
        index += 2;
40✔
64
      }
65

66
      m_diff->m_text.fill({});
20✔
67

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

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

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

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

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

102
  auto const action_eoi = [this](const auto& ctx)
10✔
103
  {
104
    m_diff->m_type = unified_diff::diff_t::LAST;
10✔
105
    m_diff->report_diff_finish();
10✔
106
    m_diff->trace("finish");
18✔
107
  };
23✔
108

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

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

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

125
  auto const parser_diff = bp::lit("--- a/") >> +(bp::char_ - "+++ b/") >>
14✔
126
                           bp::lit("+++ b/") >> +(bp::char_ - "@@") >>
14✔
127
                           +parser_hunk;
14✔
128

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

131
  auto const parser_all =
132
    *(parser_skip >> +parser_diff[action_diff]) >> bp::eoi[action_eoi];
14✔
133

134
  const bool res = bp::parse(m_diff->input(), parser_all, bp::ws);
14✔
135

136
  if (!res)
13✔
137
  {
138
    log("unified_diff_parsing") << m_diff->input();
8✔
139
  }
140

141
  return res;
13✔
142
}
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