Documentation ¶
Overview ¶
Package difflib is a partial port of Python difflib module.
It provides tools to compare sequences of strings and generate textual diffs.
The following class and functions have been ported:
- SequenceMatcher
- unified_diff
- context_diff
Getting unified diffs was the main goal of the port. Keep in mind this code is mostly suitable to output text differences in a human friendly way, there are no guarantees generated diffs are consumable by patch(1).
Index ¶
- func DirSize(path string) (int64, error)
- func Exists(path string) bool
- func GetContextDiffString(diff ContextDiff) (string, error)
- func GetDatafiles(path string) ([]string, error)
- func GetUnifiedDiffString(diff UnifiedDiff) (string, error)
- func Gzip(data []byte) ([]byte, error)
- func Int63() int64
- func NewSource(seed int64) rand.Source
- func NewSource64(seed int64) rand.Source64
- func ParseIds(fns []string) ([]int, error)
- func SplitLines(s string) []string
- func UnGzip(data []byte) ([]byte, error)
- func UnZlib(data []byte) ([]byte, error)
- func WriteContextDiff(writer io.Writer, diff ContextDiff) error
- func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error
- func Zlib(data []byte) ([]byte, error)
- type ContextDiff
- type Generator
- type Match
- type OpCode
- type SafeQueue
- func (queue *SafeQueue) DirtyLength() uint64
- func (queue *SafeQueue) DirtyPop() (item interface{})
- func (queue *SafeQueue) DirtyPurge()
- func (queue *SafeQueue) HeadItem() (res interface{})
- func (queue *SafeQueue) Length() uint64
- func (queue *SafeQueue) Pop() (item interface{})
- func (queue *SafeQueue) Purge()
- func (queue *SafeQueue) Push(item interface{})
- func (queue *SafeQueue) PushHead(item interface{})
- type SequenceMatcher
- func (m *SequenceMatcher) GetGroupedOpCodes(n int) [][]OpCode
- func (m *SequenceMatcher) GetMatchingBlocks() []Match
- func (m *SequenceMatcher) GetOpCodes() []OpCode
- func (m *SequenceMatcher) QuickRatio() float64
- func (m *SequenceMatcher) Ratio() float64
- func (m *SequenceMatcher) RealQuickRatio() float64
- func (m *SequenceMatcher) SetSeq1(a []string)
- func (m *SequenceMatcher) SetSeq2(b []string)
- func (m *SequenceMatcher) SetSeqs(a, b []string)
- type SeriesIDSet
- func (s *SeriesIDSet) Add(id SeriesID)
- func (s *SeriesIDSet) AddMany(ids ...SeriesID)
- func (s *SeriesIDSet) AddNoLock(id SeriesID)
- func (s *SeriesIDSet) And(other *SeriesIDSet) *SeriesIDSet
- func (s *SeriesIDSet) Bytes() int
- func (s *SeriesIDSet) Cardinality() uint64
- func (s *SeriesIDSet) Clear()
- func (s *SeriesIDSet) ClearNoLock()
- func (s *SeriesIDSet) Clone() *SeriesIDSet
- func (s *SeriesIDSet) CloneNoLock() *SeriesIDSet
- func (s *SeriesIDSet) Contains(id SeriesID) bool
- func (s *SeriesIDSet) ContainsNoLock(id SeriesID) bool
- func (s *SeriesIDSet) Diff(other *SeriesIDSet)
- func (s *SeriesIDSet) Equals(other *SeriesIDSet) bool
- func (s *SeriesIDSet) ForEach(f func(id SeriesID))
- func (s *SeriesIDSet) ForEachNoLock(f func(id SeriesID))
- func (s *SeriesIDSet) Iterator() SeriesIDSetIterable
- func (s *SeriesIDSet) Merge(others ...*SeriesIDSet)
- func (s *SeriesIDSet) MergeInPlace(other *SeriesIDSet)
- func (s *SeriesIDSet) Remove(id SeriesID)
- func (s *SeriesIDSet) RemoveNoLock(id SeriesID)
- func (s *SeriesIDSet) RemoveSet(other *SeriesIDSet)
- func (s *SeriesIDSet) Slice() []uint64
- func (s *SeriesIDSet) String() string
- func (s *SeriesIDSet) UnmarshalBinary(data []byte) error
- func (s *SeriesIDSet) UnmarshalBinaryUnsafe(data []byte) error
- func (s *SeriesIDSet) WriteTo(w io.Writer) (int64, error)
- type SeriesIDSetIterable
- type UnifiedDiff
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetContextDiffString ¶
func GetContextDiffString(diff ContextDiff) (string, error)
Like WriteContextDiff but returns the diff a string.
func GetDatafiles ¶
func GetUnifiedDiffString ¶
func GetUnifiedDiffString(diff UnifiedDiff) (string, error)
Like WriteUnifiedDiff but returns the diff a string.
func NewSource64 ¶
func SplitLines ¶
Split a string on "\n" while preserving them. The output can be used as input for UnifiedDiff and ContextDiff structures.
func WriteContextDiff ¶
func WriteContextDiff(writer io.Writer, diff ContextDiff) error
Compare two sequences of lines; generate the delta as a context diff.
Context diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by diff.Context which defaults to three.
By default, the diff control lines (those with *** or ---) are created with a trailing newline.
For inputs that do not have trailing newlines, set the diff.Eol argument to "" so that the output will be uniformly newline free.
The context diff format normally has a header for filenames and modification times. Any or all of these may be specified using strings for diff.FromFile, diff.ToFile, diff.FromDate, diff.ToDate. The modification times are normally expressed in the ISO 8601 format. If not specified, the strings default to blanks.
func WriteUnifiedDiff ¶
func WriteUnifiedDiff(writer io.Writer, diff UnifiedDiff) error
Compare two sequences of lines; generate the delta as a unified diff.
Unified diffs are a compact way of showing line changes and a few lines of context. The number of context lines is set by 'n' which defaults to three.
By default, the diff control lines (those with ---, +++, or @@) are created with a trailing newline. This is helpful so that inputs created from file.readlines() result in diffs that are suitable for file.writelines() since both the inputs and outputs have trailing newlines.
For inputs that do not have trailing newlines, set the lineterm argument to "" so that the output will be uniformly newline free.
The unidiff format normally has a header for filenames and modification times. Any or all of these may be specified using strings for 'fromfile', 'tofile', 'fromfiledate', and 'tofiledate'. The modification times are normally expressed in the ISO 8601 format.
Types ¶
type ContextDiff ¶
type ContextDiff UnifiedDiff
type SafeQueue ¶
SafeQueue implements thread safe FIFO queue
func NewSafeQueue ¶
NewSafeQueue returns new instance of SafeQueue
func (*SafeQueue) DirtyLength ¶
DirtyLength returns queue length DirtyLength is not thread-safe
func (*SafeQueue) DirtyPop ¶
func (queue *SafeQueue) DirtyPop() (item interface{})
DirtyPop returns item from queue head DirtyPop is not thread-safe
func (*SafeQueue) DirtyPurge ¶
func (queue *SafeQueue) DirtyPurge()
DirtyPurge clean queue DirtyPurge is not thread-safe
func (*SafeQueue) HeadItem ¶
func (queue *SafeQueue) HeadItem() (res interface{})
HeadItem returns queue head item HeadItem is not thread-safe
func (*SafeQueue) Pop ¶
func (queue *SafeQueue) Pop() (item interface{})
Pop returns item from queue head
type SequenceMatcher ¶
SequenceMatcher compares sequence of strings. The basic algorithm predates, and is a little fancier than, an algorithm published in the late 1980's by Ratcliff and Obershelp under the hyperbolic name "gestalt pattern matching". The basic idea is to find the longest contiguous matching subsequence that contains no "junk" elements (R-O doesn't address junk). The same idea is then applied recursively to the pieces of the sequences to the left and to the right of the matching subsequence. This does not yield minimal edit sequences, but does tend to yield matches that "look right" to people.
SequenceMatcher tries to compute a "human-friendly diff" between two sequences. Unlike e.g. UNIX(tm) diff, the fundamental notion is the longest *contiguous* & junk-free matching subsequence. That's what catches peoples' eyes. The Windows(tm) windiff has another interesting notion, pairing up elements that appear uniquely in each sequence. That, and the method here, appear to yield more intuitive difference reports than does diff. This method appears to be the least vulnerable to synching up on blocks of "junk lines", though (like blank lines in ordinary text files, or maybe "<P>" lines in HTML files). That may be because this is the only method of the 3 that has a *concept* of "junk" <wink>.
Timing: Basic R-O is cubic time worst case and quadratic time expected case. SequenceMatcher is quadratic time for the worst case and has expected-case behavior dependent in a complicated way on how many elements the sequences have in common; best case time is linear.
func NewMatcher ¶
func NewMatcher(a, b []string) *SequenceMatcher
func NewMatcherWithJunk ¶
func NewMatcherWithJunk(a, b []string, autoJunk bool, isJunk func(string) bool) *SequenceMatcher
func (*SequenceMatcher) GetGroupedOpCodes ¶
func (m *SequenceMatcher) GetGroupedOpCodes(n int) [][]OpCode
Isolate change clusters by eliminating ranges with no changes.
Return a generator of groups with up to n lines of context. Each group is in the same format as returned by GetOpCodes().
func (*SequenceMatcher) GetMatchingBlocks ¶
func (m *SequenceMatcher) GetMatchingBlocks() []Match
Return list of triples describing matching subsequences.
Each triple is of the form (i, j, n), and means that a[i:i+n] == b[j:j+n]. The triples are monotonically increasing in i and in j. It's also guaranteed that if (i, j, n) and (i', j', n') are adjacent triples in the list, and the second is not the last triple in the list, then i+n != i' or j+n != j'. IOW, adjacent triples never describe adjacent equal blocks.
The last triple is a dummy, (len(a), len(b), 0), and is the only triple with n==0.
func (*SequenceMatcher) GetOpCodes ¶
func (m *SequenceMatcher) GetOpCodes() []OpCode
Return list of 5-tuples describing how to turn a into b.
Each tuple is of the form (tag, i1, i2, j1, j2). The first tuple has i1 == j1 == 0, and remaining tuples have i1 == the i2 from the tuple preceding it, and likewise for j1 == the previous j2.
The tags are characters, with these meanings:
'r' (replace): a[i1:i2] should be replaced by b[j1:j2]
'd' (delete): a[i1:i2] should be deleted, j1==j2 in this case.
'i' (insert): b[j1:j2] should be inserted at a[i1:i1], i1==i2 in this case.
'e' (equal): a[i1:i2] == b[j1:j2]
func (*SequenceMatcher) QuickRatio ¶
func (m *SequenceMatcher) QuickRatio() float64
Return an upper bound on ratio() relatively quickly.
This isn't defined beyond that it is an upper bound on .Ratio(), and is faster to compute.
func (*SequenceMatcher) Ratio ¶
func (m *SequenceMatcher) Ratio() float64
Return a measure of the sequences' similarity (float in [0,1]).
Where T is the total number of elements in both sequences, and M is the number of matches, this is 2.0*M / T. Note that this is 1 if the sequences are identical, and 0 if they have nothing in common.
.Ratio() is expensive to compute if you haven't already computed .GetMatchingBlocks() or .GetOpCodes(), in which case you may want to try .QuickRatio() or .RealQuickRation() first to get an upper bound.
func (*SequenceMatcher) RealQuickRatio ¶
func (m *SequenceMatcher) RealQuickRatio() float64
Return an upper bound on ratio() very quickly.
This isn't defined beyond that it is an upper bound on .Ratio(), and is faster to compute than either .Ratio() or .QuickRatio().
func (*SequenceMatcher) SetSeq1 ¶
func (m *SequenceMatcher) SetSeq1(a []string)
Set the first sequence to be compared. The second sequence to be compared is not changed.
SequenceMatcher computes and caches detailed information about the second sequence, so if you want to compare one sequence S against many sequences, use .SetSeq2(s) once and call .SetSeq1(x) repeatedly for each of the other sequences.
See also SetSeqs() and SetSeq2().
func (*SequenceMatcher) SetSeq2 ¶
func (m *SequenceMatcher) SetSeq2(b []string)
Set the second sequence to be compared. The first sequence to be compared is not changed.
func (*SequenceMatcher) SetSeqs ¶
func (m *SequenceMatcher) SetSeqs(a, b []string)
Set two sequences to be compared.
type SeriesIDSet ¶
SeriesIDSet represents a lockable bitmap of series ids.
func NewSeriesIDSet ¶
func NewSeriesIDSet(a ...SeriesID) *SeriesIDSet
NewSeriesIDSet returns a new instance of SeriesIDSet.
func NewSeriesIDSetNegate ¶
func NewSeriesIDSetNegate(a, b *SeriesIDSet) *SeriesIDSet
NewSeriesIDSetNegate returns a new SeriesIDSet containing all the elements in a that are not present in b. That is, the set difference between a and b.
func (*SeriesIDSet) AddMany ¶
func (s *SeriesIDSet) AddMany(ids ...SeriesID)
AddMany adds multiple ids to the SeriesIDSet. AddMany takes a lock, so may not be optimal to call many times with few ids.
func (*SeriesIDSet) AddNoLock ¶
func (s *SeriesIDSet) AddNoLock(id SeriesID)
AddNoLock adds the series id to the set. Add is not safe for use from multiple goroutines. Callers must manage synchronization.
func (*SeriesIDSet) And ¶
func (s *SeriesIDSet) And(other *SeriesIDSet) *SeriesIDSet
And returns a new SeriesIDSet containing elements that were present in s and other.
func (*SeriesIDSet) Bytes ¶
func (s *SeriesIDSet) Bytes() int
Bytes estimates the memory footprint of this SeriesIDSet, in bytes.
func (*SeriesIDSet) Cardinality ¶
func (s *SeriesIDSet) Cardinality() uint64
Cardinality returns the cardinality of the SeriesIDSet.
func (*SeriesIDSet) Clear ¶
func (s *SeriesIDSet) Clear()
Clear clears the underlying bitmap for re-use. Clear is safe for use by multiple goroutines.
func (*SeriesIDSet) ClearNoLock ¶
func (s *SeriesIDSet) ClearNoLock()
ClearNoLock clears the underlying bitmap for re-use without taking a lock.
func (*SeriesIDSet) Clone ¶
func (s *SeriesIDSet) Clone() *SeriesIDSet
Clone returns a new SeriesIDSet with a deep copy of the underlying bitmap.
func (*SeriesIDSet) CloneNoLock ¶
func (s *SeriesIDSet) CloneNoLock() *SeriesIDSet
CloneNoLock calls Clone without taking a lock.
func (*SeriesIDSet) Contains ¶
func (s *SeriesIDSet) Contains(id SeriesID) bool
Contains returns true if the id exists in the set.
func (*SeriesIDSet) ContainsNoLock ¶
func (s *SeriesIDSet) ContainsNoLock(id SeriesID) bool
ContainsNoLock returns true if the id exists in the set. ContainsNoLock is not safe for use from multiple goroutines. The caller must manage synchronization.
func (*SeriesIDSet) Diff ¶
func (s *SeriesIDSet) Diff(other *SeriesIDSet)
Diff removes from s any elements also present in other.
func (*SeriesIDSet) Equals ¶
func (s *SeriesIDSet) Equals(other *SeriesIDSet) bool
Equals returns true if other and s are the same set of ids.
func (*SeriesIDSet) ForEach ¶
func (s *SeriesIDSet) ForEach(f func(id SeriesID))
ForEach calls f for each id in the set. The function is applied to the IDs in ascending order.
func (*SeriesIDSet) ForEachNoLock ¶
func (s *SeriesIDSet) ForEachNoLock(f func(id SeriesID))
ForEachNoLock calls f for each id in the set without taking a lock.
func (*SeriesIDSet) Iterator ¶
func (s *SeriesIDSet) Iterator() SeriesIDSetIterable
Iterator returns an iterator to the underlying bitmap. This iterator is not protected by a lock.
func (*SeriesIDSet) Merge ¶
func (s *SeriesIDSet) Merge(others ...*SeriesIDSet)
Merge merged the contents of others into s. The caller does not need to provide s as an argument, and the contents of s will always be present in s after Merge returns.
func (*SeriesIDSet) MergeInPlace ¶
func (s *SeriesIDSet) MergeInPlace(other *SeriesIDSet)
MergeInPlace merges other into s, modifying s in the process.
func (*SeriesIDSet) Remove ¶
func (s *SeriesIDSet) Remove(id SeriesID)
Remove removes the id from the set.
func (*SeriesIDSet) RemoveNoLock ¶
func (s *SeriesIDSet) RemoveNoLock(id SeriesID)
RemoveNoLock removes the id from the set. RemoveNoLock is not safe for use from multiple goroutines. The caller must manage synchronization.
func (*SeriesIDSet) RemoveSet ¶
func (s *SeriesIDSet) RemoveSet(other *SeriesIDSet)
RemoveSet removes all values in other from s, if they exist.
func (*SeriesIDSet) Slice ¶
func (s *SeriesIDSet) Slice() []uint64
Slice returns a slice of series ids.
func (*SeriesIDSet) String ¶
func (s *SeriesIDSet) String() string
func (*SeriesIDSet) UnmarshalBinary ¶
func (s *SeriesIDSet) UnmarshalBinary(data []byte) error
UnmarshalBinary unmarshals data into the set.
func (*SeriesIDSet) UnmarshalBinaryUnsafe ¶
func (s *SeriesIDSet) UnmarshalBinaryUnsafe(data []byte) error
UnmarshalBinaryUnsafe unmarshals data into the set. References to the underlying data are used so data should not be reused by caller.
type SeriesIDSetIterable ¶
type UnifiedDiff ¶
type UnifiedDiff struct { A []string // First sequence lines FromFile string // First file name FromDate string // First file time B []string // Second sequence lines ToFile string // Second file name ToDate string // Second file time Eol string // Headers end of line, defaults to LF Context int // Number of context lines }
Unified diff parameters