diff options
| author | Shulhan <ms@kilabit.info> | 2026-04-12 20:35:12 +0700 |
|---|---|---|
| committer | Shulhan <ms@kilabit.info> | 2026-04-12 20:35:18 +0700 |
| commit | 05849aa15abc0bcfc6a76fc86af2ef6590023ce2 (patch) | |
| tree | ee88bb0b639b904232e79fcd5ecd94ba62ae2180 | |
| parent | 986ad438d702ebf7d2604def94eaf3e807be7cfd (diff) | |
| download | pakakeh.go-05849aa15abc0bcfc6a76fc86af2ef6590023ce2.tar.xz | |
text/diff: rename LevelWords to LevelChunks
Using Words seems like misleading since we capture the characters
difference, not a word as in human language.
| -rw-r--r-- | lib/text/diff/diff.go | 23 | ||||
| -rw-r--r-- | lib/text/diff/diff_test.go | 4 | ||||
| -rw-r--r-- | lib/text/diff/func.go | 17 |
3 files changed, 24 insertions, 20 deletions
diff --git a/lib/text/diff/diff.go b/lib/text/diff/diff.go index 129a8d6d..91cb1372 100644 --- a/lib/text/diff/diff.go +++ b/lib/text/diff/diff.go @@ -11,6 +11,27 @@ import ( "strings" ) +// DefMatchLen minimum number of bytes used for searching the next +// matched chunk in line. +const DefMatchLen = 5 + +// DefMatchRatio define default minimum match ratio to be considered as +// change. +const DefMatchRatio = 0.7 + +// List of comparison levels. +const ( + // LevelLines captures the differences on lines level. + // This will fills the [Data.Adds] and [Data.Dels] only. + LevelLines = iota + + // LevelChunks captures the differences on character level, when + // changes is on the same line. + // This will fills the [Data.LineChunks], [Data.Adds], and + // [Data.Dels]. + LevelChunks +) + // Data stores additions, deletions, and changes between two texts. type Data struct { // OldName and NewName stores the file names being compared from @@ -183,7 +204,7 @@ func Lines(oldlines, newlines []Line, level int) (diff *Data) { diff.PushAdd(newlines[y]) } - if level == LevelWords { + if level == LevelChunks { // Process each changes to find modified chunkes. for x, change := range diff.LineChunks { adds, dels := Bytes(change.Old.Val, change.New.Val, 0, 0) diff --git a/lib/text/diff/diff_test.go b/lib/text/diff/diff_test.go index 1c3c8479..001e962c 100644 --- a/lib/text/diff/diff_test.go +++ b/lib/text/diff/diff_test.go @@ -101,7 +101,7 @@ func TestBytesRatio(t *testing.T) { } } -func TestFiles_LevelWords(t *testing.T) { +func TestFiles_LevelChunks(t *testing.T) { listCase := []struct { desc string oldFile string @@ -191,7 +191,7 @@ func TestFiles_LevelWords(t *testing.T) { var sb strings.Builder for _, tc := range listCase { - diff, err := Files(tc.oldFile, tc.newFile, LevelWords) + diff, err := Files(tc.oldFile, tc.newFile, LevelChunks) if err != nil { t.Fatalf(`%s: %s`, tc.desc, err) } diff --git a/lib/text/diff/func.go b/lib/text/diff/func.go index 6acc5178..7efe55e2 100644 --- a/lib/text/diff/func.go +++ b/lib/text/diff/func.go @@ -8,23 +8,6 @@ import ( "git.sr.ht/~shulhan/pakakeh.go/lib/text" ) -const ( - // LevelLines define that we want only lines change set. - LevelLines = iota - // LevelWords define that we want the change not only capture the - // different per line, but also changes inside the line. - LevelWords -) - -const ( - // DefMatchLen minimum number of bytes used for searching the next - // matched chunk in line. - DefMatchLen = 5 - // DefMatchRatio define default minimum match ratio to be considered as - // change. - DefMatchRatio = 0.7 -) - // Bytes returns the character differences (additions and deletion) between // old and new bytes, start at specific position. func Bytes(old, new []byte, oldat, newat int) (adds, dels text.Chunks) { |
