Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
diff.h
1#ifndef SOURCEMETA_CORE_DIFF_H_
2#define SOURCEMETA_CORE_DIFF_H_
3
4#ifndef SOURCEMETA_CORE_DIFF_EXPORT
5#include <sourcemeta/core/diff_export.h>
6#endif
7
8#include <cstddef> // std::size_t
9#include <cstdint> // std::uint8_t
10#include <functional> // std::function
11#include <ostream> // std::ostream
12#include <string_view> // std::string_view
13#include <vector> // std::vector
14
23
24namespace sourcemeta::core {
25
34struct Diff {
36 enum class Mode : std::uint8_t {
39 };
40
42 enum class Algorithm : std::uint8_t {
48 };
49
51 enum class Format : std::uint8_t {
55 };
56
64 struct Operation {
66 enum class Type : std::uint8_t {
73 };
74
78 std::size_t original_start;
80 std::size_t original_end;
82 std::size_t modified_start;
84 std::size_t modified_end;
85
86 auto operator==(const Operation &) const noexcept -> bool = default;
87 };
88
108
118 std::function<void(std::ostream &stream, LineType type,
119 std::string_view prefix, std::string_view content)>;
120
122 std::string_view original_label{"a"};
124 std::string_view modified_label{"b"};
126 std::size_t context{3};
129 };
130
132 std::vector<std::string_view> original;
134 std::vector<std::string_view> modified;
136 std::vector<Operation> operations;
141};
142
166SOURCEMETA_CORE_DIFF_EXPORT
167auto diff(const std::string_view original, const std::string_view modified,
168 const Diff::Mode mode, const Diff::Algorithm algorithm) -> Diff;
169
207SOURCEMETA_CORE_DIFF_EXPORT
208auto stringify(const Diff &document, std::ostream &stream,
209 const Diff::Format format,
210 const Diff::FormatOptions &options = {}) -> void;
211
212} // namespace sourcemeta::core
213
214#endif
bool original_ends_with_newline
Whether the original input ended with a line terminator.
Definition diff.h:138
std::vector< std::string_view > original
The tokens of the original input.
Definition diff.h:132
std::vector< std::string_view > modified
The tokens of the modified input.
Definition diff.h:134
bool modified_ends_with_newline
Whether the modified input ended with a line terminator.
Definition diff.h:140
Algorithm
The strategy used to locate the common subsequences of two inputs.
Definition diff.h:42
@ Myers
Definition diff.h:47
std::vector< Operation > operations
The transformations that turn the original input into the modified one.
Definition diff.h:136
Format
The textual serialisation used to render a set of differences.
Definition diff.h:51
@ Unified
Definition diff.h:54
Mode
The granularity at which inputs are compared.
Definition diff.h:36
@ Line
Compare newline-delimited lines.
Definition diff.h:38
SOURCEMETA_CORE_DIFF_EXPORT auto diff(const std::string_view original, const std::string_view modified, const Diff::Mode mode, const Diff::Algorithm algorithm) -> Diff
SOURCEMETA_CORE_DIFF_EXPORT auto stringify(const Diff &document, std::ostream &stream, const Diff::Format format, const Diff::FormatOptions &options={}) -> void
Definition diff.h:34
Controls the presentation of a rendered set of differences.
Definition diff.h:90
LineWriter line_writer
Optional custom line writer for logical unified-diff lines.
Definition diff.h:128
std::string_view modified_label
The name given to the modified input in the header.
Definition diff.h:124
std::string_view original_label
The name given to the original input in the header.
Definition diff.h:122
LineType
The semantic role of a line in a unified diff.
Definition diff.h:92
@ HeaderOriginal
The header indicating the original document label.
Definition diff.h:94
@ NoNewline
The marker indicating a missing final newline.
Definition diff.h:106
@ Hunk
A hunk range header.
Definition diff.h:98
@ Insert
An inserted line into the modified document.
Definition diff.h:104
@ Context
An unchanged context line.
Definition diff.h:100
@ HeaderModified
The header indicating the modified document label.
Definition diff.h:96
@ Delete
A deleted line from the original document.
Definition diff.h:102
std::size_t context
The number of unchanged lines shown around each change.
Definition diff.h:126
std::function< void(std::ostream &stream, LineType type, std::string_view prefix, std::string_view content)> LineWriter
Definition diff.h:117
std::size_t modified_end
The index one past the last affected token of the modified input.
Definition diff.h:84
std::size_t original_start
The index of the first affected token of the original input.
Definition diff.h:78
std::size_t modified_start
The index of the first affected token of the modified input.
Definition diff.h:82
Type type
The kind of transformation.
Definition diff.h:76
std::size_t original_end
The index one past the last affected token of the original input.
Definition diff.h:80
Type
The kind of transformation that a difference operation represents.
Definition diff.h:66
@ Insert
The span is only present in the modified input.
Definition diff.h:72
@ Delete
The span is only present in the original input.
Definition diff.h:70
@ Equal
The spans are present in both inputs.
Definition diff.h:68