difflib

package module
v0.0.0-...-596a7a5 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 18, 2024 License: MIT Imports: 5 Imported by: 1

README

GoDoc

difflib

difflib is a simple library written in Go for diffing two sequences of text.

Installing

To install, issue:

go get github.com/aryann/difflib

Using

To start using difflib, create a new file in your workspace and import difflib:

import (
        ...
        "fmt"
        "github.com/aryann/difflib"
        ...
)

Then call either difflib.Diff or difflib.HTMLDiff:

fmt.Println(difflib.HTMLDiff([]string{"one", "two", "three"}, []string{"two", "four", "three"}))

If you'd like more control over the output, see how the function HTMLDiff relies on Diff in difflib.go.

Running the Demo

There is a demo application in the difflib_demo directory. To run it, navigate to your $GOPATH and run:

go run src/github.com/aryann/difflib/difflib_server/difflib_demo.go <file-1> <file-2>

Where <file-1> and <file-2> are two text files you'd like to diff. The demo will launch a web server that will contain a table of the diff results.

Documentation

Overview

Package difflib provides functionality for computing the difference between two sequences of strings.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func HTMLDiff

func HTMLDiff(seq1, seq2 []string) string

HTMLDiff returns the results of diffing seq1 and seq2 as an HTML string. The resulting HTML is a table without the opening and closing table tags. Each table row represents a DiffRecord. The first and last columns contain the "line numbers" for seq1 and seq2, respectively (the function assumes that seq1 and seq2 represent the lines in a file). The second and third columns contain the actual file contents.

The cells that contain line numbers are decorated with the class "line-num". The cells that contain deleted elements are decorated with "deleted" and the cells that contain added elements are decorated with "added".

func PPDiff

func PPDiff(left, right []string) string

PPDiff returns the results of diffing left and right as an pretty printed string. It will display all the lines of both the sequences that are being compared. When the left is different from right it will prepend a " - |" before the line. When the right is different from left it will prepend a " + |" before the line. When the right and left are equal it will prepend a " |" before the line.

Types

type DeltaType

type DeltaType int

DeltaType describes the relationship of elements in two sequences. The following table provides a summary:

 Constant    Code   Meaning
----------  ------ ---------------------------------------
 Common      " "    The element occurs in both sequences.
 LeftOnly    "-"    The element is unique to sequence 1.
 RightOnly   "+"    The element is unique to sequence 2.
const (
	Common DeltaType = iota
	LeftOnly
	RightOnly
)

func (DeltaType) String

func (t DeltaType) String() string

String returns a string representation for DeltaType.

type DiffRecord

type DiffRecord struct {
	Payload   string
	Delta     DeltaType
	LineLeft  int
	LineRight int
}

func AnchoredDiff

func AnchoredDiff(seq1, seq2 []string) []DiffRecord

Diff returns an anchored diff of the two texts old and new in the “unified diff” format. If old and new are identical, Diff returns a nil slice (no output).

Unix diff implementations typically look for a diff with the smallest number of lines inserted and removed, which can in the worst case take time quadratic in the number of lines in the texts. As a result, many implementations either can be made to run for a long time or cut off the search after a predetermined amount of work.

In contrast, this implementation looks for a diff with the smallest number of “unique” lines inserted and removed, where unique means a line that appears just once in both old and new. We call this an “anchored diff” because the unique lines anchor the chosen matching regions. An anchored diff is usually clearer than a standard diff, because the algorithm does not try to reuse unrelated blank lines or closing braces. The algorithm also guarantees to run in O(n log n) time instead of the standard O(n²) time.

Some systems call this approach a “patience diff,” named for the “patience sorting” algorithm, itself named for a solitaire card game. We avoid that name for two reasons. First, the name has been used for a few different variants of the algorithm, so it is imprecise. Second, the name is frequently interpreted as meaning that you have to wait longer (to be patient) for the diff, meaning that it is a slower algorithm, when in fact the algorithm is faster than the standard one.

func Diff

func Diff(seq1, seq2 []string) (diff []DiffRecord)

Diff returns the result of diffing the seq1 and seq2.

func (DiffRecord) String

func (d DiffRecord) String() string

String returns a string representation of d. The string is a concatenation of the delta type and the payload.

Directories

Path Synopsis
A demo for difflib.
A demo for difflib.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL