-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhighlight.h
More file actions
59 lines (47 loc) · 1.6 KB
/
highlight.h
File metadata and controls
59 lines (47 loc) · 1.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#ifndef GUT_HIGHLIGHT_H
#define GUT_HIGHLIGHT_H
#include "utils.h"
#include <algorithm>
namespace gut {
namespace highlight {
// enable highlighting the first mismatching characters in
// trings that do not compare equal in a `==` test expression
struct FirstDiffInStrings_ {};
typedef StaticFlag<FirstDiffInStrings_> FirstDiffInStrings;
#define GUT_ENABLE_HIGHLIGHTFIRSTDIFFINSTRINGS \
gut::highlight::FirstDiffInStrings firstDiffInStrings_;
std::string firstDiffInStrings(
const std::string& lhs, const std::string& rhs)
{
if (!highlight::FirstDiffInStrings::enabled())
return "";
static const int prefixLength = 40;
static const int suffixLength = 32;
const auto diff = std::mismatch(
lhs.begin(), lhs.end(), rhs.begin(), rhs.end());
if (diff.first == lhs.end() && diff.second == rhs.end())
return "";
const int diffPos = static_cast<int>(
(diff.first != lhs.end())
? diff.first - lhs.begin()
: diff.second - rhs.begin());
const auto fragmentStartPos = std::max(0, diffPos - prefixLength);
const auto markerPos = std::min(diffPos, prefixLength);
const auto fragmentLength = markerPos + suffixLength;
std::ostringstream os;
os
<< "\n"
<< "first difference found at index "
<< diffPos
<< ":\n"
<< lhs.substr(fragmentStartPos, fragmentLength)
<< "\n"
<< rhs.substr(fragmentStartPos, fragmentLength)
<< "\n"
<< std::string(markerPos, '-')
<< "^\n";
return os.str();
}
} // namespace highlight
} // namespace gut
#endif // GUT_HIGHLIGHT_H