Documentation
¶
Overview ¶
Package diff provides implementations for computing the differences between strings or string slices.
The package offers two diff algorithms: a simple longest common subsequence (LCS) based approach and Myers' efficient diff algorithm. It also provides flexible options for customizing the output format, including context and unified diff formats, through the use of formatters.
Subpackages ¶
The package is organized into the following subpackages:
simple: Provides a straightforward implementation of a diff algorithm using a longest common subsequence (LCS) based approach. The `Diff` and `DiffStrings` functions can be used directly, or a `Differ` instance can be created for more complex scenarios. This implementation prioritizes simplicity and readability, suitable for small to medium-sized inputs.
myers: Implements the Myers' diff algorithm, which is an efficient method for computing the shortest edit script between two sequences. The `Diff` and `DiffStrings` functions can be used directly, or a `Differ` instance can be created. The Myers implementation includes a linear space optimization for large inputs and a fallback to the LCS algorithm for large (> 10000 lines) inputs or when recursion limits are reached.
Usage ¶
To use the diff package, you can choose either the simple or myers subpackage depending on your performance requirements. Both subpackages provide a `Differ` interface with `Diff` and `DiffStrings` methods for computing differences between strings and string slices, respectively. The `Diff` and `DiffStrings` functions in each subpackage can also be used directly for simpler use cases. The format of the diff output is controlled by a `Formatter`. The default formatter produces a context diff with line numbers.
Example using the simple subpackage:
import ( "fmt" "github.com/neticdk/go-stdlib/diff/simple" ) differ := simple.NewDiffer() diff, err := differ.Diff("hello\nworld", "hello\nthere\nworld") fmt.Println(diff)
Use the Diff function directly:
simpleDiff, err := simple.Diff("hello\nworld", "hello\nthere\nworld")
Example using the myers subpackage:
import ( "fmt" "github.com/neticdk/go-stdlib/diff/myers" ) differ := myers.NewDiffer() diff, err := differ.Diff("hello\nworld", "hello\nthere\nworld") fmt.Println(diff)
Use the Diff function directly:
myersDiff, err := myers.Diff("hello\nworld", "hello\nthere\nworld")
Index ¶
Constants ¶
const ( DefaultContextLines = 3 DefaultOutputFormat = FormatContext DefaultShowLineNumbers = true )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type ContextFormatter ¶
type ContextFormatter struct{}
ContextFormatter is a Formatter implementation using context diffs
func (ContextFormatter) Format ¶
func (c ContextFormatter) Format(edits []Line, options FormatOptions) string
Format formats the diff output using the ContextFormatter
type Differ ¶
type Differ interface { // Diff returns a string representation of the differences between two // strings. It returns an error if invalid options are provided or diffing // fails. Diff(a, b string) (string, error) // DiffStrings returns a string representation of the differences between // two slices of strings. It returns an error if invalid options are // provided or diffing fails. DiffStrings(a, b []string) (string, error) }
The Differ interface defines the contract for diffing two slices of strings Use the factory functions from each package to create a Differ implementation
type FormatOptions ¶
type FormatOptions struct { // OutputFormat specifies the format of the diff output // Default: FormatContext OutputFormat OutputFormat // ContextLines specifies the number of lines of context to show around each change // If ContextLines is 0, no context lines will be shown // Default: 3 ContextLines int // ShowLineNumbers indicates whether line numbers should be shown in the diff output // Default: false ShowLineNumbers bool }
FormatOptions represents the options for formatting the diff output
func (*FormatOptions) Validate ¶
func (fo *FormatOptions) Validate() error
Validate validates the FormatOptions instance
type Formatter ¶
type Formatter interface { // Format formats a diff Format(edits []Line, options FormatOptions) string }
The Formatter interface
type Line ¶
type Line struct { // OpKind is the operation performed on the line Kind OpKind // Text is the content of the line Text string }
Line represents a line of text in the diff with an associated operation
type OutputFormat ¶
type OutputFormat int
OutputFormat represents the format of the diff output
const ( FormatContext OutputFormat = iota FormatUnified )
func (OutputFormat) String ¶
func (o OutputFormat) String() string
type UnifiedFormatter ¶
type UnifiedFormatter struct{}
UnifiedFormatter is a Formatter implementation using unified diffs
func (UnifiedFormatter) Format ¶
func (u UnifiedFormatter) Format(edits []Line, options FormatOptions) string
Format formats the diff output using the UnifiedFormatter
Directories
¶
Path | Synopsis |
---|---|
internal
|
|
Package myers implements the Myers' diff algorithm.
|
Package myers implements the Myers' diff algorithm. |
Package simple provides a straightforward implementation of a diff algorithm for comparing strings or string slices.
|
Package simple provides a straightforward implementation of a diff algorithm for comparing strings or string slices. |