v0.0.0-...-2ab8e39 Latest Latest

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

Go to latest
Published: Jun 26, 2020 License: BSD-3-Clause Imports: 2 Imported by: 10



Package bytediff provides a simple diff utility for looking at differences in byte slices. It's slow, clunky, and not particularly good by any measure, but it does provide very useful visualizations for diffs between small byte slices.

Our diff algorithm uses a dynamic programming implementation of longest common substring to find matching parts of slices, then recursively calls itself on the prefix/suffix of that matching part for each packet. This is a Bad Idea (tm) for normal (especially large) input, but for packets where large portions repeat frequently and we expect minor changes between results, it's actually quite useful.



This section is empty.


View Source
var (
	// BashOutput uses bash escape sequences to color output.
	BashOutput = &OutputFormat{
		reset:  "\033[0m",
		remove: "\033[32m",
		add:    "\033[31m",
		change: "\033[33m",
	// HTMLOutput uses a <pre> to wrap output, and <span>s to color it.
	// HTMLOutput is pretty experimental, so use at your own risk ;)
	HTMLOutput = &OutputFormat{
		start:  "<pre>",
		finish: "</pre>",
		reset:  "</span>",
		remove: "<span style='color:red'>",
		add:    "<span style='color:green'>",
		change: "<span style='color:yellow'>",


This section is empty.


type Difference

type Difference struct {
	Replace  bool
	From, To []byte

Difference represents a single part of the data being diffed, containing information about both the original and new values. From and To are the sets of bytes in the original and the new byte slice.

!Replace        implies  From == To (no change)
len(To) == 0    implies  From is being deleted
len(From) == 0  implies  To is being inserted
else            implies  From is being replaced by To

type Differences

type Differences []Difference

Differences is a set of differences for a given diff'd pair of byte slices.

func Diff

func Diff(strA, strB []byte) Differences

Diff diffs strA and strB, returning a list of differences which can be used to construct either the original or new string.

Diff is optimized for comparing VERY SHORT slices. It's meant for comparing things like packets off the wire, not large files or the like. As such, its runtime can be catastrophic if large inputs are passed in. You've been warned.

type OutputFormat

type OutputFormat struct {
	// contains filtered or unexported fields

OutputFormat tells a Differences.String call how to format the set of differences into a human-readable string. Its internals are currently unexported because we may want to change them drastically in the future. For the moment, please just use one of the provided OutputFormats that comes with this library.

func (*OutputFormat) String

func (c *OutputFormat) String(diffs Differences) string

String outputs a previously diff'd set of strings, showing differences between them, highlighted by colors.

The output format of this function is NOT guaranteed consistent, and may be changed at any time by the library authors. It's meant solely for human consumption.

Jump to

Keyboard shortcuts

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