Skip to content

Commit f88ac6d

Browse files
authored
Merge pull request zyedidia#3430 from dmaluka/calchash-fixes
Various bugfixes and improvements around buffer md5 hash calculation and `fastdirty`
2 parents 3737979 + 0b15b57 commit f88ac6d

File tree

2 files changed

+22
-30
lines changed

2 files changed

+22
-30
lines changed

internal/buffer/buffer.go

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -64,10 +64,6 @@ var (
6464
// BTStdout is a buffer that only writes to stdout
6565
// when closed
6666
BTStdout = BufType{6, false, true, true}
67-
68-
// ErrFileTooLarge is returned when the file is too large to hash
69-
// (fastdirty is automatically enabled)
70-
ErrFileTooLarge = errors.New("File is too large to hash")
7167
)
7268

7369
// SharedBuffer is a struct containing info that is shared among buffers
@@ -554,7 +550,11 @@ func (b *Buffer) ReOpen() error {
554550

555551
err = b.UpdateModTime()
556552
if !b.Settings["fastdirty"].(bool) {
557-
calcHash(b, &b.origHash)
553+
if len(data) > LargeFileThreshold {
554+
b.Settings["fastdirty"] = true
555+
} else {
556+
calcHash(b, &b.origHash)
557+
}
558558
}
559559
b.isModified = false
560560
b.RelocateCursors()
@@ -649,37 +649,23 @@ func (b *Buffer) Size() int {
649649
}
650650

651651
// calcHash calculates md5 hash of all lines in the buffer
652-
func calcHash(b *Buffer, out *[md5.Size]byte) error {
652+
func calcHash(b *Buffer, out *[md5.Size]byte) {
653653
h := md5.New()
654654

655-
size := 0
656655
if len(b.lines) > 0 {
657-
n, e := h.Write(b.lines[0].data)
658-
if e != nil {
659-
return e
660-
}
661-
size += n
656+
h.Write(b.lines[0].data)
662657

663658
for _, l := range b.lines[1:] {
664-
n, e = h.Write([]byte{'\n'})
665-
if e != nil {
666-
return e
667-
}
668-
size += n
669-
n, e = h.Write(l.data)
670-
if e != nil {
671-
return e
659+
if b.Endings == FFDos {
660+
h.Write([]byte{'\r', '\n'})
661+
} else {
662+
h.Write([]byte{'\n'})
672663
}
673-
size += n
664+
h.Write(l.data)
674665
}
675666
}
676667

677-
if size > LargeFileThreshold {
678-
return ErrFileTooLarge
679-
}
680-
681668
h.Sum((*out)[:0])
682-
return nil
683669
}
684670

685671
func parseDefFromFile(f config.RuntimeFile, header *highlight.Header) *highlight.Def {

internal/buffer/settings.go

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package buffer
22

33
import (
4+
"crypto/md5"
5+
46
"github.com/zyedidia/micro/v2/internal/config"
57
"github.com/zyedidia/micro/v2/internal/screen"
68
)
@@ -10,10 +12,14 @@ func (b *Buffer) SetOptionNative(option string, nativeValue interface{}) error {
1012

1113
if option == "fastdirty" {
1214
if !nativeValue.(bool) {
13-
if !b.Modified() {
14-
e := calcHash(b, &b.origHash)
15-
if e == ErrFileTooLarge {
16-
b.Settings["fastdirty"] = false
15+
if b.Size() > LargeFileThreshold {
16+
b.Settings["fastdirty"] = true
17+
} else {
18+
if !b.isModified {
19+
calcHash(b, &b.origHash)
20+
} else {
21+
// prevent using an old stale origHash value
22+
b.origHash = [md5.Size]byte{}
1723
}
1824
}
1925
}

0 commit comments

Comments
 (0)