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

antonvw / wex / 22037391453

15 Feb 2026 02:31PM UTC coverage: 64.27% (+0.009%) from 64.261%
22037391453

push

github

antonvw
added error message

18550 of 31652 branches covered (58.61%)

Branch coverage included in aggregate %.

14768 of 20189 relevant lines covered (73.15%)

1500.47 hits per line

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

93.23
/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
      int i = 0;
20✔
69

70
      for (const auto& line : std::get<1>(hunk))
68✔
71
      {
72
        log::trace("unified_diff hunk") << i << line;
48✔
73

74
        auto fix(line);
48✔
75

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

81
        switch (fix[0])
48!
82
        {
83
          case '+':
13✔
84
            m_diff->m_text[1].push_back(fix.substr(1));
13✔
85
            break;
13✔
86
          case '-':
20✔
87
            m_diff->m_text[0].push_back(fix.substr(1));
20✔
88
            break;
20✔
89
          case ' ':
×
90
            m_diff->m_text[0].push_back(fix.substr(1));
×
91
            m_diff->m_text[1].push_back(fix.substr(1));
×
92
            break;
×
93
          default:
15✔
94
            log("unified_diff unexpected hunk") << i << fix;
30✔
95
        }
96

97
        i++;
48✔
98
      }
99

100
      m_diff->m_type =
20✔
101
        (m_diff->m_type == unified_diff::diff_t::UNKNOWN ?
20✔
102
           unified_diff::diff_t::FIRST :
103
           unified_diff::diff_t::OTHER);
104

105
      m_diff->report_diff();
20✔
106
      m_diff->trace("found");
60✔
107
    }
108
  };
28✔
109

110
  auto const action_eoi = [this](const auto& ctx)
10✔
111
  {
112
    m_diff->m_type = unified_diff::diff_t::LAST;
10✔
113
    m_diff->report_diff_finish();
10✔
114
    m_diff->trace("finish");
18✔
115
  };
23✔
116

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

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

129
  auto const parser_hunk =
130
    bp::lit("@@") >> bp::repeat(2)[bp::int_ >> ',' >> bp::int_ | bp::int_] >>
14✔
131
    bp::lit("@@") >> bp::lexeme[+(bp::char_ - bp::eol)] >> parser_diff_lines;
14✔
132

133
  auto const parser_diff = bp::lit("--- a/") >> +(bp::char_ - "+++ b/") >>
14✔
134
                           bp::lit("+++ b/") >> +(bp::char_ - "@@") >>
14✔
135
                           +parser_hunk;
14✔
136

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

139
  auto const parser_all =
140
    *(parser_skip >> +parser_diff[action_diff]) >> bp::eoi[action_eoi];
14✔
141

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

144
  if (!res)
13✔
145
  {
146
    log("unified_diff_parsing") << m_diff->input();
8✔
147
    log::status("Unified diff parsing failed");
8✔
148
  }
149

150
  return res;
13✔
151
}
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