difflib

package module
Version: v0.0.0-...-ff5ff6d Latest Latest
Warning

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

Go to latest
Published: Mar 28, 2021 License: MIT Imports: 3 Imported by: 42

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".

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
}

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.

Source Files

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