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 <ostream> // std::ostream
11#include <string_view> // std::string_view
12#include <vector> // std::vector
13
22
23namespace sourcemeta::core {
24
33struct Diff {
35 enum class Mode : std::uint8_t {
38 };
39
41 enum class Algorithm : std::uint8_t {
47 };
48
50 enum class Format : std::uint8_t {
54 };
55
63 struct Operation {
65 enum class Type : std::uint8_t {
72 };
73
77 std::size_t original_start;
79 std::size_t original_end;
81 std::size_t modified_start;
83 std::size_t modified_end;
84
85 auto operator==(const Operation &) const noexcept -> bool = default;
86 };
87
91 std::string_view original_label{"a"};
93 std::string_view modified_label{"b"};
95 std::size_t context{3};
96 };
97
99 std::vector<std::string_view> original;
101 std::vector<std::string_view> modified;
103 std::vector<Operation> operations;
108};
109
133SOURCEMETA_CORE_DIFF_EXPORT
134auto diff(const std::string_view original, const std::string_view modified,
135 const Diff::Mode mode, const Diff::Algorithm algorithm) -> Diff;
136
162SOURCEMETA_CORE_DIFF_EXPORT
163auto stringify(const Diff &document, std::ostream &stream,
164 const Diff::Format format,
165 const Diff::FormatOptions &options = {}) -> void;
166
167} // namespace sourcemeta::core
168
169#endif
bool original_ends_with_newline
Whether the original input ended with a line terminator.
Definition diff.h:105
std::vector< std::string_view > original
The tokens of the original input.
Definition diff.h:99
std::vector< std::string_view > modified
The tokens of the modified input.
Definition diff.h:101
bool modified_ends_with_newline
Whether the modified input ended with a line terminator.
Definition diff.h:107
Algorithm
The strategy used to locate the common subsequences of two inputs.
Definition diff.h:41
@ Myers
Definition diff.h:46
std::vector< Operation > operations
The transformations that turn the original input into the modified one.
Definition diff.h:103
Format
The textual serialisation used to render a set of differences.
Definition diff.h:50
@ Unified
Definition diff.h:53
Mode
The granularity at which inputs are compared.
Definition diff.h:35
@ Line
Compare newline-delimited lines.
Definition diff.h:37
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:33
Controls the presentation of a rendered set of differences.
Definition diff.h:89
std::string_view modified_label
The name given to the modified input in the header.
Definition diff.h:93
std::string_view original_label
The name given to the original input in the header.
Definition diff.h:91
std::size_t context
The number of unchanged lines shown around each change.
Definition diff.h:95
std::size_t modified_end
The index one past the last affected token of the modified input.
Definition diff.h:83
std::size_t original_start
The index of the first affected token of the original input.
Definition diff.h:77
std::size_t modified_start
The index of the first affected token of the modified input.
Definition diff.h:81
Type type
The kind of transformation.
Definition diff.h:75
std::size_t original_end
The index one past the last affected token of the original input.
Definition diff.h:79
Type
The kind of transformation that a difference operation represents.
Definition diff.h:65
@ Insert
The span is only present in the modified input.
Definition diff.h:71
@ Delete
The span is only present in the original input.
Definition diff.h:69
@ Equal
The spans are present in both inputs.
Definition diff.h:67