Make calcHash() a method of SharedBuffer

This will make it easier to use calcHash() in other SharedBuffer
methods.
This commit is contained in:
Dmytro Maluka
2025-07-26 22:38:13 +02:00
parent c9f84cd2b7
commit 4ade5cdf24
3 changed files with 25 additions and 25 deletions

View File

@@ -140,6 +140,26 @@ func (b *SharedBuffer) remove(start, end Loc) []byte {
return b.LineArray.remove(start, end)
}
// calcHash calculates md5 hash of all lines in the buffer
func (b *SharedBuffer) calcHash(out *[md5.Size]byte) {
h := md5.New()
if len(b.lines) > 0 {
h.Write(b.lines[0].data)
for _, l := range b.lines[1:] {
if b.Endings == FFDos {
h.Write([]byte{'\r', '\n'})
} else {
h.Write([]byte{'\n'})
}
h.Write(l.data)
}
}
h.Sum((*out)[:0])
}
// MarkModified marks the buffer as modified for this frame
// and performs rehighlighting if syntax highlighting is enabled
func (b *SharedBuffer) MarkModified(start, end int) {
@@ -416,7 +436,7 @@ func NewBuffer(r io.Reader, size int64, path string, startcursor Loc, btype BufT
} else if !hasBackup {
// since applying a backup does not save the applied backup to disk, we should
// not calculate the original hash based on the backup data
calcHash(b, &b.origHash)
b.calcHash(&b.origHash)
}
}
@@ -558,7 +578,7 @@ func (b *Buffer) ReOpen() error {
if len(data) > LargeFileThreshold {
b.Settings["fastdirty"] = true
} else {
calcHash(b, &b.origHash)
b.calcHash(&b.origHash)
}
}
b.isModified = false
@@ -643,7 +663,7 @@ func (b *Buffer) Modified() bool {
var buff [md5.Size]byte
calcHash(b, &buff)
b.calcHash(&buff)
return buff != b.origHash
}
@@ -663,26 +683,6 @@ func (b *Buffer) Size() int {
return nb
}
// calcHash calculates md5 hash of all lines in the buffer
func calcHash(b *Buffer, out *[md5.Size]byte) {
h := md5.New()
if len(b.lines) > 0 {
h.Write(b.lines[0].data)
for _, l := range b.lines[1:] {
if b.Endings == FFDos {
h.Write([]byte{'\r', '\n'})
} else {
h.Write([]byte{'\n'})
}
h.Write(l.data)
}
}
h.Sum((*out)[:0])
}
func parseDefFromFile(f config.RuntimeFile, header *highlight.Header) *highlight.Def {
data, err := f.Data()
if err != nil {

View File

@@ -318,7 +318,7 @@ func (b *Buffer) saveToFile(filename string, withSudo bool, autoSave bool) error
// For large files 'fastdirty' needs to be on
b.Settings["fastdirty"] = true
} else {
calcHash(b, &b.origHash)
b.calcHash(&b.origHash)
}
}

View File

@@ -73,7 +73,7 @@ func (b *Buffer) DoSetOptionNative(option string, nativeValue interface{}) {
b.Settings["fastdirty"] = true
} else {
if !b.isModified {
calcHash(b, &b.origHash)
b.calcHash(&b.origHash)
} else {
// prevent using an old stale origHash value
b.origHash = [md5.Size]byte{}