feat: port the client to MyGo native UI (0.3.0)
Switch from Asuka to MyGo, keeping the API client and the feature set: - servers: saved servers, passwordless and password login, proxy, self-signed certificates, per-server device tokens (Keystore), connect form, instance list sheet with switch/disconnect - sessions: virtualized list, search, new session, rename/archive/delete, running indicator, event stream keeps it live - chat: pushed window with system back bar, streaming Markdown via ui/markdown, reasoning and tool rows with image previews, footer menu (read aloud via a Kotlin TTS plugin, copy, fork), user message menu (copy, revert, fork), context usage ring and detail sheet, attachments (photos/files via the document picker), permissions - release builds allow plain http:// LAN servers (Go's net/http dials directly, no cleartext manifest needed) - a local fork of MyGo (third_party/mygo) lowers the Android host's minSdk to 24 (Android 7.0); Go 1.27.4 via mise Version 0.3.0.
This commit is contained in:
1 parent
2fb4a8ea79
commit
6c0ee61750
2538 files changed
+675779
-4792
No files matched your search
+252
@@ -0,0 +1,252 @@
|
||||
package diff
|
||||
|
||||
// compact slides the groups of changed lines of one side (r, its lines
|
||||
// interned as ids and as text) up and down where the same lines frame
|
||||
// them, as git's xdl_change_compact: groups merge when they meet, line
|
||||
// up with a group of the other side (ro) where they can, and otherwise
|
||||
// take the place the indent heuristic scores best.
|
||||
func compact(r, ro []bool, ids []int32, lines []string) {
|
||||
g, gOther := groupInit(r), groupInit(ro)
|
||||
for {
|
||||
if g.end != g.start {
|
||||
var size, earliestEnd int
|
||||
endMatchingOther := -1
|
||||
for {
|
||||
size = g.end - g.start
|
||||
endMatchingOther = -1
|
||||
for g.slideUp(r, ids) {
|
||||
gOther.previous(ro)
|
||||
}
|
||||
earliestEnd = g.end
|
||||
if gOther.end > gOther.start {
|
||||
endMatchingOther = g.end
|
||||
}
|
||||
for g.slideDown(r, ids) {
|
||||
gOther.next(ro)
|
||||
if gOther.end > gOther.start {
|
||||
endMatchingOther = g.end
|
||||
}
|
||||
}
|
||||
if size == g.end-g.start {
|
||||
break
|
||||
}
|
||||
}
|
||||
switch {
|
||||
case g.end == earliestEnd:
|
||||
// It could not move.
|
||||
case endMatchingOther != -1:
|
||||
// Back to the last place matching a group of the other side.
|
||||
for gOther.end == gOther.start {
|
||||
if !g.slideUp(r, ids) || !gOther.previous(ro) {
|
||||
break
|
||||
}
|
||||
}
|
||||
default:
|
||||
shift := max(earliestEnd, g.end-size-1, g.end-maxSliding)
|
||||
best := -1
|
||||
var bestScore score
|
||||
for ; shift <= g.end; shift++ {
|
||||
var s score
|
||||
s.add(measure(lines, shift))
|
||||
s.add(measure(lines, shift-size))
|
||||
if best == -1 || s.cmp(bestScore) <= 0 {
|
||||
bestScore, best = s, shift
|
||||
}
|
||||
}
|
||||
for g.end > best {
|
||||
if !g.slideUp(r, ids) || !gOther.previous(ro) {
|
||||
break
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
if !g.next(r) || !gOther.next(ro) {
|
||||
return
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// group is a run of changed lines, maybe empty, between unchanged ones.
|
||||
type group struct{ start, end int }
|
||||
|
||||
func groupInit(r []bool) group {
|
||||
g := group{}
|
||||
for g.end < len(r) && r[g.end] {
|
||||
g.end++
|
||||
}
|
||||
return g
|
||||
}
|
||||
|
||||
func (g *group) next(r []bool) bool {
|
||||
if g.end >= len(r) {
|
||||
return false
|
||||
}
|
||||
g.start = g.end + 1
|
||||
for g.end = g.start; g.end < len(r) && r[g.end]; g.end++ {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (g *group) previous(r []bool) bool {
|
||||
if g.start <= 0 {
|
||||
return false
|
||||
}
|
||||
g.end = g.start - 1
|
||||
for g.start = g.end; g.start > 0 && r[g.start-1]; g.start-- {
|
||||
}
|
||||
return true
|
||||
}
|
||||
|
||||
func (g *group) slideDown(r []bool, ids []int32) bool {
|
||||
if g.start < g.end && g.end < len(r) && ids[g.start] == ids[g.end] {
|
||||
r[g.start] = false
|
||||
g.start++
|
||||
r[g.end] = true
|
||||
g.end++
|
||||
for g.end < len(r) && r[g.end] {
|
||||
g.end++
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func (g *group) slideUp(r []bool, ids []int32) bool {
|
||||
if g.start < g.end && g.start > 0 && ids[g.start-1] == ids[g.end-1] {
|
||||
g.start--
|
||||
r[g.start] = true
|
||||
g.end--
|
||||
r[g.end] = false
|
||||
for g.start > 0 && r[g.start-1] {
|
||||
g.start--
|
||||
}
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
// The weights of git's indent heuristic (xdiffi.c), tuned on a corpus
|
||||
// of real changes.
|
||||
const (
|
||||
maxIndent = 200
|
||||
maxBlanks = 20
|
||||
startOfFilePenalty = 1
|
||||
endOfFilePenalty = 21
|
||||
totalBlankWeight = -30
|
||||
postBlankWeight = 6
|
||||
relativeIndentPenalty = -4
|
||||
relativeIndentWithBlankPenalty = 10
|
||||
relativeOutdentPenalty = 24
|
||||
relativeOutdentWithBlank = 17
|
||||
relativeDedentPenalty = 23
|
||||
relativeDedentWithBlank = 17
|
||||
indentWeight = 60
|
||||
maxSliding = 100
|
||||
)
|
||||
|
||||
// indent is how far a line is indented, tabs to multiples of 8, or -1
|
||||
// for a blank line.
|
||||
func indent(s string) int {
|
||||
n := 0
|
||||
for i := 0; i < len(s); i++ {
|
||||
switch s[i] {
|
||||
case ' ':
|
||||
n++
|
||||
case '\t':
|
||||
n += 8 - n%8
|
||||
case '\r', '\n', '\f', '\v':
|
||||
default:
|
||||
return n
|
||||
}
|
||||
if n >= maxIndent {
|
||||
return maxIndent
|
||||
}
|
||||
}
|
||||
return -1
|
||||
}
|
||||
|
||||
type split struct {
|
||||
endOfFile bool
|
||||
indent int
|
||||
preBlank, preIndent int
|
||||
postBlank, postIndent int
|
||||
}
|
||||
|
||||
func measure(lines []string, at int) split {
|
||||
var m split
|
||||
if at >= len(lines) {
|
||||
m.endOfFile, m.indent = true, -1
|
||||
} else {
|
||||
m.indent = indent(lines[at])
|
||||
}
|
||||
m.preIndent = -1
|
||||
for i := at - 1; i >= 0; i-- {
|
||||
if m.preIndent = indent(lines[i]); m.preIndent != -1 {
|
||||
break
|
||||
}
|
||||
if m.preBlank++; m.preBlank == maxBlanks {
|
||||
m.preIndent = 0
|
||||
break
|
||||
}
|
||||
}
|
||||
m.postIndent = -1
|
||||
for i := at + 1; i < len(lines); i++ {
|
||||
if m.postIndent = indent(lines[i]); m.postIndent != -1 {
|
||||
break
|
||||
}
|
||||
if m.postBlank++; m.postBlank == maxBlanks {
|
||||
m.postIndent = 0
|
||||
break
|
||||
}
|
||||
}
|
||||
return m
|
||||
}
|
||||
|
||||
type score struct{ effectiveIndent, penalty int }
|
||||
|
||||
func (s *score) add(m split) {
|
||||
if m.preIndent == -1 && m.preBlank == 0 {
|
||||
s.penalty += startOfFilePenalty
|
||||
}
|
||||
if m.endOfFile {
|
||||
s.penalty += endOfFilePenalty
|
||||
}
|
||||
postBlank := 0
|
||||
if m.indent == -1 {
|
||||
postBlank = 1 + m.postBlank
|
||||
}
|
||||
totalBlank := m.preBlank + postBlank
|
||||
s.penalty += totalBlankWeight*totalBlank + postBlankWeight*postBlank
|
||||
in := m.indent
|
||||
if in == -1 {
|
||||
in = m.postIndent
|
||||
}
|
||||
blanks := totalBlank != 0
|
||||
s.effectiveIndent += in
|
||||
switch {
|
||||
case in == -1, m.preIndent == -1, in == m.preIndent:
|
||||
case in > m.preIndent:
|
||||
s.penalty += pick(blanks, relativeIndentWithBlankPenalty, relativeIndentPenalty)
|
||||
case m.postIndent != -1 && m.postIndent > in:
|
||||
s.penalty += pick(blanks, relativeOutdentWithBlank, relativeOutdentPenalty)
|
||||
default:
|
||||
s.penalty += pick(blanks, relativeDedentWithBlank, relativeDedentPenalty)
|
||||
}
|
||||
}
|
||||
|
||||
func pick(c bool, a, b int) int {
|
||||
if c {
|
||||
return a
|
||||
}
|
||||
return b
|
||||
}
|
||||
|
||||
func (s score) cmp(o score) int {
|
||||
c := 0
|
||||
if s.effectiveIndent > o.effectiveIndent {
|
||||
c = 1
|
||||
} else if s.effectiveIndent < o.effectiveIndent {
|
||||
c = -1
|
||||
}
|
||||
return indentWeight*c + s.penalty - o.penalty
|
||||
}
|
||||
+119
@@ -0,0 +1,119 @@
|
||||
// Package diff compares sequences: the lines of two texts, or the words
|
||||
// of two lines. It finds the changes with the histogram algorithm of git
|
||||
// (xhistogram.c), which anchors on lines that occur rarely and so keeps
|
||||
// the matches people expect in source code, falling back to Myers's O(ND)
|
||||
// algorithm, with xdiff's cost limit, where every line repeats too often.
|
||||
// Groups of changed lines are then slid to start and end where blank
|
||||
// lines and indentation make them read as whole blocks, with git's indent
|
||||
// heuristic.
|
||||
package diff
|
||||
|
||||
// Hunk is a change: the elements A0 to A1 of the old sequence replaced by
|
||||
// B0 to B1 of the new one. A0 == A1 is an insertion before A0, B0 == B1 a
|
||||
// deletion.
|
||||
type Hunk struct{ A0, A1, B0, B1 int }
|
||||
|
||||
// Ints compares two sequences of interned elements, equal elements having
|
||||
// equal values, and returns the hunks turning a into b, in order.
|
||||
func Ints(a, b []int32) []Hunk {
|
||||
d := differ{a: a, b: b}
|
||||
d.histogram(0, len(a), 0, len(b), 0)
|
||||
return d.hunks()
|
||||
}
|
||||
|
||||
// Lines compares the lines of two texts and returns the hunks of lines
|
||||
// turning a into b, slid to read as blocks of code.
|
||||
func Lines(a, b []string) []Hunk {
|
||||
ia, ib := Intern(a, b)
|
||||
d := differ{a: ia, b: ib}
|
||||
d.histogram(0, len(ia), 0, len(ib), 0)
|
||||
if d.da == nil {
|
||||
return nil
|
||||
}
|
||||
compact(d.da, d.db, ia, a)
|
||||
compact(d.db, d.da, ib, b)
|
||||
return d.hunks()
|
||||
}
|
||||
|
||||
// Intern numbers the distinct strings of a and b, equal strings sharing a
|
||||
// number.
|
||||
func Intern(a, b []string) ([]int32, []int32) {
|
||||
ids := make(map[string]int32, len(a)+len(b))
|
||||
conv := func(s []string) []int32 {
|
||||
out := make([]int32, len(s))
|
||||
for i, v := range s {
|
||||
id, ok := ids[v]
|
||||
if !ok {
|
||||
id = int32(len(ids))
|
||||
ids[v] = id
|
||||
}
|
||||
out[i] = id
|
||||
}
|
||||
return out
|
||||
}
|
||||
return conv(a), conv(b)
|
||||
}
|
||||
|
||||
// differ marks the elements of a and b that the hunks change.
|
||||
type differ struct {
|
||||
a, b []int32
|
||||
da, db []bool // changed elements
|
||||
next, cnt []int // histogram scratch, by position in a
|
||||
recs map[int32]record
|
||||
recsCap int
|
||||
myersV []int
|
||||
myersExtra int
|
||||
}
|
||||
|
||||
type record struct{ first, count int }
|
||||
|
||||
func (d *differ) mark(a0, a1, b0, b1 int) {
|
||||
if a0 == a1 && b0 == b1 {
|
||||
return
|
||||
}
|
||||
if d.da == nil {
|
||||
d.da, d.db = make([]bool, len(d.a)), make([]bool, len(d.b))
|
||||
}
|
||||
for i := a0; i < a1; i++ {
|
||||
d.da[i] = true
|
||||
}
|
||||
for i := b0; i < b1; i++ {
|
||||
d.db[i] = true
|
||||
}
|
||||
}
|
||||
|
||||
// hunks turns the marks into hunks.
|
||||
func (d *differ) hunks() []Hunk {
|
||||
if d.da == nil {
|
||||
return nil
|
||||
}
|
||||
var out []Hunk
|
||||
i, j := 0, 0
|
||||
for i < len(d.a) || j < len(d.b) {
|
||||
if i < len(d.a) && j < len(d.b) && !d.da[i] && !d.db[j] {
|
||||
i++
|
||||
j++
|
||||
continue
|
||||
}
|
||||
h := Hunk{A0: i, B0: j}
|
||||
for i < len(d.a) && d.da[i] {
|
||||
i++
|
||||
}
|
||||
for j < len(d.b) && d.db[j] {
|
||||
j++
|
||||
}
|
||||
h.A1, h.B1 = i, j
|
||||
if h.A0 == h.A1 && h.B0 == h.B1 {
|
||||
// Unpaired unchanged elements cannot happen; never loop.
|
||||
if i < len(d.a) {
|
||||
i++
|
||||
}
|
||||
if j < len(d.b) {
|
||||
j++
|
||||
}
|
||||
continue
|
||||
}
|
||||
out = append(out, h)
|
||||
}
|
||||
return out
|
||||
}
|
||||
+194
@@ -0,0 +1,194 @@
|
||||
package diff
|
||||
|
||||
import (
|
||||
"math/rand/v2"
|
||||
"reflect"
|
||||
"slices"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
// apply turns a into b with the hunks, checking that what they leave
|
||||
// alone is the same on both sides.
|
||||
func apply(t *testing.T, a, b []string, hunks []Hunk) {
|
||||
t.Helper()
|
||||
var out []string
|
||||
i, j := 0, 0
|
||||
for _, h := range hunks {
|
||||
if h.A0 < i || h.B0 < j || h.A0-i != h.B0-j || h.A1 < h.A0 || h.B1 < h.B0 {
|
||||
t.Fatalf("hunk %+v out of order after %d, %d", h, i, j)
|
||||
}
|
||||
for ; i < h.A0; i, j = i+1, j+1 {
|
||||
if a[i] != b[j] {
|
||||
t.Fatalf("unchanged line %d of a %q is %q in b", i, a[i], b[j])
|
||||
}
|
||||
out = append(out, a[i])
|
||||
}
|
||||
out = append(out, b[h.B0:h.B1]...)
|
||||
i, j = h.A1, h.B1
|
||||
}
|
||||
if len(a)-i != len(b)-j {
|
||||
t.Fatalf("tails differ: %d of a, %d of b", len(a)-i, len(b)-j)
|
||||
}
|
||||
out = append(out, a[i:]...)
|
||||
if !reflect.DeepEqual(out, b) && !(len(out) == 0 && len(b) == 0) {
|
||||
t.Fatalf("applying %v gives %q, want %q", hunks, out, b)
|
||||
}
|
||||
}
|
||||
|
||||
func lines(s string) []string {
|
||||
if s == "" {
|
||||
return nil
|
||||
}
|
||||
return strings.Split(s, "\n")
|
||||
}
|
||||
|
||||
func TestLines(t *testing.T) {
|
||||
for _, c := range []struct {
|
||||
a, b string
|
||||
want []Hunk
|
||||
}{
|
||||
{"", "", nil},
|
||||
{"a", "a", nil},
|
||||
{"", "a\nb", []Hunk{{0, 0, 0, 2}}},
|
||||
{"a\nb", "", []Hunk{{0, 2, 0, 0}}},
|
||||
{"a\nb\nc", "a\nx\nc", []Hunk{{1, 2, 1, 2}}},
|
||||
{"a\nb\nc", "a\nc", []Hunk{{1, 2, 1, 1}}},
|
||||
{"a\nc", "a\nb\nc", []Hunk{{1, 1, 1, 2}}},
|
||||
{"a\nb\nc\nd", "b\nc\nd\ne", []Hunk{{0, 1, 0, 0}, {4, 4, 3, 4}}},
|
||||
} {
|
||||
got := Lines(lines(c.a), lines(c.b))
|
||||
if !reflect.DeepEqual(got, c.want) {
|
||||
t.Errorf("Lines(%q, %q) = %v, want %v", c.a, c.b, got, c.want)
|
||||
}
|
||||
apply(t, lines(c.a), lines(c.b), got)
|
||||
}
|
||||
}
|
||||
|
||||
// A function added between two others shows as the function and the
|
||||
// blank line after it, not as the end of one function and the start of
|
||||
// the next, as git's indent heuristic places it.
|
||||
func TestLinesSlide(t *testing.T) {
|
||||
a := lines("func a() {\n\tx()\n}\n\nfunc c() {\n\tx()\n}")
|
||||
b := lines("func a() {\n\tx()\n}\n\nfunc b() {\n\tx()\n}\n\nfunc c() {\n\tx()\n}")
|
||||
got := Lines(a, b)
|
||||
want := []Hunk{{4, 4, 4, 8}}
|
||||
if !reflect.DeepEqual(got, want) {
|
||||
t.Fatalf("Lines = %v, want %v: %q", got, want, b[got[0].B0:got[0].B1])
|
||||
}
|
||||
apply(t, a, b, got)
|
||||
}
|
||||
|
||||
// Histogram anchors on rare lines: moving a block keeps the unique lines
|
||||
// matched rather than the braces that repeat.
|
||||
func TestLinesHistogram(t *testing.T) {
|
||||
a := lines("one\n{\n}\ntwo\n{\n}\nthree")
|
||||
b := lines("two\n{\n}\none\n{\n}\nthree")
|
||||
got := Lines(a, b)
|
||||
apply(t, a, b, got)
|
||||
changed := 0
|
||||
for _, h := range got {
|
||||
changed += h.A1 - h.A0 + h.B1 - h.B0
|
||||
}
|
||||
if changed > 6 {
|
||||
t.Fatalf("Lines = %v, %d lines changed", got, changed)
|
||||
}
|
||||
}
|
||||
|
||||
func TestLinesRandom(t *testing.T) {
|
||||
r := rand.New(rand.NewPCG(1, 2))
|
||||
words := []string{"a", "b", "c", "{", "}", "", "x := 1", "return"}
|
||||
gen := func(n int) []string {
|
||||
out := make([]string, n)
|
||||
for i := range out {
|
||||
out[i] = words[r.IntN(len(words))]
|
||||
}
|
||||
return out
|
||||
}
|
||||
for range 2000 {
|
||||
a := gen(r.IntN(40))
|
||||
b := append([]string(nil), a...)
|
||||
// Edit some.
|
||||
for range r.IntN(6) {
|
||||
if len(b) == 0 || r.IntN(2) == 0 {
|
||||
at := r.IntN(len(b) + 1)
|
||||
b = append(b[:at], append(gen(r.IntN(4)+1), b[at:]...)...)
|
||||
} else {
|
||||
at := r.IntN(len(b))
|
||||
b = append(b[:at], b[min(len(b), at+r.IntN(3)+1):]...)
|
||||
}
|
||||
}
|
||||
if r.IntN(5) == 0 {
|
||||
b = gen(r.IntN(40))
|
||||
}
|
||||
apply(t, a, b, Lines(a, b))
|
||||
ia, ib := Intern(a, b)
|
||||
d := differ{a: ia, b: ib}
|
||||
d.myers(0, len(ia), 0, len(ib))
|
||||
apply(t, a, b, d.hunks())
|
||||
}
|
||||
}
|
||||
|
||||
// Myers's cost limit keeps unrelated texts quick and the result valid.
|
||||
func TestMyersCostLimit(t *testing.T) {
|
||||
r := rand.New(rand.NewPCG(3, 4))
|
||||
gen := func(n int) []string {
|
||||
out := make([]string, n)
|
||||
for i := range out {
|
||||
out[i] = string(rune('a' + r.IntN(3)))
|
||||
}
|
||||
return out
|
||||
}
|
||||
a, b := gen(5000), gen(5000)
|
||||
apply(t, a, b, Lines(a, b))
|
||||
}
|
||||
|
||||
func TestWords(t *testing.T) {
|
||||
a := "return foo(bar, baz)"
|
||||
b := "return foo(qux, baz)"
|
||||
ra, rb, same := Words(a, b)
|
||||
if !reflect.DeepEqual(ra, []Range{{11, 14}}) || !reflect.DeepEqual(rb, []Range{{11, 14}}) {
|
||||
t.Fatalf("Words = %v, %v", ra, rb)
|
||||
}
|
||||
if same < 0.8 {
|
||||
t.Fatalf("same = %v", same)
|
||||
}
|
||||
// Changed words across whitespace join.
|
||||
ra, rb, _ = Words("a b c d", "a x y d")
|
||||
if !reflect.DeepEqual(ra, []Range{{2, 5}}) || !reflect.DeepEqual(rb, []Range{{2, 5}}) {
|
||||
t.Fatalf("Words joined = %v, %v", ra, rb)
|
||||
}
|
||||
ra, rb, _ = Chars("colour", "color")
|
||||
if !reflect.DeepEqual(ra, []Range{{4, 5}}) || rb != nil {
|
||||
t.Fatalf("Chars = %v, %v", ra, rb)
|
||||
}
|
||||
}
|
||||
|
||||
func BenchmarkLines(b *testing.B) {
|
||||
var sb strings.Builder
|
||||
for i := range 20000 {
|
||||
sb.WriteString("line ")
|
||||
sb.WriteString(strings.Repeat("x", i%40))
|
||||
sb.WriteString("\n")
|
||||
}
|
||||
a := lines(sb.String())
|
||||
c := append([]string(nil), a...)
|
||||
for i := 100; i < len(c); i += 997 {
|
||||
c[i] = "changed"
|
||||
}
|
||||
b.ResetTimer()
|
||||
for range b.N {
|
||||
Lines(a, c)
|
||||
}
|
||||
}
|
||||
|
||||
// The rarity of a line is its record's count in the region, as git's,
|
||||
// so that the last of repeated lines is not taken for a unique anchor.
|
||||
func TestHistogramRarity(t *testing.T) {
|
||||
a := []string{"", "", "", "\tv14 := f13()", "\tv22 := f25()", "}", ""}
|
||||
b := []string{"\t\treturn err", "\treturn nil", "", "", "\tv22 := f25()", "}", ""}
|
||||
got, want := Lines(a, b), []Hunk{{0, 0, 0, 2}, {2, 4, 4, 4}} // git diff --histogram's
|
||||
if !slices.Equal(got, want) {
|
||||
t.Errorf("hunks %v, want git's %v", got, want)
|
||||
}
|
||||
}
|
||||
+141
@@ -0,0 +1,141 @@
|
||||
package diff
|
||||
|
||||
// maxChain is how often an element may occur in a region of a for the
|
||||
// histogram algorithm to anchor on it, as git's.
|
||||
const maxChain = 64
|
||||
|
||||
// maxDepth bounds the recursion of histogram, past which a region is
|
||||
// compared with Myers's algorithm.
|
||||
const maxDepth = 256
|
||||
|
||||
// histogram compares a[a0:a1] with b[b0:b1]: it finds the longest common
|
||||
// run of elements that occur least often in a, and compares what is
|
||||
// before and after it the same way.
|
||||
func (d *differ) histogram(a0, a1, b0, b1, depth int) {
|
||||
for {
|
||||
// Common prefix and suffix.
|
||||
for a0 < a1 && b0 < b1 && d.a[a0] == d.b[b0] {
|
||||
a0++
|
||||
b0++
|
||||
}
|
||||
for a0 < a1 && b0 < b1 && d.a[a1-1] == d.b[b1-1] {
|
||||
a1--
|
||||
b1--
|
||||
}
|
||||
if a0 == a1 || b0 == b1 {
|
||||
d.mark(a0, a1, b0, b1)
|
||||
return
|
||||
}
|
||||
if depth > maxDepth {
|
||||
d.myers(a0, a1, b0, b1)
|
||||
return
|
||||
}
|
||||
la0, la1, lb0, lb1, ok := d.lcs(a0, a1, b0, b1)
|
||||
if !ok {
|
||||
d.myers(a0, a1, b0, b1)
|
||||
return
|
||||
}
|
||||
if la0 == la1 {
|
||||
// Nothing in common.
|
||||
d.mark(a0, a1, b0, b1)
|
||||
return
|
||||
}
|
||||
depth++
|
||||
// Recurse into the smaller side, loop on the other.
|
||||
if la0-a0+lb0-b0 < a1-la1+b1-lb1 {
|
||||
d.histogram(a0, la0, b0, lb0, depth)
|
||||
a0, b0 = la1, lb1
|
||||
} else {
|
||||
d.histogram(la1, a1, lb1, b1, depth)
|
||||
a1, b1 = la0, lb0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// lcs finds the longest run common to a[a0:a1] and b[b0:b1] whose
|
||||
// rarest element occurs least often in a. ok is false when every
|
||||
// element both have occurs too often in a, for Myers's algorithm.
|
||||
func (d *differ) lcs(a0, a1, b0, b1 int) (la0, la1, lb0, lb1 int, ok bool) {
|
||||
n := a1 - a0
|
||||
if cap(d.next) < n {
|
||||
d.next, d.cnt = make([]int, n), make([]int, n)
|
||||
}
|
||||
next, cnt := d.next[:n], d.cnt[:n]
|
||||
// Clearing costs the map's capacity: a small region takes a new one.
|
||||
if d.recs == nil || d.recsCap > 8*n+64 {
|
||||
d.recs, d.recsCap = make(map[int32]record, n), n
|
||||
} else {
|
||||
clear(d.recs)
|
||||
}
|
||||
recs := d.recs
|
||||
// From the end, so each record's chain goes forward from its first.
|
||||
for i := a1 - 1; i >= a0; i-- {
|
||||
r, seen := recs[d.a[i]]
|
||||
if seen {
|
||||
next[i-a0] = r.first
|
||||
r.count++
|
||||
} else {
|
||||
next[i-a0] = -1
|
||||
r.count = 1
|
||||
}
|
||||
r.first = i
|
||||
recs[d.a[i]] = r
|
||||
}
|
||||
// How rare each line is: its record's count in the region, as git's
|
||||
// CNT, not the count from it to the region's end, which makes the last
|
||||
// of many blank lines look unique.
|
||||
for i := a0; i < a1; i++ {
|
||||
cnt[i-a0] = recs[d.a[i]].count
|
||||
}
|
||||
best := maxChain + 1
|
||||
common := false
|
||||
la0, la1 = a0, a0
|
||||
for bi := b0; bi < b1; {
|
||||
bnext := bi + 1
|
||||
r, found := recs[d.b[bi]]
|
||||
if !found {
|
||||
bi = bnext
|
||||
continue
|
||||
}
|
||||
common = true
|
||||
if r.count > best {
|
||||
bi = bnext
|
||||
continue
|
||||
}
|
||||
for as := r.first; as >= 0; {
|
||||
s, bs, e, be := as, bi, as+1, bi+1
|
||||
rc := cnt[as-a0]
|
||||
for s > a0 && bs > b0 && d.a[s-1] == d.b[bs-1] {
|
||||
s--
|
||||
bs--
|
||||
if rc > 1 {
|
||||
rc = min(rc, cnt[s-a0])
|
||||
}
|
||||
}
|
||||
for e < a1 && be < b1 && d.a[e] == d.b[be] {
|
||||
if rc > 1 {
|
||||
rc = min(rc, cnt[e-a0])
|
||||
}
|
||||
e++
|
||||
be++
|
||||
}
|
||||
if bnext < be {
|
||||
bnext = be
|
||||
}
|
||||
if la1-la0 < e-s || rc < best {
|
||||
la0, la1, lb0, lb1, best = s, e, bs, be, rc
|
||||
}
|
||||
// The next occurrence past this run.
|
||||
np := next[as-a0]
|
||||
for np >= 0 && np < e {
|
||||
np = next[np-a0]
|
||||
}
|
||||
as = np
|
||||
}
|
||||
bi = bnext
|
||||
}
|
||||
if common && best > maxChain {
|
||||
return 0, 0, 0, 0, false
|
||||
}
|
||||
return la0, la1, lb0, lb1, true
|
||||
}
|
||||
+152
@@ -0,0 +1,152 @@
|
||||
package diff
|
||||
|
||||
// Myers's algorithm in linear space, as git's xdiff runs it (xdiffi.c):
|
||||
// the middle snake of each region splits it in two, until the cost of a
|
||||
// region passes a limit, where the furthest diagonals reached split it,
|
||||
// which bounds the time on large unrelated texts at the price of a
|
||||
// minimal diff.
|
||||
|
||||
const (
|
||||
maxCostMin = 256 // XDL_MAX_COST_MIN
|
||||
lineMax = int(^uint(0) >> 2)
|
||||
)
|
||||
|
||||
func (d *differ) myers(a0, a1, b0, b1 int) {
|
||||
n := len(d.a) + len(d.b) + 3
|
||||
if len(d.myersV) < 2*n {
|
||||
d.myersV = make([]int, 2*n)
|
||||
}
|
||||
cost := 1
|
||||
for cost*cost < n {
|
||||
cost <<= 1
|
||||
}
|
||||
d.myersExtra = max(cost, maxCostMin)
|
||||
d.recsCmp(a0, a1, b0, b1, false)
|
||||
}
|
||||
|
||||
func (d *differ) recsCmp(off1, lim1, off2, lim2 int, needMin bool) {
|
||||
for off1 < lim1 && off2 < lim2 && d.a[off1] == d.b[off2] {
|
||||
off1++
|
||||
off2++
|
||||
}
|
||||
for off1 < lim1 && off2 < lim2 && d.a[lim1-1] == d.b[lim2-1] {
|
||||
lim1--
|
||||
lim2--
|
||||
}
|
||||
if off1 == lim1 || off2 == lim2 {
|
||||
d.mark(off1, lim1, off2, lim2)
|
||||
return
|
||||
}
|
||||
i1, i2, minLo, minHi := d.split(off1, lim1, off2, lim2, needMin)
|
||||
d.recsCmp(off1, i1, off2, i2, minLo)
|
||||
d.recsCmp(i1, lim1, i2, lim2, minHi)
|
||||
}
|
||||
|
||||
// split finds where to split a[off1:lim1] and b[off2:lim2]: the middle
|
||||
// snake, or past the cost limit the best diagonal reached.
|
||||
func (d *differ) split(off1, lim1, off2, lim2 int, needMin bool) (int, int, bool, bool) {
|
||||
ha1, ha2 := d.a, d.b
|
||||
nd := len(d.a) + len(d.b) + 3
|
||||
base := len(d.b) + 1
|
||||
// Diagonal k is at base+k of each vector.
|
||||
vf, vb := d.myersV[:nd], d.myersV[nd:2*nd]
|
||||
dmin, dmax := off1-lim2, lim1-off2
|
||||
fmid, bmid := off1-off2, lim1-lim2
|
||||
odd := (fmid-bmid)&1 != 0
|
||||
fmin, fmax := fmid, fmid
|
||||
bmin, bmax := bmid, bmid
|
||||
vf[base+fmid] = off1
|
||||
vb[base+bmid] = lim1
|
||||
for ec := 1; ; ec++ {
|
||||
// Forward.
|
||||
if fmin > dmin {
|
||||
fmin--
|
||||
vf[base+fmin-1] = -1
|
||||
} else {
|
||||
fmin++
|
||||
}
|
||||
if fmax < dmax {
|
||||
fmax++
|
||||
vf[base+fmax+1] = -1
|
||||
} else {
|
||||
fmax--
|
||||
}
|
||||
for k := fmax; k >= fmin; k -= 2 {
|
||||
var i1 int
|
||||
if vf[base+k-1] >= vf[base+k+1] {
|
||||
i1 = vf[base+k-1] + 1
|
||||
} else {
|
||||
i1 = vf[base+k+1]
|
||||
}
|
||||
i2 := i1 - k
|
||||
for i1 < lim1 && i2 < lim2 && ha1[i1] == ha2[i2] {
|
||||
i1++
|
||||
i2++
|
||||
}
|
||||
vf[base+k] = i1
|
||||
if odd && bmin <= k && k <= bmax && vb[base+k] <= i1 {
|
||||
return i1, i2, true, true
|
||||
}
|
||||
}
|
||||
// Backward.
|
||||
if bmin > dmin {
|
||||
bmin--
|
||||
vb[base+bmin-1] = lineMax
|
||||
} else {
|
||||
bmin++
|
||||
}
|
||||
if bmax < dmax {
|
||||
bmax++
|
||||
vb[base+bmax+1] = lineMax
|
||||
} else {
|
||||
bmax--
|
||||
}
|
||||
for k := bmax; k >= bmin; k -= 2 {
|
||||
var i1 int
|
||||
if vb[base+k-1] < vb[base+k+1] {
|
||||
i1 = vb[base+k-1]
|
||||
} else {
|
||||
i1 = vb[base+k+1] - 1
|
||||
}
|
||||
i2 := i1 - k
|
||||
for i1 > off1 && i2 > off2 && ha1[i1-1] == ha2[i2-1] {
|
||||
i1--
|
||||
i2--
|
||||
}
|
||||
vb[base+k] = i1
|
||||
if !odd && fmin <= k && k <= fmax && i1 <= vf[base+k] {
|
||||
return i1, i2, true, true
|
||||
}
|
||||
}
|
||||
if needMin || ec < d.myersExtra {
|
||||
continue
|
||||
}
|
||||
// Too costly: split where the furthest diagonals got.
|
||||
fbest, fbest1 := -1, -1
|
||||
for k := fmax; k >= fmin; k -= 2 {
|
||||
i1 := min(vf[base+k], lim1)
|
||||
i2 := i1 - k
|
||||
if lim2 < i2 {
|
||||
i1, i2 = lim2+k, lim2
|
||||
}
|
||||
if fbest < i1+i2 {
|
||||
fbest, fbest1 = i1+i2, i1
|
||||
}
|
||||
}
|
||||
bbest, bbest1 := lineMax, lineMax
|
||||
for k := bmax; k >= bmin; k -= 2 {
|
||||
i1 := max(off1, vb[base+k])
|
||||
i2 := i1 - k
|
||||
if i2 < off2 {
|
||||
i1, i2 = off2+k, off2
|
||||
}
|
||||
if i1+i2 < bbest {
|
||||
bbest, bbest1 = i1+i2, i1
|
||||
}
|
||||
}
|
||||
if (lim1+lim2)-bbest < fbest-(off1+off2) {
|
||||
return fbest1, fbest - fbest1, true, false
|
||||
}
|
||||
return bbest1, bbest - bbest1, false, true
|
||||
}
|
||||
}
|
||||
+132
@@ -0,0 +1,132 @@
|
||||
package diff
|
||||
|
||||
import (
|
||||
"unicode"
|
||||
"unicode/utf8"
|
||||
)
|
||||
|
||||
// Range is a range of bytes, Start to End.
|
||||
type Range struct{ Start, End int }
|
||||
|
||||
// Words compares two texts word by word, as review tools highlight what
|
||||
// changed within lines: words (letters, digits and underscores), runs of
|
||||
// whitespace and other characters one at a time. It returns the byte
|
||||
// ranges of a and b that changed, joined across the whitespace between
|
||||
// them, and how much of the two texts stayed the same, from 0 to 1, by
|
||||
// their bytes.
|
||||
func Words(a, b string) (ra, rb []Range, same float64) {
|
||||
return tokens(a, b, wordTokens)
|
||||
}
|
||||
|
||||
// Chars compares two texts character by character, as Words does by words.
|
||||
func Chars(a, b string) (ra, rb []Range, same float64) {
|
||||
return tokens(a, b, charTokens)
|
||||
}
|
||||
|
||||
func tokens(a, b string, split func(string, []Range) []Range) (ra, rb []Range, same float64) {
|
||||
ta, tb := split(a, nil), split(b, nil)
|
||||
ids := make(map[string]int32, len(ta)+len(tb))
|
||||
conv := func(s string, t []Range) []int32 {
|
||||
out := make([]int32, len(t))
|
||||
for i, r := range t {
|
||||
v := s[r.Start:r.End]
|
||||
id, ok := ids[v]
|
||||
if !ok {
|
||||
id = int32(len(ids))
|
||||
ids[v] = id
|
||||
}
|
||||
out[i] = id
|
||||
}
|
||||
return out
|
||||
}
|
||||
ia, ib := conv(a, ta), conv(b, tb)
|
||||
hunks := Ints(ia, ib)
|
||||
changed := 0
|
||||
for _, h := range hunks {
|
||||
if h.A0 < h.A1 {
|
||||
ra = join(ra, Range{ta[h.A0].Start, ta[h.A1-1].End}, a)
|
||||
changed += ta[h.A1-1].End - ta[h.A0].Start
|
||||
}
|
||||
if h.B0 < h.B1 {
|
||||
rb = join(rb, Range{tb[h.B0].Start, tb[h.B1-1].End}, b)
|
||||
changed += tb[h.B1-1].End - tb[h.B0].Start
|
||||
}
|
||||
}
|
||||
if total := len(a) + len(b); total > 0 {
|
||||
same = 1 - float64(changed)/float64(total)
|
||||
} else {
|
||||
same = 1
|
||||
}
|
||||
return ra, rb, same
|
||||
}
|
||||
|
||||
// join appends r to rs, merged with the last range when only whitespace
|
||||
// is between them.
|
||||
func join(rs []Range, r Range, s string) []Range {
|
||||
if n := len(rs); n > 0 {
|
||||
last := &rs[n-1]
|
||||
gap := s[last.End:r.Start]
|
||||
blank := true
|
||||
for _, c := range gap {
|
||||
if c == '\n' || !unicode.IsSpace(c) {
|
||||
blank = false
|
||||
break
|
||||
}
|
||||
}
|
||||
if blank {
|
||||
last.End = r.End
|
||||
return rs
|
||||
}
|
||||
}
|
||||
return append(rs, r)
|
||||
}
|
||||
|
||||
func isWord(r rune) bool {
|
||||
return r == '_' || unicode.IsLetter(r) || unicode.IsDigit(r)
|
||||
}
|
||||
|
||||
func wordTokens(s string, out []Range) []Range {
|
||||
for i := 0; i < len(s); {
|
||||
r, n := utf8.DecodeRuneInString(s[i:])
|
||||
j := i + n
|
||||
switch {
|
||||
case isWord(r):
|
||||
for j < len(s) {
|
||||
r, n := utf8.DecodeRuneInString(s[j:])
|
||||
if !isWord(r) {
|
||||
break
|
||||
}
|
||||
j += n
|
||||
}
|
||||
case r != '\n' && unicode.IsSpace(r):
|
||||
for j < len(s) {
|
||||
r, n := utf8.DecodeRuneInString(s[j:])
|
||||
if r == '\n' || !unicode.IsSpace(r) {
|
||||
break
|
||||
}
|
||||
j += n
|
||||
}
|
||||
}
|
||||
out = append(out, Range{i, j})
|
||||
i = j
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func charTokens(s string, out []Range) []Range {
|
||||
for i := 0; i < len(s); {
|
||||
_, n := utf8.DecodeRuneInString(s[i:])
|
||||
// Combining marks stay with their base.
|
||||
j := i + n
|
||||
for j < len(s) {
|
||||
r, n := utf8.DecodeRuneInString(s[j:])
|
||||
if !unicode.Is(unicode.Mn, r) && !unicode.Is(unicode.Me, r) && r != 0x200d && !(r >= 0xfe00 && r <= 0xfe0f) {
|
||||
break
|
||||
}
|
||||
j += n
|
||||
}
|
||||
out = append(out, Range{i, j})
|
||||
i = j
|
||||
}
|
||||
return out
|
||||
}
|
||||
Reference in new issue
Block a user