Categorygithub.com/nao1215/diff
modulepackage
0.0.1
Repository: https://github.com/nao1215/diff.git
Documentation: pkg.go.dev

# README

LinuxUnitTest MacUnitTest WindowsUnitTest Vuluncheck reviewdog Gosec Coverage

nao1215/diff package

This repository is forked from pmezard/go-difflib by Stein Fletcher. He was maintaining difflib within the apitest package.

When I forked the apitest package as the spectest package, I moved the nao1215/diff package to a separate repository. I do not have the intention to extend the functionality of nao1215/diff, but I will maintain it while maintaining the spectest package.

The difference from the original version is that the standard output ("+++", "---" output) is colored even in the Windows environment. In other words, there is no longer any functional difference from Linux or Mac even on Windows.

What is nao1215/diff package ?

The nao1215/diff is a partial port of python 3 difflib package. Its main goal was to make unified and context diff available in pure Go, mostly for testing purposes.

The following class and functions (and related tests) have be ported:

  • SequenceMatcher
  • unified_diff()
  • context_diff()

Installation

$ go get github.com/nao1215/diff

Supported OS

  • Linux
  • macOS
  • Windows

Quick Start

Diffs are configured with Unified (or ContextDiff) structures, and can be output to an io.Writer or returned as a string.

diff := diff.UnifiedDiff{
    A:        diff.SplitLines("foo\nbar\n"),
    B:        diff.SplitLines("foo\nbaz\n"),
    FromFile: "Original",
    ToFile:   "Current",
    Context:  3,
}
text, _ := diff.GetUnifiedDiffString(diff)
fmt.Printf(text)

would output:

--- Original
+++ Current
@@ -1,3 +1,3 @@
 foo
-bar
+baz

LICENSE

3 clause BSD license. See LICENSE file for details.

# Functions

GetContextDiffString like WriteContextDiff but returns the diff a string.
GetUnifiedDiffString like WriteUnifiedDiff but returns the diff a string.
NewMatcher return new SequenceMatcher.
NewMatcherWithJunk return new SequenceMatcher with junk detection.
SplitLines split a string on "\n" while preserving them.
WriteContextDiff compare two sequences of lines; generate the delta as a context diff.
WriteUnifiedDiff compare two sequences of lines; generate the delta as a unified diff.

# Structs

Match object.
OpCode object.
SequenceMatcher compares sequence of strings.
UnifiedDiff diff parameters.

# Type aliases

ContextDiff is UnifiedDiff for managing context.