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

antonvw / wex / 19616694313

23 Nov 2025 08:15PM UTC coverage: 64.297% (-0.1%) from 64.402%
19616694313

push

github

antonvw
added diff type to unified_diff

18528 of 31566 branches covered (58.7%)

Branch coverage included in aggregate %.

14666 of 20060 relevant lines covered (73.11%)

1525.53 hits per line

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

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

9
#include <boost/tokenizer.hpp>
10
#include <wex/core/log.h>
11
#include <wex/core/regex.h>
12
#include <wex/factory/frame.h>
13
#include <wex/factory/unified-diff.h>
14

15
#include <iostream>
16
#include <utility>
17

18
#define NEXT_TOKEN                                                             \
19
  if (++tok_iter == tokens.end())                                              \
20
  {                                                                            \
21
    return false;                                                              \
22
  }
23

24
#define CHANGES_LINES(RANGE, TEXT)                                             \
25
  for (int i = 0; i < m_range[RANGE]; i++)                                     \
26
  {                                                                            \
27
    NEXT_TOKEN                                                                 \
28
    m_text[TEXT].push_back((*tok_iter).substr(1));                             \
29
  }
30

31
#define HEADER_LINES(REGEX, INTO)                                              \
32
  if (!parse_header(REGEX, *tok_iter, INTO))                                   \
33
  {                                                                            \
34
    return false;                                                              \
35
  }                                                                            \
36
  NEXT_TOKEN
37

38
#define SKIP_LINES                                                             \
39
  while (tok_iter != tokens.end())                                             \
40
  {                                                                            \
41
    NEXT_TOKEN                                                                 \
42
    if (!tok_iter->starts_with("diff ") && !tok_iter->starts_with("index "))   \
43
    {                                                                          \
44
      break;                                                                   \
45
    }                                                                          \
46
  }
47

48
namespace wex
49
{
50
size_t stoi(const std::string& i)
68✔
51
{
52
  return i.empty() ? 1 : std::stoi(i);
68✔
53
}
54
} // namespace wex
55

56
wex::factory::unified_diff::unified_diff(std::string input)
15✔
57
  : m_input(std::move(input))
15✔
58
{
59
  m_range.fill({0});
15✔
60
}
15✔
61

62
bool wex::factory::unified_diff::parse()
11✔
63
{
64
  using tokenizer = boost::tokenizer<boost::char_separator<char>>;
65

66
  tokenizer tokens(m_input, boost::char_separator<char>("\r\n"));
11✔
67

68
  tokenizer::iterator tok_iter = tokens.begin();
11✔
69

70
  m_diffs = 0;
11✔
71
  m_type  = diff_t::UNKNOWN;
11✔
72

73
  while (tok_iter != tokens.end())
34✔
74
  {
75
    // skip first lines
76
    SKIP_LINES;
25!
77

78
    // The unified output format starts with a two-line header
79
    HEADER_LINES("--- a/(.*)", m_path[0]);
42!
80
    HEADER_LINES("\\+\\+\\+ b/(.*)", m_path[1]);
42!
81

82
    m_is_first = true;
14✔
83
    m_is_last  = false;
14✔
84
    m_type     = (m_type == diff_t::UNKNOWN ? diff_t::FIRST : diff_t::OTHER);
14✔
85

86
    // Next come one or more chunks of differences
87
    while (tok_iter != tokens.end())
25✔
88
    {
89
      regex r_chunk("@@ -([0-9]+),?([0-9]*) \\+([0-9]+),?([0-9]*) @@.*");
19✔
90

91
      if (r_chunk.match(*tok_iter) != 4)
19✔
92
      {
93
        log("unified_diff") << *tok_iter << r_chunk.size();
2✔
94
        return false;
2✔
95
      }
96

97
      m_range[0] = wex::stoi(r_chunk[0]);
17✔
98
      m_range[1] = wex::stoi(r_chunk[1]);
17✔
99
      m_range[2] = wex::stoi(r_chunk[2]);
17✔
100
      m_range[3] = wex::stoi(r_chunk[3]);
17✔
101

102
      m_text.fill({});
17✔
103

104
      // Now get all - lines and all + lines, collect them, and invoke callback.
105
      CHANGES_LINES(1, 0);
33!
106
      CHANGES_LINES(3, 1);
29!
107

108
      log::trace("diff parse")
34✔
109
        << std::to_underlying(m_type) << m_path[0].string() << m_diffs;
17✔
110
      if (!report_diff())
17!
111
      {
112
        return false;
×
113
      }
114

115
      m_is_first = false;
17✔
116

117
      if (++tok_iter != tokens.end() && !(*tok_iter).starts_with("@@"))
17!
118
      {
119
        m_is_last = true;
6✔
120
        report_diff();
6✔
121
        break; // this was last chunk, continue with header lines
6✔
122
      }
123
    }
19✔
124
  }
125

126
  m_is_last = true;
8✔
127

128
  if (m_type != diff_t::UNKNOWN)
8✔
129
  {
130
    m_type = diff_t::LAST;
6✔
131
    log::trace("diff parse LAST") << m_path[0].string() << " " << m_diffs;
12✔
132
  }
133

134
  report_diff();
8✔
135
  report_diff_finish();
8✔
136

137
  return true;
8✔
138
}
11✔
139

140
bool wex::factory::unified_diff::parse_header(
28✔
141
  const std::string& r,
142
  const std::string& line,
143
  path&              p)
144
{
145
  regex re(r);
28✔
146

147
  if (!re.match(line))
28!
148
  {
149
    log("unified_diff") << line << re.match_data().text();
×
150
    return false;
×
151
  }
152

153
  p = path(re[0]);
28✔
154

155
  return true;
28✔
156
}
28✔
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