Sourcemeta Core 0.0.0
Loading...
Searching...
No Matches
Diff

A line-oriented textual difference implementation. More...

Classes

struct  sourcemeta::core::Diff

Functions

SOURCEMETA_CORE_DIFF_EXPORT auto sourcemeta::core::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 sourcemeta::core::stringify (const Diff &document, std::ostream &stream, const Diff::Format format, const Diff::FormatOptions &options={}) -> void

Detailed Description

A line-oriented textual difference implementation.

This functionality is included as follows:

#include <sourcemeta/core/diff.h>

Class Documentation

◆ sourcemeta::core::Diff

struct sourcemeta::core::Diff

The result of comparing two inputs, holding the tokens of each alongside the operations that relate them, plus the vocabulary that describes how a comparison is performed and rendered.

The tokens are views into the inputs that were compared, which must outlive this result.

Public Types

enum class  Mode : std::uint8_t { Line }
 The granularity at which inputs are compared. More...
enum class  Algorithm : std::uint8_t { Myers }
 The strategy used to locate the common subsequences of two inputs. More...
enum class  Format : std::uint8_t { Unified }
 The textual serialisation used to render a set of differences. More...

Public Attributes

std::vector< std::string_view > original
 The tokens of the original input.
std::vector< std::string_view > modified
 The tokens of the modified input.
std::vector< Operationoperations
 The transformations that turn the original input into the modified one.
bool original_ends_with_newline {true}
 Whether the original input ended with a line terminator.
bool modified_ends_with_newline {true}
 Whether the modified input ended with a line terminator.

Member Enumeration Documentation

◆ Algorithm

enum class sourcemeta::core::Diff::Algorithm : std::uint8_t
strong

The strategy used to locate the common subsequences of two inputs.

Enumerator
Myers 

A greedy shortest edit script search over the edit graph, as described by Eugene W. Myers in "An O(ND) Difference Algorithm and Its Variations", Algorithmica Vol. 1, 1986, pp. 251-266 (http://www.xmailserver.org/diff2.pdf)

◆ Format

enum class sourcemeta::core::Diff::Format : std::uint8_t
strong

The textual serialisation used to render a set of differences.

Enumerator
Unified 

The unified format, as standardised by POSIX in IEEE Std 1003.1-2024 (https://pubs.opengroup.org/onlinepubs/9799919799/utilities/diff.html)

◆ Mode

enum class sourcemeta::core::Diff::Mode : std::uint8_t
strong

The granularity at which inputs are compared.

Enumerator
Line 

Compare newline-delimited lines.

Function Documentation

◆ diff()

SOURCEMETA_CORE_DIFF_EXPORT auto sourcemeta::core::diff ( const std::string_view original,
const std::string_view modified,
const Diff::Mode mode,
const Diff::Algorithm algorithm ) -> Diff

Compute the differences between two inputs. For example:

#include <sourcemeta/core/diff.h>
#include <cassert>
#include <vector>
const auto result{sourcemeta::core::diff(
"foo\nbar\n", "foo\nbaz\n", sourcemeta::core::Diff::Mode::Line,
assert((result.operations ==
std::vector<sourcemeta::core::Diff::Operation>{
{sourcemeta::core::Diff::Operation::Type::Equal, 0, 1, 0, 1},
{sourcemeta::core::Diff::Operation::Type::Delete, 1, 2, 1, 1},
{sourcemeta::core::Diff::Operation::Type::Insert, 2, 2, 1,
2}}));
@ Myers
Definition diff.h:46
@ 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

The tokens of the result are views into the given inputs, which must outlive it.

◆ stringify()

SOURCEMETA_CORE_DIFF_EXPORT auto sourcemeta::core::stringify ( const Diff & document,
std::ostream & stream,
const Diff::Format format,
const Diff::FormatOptions & options = {} ) -> void

Render a set of differences into a given C++ standard output stream. For example:

#include <sourcemeta/core/diff.h>
#include <cassert>
#include <sstream>
const auto result{sourcemeta::core::diff(
"foo\nbar\n", "foo\nbaz\n", sourcemeta::core::Diff::Mode::Line,
std::ostringstream stream;
assert(stream.str() == "--- a\n"
"+++ b\n"
"@@ -1,2 +1,2 @@\n"
" foo\n"
"-bar\n"
"+baz\n");
@ Unified
Definition diff.h:53
SOURCEMETA_CORE_DIFF_EXPORT auto stringify(const Diff &document, std::ostream &stream, const Diff::Format format, const Diff::FormatOptions &options={}) -> void